Ejemplo n.º 1
0
    def test_liveaction_create_with_notify_on_success_only(self):
        created = LiveActionDB()
        created.action = 'core.local'
        created.description = ''
        created.status = 'running'
        created.parameters = {}
        notify_db = NotificationSchema()
        notify_sub_schema = NotificationSubSchema()
        notify_sub_schema.message = 'Action succeeded.'
        notify_sub_schema.data = {
            'foo': 'bar',
            'bar': 1,
            'baz': {'k1': 'v1'}
        }
        notify_db.on_success = notify_sub_schema
        created.notify = notify_db
        saved = LiveActionModelTest._save_liveaction(created)
        retrieved = LiveAction.get_by_id(saved.id)
        self.assertEqual(saved.action, retrieved.action,
                         'Same triggertype was not returned.')

        # Assert notify settings saved are right.
        self.assertEqual(notify_sub_schema.message,
                         retrieved.notify.on_success.message)
        self.assertDictEqual(notify_sub_schema.data, retrieved.notify.on_success.data)
        self.assertListEqual(notify_sub_schema.routes, retrieved.notify.on_success.routes)
        self.assertEqual(retrieved.notify.on_failure, None)
        self.assertEqual(retrieved.notify.on_complete, None)
Ejemplo n.º 2
0
 def _get_notify_field(self, payload):
     on_complete = NotificationSubSchema()
     on_complete.channels = [payload.notification_channel]
     on_complete.data = {
         'user': payload.user,
         'source_channel': payload.source_channel
     }
     notify = NotificationSchema()
     notify.on_complete = on_complete
     return notify
Ejemplo n.º 3
0
 def _get_notify_field(self, payload):
     on_complete = NotificationSubSchema()
     route = (getattr(payload, 'notification_route', None) or
              getattr(payload, 'notification_channel', None))
     on_complete.routes = [route]
     on_complete.data = {
         'user': payload.user,
         'source_channel': payload.source_channel
     }
     notify = NotificationSchema()
     notify.on_complete = on_complete
     return notify
Ejemplo n.º 4
0
 def to_model(notify_api_object):
     model = NotificationSchema()
     if notify_api_object.get('on-complete', None):
         model.on_complete = NotificationsHelper._to_model_sub_schema(
             notify_api_object['on-complete'])
     if notify_api_object.get('on-success', None):
         model.on_success = NotificationsHelper._to_model_sub_schema(
             notify_api_object['on-success'])
     if notify_api_object.get('on-failure', None):
         model.on_failure = NotificationsHelper._to_model_sub_schema(
             notify_api_object['on-failure'])
     return model
Ejemplo n.º 5
0
    def to_model(notify_api_object):
        if notify_api_object.get('on-success', None):
            on_success = NotificationsHelper._to_model_sub_schema(
                notify_api_object['on-success'])
        else:
            on_success = None

        if notify_api_object.get('on-complete', None):
            on_complete = NotificationsHelper._to_model_sub_schema(
                notify_api_object['on-complete'])
        else:
            on_complete = None

        if notify_api_object.get('on-failure', None):
            on_failure = NotificationsHelper._to_model_sub_schema(
                notify_api_object['on-failure'])
        else:
            on_failure = None

        model = NotificationSchema(on_success=on_success,
                                   on_failure=on_failure,
                                   on_complete=on_complete)
        return model
Ejemplo n.º 6
0
    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_notify_triggers_jinja_patterns(self, dispatch):
        liveaction = LiveActionDB(action='core.local')
        liveaction.description = ''
        liveaction.status = 'succeeded'
        liveaction.parameters = {'cmd': 'mamma mia', 'runner_foo': 'foo'}
        on_success = NotificationSubSchema(message='Command {{action_parameters.cmd}} succeeded.',
                                           data={'stdout': '{{action_results.stdout}}'})
        liveaction.notify = NotificationSchema(on_success=on_success)
        liveaction.start_timestamp = date_utils.get_datetime_utc_now()
        liveaction.end_timestamp = liveaction.start_timestamp + datetime.timedelta(seconds=50)

        notifier = Notifier(connection=None, queues=[])
        notifier.process(liveaction)
        exp = {'status': 'succeeded',
               'start_timestamp': isotime.format(liveaction.start_timestamp),
               'route': 'notify.default', 'runner_ref': 'run-local-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.end_timestamp)}
        dispatch.assert_called_once_with('core.st2.generic.notifytrigger', payload=exp,
                                         trace_context={})
        notifier.process(liveaction)