Implement power_off/power_on for the FakeDriver

When trying to recreate hundreds of instance action
events for scale testing with the FakeDriver, a nice
simple way to do that is by stopping those instances
and starting them again.

However, since power_off/on aren't implemented, once
you "stop" them the sync_instance_power_state periodic
task in the compute manager thinks they are still running
on the "hypervisor" and will stop them again via the API,
which records yet another instance action and set of
events.

This just toggles the power state bit on the fake instance
in the FakeDriver to make the periodic task do the right
thing.

As a result, we also have more realistic API and
notification samples.

Change-Id: Ie621686053ad774c4ae4f22bb2a455f98900b611
(cherry picked from commit 211e9b1961)
This commit is contained in:
Matt Riedemann 2017-10-05 19:19:36 -04:00
parent fc8cd8f3b3
commit 4a3eea8c30
12 changed files with 23 additions and 15 deletions

View File

@ -58,7 +58,7 @@
"OS-EXT-SRV-ATTR:host": "b8b357f7100d4391828f2177c922ef93",
"OS-EXT-SRV-ATTR:hypervisor_hostname": "fake-mini",
"OS-EXT-SRV-ATTR:instance_name": "instance-00000001",
"OS-EXT-STS:power_state": 1,
"OS-EXT-STS:power_state": 4,
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "rescued",
"os-extended-volumes:volumes_attached": [],

View File

@ -59,7 +59,7 @@
"OS-EXT-SRV-ATTR:host": "b8b357f7100d4391828f2177c922ef93",
"OS-EXT-SRV-ATTR:hypervisor_hostname": "fake-mini",
"OS-EXT-SRV-ATTR:instance_name": "instance-00000001",
"OS-EXT-STS:power_state": 1,
"OS-EXT-STS:power_state": 4,
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "active",
"os-extended-volumes:volumes_attached": [],

View File

@ -50,7 +50,7 @@
"reservation_id":"r-npxv0e40",
"state":"stopped",
"task_state":"powering-on",
"power_state":"running",
"power_state":"shutdown",
"tenant_id":"6f70656e737461636b20342065766572",
"terminated_at":null,
"auto_disk_config":"MANUAL",

View File

@ -50,7 +50,7 @@
"reservation_id":"r-npxv0e40",
"state":"shelved",
"task_state":null,
"power_state":"running",
"power_state":"shutdown",
"tenant_id":"6f70656e737461636b20342065766572",
"terminated_at":null,
"auto_disk_config":"MANUAL",

View File

@ -50,7 +50,7 @@
"reservation_id":"r-npxv0e40",
"state":"shelved_offloaded",
"task_state":null,
"power_state":"running",
"power_state":"shutdown",
"tenant_id":"6f70656e737461636b20342065766572",
"terminated_at":null,
"auto_disk_config":"MANUAL",

View File

@ -50,7 +50,7 @@
"reservation_id":"r-npxv0e40",
"state":"shelved",
"task_state":"shelving_offloading",
"power_state":"running",
"power_state":"shutdown",
"tenant_id":"6f70656e737461636b20342065766572",
"terminated_at":null,
"auto_disk_config":"MANUAL",

View File

@ -50,7 +50,7 @@
"reservation_id":"r-npxv0e40",
"state":"shelved_offloaded",
"task_state":"unshelving",
"power_state":"running",
"power_state":"shutdown",
"tenant_id":"6f70656e737461636b20342065766572",
"terminated_at":null,
"auto_disk_config":"MANUAL",

View File

@ -58,7 +58,7 @@
"OS-EXT-SRV-ATTR:host": "%(compute_host)s",
"OS-EXT-SRV-ATTR:hypervisor_hostname": "%(hypervisor_hostname)s",
"OS-EXT-SRV-ATTR:instance_name": "%(instance_name)s",
"OS-EXT-STS:power_state": 1,
"OS-EXT-STS:power_state": 4,
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "rescued",
"os-extended-volumes:volumes_attached": [],

View File

@ -59,7 +59,7 @@
"OS-EXT-SRV-ATTR:host": "%(compute_host)s",
"OS-EXT-SRV-ATTR:hypervisor_hostname": "%(hypervisor_hostname)s",
"OS-EXT-SRV-ATTR:instance_name": "%(instance_name)s",
"OS-EXT-STS:power_state": 1,
"OS-EXT-STS:power_state": 4,
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "active",
"os-extended-volumes:volumes_attached": [],

View File

@ -423,7 +423,6 @@ class TestInstanceNotificationSample(
'instance-power_off-end',
replacements={
'reservation_id': server['reservation_id'],
'power_state': 'running',
'uuid': server['id']},
actual=fake_notifier.VERSIONED_NOTIFICATIONS[1])

View File

@ -2402,9 +2402,10 @@ class ComputeTestCase(BaseTestCase,
self.compute.terminate_instance(self.context, instance, [], [])
@mock.patch.object(fake.FakeDriver, 'power_off')
@mock.patch.object(fake.FakeDriver, 'rescue')
@mock.patch.object(compute_manager.ComputeManager, '_get_rescue_image')
def test_rescue_handle_err(self, mock_get, mock_rescue):
def test_rescue_handle_err(self, mock_get, mock_rescue, mock_power_off):
# If the driver fails to rescue, instance state should got to ERROR
# and the exception should be converted to InstanceNotRescuable
inst_obj = self._create_fake_instance_obj()
@ -2427,8 +2428,9 @@ class ComputeTestCase(BaseTestCase,
mock.ANY, 'password')
@mock.patch.object(image_api.API, "get")
@mock.patch.object(fake.FakeDriver, 'power_off')
@mock.patch.object(nova.virt.fake.FakeDriver, "rescue")
def test_rescue_with_image_specified(self, mock_rescue,
def test_rescue_with_image_specified(self, mock_rescue, mock_power_off,
mock_image_get):
image_ref = uuids.image_instance
rescue_image_meta = {}
@ -2452,9 +2454,10 @@ class ComputeTestCase(BaseTestCase,
self.compute.terminate_instance(ctxt, instance, [], [])
@mock.patch.object(image_api.API, "get")
@mock.patch.object(fake.FakeDriver, 'power_off')
@mock.patch.object(nova.virt.fake.FakeDriver, "rescue")
def test_rescue_with_base_image_when_image_not_specified(self,
mock_rescue, mock_image_get):
mock_rescue, mock_power_off, mock_image_get):
image_ref = FAKE_IMAGE_REF
system_meta = {"image_base_image_ref": image_ref}
rescue_image_meta = {}

View File

@ -246,11 +246,17 @@ class FakeDriver(driver.ComputeDriver):
pass
def power_off(self, instance, timeout=0, retry_interval=0):
pass
if instance.uuid in self.instances:
self.instances[instance.uuid].state = power_state.SHUTDOWN
else:
raise exception.InstanceNotFound(instance_id=instance.uuid)
def power_on(self, context, instance, network_info,
block_device_info=None):
pass
if instance.uuid in self.instances:
self.instances[instance.uuid].state = power_state.RUNNING
else:
raise exception.InstanceNotFound(instance_id=instance.uuid)
def trigger_crash_dump(self, instance):
pass