diff --git a/doc/notification_samples/instance-snapshot-end.json b/doc/notification_samples/instance-snapshot-end.json new file mode 100644 index 000000000000..fc6c00496735 --- /dev/null +++ b/doc/notification_samples/instance-snapshot-end.json @@ -0,0 +1,62 @@ +{ + "event_type":"instance.snapshot.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":"active", + "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-snapshot-start.json b/doc/notification_samples/instance-snapshot-start.json new file mode 100644 index 000000000000..06eb419cc052 --- /dev/null +++ b/doc/notification_samples/instance-snapshot-start.json @@ -0,0 +1,62 @@ +{ + "event_type":"instance.snapshot.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":"image_snapshot", + "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 242376762a0e..322c96320db5 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -3113,6 +3113,9 @@ class ComputeManager(manager.Manager): self._notify_about_instance_usage( context, instance, "snapshot.start") + compute_utils.notify_about_instance_action(context, instance, + self.host, action=fields.NotificationAction.SNAPSHOT, + phase=fields.NotificationPhase.START) def update_task_state(task_state, expected_state=expected_task_state): @@ -3127,6 +3130,9 @@ class ComputeManager(manager.Manager): self._notify_about_instance_usage(context, instance, "snapshot.end") + compute_utils.notify_about_instance_action(context, instance, + self.host, action=fields.NotificationAction.SNAPSHOT, + phase=fields.NotificationPhase.END) except (exception.InstanceNotFound, exception.UnexpectedDeletingTaskStateError): # the instance got deleted during the snapshot diff --git a/nova/notifications/objects/instance.py b/nova/notifications/objects/instance.py index 1a6c96b4d3e0..3f495b00451d 100644 --- a/nova/notifications/objects/instance.py +++ b/nova/notifications/objects/instance.py @@ -261,8 +261,8 @@ class InstanceStateUpdatePayload(base.NotificationPayloadBase): # @base.notification_sample('instance-reboot-end.json') @base.notification_sample('instance-shutdown-start.json') @base.notification_sample('instance-shutdown-end.json') -# @base.notification_sample('instance-snapshot-start.json') -# @base.notification_sample('instance-snapshot-end.json') +@base.notification_sample('instance-snapshot-start.json') +@base.notification_sample('instance-snapshot-end.json') # @base.notification_sample('instance-add_fixed_ip-start.json') # @base.notification_sample('instance-add_fixed_ip-end.json') @base.notification_sample('instance-shelve-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 9bd2055e38dd..a6fac9e184c6 100644 --- a/nova/tests/functional/notification_sample_tests/test_instance.py +++ b/nova/tests/functional/notification_sample_tests/test_instance.py @@ -72,6 +72,7 @@ class TestInstanceNotificationSample( self._test_shelve_offload_server, self._test_unshelve_server, self._test_resize_server, + self._test_snapshot_server, ] for action in actions: @@ -515,6 +516,25 @@ class TestInstanceNotificationSample( self.api.post_server_action(server['id'], post) self._wait_for_state_change(self.api, server, 'ACTIVE') + def _test_snapshot_server(self, server): + post = {'createImage': {'name': 'test-snap'}} + self.api.post_server_action(server['id'], post) + self._wait_for_notification('instance.snapshot.end') + + self.assertEqual(2, len(fake_notifier.VERSIONED_NOTIFICATIONS)) + self._verify_notification( + 'instance-snapshot-start', + replacements={ + 'reservation_id': server['reservation_id'], + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[0]) + self._verify_notification( + 'instance-snapshot-end', + replacements={ + 'reservation_id': server['reservation_id'], + 'uuid': server['id']}, + actual=fake_notifier.VERSIONED_NOTIFICATIONS[1]) + def _test_restore_server(self, server): self.flags(reclaim_instance_interval=30) self.api.delete_server(server['id']) diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index e8c21e5949f1..daebf6de9305 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -3168,10 +3168,20 @@ class ComputeTestCase(BaseTestCase): instance.save() return instance - def test_snapshot(self): + @mock.patch.object(nova.compute.utils, 'notify_about_instance_action') + def test_snapshot(self, mock_notify_action): inst_obj = self._get_snapshotting_instance() - self.compute.snapshot_instance(self.context, image_id='fakesnap', - instance=inst_obj) + mock_context = mock.Mock() + with mock.patch.object(self.context, 'elevated', + return_value=mock_context) as mock_context_elevated: + self.compute.snapshot_instance(self.context, image_id='fakesnap', + instance=inst_obj) + mock_context_elevated.assert_called_once_with() + mock_notify_action.assert_has_calls([ + mock.call(mock_context, inst_obj, 'fake-mini', + action='snapshot', phase='start'), + mock.call(mock_context, inst_obj, 'fake-mini', + action='snapshot', phase='end')]) def test_snapshot_no_image(self): inst_obj = self._get_snapshotting_instance() diff --git a/nova/virt/fake.py b/nova/virt/fake.py index 1027e2a1fba4..8d7668654204 100644 --- a/nova/virt/fake.py +++ b/nova/virt/fake.py @@ -226,7 +226,8 @@ class FakeDriver(driver.ComputeDriver): def finish_revert_migration(self, context, instance, network_info, block_device_info=None, power_on=True): - pass + self.instances[instance.uuid] = FakeInstance( + instance.name, power_state.RUNNING, instance.uuid) def post_live_migration_at_destination(self, context, instance, network_info,