diff options
-rw-r--r-- | nova/tests/compute/test_compute.py | 183 |
1 files changed, 98 insertions, 85 deletions
diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index bf619bbec..d44899350 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -60,7 +60,6 @@ from nova import test from nova.tests.compute import fake_resource_tracker from nova.tests.db.fakes import FakeModel from nova.tests import fake_network -from nova.tests import fake_network_cache_model from nova.tests.image import fake as fake_image from nova.tests import matchers from nova import utils @@ -146,10 +145,11 @@ class BaseTestCase(test.TestCase): fake_network.set_stub_network_methods(self.stubs) def tearDown(self): + ctxt = context.get_admin_context() fake_image.FakeImageService_reset() - instances = db.instance_get_all(self.context.elevated()) + instances = db.instance_get_all(ctxt) for instance in instances: - db.instance_destroy(self.context.elevated(), instance['uuid']) + db.instance_destroy(ctxt, instance['uuid']) fake.restore_nodes() super(BaseTestCase, self).tearDown() @@ -996,96 +996,107 @@ class ComputeTestCase(BaseTestCase): self.compute.terminate_instance(self.context, instance=jsonutils.to_primitive(instance)) - def _stub_out_reboot(self, fake_net_info, fake_block_dev_info): - def fake_reboot(driver, inst, net_info, reboot_type, block_dev_info): - self.assertEqual(block_dev_info, fake_block_dev_info) - self.assertEqual(net_info, fake_net_info) - - self.stubs.Set(nova.virt.fake.FakeDriver, 'legacy_nwinfo', - lambda x: False) - self.stubs.Set(nova.virt.fake.FakeDriver, 'reboot', fake_reboot) + def _test_reboot(self, soft, legacy_nwinfo_driver): + # This is a true unit test, so we don't need the network stubs. + fake_network.unset_stub_network_methods(self.stubs) - def test_reboot_soft(self): - # Ensure instance can be soft rebooted. - instance = jsonutils.to_primitive(self._create_fake_instance()) - self.compute.run_instance(self.context, instance=instance) - db.instance_update(self.context, instance['uuid'], - {'task_state': task_states.REBOOTING}) + self.mox.StubOutWithMock(network_model.NetworkInfo, + 'hydrate') + self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage') + self.mox.StubOutWithMock(self.compute, '_instance_update') + self.mox.StubOutWithMock(self.compute, '_get_power_state') + self.mox.StubOutWithMock(self.compute.driver, 'legacy_nwinfo') + self.mox.StubOutWithMock(self.compute.driver, 'reboot') + + instance = dict(uuid='fake-instance', + power_state='unknown') + + fake_nw_info = 'fake-network-info' + fake_nw_model = network_model.NetworkInfo() + self.mox.StubOutWithMock(fake_nw_model, 'legacy') + + fake_block_dev_info = 'fake_block_dev_info' + fake_power_state1 = 'fake_power_state1' + fake_power_state2 = 'fake_power_state2' + reboot_type = soft and 'SOFT' or 'HARD' + + # Beginning of calls we expect. + + # FIXME(comstud): I don't feel like the context needs to + # be elevated at all. Hopefully remove elevated from + # reboot_instance and remove the stub here in a future patch. + # econtext would just become self.context below then. + econtext = self.context.elevated() + + self.mox.StubOutWithMock(self.context, 'elevated') + self.context.elevated().AndReturn(econtext) + + network_model.NetworkInfo.hydrate(fake_nw_info).AndReturn( + fake_nw_model) + self.compute._notify_about_instance_usage(econtext, + instance, + 'reboot.start') + self.compute._get_power_state(econtext, + instance).AndReturn(fake_power_state1) + self.compute._instance_update(econtext, instance['uuid'], + power_state=fake_power_state1, + vm_state=vm_states.ACTIVE) + + # Reboot should check the driver to see if legacy nwinfo is + # needed. If it is, the model's legacy() method should be + # called and the result passed to driver.reboot. If the + # driver wants the model, we pass the model. + self.compute.driver.legacy_nwinfo().AndReturn(legacy_nwinfo_driver) + if legacy_nwinfo_driver: + expected_nw_info = 'legacy-nwinfo' + fake_nw_model.legacy().AndReturn(expected_nw_info) + else: + expected_nw_info = fake_nw_model + + # Annoying. driver.reboot is wrapped in a try/except, and + # doesn't re-raise. It eats exception generated by mox if + # this is called with the wrong args, so we have to hack + # around it. + reboot_call_info = {} + expected_call_info = {'args': (instance, expected_nw_info, + reboot_type, fake_block_dev_info), + 'kwargs': {}} + + def fake_reboot(*args, **kwargs): + reboot_call_info['args'] = args + reboot_call_info['kwargs'] = kwargs + + self.stubs.Set(self.compute.driver, 'reboot', fake_reboot) + + # Power state should be updated again + self.compute._get_power_state(econtext, + instance).AndReturn(fake_power_state2) + self.compute._instance_update(econtext, instance['uuid'], + power_state=fake_power_state2, + task_state=None, + vm_state=vm_states.ACTIVE) + self.compute._notify_about_instance_usage(econtext, + instance, + 'reboot.end') - reboot_type = "SOFT" - fake_net_info = [] - fake_block_dev_info = {'foo': 'bar'} - self._stub_out_reboot(fake_net_info, fake_block_dev_info) + self.mox.ReplayAll() self.compute.reboot_instance(self.context, instance=instance, - network_info=fake_net_info, block_device_info=fake_block_dev_info, + network_info=fake_nw_info, reboot_type=reboot_type) + self.assertEqual(expected_call_info, reboot_call_info) - inst_ref = db.instance_get_by_uuid(self.context, instance['uuid']) - self.assertEqual(inst_ref['power_state'], power_state.RUNNING) - self.assertEqual(inst_ref['task_state'], None) - - self.compute.terminate_instance(self.context, - instance=jsonutils.to_primitive(inst_ref)) + def test_reboot_soft(self): + self._test_reboot(True, False) def test_reboot_hard(self): - # Ensure instance can be hard rebooted. - instance = jsonutils.to_primitive(self._create_fake_instance()) - self.compute.run_instance(self.context, instance=instance) - db.instance_update(self.context, instance['uuid'], - {'task_state': task_states.REBOOTING_HARD}) + self._test_reboot(False, False) - reboot_type = "HARD" - fake_net_info = [] - fake_block_dev_info = {'foo': 'bar'} - self._stub_out_reboot(fake_net_info, fake_block_dev_info) - self.compute.reboot_instance(self.context, instance=instance, - network_info=fake_net_info, - block_device_info=fake_block_dev_info, - reboot_type=reboot_type) - - inst_ref = db.instance_get_by_uuid(self.context, instance['uuid']) - self.assertEqual(inst_ref['power_state'], power_state.RUNNING) - self.assertEqual(inst_ref['task_state'], None) - - self.compute.terminate_instance(self.context, - instance=jsonutils.to_primitive(inst_ref)) + def test_reboot_soft_legacy_nwinfo_driver(self): + self._test_reboot(True, True) - def test_reboot_nwinfo(self): - # Ensure instance network info is rehydrated in reboot. - instance = jsonutils.to_primitive(self._create_fake_instance()) - self.compute.run_instance(self.context, instance=instance) - db.instance_update(self.context, instance['uuid'], - {'task_state': task_states.REBOOTING_HARD}) - - result = {'was_instance': []} - - # NOTE(danms): Beware the dragons ahead: - # Since the _legacy_nw_info() method in manager runs inside a - # try..except block, we can't assert from here. Further, this - # will be run more than once during the operation we're about - # to fire off, which means we need to make sure that it doesn't - # fail any of the times it is run. Hence the obscurity below. - def fake_legacy_nw_info(network_info): - result['was_instance'].append( - isinstance(network_info, network_model.NetworkInfo)) - self.stubs.Set(self.compute, '_legacy_nw_info', fake_legacy_nw_info) - - fake_net_info = network_model.NetworkInfo([ - fake_network_cache_model.new_vif(), - fake_network_cache_model.new_vif( - {'address': 'bb:bb:bb:bb:bb:bb'})]) - fake_net_info_p = jsonutils.to_primitive(fake_net_info) - fake_block_dev_info = {'foo': 'bar'} - self.compute.reboot_instance(self.context, instance=instance, - network_info=fake_net_info_p, - block_device_info=fake_block_dev_info, - reboot_type="SOFT") - - inst_ref = db.instance_get_by_uuid(self.context, instance['uuid']) - self.compute.terminate_instance(self.context, - instance=jsonutils.to_primitive(inst_ref)) - self.assertFalse(False in result['was_instance']) + def test_reboot_hard_legacy_nwinfo_driver(self): + self._test_reboot(False, True) def test_set_admin_password(self): # Ensure instance can have its admin password set. @@ -3182,7 +3193,6 @@ class ComputeTestCase(BaseTestCase): self.compute._destroy_evacuated_instances(fake_context) def test_init_host(self): - our_host = self.compute.host fake_context = 'fake-context' startup_instances = ['inst1', 'inst2', 'inst3'] @@ -3235,7 +3245,10 @@ class ComputeTestCase(BaseTestCase): self.mox.ReplayAll() self.compute.init_host() - # VerifyCall done by tearDown + # tearDown() uses context.get_admin_context(), so we have + # to do the verification here and unstub it. + self.mox.VerifyAll() + self.mox.UnsetStubs() def test_get_instances_on_driver(self): fake_context = context.get_admin_context() |