@@ -8834,6 +8834,68 @@ def doit(mock_pr, mock_r):
8834
8834
8835
8835
doit ()
8836
8836
8837
+ def test_prep_resize_fails_unable_to_migrate_to_self (self ):
8838
+ """Asserts that _prep_resize handles UnableToMigrateToSelf when
8839
+ _prep_resize is called on the host on which the instance lives and
8840
+ the flavor is not changing.
8841
+ """
8842
+ instance = fake_instance .fake_instance_obj (
8843
+ self .context , host = self .compute .host ,
8844
+ expected_attrs = ['system_metadata' , 'flavor' ])
8845
+ migration = mock .MagicMock (spec = 'nova.objects.Migration' )
8846
+ with mock .patch .dict (self .compute .driver .capabilities ,
8847
+ {'supports_migrate_to_same_host' : False }):
8848
+ ex = self .assertRaises (
8849
+ exception .InstanceFaultRollback , self .compute ._prep_resize ,
8850
+ self .context , instance .image_meta , instance , instance .flavor ,
8851
+ filter_properties = {}, node = instance .node , migration = migration )
8852
+ self .assertIsInstance (
8853
+ ex .inner_exception , exception .UnableToMigrateToSelf )
8854
+
8855
+ @mock .patch ('nova.compute.utils.notify_usage_exists' )
8856
+ @mock .patch ('nova.compute.manager.ComputeManager.'
8857
+ '_notify_about_instance_usage' )
8858
+ @mock .patch ('nova.compute.utils.notify_about_resize_prep_instance' )
8859
+ @mock .patch ('nova.objects.Instance.save' )
8860
+ @mock .patch ('nova.compute.manager.ComputeManager._revert_allocation' )
8861
+ @mock .patch ('nova.compute.manager.ComputeManager.'
8862
+ '_reschedule_resize_or_reraise' )
8863
+ @mock .patch ('nova.compute.utils.add_instance_fault_from_exc' )
8864
+ def test_prep_resize_fails_rollback (
8865
+ self , add_instance_fault_from_exc , _reschedule_resize_or_reraise ,
8866
+ _revert_allocation , mock_instance_save ,
8867
+ notify_about_resize_prep_instance , _notify_about_instance_usage ,
8868
+ notify_usage_exists ):
8869
+ """Tests that if _prep_resize raises InstanceFaultRollback, the
8870
+ instance.vm_state is reset properly in _error_out_instance_on_exception
8871
+ """
8872
+ instance = fake_instance .fake_instance_obj (
8873
+ self .context , host = self .compute .host , vm_state = vm_states .STOPPED ,
8874
+ expected_attrs = ['system_metadata' , 'flavor' ])
8875
+ migration = mock .MagicMock (spec = 'nova.objects.Migration' )
8876
+ request_spec = mock .MagicMock (spec = 'nova.objects.RequestSpec' )
8877
+ ex = exception .InstanceFaultRollback (
8878
+ inner_exception = exception .UnableToMigrateToSelf (
8879
+ instance_id = instance .uuid , host = instance .host ))
8880
+
8881
+ def fake_reschedule_resize_or_reraise (* args , ** kwargs ):
8882
+ raise ex
8883
+
8884
+ _reschedule_resize_or_reraise .side_effect = (
8885
+ fake_reschedule_resize_or_reraise )
8886
+
8887
+ with mock .patch .object (self .compute , '_prep_resize' , side_effect = ex ):
8888
+ self .assertRaises (
8889
+ # _error_out_instance_on_exception should reraise the
8890
+ # UnableToMigrateToSelf inside InstanceFaultRollback.
8891
+ exception .UnableToMigrateToSelf , self .compute .prep_resize ,
8892
+ self .context , instance .image_meta , instance , instance .flavor ,
8893
+ request_spec , filter_properties = {}, node = instance .node ,
8894
+ clean_shutdown = True , migration = migration , host_list = [])
8895
+ # The instance.vm_state should remain unchanged
8896
+ # (_error_out_instance_on_exception will set to ACTIVE by default).
8897
+ self .assertEqual (vm_states .STOPPED , instance .vm_state )
8898
+
8837
8899
def test__claim_pci_for_instance_vifs (self ):
8838
8900
@mock .patch .object (self .compute , 'rt' )
8839
8901
@mock .patch .object (pci_request , 'get_instance_pci_request_from_vif' )
0 commit comments