From 865f0d2050627cc4d8404058e6a7e2d7887db40f Mon Sep 17 00:00:00 2001 From: Andy Hsiang Date: Thu, 16 Jun 2016 15:27:06 -0400 Subject: [PATCH] Transform instance.power_off notifications The instance.power_off.start and instance.power_off.end notifications are transformed to the versioned framework. Change-Id: I45b7b2175f318747158564eb14e21129add86fe9 Implements: bp versioned-notification-transformation-ocata --- .../instance-power_off-end.json | 62 +++++++++++++++++++ .../instance-power_off-start.json | 62 +++++++++++++++++++ nova/compute/manager.py | 9 +++ nova/notifications/objects/instance.py | 4 +- .../test_instance.py | 24 +++++-- nova/tests/unit/compute/test_compute.py | 8 ++- nova/tests/unit/compute/test_compute_mgr.py | 9 ++- 7 files changed, 169 insertions(+), 9 deletions(-) create mode 100644 doc/notification_samples/instance-power_off-end.json create mode 100644 doc/notification_samples/instance-power_off-start.json diff --git a/doc/notification_samples/instance-power_off-end.json b/doc/notification_samples/instance-power_off-end.json new file mode 100644 index 0000000000..f38392e3b5 --- /dev/null +++ b/doc/notification_samples/instance-power_off-end.json @@ -0,0 +1,62 @@ +{ + "event_type":"instance.power_off.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":"stopped", + "task_state":null, + "power_state":"shutdown", + "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-power_off-start.json b/doc/notification_samples/instance-power_off-start.json new file mode 100644 index 0000000000..4b6481a67f --- /dev/null +++ b/doc/notification_samples/instance-power_off-start.json @@ -0,0 +1,62 @@ +{ + "event_type":"instance.power_off.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":"powering-off", + "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 a9bb9998f3..de8f8238a7 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -2424,6 +2424,11 @@ class ComputeManager(manager.Manager): self._notify_about_instance_usage(context, instance, "power_off.start") + + compute_utils.notify_about_instance_action(context, instance, + self.host, action=fields.NotificationAction.POWER_OFF, + phase=fields.NotificationPhase.START) + self._power_off_instance(context, instance, clean_shutdown) instance.power_state = self._get_power_state(context, instance) instance.vm_state = vm_states.STOPPED @@ -2432,6 +2437,10 @@ class ComputeManager(manager.Manager): self._notify_about_instance_usage(context, instance, "power_off.end") + compute_utils.notify_about_instance_action(context, instance, + self.host, action=fields.NotificationAction.POWER_OFF, + phase=fields.NotificationPhase.END) + do_stop_instance() def _power_on(self, context, instance): diff --git a/nova/notifications/objects/instance.py b/nova/notifications/objects/instance.py index 5efdd45717..8fc7a20c3d 100644 --- a/nova/notifications/objects/instance.py +++ b/nova/notifications/objects/instance.py @@ -233,8 +233,8 @@ class InstanceStateUpdatePayload(base.NotificationPayloadBase): @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') -# @base.notification_sample('instance-power_off-end.json') +@base.notification_sample('instance-power_off-start.json') +@base.notification_sample('instance-power_off-end.json') # @base.notification_sample('instance-reboot-start.json') # @base.notification_sample('instance-reboot-end.json') # @base.notification_sample('instance-shutdown-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 943df07aa5..d422937228 100644 --- a/nova/tests/functional/notification_sample_tests/test_instance.py +++ b/nova/tests/functional/notification_sample_tests/test_instance.py @@ -39,7 +39,7 @@ class TestInstanceNotificationSample( extra_params={'networks': [{'port': self.neutron.port_1['id']}]}) actions = [ - self._test_power_on_server, + self._test_power_off_on_server, self._test_restore_server, self._test_suspend_server, self._test_pause_server, @@ -212,7 +212,7 @@ class TestInstanceNotificationSample( self._verify_instance_update_steps(delete_steps, instance_updates, initial=replacements) - def _test_power_on_server(self, server): + def _test_power_off_on_server(self, server): self.api.post_server_action(server['id'], {'os-stop': {}}) self._wait_for_state_change(self.api, server, expected_status='SHUTOFF') @@ -220,20 +220,34 @@ class TestInstanceNotificationSample( self._wait_for_state_change(self.api, server, expected_status='ACTIVE') - self.assertEqual(2, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + self.assertEqual(4, len(fake_notifier.VERSIONED_NOTIFICATIONS)) self._verify_notification( - 'instance-power_on-start', + 'instance-power_off-start', replacements={ 'reservation_id': server['reservation_id'], 'uuid': server['id']}, actual=fake_notifier.VERSIONED_NOTIFICATIONS[0]) self._verify_notification( - 'instance-power_on-end', + 'instance-power_off-end', replacements={ 'reservation_id': server['reservation_id'], + 'power_state': 'running', 'uuid': server['id']}, actual=fake_notifier.VERSIONED_NOTIFICATIONS[1]) + self._verify_notification( + 'instance-power_on-start', + replacements={ + 'reservation_id': server['reservation_id'], + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[2]) + self._verify_notification( + 'instance-power_on-end', + replacements={ + 'reservation_id': server['reservation_id'], + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[3]) + def _test_shelve_server(self, server): self.flags(shelved_offload_time = -1) diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index 19bfb83b85..6d7d091551 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -2051,7 +2051,8 @@ class ComputeTestCase(BaseTestCase): instance = db.instance_get_by_uuid(self.context, instance['uuid']) self.assertEqual(instance['vm_state'], vm_states.ERROR) - def test_stop(self): + @mock.patch('nova.compute.utils.notify_about_instance_action') + def test_stop(self, mock_notify): # Ensure instance can be stopped. instance = self._create_fake_instance_obj() self.compute.build_and_run_instance(self.context, instance, {}, {}, @@ -2065,6 +2066,11 @@ class ComputeTestCase(BaseTestCase): expected_attrs=extra) self.compute.stop_instance(self.context, instance=inst_obj, clean_shutdown=True) + mock_notify.assert_has_calls([ + mock.call(self.context, inst_obj, 'fake-mini', action='power_off', + phase='start'), + mock.call(self.context, inst_obj, 'fake-mini', action='power_off', + phase='end')]) self.compute.terminate_instance(self.context, instance, [], []) @mock.patch('nova.compute.utils.notify_about_instance_action') diff --git a/nova/tests/unit/compute/test_compute_mgr.py b/nova/tests/unit/compute/test_compute_mgr.py index 18bb50cfd4..c2c78ef86d 100755 --- a/nova/tests/unit/compute/test_compute_mgr.py +++ b/nova/tests/unit/compute/test_compute_mgr.py @@ -2852,12 +2852,13 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase): @mock.patch.object(self.compute, '_get_power_state', return_value=power_state.SHUTDOWN) + @mock.patch.object(compute_utils, 'notify_about_instance_action') @mock.patch.object(self.compute, '_notify_about_instance_usage') @mock.patch.object(self.compute, '_power_off_instance') @mock.patch.object(instance, 'save') @mock.patch.object(compute_utils, 'EventReporter') def do_test(event_mock, save_mock, power_off_mock, notify_mock, - get_state_mock): + notify_action_mock, get_state_mock): # run the code self.compute.stop_instance(self.context, instance, True) # assert the calls @@ -2866,6 +2867,12 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase): mock.call(self.context, instance, 'power_off.start'), mock.call(self.context, instance, 'power_off.end') ]) + notify_action_mock.assert_has_calls([ + mock.call(self.context, instance, 'fake-mini', + action='power_off', phase='start'), + mock.call(self.context, instance, 'fake-mini', + action='power_off', phase='end'), + ]) power_off_mock.assert_called_once_with( self.context, instance, True) save_mock.assert_called_once_with( -- 2.39.5