def setUp(self, mock_rpc): super(NotificationAPITestCase, self).setUp() self.notification_api = ha_api.NotificationAPI() self.req = fakes.HTTPRequest.blank('/v1/notifications', use_admin_context=True) self.context = self.req.environ['masakari.context'] self.host = fakes_data.create_fake_host( name="host_1", id=1, reserved=False, on_maintenance=False, type="fake", control_attributes="fake-control_attributes", uuid=uuidsentinel.fake_host_1, failover_segment_id=uuidsentinel.fake_segment1) self.notification = fakes_data.create_fake_notification( type="VM", id=1, payload={ 'event': 'STOPPED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification)
def test_create(self, mock_host_obj, mock_create, mock_notification_obj, mock_get_all): fake_notification = fakes_data.create_fake_notification( type="COMPUTE_HOST", id=2, payload={ 'event': 'STARTED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification_2) fake_notification_list = [self.notification, fake_notification] mock_get_all.return_value = fake_notification_list notification_data = { "hostname": "fake_host", "payload": { "event": "STARTED", "host_status": "NORMAL", "cluster_status": "OFFLINE" }, "type": "VM", "generated_time": "2016-10-13T09:11:21.656788" } mock_host_obj.return_value = self.host mock_notification_obj.return_value = self.notification result = (self.notification_api.create_notification( self.context, notification_data)) self._assert_notification_data(self.notification, _make_notification_obj(result))
def test_create(self, mock_host_obj, mock_create, mock_notification_obj, mock_get_all): fake_notification = fakes_data.create_fake_notification( type="COMPUTE_HOST", id=2, payload={ 'event': 'STARTED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification_2 ) fake_notification_list = [self.notification, fake_notification] mock_get_all.return_value = fake_notification_list notification_data = {"hostname": "fake_host", "payload": {"event": "STARTED", "host_status": "NORMAL", "cluster_status": "OFFLINE"}, "type": "VM", "generated_time": "2016-10-13T09:11:21.656788"} mock_host_obj.return_value = self.host mock_notification_obj.return_value = self.notification result = (self.notification_api. create_notification(self.context, notification_data)) self._assert_notification_data( self.notification, _make_notification_obj(result))
def setUp(self, mock_rpc): super(NotificationAPITestCase, self).setUp() self.notification_api = ha_api.NotificationAPI() self.req = fakes.HTTPRequest.blank('/v1/notifications', use_admin_context=True) self.context = self.req.environ['masakari.context'] self.failover_segment = fakes_data.create_fake_failover_segment( name="segment1", id=1, description="something", service_type="COMPUTE", recovery_method="auto", uuid=uuidsentinel.fake_segment) self.host = fakes_data.create_fake_host( name="host_1", id=1, reserved=False, on_maintenance=False, type="fake", control_attributes="fake-control_attributes", uuid=uuidsentinel.fake_host_1) self.notification = fakes_data.create_fake_notification( type="VM", id=1, payload={ 'event': 'STOPPED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification) self.exception_duplicate = exception.DuplicateNotification( host='host_1', type='COMPUTE_HOST')
def _get_process_type_notification(self): return fakes.create_fake_notification( type="PROCESS", id=1, payload={ 'event': 'stopped', 'process_name': 'fake_service' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="new", notification_uuid=uuidsentinel.fake_notification)
def _get_vm_type_notification(self): return fakes.create_fake_notification( type="VM", id=1, payload={ 'event': 'LIFECYCLE', 'instance_uuid': uuidsentinel.fake_ins, 'vir_domain_event': 'STOPPED_FAILED' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="new", notification_uuid=uuidsentinel.fake_notification)
def _get_compute_host_type_notification(self): return fakes.create_fake_notification( type="COMPUTE_HOST", id=1, payload={ 'event': 'stopped', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="new", notification_uuid=uuidsentinel.fake_notification)
def _get_process_type_notification(self): return fakes.create_fake_notification( type="PROCESS", id=1, payload={ 'event': 'stopped', 'process_name': 'fake_service' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="new", notification_uuid=uuidsentinel.fake_notification)
def _get_compute_host_type_notification(self): return fakes.create_fake_notification( type="COMPUTE_HOST", id=1, payload={ 'event': 'stopped', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="new", notification_uuid=uuidsentinel.fake_notification)
def _get_vm_type_notification(self): return fakes.create_fake_notification( type="VM", id=1, payload={ 'event': 'LIFECYCLE', 'instance_uuid': uuidsentinel.fake_ins, 'vir_domain_event': 'STOPPED_FAILED' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="new", notification_uuid=uuidsentinel.fake_notification)
def test_process_notification_type_vm_error_event_unmatched( self, mock_save): notification = fakes.create_fake_notification( type="VM", id=1, payload={ 'event': 'fake_event', 'instance_uuid': uuidsentinel.fake_ins, 'vir_domain_event': 'fake_vir_domain_event' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="new", notification_uuid=uuidsentinel.fake_notification) self.engine.process_notification(self.context, notification=notification) self.assertEqual("ignored", notification.status)
def test_create_notification_exception(self, mock_host_obj, mock_notification_obj, mock_get_all, mock_notify_about_notification_api): fake_notification = fakes_data.create_fake_notification( type="COMPUTE_HOST", id=2, payload={ 'event': 'STARTED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification_2) fake_notification_list = [self.notification, fake_notification] mock_get_all.return_value = fake_notification_list notification_data = { "hostname": "fake_host", "payload": { "event": "STARTED", "host_status": "NORMAL", "cluster_status": "OFFLINE" }, "type": "VM", "generated_time": "2016-10-13T09:11:21.656788" } mock_host_obj.return_value = self.host e = exception.InvalidInput(reason="TEST") mock_notification_obj.side_effect = e mock_notify_about_notification_api.return_value = mock.Mock() self.assertRaises(exception.InvalidInput, self.notification_api.create_notification, self.context, notification_data) action = fields.EventNotificationAction.NOTIFICATION_CREATE phase_error = fields.EventNotificationPhase.ERROR notify_calls = [ mock.call(self.context, mock.ANY, action=action, phase=phase_error, exception=e, tb=mock.ANY) ] mock_notify_about_notification_api.assert_has_calls(notify_calls)
def test_get_all(self, mock_get_all): fake_notification = fakes_data.create_fake_notification( type="VM", id=2, payload={ 'event': 'STOPPED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification_2 ) fake_notification_list = [self.notification, fake_notification] mock_get_all.return_value = fake_notification_list result = self.notification_api.get_all(self.context, self.req) for i in range(len(result)): self._assert_notification_data( fake_notification_list[i], result[i])
def test_process_notification_type_vm_error_event_unmatched( self, mock_save): notification = fakes.create_fake_notification( type="VM", id=1, payload={ 'event': 'fake_event', 'instance_uuid': uuidsentinel.fake_ins, 'vir_domain_event': 'fake_vir_domain_event' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="new", notification_uuid=uuidsentinel.fake_notification) self.engine.process_notification(self.context, notification=notification) self.assertEqual("ignored", notification.status)
def test_get_all(self, mock_get_all): fake_notification = fakes_data.create_fake_notification( type="VM", id=2, payload={ 'event': 'STOPPED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification_2) fake_notification_list = [self.notification, fake_notification] mock_get_all.return_value = fake_notification_list result = self.notification_api.get_all(self.context, self.req) for i in range(len(result)): self._assert_notification_data(fake_notification_list[i], result[i])
def setUp(self, mock_rpc): super(NotificationAPITestCase, self).setUp() self.notification_api = ha_api.NotificationAPI() self.req = fakes.HTTPRequest.blank('/v1/notifications', use_admin_context=True) self.context = self.req.environ['masakari.context'] self.host = fakes_data.create_fake_host( name="host_1", id=1, reserved=False, on_maintenance=False, type="fake", control_attributes="fake-control_attributes", uuid=uuidsentinel.fake_host_1, failover_segment_id=uuidsentinel.fake_segment1 ) self.notification = fakes_data.create_fake_notification( type="VM", id=1, payload={ 'event': 'STOPPED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification )
def test_create_notification_exception(self, mock_host_obj, mock_notification_obj, mock_get_all, mock_notify_about_notification_api): fake_notification = fakes_data.create_fake_notification( type="COMPUTE_HOST", id=2, payload={ 'event': 'STARTED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification_2 ) fake_notification_list = [self.notification, fake_notification] mock_get_all.return_value = fake_notification_list notification_data = {"hostname": "fake_host", "payload": {"event": "STARTED", "host_status": "NORMAL", "cluster_status": "OFFLINE"}, "type": "VM", "generated_time": "2016-10-13T09:11:21.656788"} mock_host_obj.return_value = self.host e = exception.InvalidInput(reason="TEST") mock_notification_obj.side_effect = e mock_notify_about_notification_api.return_value = mock.Mock() self.assertRaises(exception.InvalidInput, self.notification_api.create_notification, self.context, notification_data) action = fields.EventNotificationAction.NOTIFICATION_CREATE phase_error = fields.EventNotificationPhase.ERROR notify_calls = [ mock.call(self.context, mock.ANY, action=action, phase=phase_error, exception=e, tb=mock.ANY)] mock_notify_about_notification_api.assert_has_calls(notify_calls)
def test_get_notification_recovery_workflow_details_raises_keyerror( self, mock_get_flows_for_book, mock_get_atoms_for_flow): notification = fakes.create_fake_notification( payload={ 'event': 'LIFECYCLE', 'instance_uuid': uuidsentinel.fake_ins, 'vir_domain_event': 'STOPPED_FAILED'}, source_host_uuid=uuidsentinel.fake_host, notification_uuid=uuidsentinel.fake_notification) fd = models.FlowDetail('test', uuid=notification.notification_uuid) atom1 = models.TaskDetail('StopInstanceTask', uuid=uuidsentinel.atom_id_1) atom1.meta = { 'progress': 1.0, 'progress_details': { 'at_progress': 1.0, 'details': { 'progress_details': [ {'timestamp': '2019-03-11 05:22:20.329171', 'message': 'Stopping instance: ' '87c8ebc3-2a70-49f0-9280-d34662dc203d', 'progress': 0.0}, {'timestamp': '2019-03-11 05:22:28.902665', 'message': "Stopped instance: " "'87c8ebc3-2a70-49f0-9280-d34662dc203d'", 'progress': 1.0}]}}} atom1.state = 'SUCCESS' atom2 = models.TaskDetail('ConfirmInstanceActiveTask', uuid=uuidsentinel.atom_id_2) atom2.meta = { 'progress': 1.0, 'progress_details': { 'at_progress': 1.0, 'details': { 'progress_details': [ {'timestamp': '2019-03-11 05:22:29.597303', 'message': "Confirming instance " "'87c8ebc3-2a70-49f0-9280-d34662dc203d' " "vm_state is ACTIVE", 'progress': 0.0}, {'timestamp': '2019-03-11 05:22:31.916620', 'message': "Confirmed instance " "'87c8ebc3-2a70-49f0-9280-d34662dc203d'" " vm_state is ACTIVE", 'progress': 1.0}] }}} atom2.state = 'SUCCESS' atom3 = models.TaskDetail('StartInstanceTask', uuid=uuidsentinel.atom_id_3) atom3.state = 'RUNNING' def fd_generator(): yield fd def atom_detail_generator(): for atom in [atom1, atom2, atom3]: yield atom flow_details = fd_generator() atom_details = atom_detail_generator() mock_get_flows_for_book.return_value = flow_details mock_get_atoms_for_flow.return_value = atom_details driver.PERSISTENCE_BACKEND = 'memory://' progress_details = ( self.taskflow_driver.get_notification_recovery_workflow_details( self.ctxt, 'auto', notification)) # list of NotificationProgressDetails object expected_result = [] expected_result.append(( fakes.create_fake_notification_progress_details( name=atom1.name, uuid=atom1.uuid, progress=atom1.meta['progress'], state=atom1.state, progress_details=atom1.meta['progress_details'] ['details']['progress_details']))) expected_result.append(( fakes.create_fake_notification_progress_details( name=atom2.name, uuid=atom2.uuid, progress=atom2.meta['progress'], state=atom2.state, progress_details=atom2.meta['progress_details'] ['details']['progress_details']))) self.assertIsNotNone(progress_details) mock_get_flows_for_book.assert_called_once() mock_get_atoms_for_flow.assert_called_once() self.assertObjectList(expected_result, progress_details)
def test_get_notification_recovery_workflow_details( self, mock_get_flows_for_book, mock_get_atoms_for_flow): notification = fakes.create_fake_notification( payload={ 'event': 'LIFECYCLE', 'instance_uuid': uuidsentinel.fake_ins, 'vir_domain_event': 'STOPPED_FAILED'}, source_host_uuid=uuidsentinel.fake_host, notification_uuid=uuidsentinel.fake_notification) fd = models.FlowDetail('test', uuid=notification.notification_uuid) atom1 = models.TaskDetail('StopInstanceTask', uuid=uuidsentinel.atom_id_1) atom1.meta = { 'progress': 1.0, 'progress_details': { 'at_progress': 1.0, 'details': { 'progress_details': [ {'timestamp': '2019-03-11 05:22:20.329171', 'message': 'Stopping instance: ' '87c8ebc3-2a70-49f0-9280-d34662dc203d', 'progress': 0.0}, {'timestamp': '2019-03-11 05:22:28.902665', 'message': "Stopped instance: " "'87c8ebc3-2a70-49f0-9280-d34662dc203d'", 'progress': 1.0}]}}} atom1.state = 'SUCCESS' atom2 = models.TaskDetail('ConfirmInstanceActiveTask', uuid=uuidsentinel.atom_id_2) atom2.meta = { 'progress': 1.0, 'progress_details': { 'at_progress': 1.0, 'details': { 'progress_details': [ {'timestamp': '2019-03-11 05:22:29.597303', 'message': "Confirming instance " "'87c8ebc3-2a70-49f0-9280-d34662dc203d' " "vm_state is ACTIVE", 'progress': 0.0}, {'timestamp': '2019-03-11 05:22:31.916620', 'message': "Confirmed instance " "'87c8ebc3-2a70-49f0-9280-d34662dc203d'" " vm_state is ACTIVE", 'progress': 1.0}] }}} atom2.state = 'SUCCESS' atom3 = models.TaskDetail('StartInstanceTask', uuid=uuidsentinel.atom_id_3) atom3.meta = { 'progress': 1.0, 'progress_details': { 'at_progress': 1.0, 'details': {'progress_details': [ {'timestamp': '2019-03-11 05:22:29.130876', 'message': "Starting instance: " "'87c8ebc3-2a70-49f0-9280-d34662dc203d'", 'progress': 0.0}, {'timestamp': '2019-03-11 05:22:29.525882', 'message': "Instance started: " "'87c8ebc3-2a70-49f0-9280-d34662dc203d'", 'progress': 1.0}]}}} atom3.state = 'SUCCESS' def fd_generator(): yield fd def atom_detail_generator(): for atom in [atom1, atom2, atom3]: yield atom flow_details = fd_generator() atom_details = atom_detail_generator() mock_get_flows_for_book.return_value = flow_details mock_get_atoms_for_flow.return_value = atom_details driver.PERSISTENCE_BACKEND = 'memory://' progress_details = ( self.taskflow_driver.get_notification_recovery_workflow_details( self.ctxt, 'auto', notification)) # list of NotificationProgressDetails object expected_result = [] expected_result.append(( fakes.create_fake_notification_progress_details( name=atom1.name, uuid=atom1.uuid, progress=atom1.meta['progress'], state=atom1.state, progress_details=atom1.meta['progress_details'] ['details']['progress_details']))) expected_result.append(( fakes.create_fake_notification_progress_details( name=atom3.name, uuid=atom3.uuid, progress=atom3.meta['progress'], state=atom3.state, progress_details=atom3.meta['progress_details'] ['details']['progress_details']))) expected_result.append(( fakes.create_fake_notification_progress_details( name=atom2.name, uuid=atom2.uuid, progress=atom2.meta['progress'], state=atom2.state, progress_details=atom2.meta['progress_details'] ['details']['progress_details']))) self.assertIsNotNone(progress_details) mock_get_flows_for_book.assert_called_once() mock_get_atoms_for_flow.assert_called_once() self.assertItemsEqual(expected_result[0], progress_details[0]) self.assertItemsEqual(expected_result[1], progress_details[1]) self.assertItemsEqual(expected_result[2], progress_details[2])