def test_process_post_generic_notify_trigger_on_custom_emit_when_states(self, mock_LiveAction, mock_dispatch): # Verify that generic action trigger is posted on all completed states when action sensor # is enabled for status in LIVEACTION_STATUSES: notifier = Notifier(connection=None, queues=[]) liveaction_db = LiveActionDB(id=bson.ObjectId(), action='core.local') liveaction_db.status = status execution = MOCK_EXECUTION execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status mock_LiveAction.get_by_id.return_value = liveaction_db notifier = Notifier(connection=None, queues=[]) notifier.process(execution) if status in ['scheduled', 'pending', 'abandoned']: exp = {'status': status, 'start_timestamp': str(liveaction_db.start_timestamp), 'result': {}, 'parameters': {}, 'action_ref': u'core.local', 'runner_ref': 'local-shell-cmd', 'execution_id': str(MOCK_EXECUTION.id), 'action_name': u'core.local'} mock_dispatch.assert_called_with('core.st2.generic.actiontrigger', payload=exp, trace_context={}) self.assertEqual(mock_dispatch.call_count, 3)
def test_notify_triggers_end_timestamp_none(self): liveaction_db = LiveActionDB(action='core.local') liveaction_db.id = bson.ObjectId() liveaction_db.description = '' liveaction_db.status = 'succeeded' liveaction_db.parameters = {} on_success = NotificationSubSchema(message='Action succeeded.') on_failure = NotificationSubSchema(message='Action failed.') liveaction_db.notify = NotificationSchema(on_success=on_success, on_failure=on_failure) liveaction_db.start_timestamp = date_utils.get_datetime_utc_now() # This tests for end_timestamp being set to None, which can happen when a policy cancels # a request. # The assertions within "MockDispatcher.dispatch" will validate that the underlying code # handles this properly, so all we need to do is keep the call to "notifier.process" below liveaction_db.end_timestamp = None LiveAction.add_or_update(liveaction_db) execution = MOCK_EXECUTION execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status dispatcher = NotifierTestCase.MockDispatcher(self) notifier = Notifier(connection=None, queues=[], trigger_dispatcher=dispatcher) notifier.process(execution)
def test_notify_triggers_jinja_patterns(self, dispatch): liveaction_db = LiveActionDB(action='core.local') liveaction_db.id = bson.ObjectId() liveaction_db.description = '' liveaction_db.status = 'succeeded' liveaction_db.parameters = {'cmd': 'mamma mia', 'runner_foo': 'foo'} on_success = NotificationSubSchema(message='Command {{action_parameters.cmd}} succeeded.', data={'stdout': '{{action_results.stdout}}'}) liveaction_db.notify = NotificationSchema(on_success=on_success) liveaction_db.start_timestamp = date_utils.get_datetime_utc_now() liveaction_db.end_timestamp = \ (liveaction_db.start_timestamp + datetime.timedelta(seconds=50)) LiveAction.add_or_update(liveaction_db) execution = MOCK_EXECUTION execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status notifier = Notifier(connection=None, queues=[]) notifier.process(execution) exp = {'status': 'succeeded', 'start_timestamp': isotime.format(liveaction_db.start_timestamp), 'route': 'notify.default', 'runner_ref': 'local-shell-cmd', 'channel': 'notify.default', 'message': u'Command mamma mia succeeded.', 'data': {'result': '{}', 'stdout': 'stuff happens'}, 'action_ref': u'core.local', 'execution_id': str(MOCK_EXECUTION.id), 'end_timestamp': isotime.format(liveaction_db.end_timestamp)} dispatch.assert_called_once_with('core.st2.generic.notifytrigger', payload=exp, trace_context={}) notifier.process(execution)
def test_post_generic_trigger_with_emit_condition(self, dispatch): for status in LIVEACTION_STATUSES: liveaction_db = LiveActionDB(action='core.local') liveaction_db.status = status execution = MOCK_EXECUTION execution.liveaction = vars( LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status notifier = Notifier(connection=None, queues=[]) notifier._post_generic_trigger(liveaction_db, execution) if status in ['scheduled', 'pending', 'abandoned']: exp = { 'status': status, 'start_timestamp': str(liveaction_db.start_timestamp), 'result': {}, 'parameters': {}, 'action_ref': u'core.local', 'runner_ref': 'local-shell-cmd', 'execution_id': str(MOCK_EXECUTION.id), 'action_name': u'core.local' } dispatch.assert_called_with('core.st2.generic.actiontrigger', payload=exp, trace_context={}) self.assertEqual(dispatch.call_count, 3)
def test_post_generic_trigger_with_emit_condition(self, dispatch): for status in LIVEACTION_STATUSES: liveaction_db = LiveActionDB(action="core.local") liveaction_db.status = status execution = MOCK_EXECUTION execution.liveaction = vars( LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status notifier = Notifier(connection=None, queues=[]) notifier._post_generic_trigger(liveaction_db, execution) if status in ["scheduled", "pending", "abandoned"]: exp = { "status": status, "start_timestamp": str(liveaction_db.start_timestamp), "result": {}, "parameters": {}, "action_ref": "core.local", "runner_ref": "local-shell-cmd", "execution_id": str(MOCK_EXECUTION.id), "action_name": "core.local", } dispatch.assert_called_with("core.st2.generic.actiontrigger", payload=exp, trace_context={}) self.assertEqual(dispatch.call_count, 3)
def test_notify_triggers(self): liveaction = LiveActionDB(action='core.local') liveaction.description = '' liveaction.status = 'succeeded' liveaction.parameters = {} on_success = NotificationSubSchema(message='Action succeeded.') on_failure = NotificationSubSchema(message='Action failed.') liveaction.notify = NotificationSchema(on_success=on_success, on_failure=on_failure) liveaction.start_timestamp = date_utils.get_datetime_utc_now() dispatcher = NotifierTestCase.MockDispatcher(self) notifier = Notifier(connection=None, queues=[], trigger_dispatcher=dispatcher) notifier.process(liveaction)
def test_notify_triggers(self): liveaction = LiveActionDB(action='core.local') liveaction.description = '' liveaction.status = 'succeeded' liveaction.parameters = {} on_success = NotificationSubSchema(message='Action succeeded.') on_failure = NotificationSubSchema(message='Action failed.') liveaction.notify = NotificationSchema(on_success=on_success, on_failure=on_failure) liveaction.start_timestamp = date_utils.get_datetime_utc_now() liveaction.end_timestamp = liveaction.start_timestamp + datetime.timedelta(seconds=50) dispatcher = NotifierTestCase.MockDispatcher(self) notifier = Notifier(connection=None, queues=[], trigger_dispatcher=dispatcher) notifier.process(liveaction)
def test_notify_triggers(self): liveaction_db = LiveActionDB(action='core.local') liveaction_db.id = bson.ObjectId() liveaction_db.description = '' liveaction_db.status = 'succeeded' liveaction_db.parameters = {} on_success = NotificationSubSchema(message='Action succeeded.') on_failure = NotificationSubSchema(message='Action failed.') liveaction_db.notify = NotificationSchema(on_success=on_success, on_failure=on_failure) liveaction_db.start_timestamp = date_utils.get_datetime_utc_now() liveaction_db.end_timestamp = \ (liveaction_db.start_timestamp + datetime.timedelta(seconds=50)) LiveAction.add_or_update(liveaction_db) execution = MOCK_EXECUTION execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status dispatcher = NotifierTestCase.MockDispatcher(self) notifier = Notifier(connection=None, queues=[], trigger_dispatcher=dispatcher) notifier.process(execution)
def test_post_generic_trigger_with_emit_condition(self, dispatch): for status in LIVEACTION_STATUSES: liveaction_db = LiveActionDB(action='core.local') liveaction_db.status = status execution = MOCK_EXECUTION execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status notifier = Notifier(connection=None, queues=[]) notifier._post_generic_trigger(liveaction_db, execution) if status in ['scheduled', 'pending', 'abandoned']: exp = {'status': status, 'start_timestamp': str(liveaction_db.start_timestamp), 'result': {}, 'parameters': {}, 'action_ref': u'core.local', 'runner_ref': 'local-shell-cmd', 'execution_id': str(MOCK_EXECUTION.id), 'action_name': u'core.local'} dispatch.assert_called_with('core.st2.generic.actiontrigger', payload=exp, trace_context={}) self.assertEqual(dispatch.call_count, 3)
def test_notify_triggers_jinja_patterns(self, dispatch): liveaction_db = LiveActionDB(action="core.local") liveaction_db.id = bson.ObjectId() liveaction_db.description = "" liveaction_db.status = "succeeded" liveaction_db.parameters = {"cmd": "mamma mia", "runner_foo": "foo"} on_success = NotificationSubSchema( message="Command {{action_parameters.cmd}} succeeded.", data={"stdout": "{{action_results.stdout}}"}, ) liveaction_db.notify = NotificationSchema(on_success=on_success) liveaction_db.start_timestamp = date_utils.get_datetime_utc_now() liveaction_db.end_timestamp = (liveaction_db.start_timestamp + datetime.timedelta(seconds=50)) LiveAction.add_or_update(liveaction_db) execution = MOCK_EXECUTION execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status notifier = Notifier(connection=None, queues=[]) notifier.process(execution) exp = { "status": "succeeded", "start_timestamp": isotime.format(liveaction_db.start_timestamp), "route": "notify.default", "runner_ref": "local-shell-cmd", "channel": "notify.default", "message": "Command mamma mia succeeded.", "data": { "result": "{}", "stdout": "stuff happens" }, "action_ref": "core.local", "execution_id": str(MOCK_EXECUTION.id), "end_timestamp": isotime.format(liveaction_db.end_timestamp), } dispatch.assert_called_once_with("core.st2.generic.notifytrigger", payload=exp, trace_context={}) notifier.process(execution)
def test_process_post_generic_notify_trigger_on_completed_state_default( self, mock_LiveAction, mock_dispatch): # Verify that generic action trigger is posted on all completed states when action sensor # is enabled for status in LIVEACTION_STATUSES: notifier = Notifier(connection=None, queues=[]) liveaction_db = LiveActionDB(id=bson.ObjectId(), action='core.local') liveaction_db.status = status execution = MOCK_EXECUTION execution.liveaction = vars( LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status mock_LiveAction.get_by_id.return_value = liveaction_db notifier = Notifier(connection=None, queues=[]) notifier.process(execution) if status in LIVEACTION_COMPLETED_STATES: exp = { 'status': status, 'start_timestamp': str(liveaction_db.start_timestamp), 'result': {}, 'parameters': {}, 'action_ref': u'core.local', 'runner_ref': 'local-shell-cmd', 'execution_id': str(MOCK_EXECUTION.id), 'action_name': u'core.local' } mock_dispatch.assert_called_with( 'core.st2.generic.actiontrigger', payload=exp, trace_context={}) self.assertEqual(mock_dispatch.call_count, len(LIVEACTION_COMPLETED_STATES))
def test_process_post_generic_notify_trigger_on_custom_emit_when_states( self, mock_LiveAction, mock_dispatch): # Verify that generic action trigger is posted on all completed states when action sensor # is enabled for status in LIVEACTION_STATUSES: notifier = Notifier(connection=None, queues=[]) liveaction_db = LiveActionDB(id=bson.ObjectId(), action="core.local") liveaction_db.status = status execution = MOCK_EXECUTION execution.liveaction = vars( LiveActionAPI.from_model(liveaction_db)) execution.status = liveaction_db.status mock_LiveAction.get_by_id.return_value = liveaction_db notifier = Notifier(connection=None, queues=[]) notifier.process(execution) if status in ["scheduled", "pending", "abandoned"]: exp = { "status": status, "start_timestamp": str(liveaction_db.start_timestamp), "result": {}, "parameters": {}, "action_ref": "core.local", "runner_ref": "local-shell-cmd", "execution_id": str(MOCK_EXECUTION.id), "action_name": "core.local", } mock_dispatch.assert_called_with( "core.st2.generic.actiontrigger", payload=exp, trace_context={}) self.assertEqual(mock_dispatch.call_count, 3)