From db064bbb40f370519c5b3c5da3ce4666a34614c1 Mon Sep 17 00:00:00 2001 From: Pushkar Umaranikar Date: Fri, 17 Jun 2016 17:19:09 +0000 Subject: [PATCH] Transform instance.suspend notifications The instance.suspend.start and instance.suspend.end notifications are transformed to the versioned notifications framework. Change-Id: I6a90019773c415db9d27c893eb64466ae101b7d7 Implements: blueprint versioned-notification-transformation-newton Depends-On: #313654 --- .../instance-suspend-end.json | 62 +++++++++++++++++++ .../instance-suspend-start.json | 62 +++++++++++++++++++ nova/compute/manager.py | 7 ++- nova/notifications/objects/instance.py | 4 +- .../test_instance.py | 25 ++++++++ nova/tests/unit/compute/test_compute.py | 18 ++++-- 6 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 doc/notification_samples/instance-suspend-end.json create mode 100644 doc/notification_samples/instance-suspend-start.json diff --git a/doc/notification_samples/instance-suspend-end.json b/doc/notification_samples/instance-suspend-end.json new file mode 100644 index 000000000000..00ff2c0b6c7d --- /dev/null +++ b/doc/notification_samples/instance-suspend-end.json @@ -0,0 +1,62 @@ +{ + "event_type":"instance.suspend.end", + "payload":{ + "nova_object.data":{ + "architecture":"x86_64", + "availability_zone":null, + "created_at":"2012-10-29T13:42:11Z", + "deleted_at":null, + "display_name":"some-server", + "fault":null, + "host":"compute", + "host_name":"some-server", + "ip_addresses": [{ + "nova_object.name": "IpPayload", + "nova_object.namespace": "nova", + "nova_object.version": "1.0", + "nova_object.data": { + "mac": "fa:16:3e:4c:2c:30", + "address": "192.168.1.3", + "port_uuid": "ce531f90-199f-48c0-816c-13e38010b442", + "meta": {}, + "version": 4, + "label": "private-network", + "device_name": "tapce531f90-19" + } + }], + "kernel_id":"", + "launched_at":"2012-10-29T13:42:11Z", + "image_uuid": "155d900f-4e14-4e4c-a73d-069cbf4541e6", + "metadata":{}, + "node":"fake-mini", + "os_type":null, + "progress":0, + "ramdisk_id":"", + "reservation_id":"r-npxv0e40", + "state":"suspended", + "task_state":null, + "power_state":"running", + "tenant_id":"6f70656e737461636b20342065766572", + "terminated_at":null, + "flavor": { + "nova_object.name": "FlavorPayload", + "nova_object.data": { + "flavorid": "a22d5517-147c-4147-a0d1-e698df5cd4e3", + "root_gb": 1, + "vcpus": 1, + "ephemeral_gb": 0, + "memory_mb": 512 + }, + "nova_object.version": "1.0", + "nova_object.namespace": "nova" + }, + "user_id":"fake", + "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c" + }, + "nova_object.name":"InstanceActionPayload", + "nova_object.namespace":"nova", + "nova_object.version":"1.0" + }, + "priority":"INFO", + "publisher_id":"nova-compute:compute" +} diff --git a/doc/notification_samples/instance-suspend-start.json b/doc/notification_samples/instance-suspend-start.json new file mode 100644 index 000000000000..6518d65b06a3 --- /dev/null +++ b/doc/notification_samples/instance-suspend-start.json @@ -0,0 +1,62 @@ +{ + "event_type":"instance.suspend.start", + "payload":{ + "nova_object.data":{ + "architecture":"x86_64", + "availability_zone":null, + "created_at":"2012-10-29T13:42:11Z", + "deleted_at":null, + "display_name":"some-server", + "fault":null, + "host":"compute", + "host_name":"some-server", + "ip_addresses": [{ + "nova_object.name": "IpPayload", + "nova_object.namespace": "nova", + "nova_object.version": "1.0", + "nova_object.data": { + "mac": "fa:16:3e:4c:2c:30", + "address": "192.168.1.3", + "port_uuid": "ce531f90-199f-48c0-816c-13e38010b442", + "meta": {}, + "version": 4, + "label": "private-network", + "device_name": "tapce531f90-19" + } + }], + "kernel_id":"", + "launched_at":"2012-10-29T13:42:11Z", + "image_uuid": "155d900f-4e14-4e4c-a73d-069cbf4541e6", + "metadata":{}, + "node":"fake-mini", + "os_type":null, + "progress":0, + "ramdisk_id":"", + "reservation_id":"r-npxv0e40", + "state":"active", + "task_state":"suspending", + "power_state":"running", + "tenant_id":"6f70656e737461636b20342065766572", + "terminated_at":null, + "flavor": { + "nova_object.name": "FlavorPayload", + "nova_object.data": { + "flavorid": "a22d5517-147c-4147-a0d1-e698df5cd4e3", + "root_gb": 1, + "vcpus": 1, + "ephemeral_gb": 0, + "memory_mb": 512 + }, + "nova_object.version": "1.0", + "nova_object.namespace": "nova" + }, + "user_id":"fake", + "uuid":"178b0921-8f85-4257-88b6-2e743b5a975c" + }, + "nova_object.name":"InstanceActionPayload", + "nova_object.namespace":"nova", + "nova_object.version":"1.0" + }, + "priority":"INFO", + "publisher_id":"nova-compute:compute" +} diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 670397be33db..fc6da076bc64 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -4074,7 +4074,9 @@ class ComputeManager(manager.Manager): # Store the old state instance.system_metadata['old_vm_state'] = instance.vm_state self._notify_about_instance_usage(context, instance, 'suspend.start') - + compute_utils.notify_about_instance_action(context, instance, + self.host, action=fields.NotificationAction.SUSPEND, + phase=fields.NotificationPhase.START) with self._error_out_instance_on_exception(context, instance, instance_state=instance.vm_state): self.driver.suspend(context, instance) @@ -4083,6 +4085,9 @@ class ComputeManager(manager.Manager): instance.task_state = None instance.save(expected_task_state=task_states.SUSPENDING) self._notify_about_instance_usage(context, instance, 'suspend.end') + compute_utils.notify_about_instance_action(context, instance, + self.host, action=fields.NotificationAction.SUSPEND, + phase=fields.NotificationPhase.END) @wrap_exception() @reverts_task_state diff --git a/nova/notifications/objects/instance.py b/nova/notifications/objects/instance.py index 805a5f41a85f..ce5b19b55307 100644 --- a/nova/notifications/objects/instance.py +++ b/nova/notifications/objects/instance.py @@ -229,8 +229,8 @@ class InstanceStateUpdatePayload(base.NotificationPayloadBase): # @base.notification_sample('instance-unpause-end.json') # @base.notification_sample('instance-resize-start.json') # @base.notification_sample('instance-resize-end.json') -# @base.notification_sample('instance-suspend-start.json') -# @base.notification_sample('instance-suspend-end.json') +@base.notification_sample('instance-suspend-start.json') +@base.notification_sample('instance-suspend-end.json') @base.notification_sample('instance-power_on-start.json') @base.notification_sample('instance-power_on-end.json') # @base.notification_sample('instance-power_off-start.json') diff --git a/nova/tests/functional/notification_sample_tests/test_instance.py b/nova/tests/functional/notification_sample_tests/test_instance.py index cb7b2ef5fb14..a98acb8db031 100644 --- a/nova/tests/functional/notification_sample_tests/test_instance.py +++ b/nova/tests/functional/notification_sample_tests/test_instance.py @@ -216,3 +216,28 @@ class TestInstanceNotificationSample( notification_sample_base.NotificationSampleTestBase.ANY, 'uuid': server['id']}, actual=fake_notifier.VERSIONED_NOTIFICATIONS[1]) + + +def test_create_suspend_server(self): + server = self._boot_a_server( + extra_params={'networks': + [{'port': self.neutron.port_1['id']}]}) + post = {'suspend': {}} + self.api.post_server_action(server['id'], post) + self._wait_for_state_change(self.admin_api, server, 'SUSPENDED') + + self.assertEqual(2, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + self._verify_notification( + 'instance-suspend-start', + replacements={ + 'reservation_id': + notification_sample_base.NotificationSampleTestBase.ANY, + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[0]) + self._verify_notification( + 'instance-suspend-end', + replacements={ + 'reservation_id': + notification_sample_base.NotificationSampleTestBase.ANY, + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[1]) diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index 0a462d69c9a4..782a457d9aaa 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -2345,17 +2345,21 @@ class ComputeTestCase(BaseTestCase): 'compute.instance.unpause.end') self.compute.terminate_instance(self.context, instance, [], []) - def test_suspend(self): + @mock.patch('nova.compute.utils.notify_about_instance_action') + @mock.patch('nova.context.RequestContext.elevated') + def test_suspend(self, mock_context, mock_notify): # ensure instance can be suspended and resumed. + context = self.context + mock_context.return_value = context instance = self._create_fake_instance_obj() - self.compute.build_and_run_instance(self.context, instance, {}, {}, {}, + self.compute.build_and_run_instance(context, instance, {}, {}, {}, block_device_mapping=[]) instance.task_state = task_states.SUSPENDING instance.save() - self.compute.suspend_instance(self.context, instance) + self.compute.suspend_instance(context, instance) instance.task_state = task_states.RESUMING instance.save() - self.compute.resume_instance(self.context, instance) + self.compute.resume_instance(context, instance) self.assertEqual(len(fake_notifier.NOTIFICATIONS), 6) @@ -2365,7 +2369,11 @@ class ComputeTestCase(BaseTestCase): msg = fake_notifier.NOTIFICATIONS[3] self.assertEqual(msg.event_type, 'compute.instance.suspend.end') - + mock_notify.assert_has_calls([ + mock.call(context, instance, 'fake-mini', + action='suspend', phase='start'), + mock.call(context, instance, 'fake-mini', + action='suspend', phase='end')]) self.compute.terminate_instance(self.context, instance, [], []) def test_suspend_error(self):