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_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_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)
"k2": "v2", "k3": 3, "k4": True, "k5": { "foo": "bar" }, "k6": [1, 3], } MOCK_TRIGGER_INSTANCE_4 = TriggerInstanceDB() MOCK_TRIGGER_INSTANCE_4.id = "triggerinstance-test4" MOCK_TRIGGER_INSTANCE_4.payload = MOCK_TRIGGER_INSTANCE_PAYLOAD MOCK_TRIGGER_INSTANCE_4.occurrence_time = date_utils.get_datetime_utc_now() MOCK_LIVEACTION = LiveActionDB() MOCK_LIVEACTION.id = "liveaction-test-1.id" MOCK_LIVEACTION.status = "requested" MOCK_EXECUTION = ActionExecutionDB() MOCK_EXECUTION.id = "exec-test-1.id" MOCK_EXECUTION.status = "requested" FAILURE_REASON = "fail!" class BaseRuleEnforcerTestCase(DbTestCase): models = None @classmethod def setUpClass(cls):
MOCK_TRIGGER_INSTANCE.id = 'triggerinstance-test' MOCK_TRIGGER_INSTANCE.payload = {'t1_p': 't1_p_v'} MOCK_TRIGGER_INSTANCE.occurrence_time = date_utils.get_datetime_utc_now() MOCK_TRIGGER_INSTANCE_2 = TriggerInstanceDB() MOCK_TRIGGER_INSTANCE_2.id = 'triggerinstance-test2' MOCK_TRIGGER_INSTANCE_2.payload = {'t1_p': None} MOCK_TRIGGER_INSTANCE_2.occurrence_time = date_utils.get_datetime_utc_now() MOCK_TRIGGER_INSTANCE_3 = TriggerInstanceDB() MOCK_TRIGGER_INSTANCE_3.id = 'triggerinstance-test3' MOCK_TRIGGER_INSTANCE_3.payload = {'t1_p': None, 't2_p': 'value2'} MOCK_TRIGGER_INSTANCE_3.occurrence_time = date_utils.get_datetime_utc_now() MOCK_LIVEACTION = LiveActionDB() MOCK_LIVEACTION.id = 'liveaction-test-1.id' MOCK_LIVEACTION.status = 'requested' MOCK_EXECUTION = ActionExecutionDB() MOCK_EXECUTION.id = 'exec-test-1.id' MOCK_EXECUTION.status = 'requested' FAILURE_REASON = "fail!" class EnforceTest(DbTestCase): models = None @classmethod def setUpClass(cls):
def test_publish_compression(self): live_action_db = LiveActionDB() live_action_db.id = ObjectId() live_action_db.status = "succeeded" live_action_db.action = "core.local" live_action_db.result = {"foo": "bar"} exchange = Exchange("st2.execution.test", type="topic") queue_name = "test-" + str(random.randint(1, 10000)) queue = Queue( name=queue_name, exchange=exchange, routing_key="#", auto_delete=True ) publisher = PoolPublisher() with transport_utils.get_connection() as connection: connection.connect() watcher = QueueConsumer(connection=connection, queue=queue) watcher_thread = eventlet.greenthread.spawn(watcher.run) # Give it some time to start up since we are publishing on a new queue eventlet.sleep(0.5) self.assertEqual(len(watcher.received_messages), 0) # 1. Verify compression is off as a default publisher.publish(payload=live_action_db, exchange=exchange) eventlet.sleep(0.2) self.assertEqual(len(watcher.received_messages), 1) self.assertEqual( watcher.received_messages[0][1].properties["content_type"], "application/x-python-serialize", ) self.assertEqual( watcher.received_messages[0][1].properties["content_encoding"], "binary" ) self.assertEqual( watcher.received_messages[0][1].properties["application_headers"], {} ) self.assertEqual(watcher.received_messages[0][0].id, live_action_db.id) # 2. Verify config level option is used cfg.CONF.set_override(name="compression", group="messaging", override="zstd") publisher.publish(payload=live_action_db, exchange=exchange) eventlet.sleep(0.2) self.assertEqual(len(watcher.received_messages), 2) self.assertEqual( watcher.received_messages[1][1].properties["content_type"], "application/x-python-serialize", ) self.assertEqual( watcher.received_messages[1][1].properties["content_encoding"], "binary" ) self.assertEqual( watcher.received_messages[1][1].properties["application_headers"], {"compression": "application/zstd"}, ) self.assertEqual(watcher.received_messages[1][0].id, live_action_db.id) # 2. Verify argument level option is used and has precedence over config one cfg.CONF.set_override(name="compression", group="messaging", override="zstd") publisher.publish(payload=live_action_db, exchange=exchange, compression="gzip") eventlet.sleep(0.2) self.assertEqual(len(watcher.received_messages), 3) self.assertEqual( watcher.received_messages[2][1].properties["content_type"], "application/x-python-serialize", ) self.assertEqual( watcher.received_messages[2][1].properties["content_encoding"], "binary" ) self.assertEqual( watcher.received_messages[2][1].properties["application_headers"], {"compression": "application/x-gzip"}, ) self.assertEqual(watcher.received_messages[2][0].id, live_action_db.id) watcher_thread.kill()