From da003c175a8211d6070fa9b171e67c4cf012e9cc Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Wed, 23 Nov 2011 11:43:30 -0500 Subject: power_on/power_off in compute manager to use uuids Related to blueprint internal-uuids. Changes power_on and power_off of instances to use uuids. Also, fixes a test for get_actions to be more accurate. Later a migration will need to be written for instance_get_actions to use uuids instead of ids. Change-Id: Id0896f4bf3f0c64a77ac9c421bad702073f2fc50 --- nova/compute/api.py | 6 ++--- nova/compute/manager.py | 16 ++++++------- nova/tests/test_compute.py | 58 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/nova/compute/api.py b/nova/compute/api.py index 32f329a04..9c6921d54 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -771,7 +771,6 @@ class API(base.Base): @scheduler_api.reroute_compute("soft_delete") def soft_delete(self, context, instance): """Terminate an instance.""" - instance_id = instance["id"] instance_uuid = instance["uuid"] LOG.debug(_("Going to try to soft delete %s"), instance_uuid) @@ -790,7 +789,7 @@ class API(base.Base): deleted_at=utils.utcnow()) self._cast_compute_message('power_off_instance', context, - instance_id, host) + instance_uuid, host) else: LOG.warning(_("No host for instance %s, deleting immediately"), instance["uuid"]) @@ -822,7 +821,6 @@ class API(base.Base): @scheduler_api.reroute_compute("restore") def restore(self, context, instance): """Restore a previously deleted (but not reclaimed) instance.""" - instance_id = instance['id'] if not _is_queued_delete(instance): return @@ -839,7 +837,7 @@ class API(base.Base): instance, task_state=task_states.POWERING_ON) self._cast_compute_message('power_on_instance', context, - instance_id, host) + instance['uuid'], host) @scheduler_api.reroute_compute("force_delete") def force_delete(self, context, instance): diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 2eed738c1..1ca2b3d7e 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -625,26 +625,26 @@ class ComputeManager(manager.SchedulerDependentManager): task_state=None) @exception.wrap_exception(notifier=notifier, publisher_id=publisher_id()) - @checks_instance_lock - def power_off_instance(self, context, instance_id): + @checks_instance_lock_uuid + def power_off_instance(self, context, instance_uuid): """Power off an instance on this host.""" - instance = self.db.instance_get(context, instance_id) + instance = self.db.instance_get_by_uuid(context, instance_uuid) self.driver.power_off(instance) current_power_state = self._get_power_state(context, instance) self._instance_update(context, - instance_id, + instance_uuid, power_state=current_power_state, task_state=None) @exception.wrap_exception(notifier=notifier, publisher_id=publisher_id()) - @checks_instance_lock - def power_on_instance(self, context, instance_id): + @checks_instance_lock_uuid + def power_on_instance(self, context, instance_uuid): """Power on an instance on this host.""" - instance = self.db.instance_get(context, instance_id) + instance = self.db.instance_get_by_uuid(context, instance_uuid) self.driver.power_on(instance) current_power_state = self._get_power_state(context, instance) self._instance_update(context, - instance_id, + instance_uuid, power_state=current_power_state, task_state=None) diff --git a/nova/tests/test_compute.py b/nova/tests/test_compute.py index f03665d57..47f2fb83d 100644 --- a/nova/tests/test_compute.py +++ b/nova/tests/test_compute.py @@ -277,6 +277,44 @@ class ComputeTestCase(BaseTestCase): self.assertTrue(called['unrescued']) self.compute.terminate_instance(self.context, instance_id) + def test_power_on(self): + """Ensure instance can be powered on""" + + called = {'power_on': False} + + def fake_driver_power_on(self, instance): + called['power_on'] = True + + self.stubs.Set(nova.virt.driver.ComputeDriver, 'power_on', + fake_driver_power_on) + + instance = self._create_fake_instance() + instance_id = instance['id'] + instance_uuid = instance['uuid'] + self.compute.run_instance(self.context, instance_id) + self.compute.power_on_instance(self.context, instance_uuid) + self.assertTrue(called['power_on']) + self.compute.terminate_instance(self.context, instance_id) + + def test_power_off(self): + """Ensure instance can be powered off""" + + called = {'power_off': False} + + def fake_driver_power_off(self, instance): + called['power_off'] = True + + self.stubs.Set(nova.virt.driver.ComputeDriver, 'power_off', + fake_driver_power_off) + + instance = self._create_fake_instance() + instance_id = instance['id'] + instance_uuid = instance['uuid'] + self.compute.run_instance(self.context, instance_id) + self.compute.power_off_instance(self.context, instance_uuid) + self.assertTrue(called['power_off']) + self.compute.terminate_instance(self.context, instance_id) + def test_pause(self): """Ensure instance can be paused and unpaused""" instance = self._create_fake_instance() @@ -2266,10 +2304,24 @@ class ComputeAPITestCase(BaseTestCase): self.compute_api.delete(self.context, instance) def test_get_actions(self): + + expected = [{'id': 1, + 'instance_id': 5, + 'action': 'rebuild', + 'error': '', + }] + + def fake_get_actions(context, instance): + return expected + + self.stubs.Set(nova.db, 'instance_get_actions', + fake_get_actions) + + instance = self._create_fake_instance() context = self.context.elevated() - instance_id = self._create_instance() - instance = self.compute_api.get(self.context, instance_id) - self.compute_api.get_actions(context, instance) + actual = self.compute_api.get_actions(context, instance) + self.assertEqual(expected, actual) + self.compute_api.delete(self.context, instance) def test_inject_file(self): -- cgit