From 9250a5d8c18311005329ec622482d1c2a29de307 Mon Sep 17 00:00:00 2001 From: Eoghan Glynn Date: Tue, 17 Jul 2012 18:18:07 +0100 Subject: Fix EC2 CreateImage no_reboot logic Previously the call to restart the imaged instance was incorrect. This is now fixed and unit tested. Change-Id: Ifde193e084ecdbc37a57150f28d833ae276f49c6 --- nova/api/ec2/cloud.py | 2 +- nova/tests/api/ec2/test_cloud.py | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index e0fb434d0..8a9f5f158 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -1578,7 +1578,7 @@ class CloudController(object): ec2_id = ec2utils.glance_id_to_ec2_id(context, new_image['id']) if restart_instance: - self.compute_api.start(context, instance_id=instance_id) + self.compute_api.start(context, instance) return {'imageId': ec2_id} diff --git a/nova/tests/api/ec2/test_cloud.py b/nova/tests/api/ec2/test_cloud.py index ee48e8990..61b69601b 100644 --- a/nova/tests/api/ec2/test_cloud.py +++ b/nova/tests/api/ec2/test_cloud.py @@ -43,6 +43,7 @@ from nova.openstack.common import rpc from nova import test from nova.tests.image import fake from nova import utils +from nova.virt import fake as fake_virt LOG = logging.getLogger(__name__) @@ -2158,7 +2159,7 @@ class CloudTestCase(test.TestCase): for snapshot_id in (ec2_snapshot1_id, ec2_snapshot2_id): self.cloud.delete_snapshot(self.context, snapshot_id) - def test_create_image(self): + def _do_test_create_image(self, no_reboot): """Make sure that CreateImage works""" # enforce periodic tasks run in short time to avoid wait for 60s. self._restart_compute_service(periodic_interval=0.3) @@ -2210,8 +2211,20 @@ class CloudTestCase(test.TestCase): self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', fake_block_device_mapping_get_all_by_instance) + virt_driver = {} + + def fake_power_on(self, instance): + virt_driver['powered_on'] = True + + self.stubs.Set(fake_virt.FakeDriver, 'power_on', fake_power_on) + + def fake_power_off(self, instance): + virt_driver['powered_off'] = True + + self.stubs.Set(fake_virt.FakeDriver, 'power_off', fake_power_off) + result = self.cloud.create_image(self.context, ec2_instance_id, - no_reboot=True) + no_reboot=no_reboot) ec2_ids = [result['imageId']] created_image = self.cloud.describe_images(self.context, ec2_ids)['imagesSet'][0] @@ -2225,6 +2238,8 @@ class CloudTestCase(test.TestCase): self.assertEquals(created_image.get('kernelId'), 'aki-00000001') self.assertEquals(created_image.get('ramdiskId'), 'ari-00000002') self.assertEquals(created_image.get('rootDeviceType'), 'ebs') + self.assertNotEqual(virt_driver.get('powered_on'), no_reboot) + self.assertNotEqual(virt_driver.get('powered_off'), no_reboot) self.cloud.terminate_instances(self.context, [ec2_instance_id]) for vol in volumes: @@ -2235,6 +2250,14 @@ class CloudTestCase(test.TestCase): self._restart_compute_service() + def test_create_image_no_reboot(self): + """Make sure that CreateImage works""" + self._do_test_create_image(True) + + def test_create_image_with_reboot(self): + """Make sure that CreateImage works""" + self._do_test_create_image(False) + def test_create_image_instance_store(self): """ Ensure CreateImage fails as expected for an instance-store-backed -- cgit