From bafb4e59684047fe191be25becfe278bb9ddd9a6 Mon Sep 17 00:00:00 2001 From: Matthew Booth Date: Tue, 15 Jul 2014 12:49:30 +0100 Subject: [PATCH] Use instance objects consistently in suspend tests test_suspend() was passing an instance object, but test_suspend_error and test_suspend_not_implemented were passing a dict. This change makes them all pass an object. In updating test_suspend_error and test_suspend_not_implemented, we also convert both to use mock. Change-Id: I97e28d50a99e920f7aa4f3586446e3fefe807658 --- nova/tests/compute/test_compute.py | 49 ++++++++++++++---------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index 1fbaff2a12ac..7a58df731a51 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -2280,43 +2280,38 @@ class ComputeTestCase(BaseTestCase): def test_suspend_error(self): # Ensure vm_state is ERROR when suspend error occurs. - def fake(*args, **kwargs): - raise test.TestingException() - self.stubs.Set(self.compute.driver, 'suspend', fake) - instance = jsonutils.to_primitive(self._create_fake_instance()) - instance_uuid = instance['uuid'] self.compute.run_instance(self.context, instance, {}, {}, [], None, None, True, None, False) - self.assertRaises(test.TestingException, - self.compute.suspend_instance, - self.context, - instance=instance) - instance = db.instance_get_by_uuid(self.context, instance_uuid) - self.assertEqual(instance['vm_state'], vm_states.ERROR) - self.compute.terminate_instance(self.context, - self._objectify(instance), [], []) + instance = self._objectify(instance) + + with mock.patch.object(self.compute.driver, 'suspend', + side_effect=test.TestingException): + self.assertRaises(test.TestingException, + self.compute.suspend_instance, + self.context, + instance=instance) + + instance = db.instance_get_by_uuid(self.context, instance.uuid) + self.assertEqual(vm_states.ERROR, instance.vm_state) def test_suspend_not_implemented(self): # Ensure expected exception is raised and the vm_state of instance # restore to original value if suspend is not implemented by driver - def fake(*args, **kwargs): - raise NotImplementedError('suspend test') - self.stubs.Set(self.compute.driver, 'suspend', fake) - instance = jsonutils.to_primitive(self._create_fake_instance()) - instance_state = instance['vm_state'] - self.compute.run_instance(self.context, instance, {}, {}, [], None, None, True, None, False) - self.assertRaises(NotImplementedError, - self.compute.suspend_instance, - self.context, - instance=instance) - instance = db.instance_get_by_uuid(self.context, instance['uuid']) - self.assertEqual(instance_state, instance['vm_state']) - self.compute.terminate_instance(self.context, - self._objectify(instance), [], []) + instance = self._objectify(instance) + + with mock.patch.object(self.compute.driver, 'suspend', + side_effect=NotImplementedError('suspend test')): + self.assertRaises(NotImplementedError, + self.compute.suspend_instance, + self.context, + instance=instance) + + instance = db.instance_get_by_uuid(self.context, instance.uuid) + self.assertEqual(vm_states.ACTIVE, instance.vm_state) def test_rebuild(self): # Ensure instance can be rebuilt.