Exemple #1
0
    def test_handle_trigger_instances(self):
        trigger_instance_1 = container_utils.create_trigger_instance(
            'dummy_pack_1.st2.test.trigger1', {
                'k1': 't1_p_v',
                'k2': 'v2'
            }, date_utils.get_datetime_utc_now())

        trigger_instance_2 = container_utils.create_trigger_instance(
            'dummy_pack_1.st2.test.trigger1', {
                'k1': 't1_p_v',
                'k2': 'v2',
                'k3': 'v3'
            }, date_utils.get_datetime_utc_now())

        trigger_instance_3 = container_utils.create_trigger_instance(
            'dummy_pack_1.st2.test.trigger2', {
                'k1': 't1_p_v',
                'k2': 'v2',
                'k3': 'v3'
            }, date_utils.get_datetime_utc_now())
        instances = [
            trigger_instance_1, trigger_instance_2, trigger_instance_3
        ]
        rules_engine = RulesEngine()
        for instance in instances:
            rules_engine.handle_trigger_instance(instance)
Exemple #2
0
    def test_rule_enforcement_is_created_on_exception_2(self):
        # 1. Exception in payload_lookup.get_value
        rule_enforcement_dbs = list(RuleEnforcement.get_all())
        self.assertEqual(rule_enforcement_dbs, [])

        self._setup_sample_trigger('st2.test.trigger4')
        rule_4_db = self._setup_sample_rule(RULE_4)
        rules = [rule_4_db]
        trigger_instance = container_utils.create_trigger_instance(
            'dummy_pack_1.st2.test.trigger4', {
                'k1': 't2_p_v',
                'k2': 'v2'
            }, date_utils.get_datetime_utc_now())
        trigger = get_trigger_db_by_ref(trigger_instance.trigger)

        rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
        matching_rules = rules_matcher.get_matching_rules()
        self.assertEqual(matching_rules, [])
        self.assertEqual(len(matching_rules), 0)

        rule_enforcement_dbs = list(RuleEnforcement.get_all())
        self.assertEqual(len(rule_enforcement_dbs), 1)

        expected_failure = (
            'Failed to match rule "yoyohoneysingh.st2.test.rule4" against trigger '
            'instance "%s": Failed transforming criteria key trigger.k1: '
            'exception in get_value' % (str(trigger_instance.id)))
        self.assertEqual(rule_enforcement_dbs[0].failure_reason,
                         expected_failure)
        self.assertEqual(rule_enforcement_dbs[0].trigger_instance_id,
                         str(trigger_instance.id))
        self.assertEqual(rule_enforcement_dbs[0].rule['id'], str(rule_4_db.id))
        self.assertEqual(rule_enforcement_dbs[0].status,
                         RULE_ENFORCEMENT_STATUS_FAILED)
Exemple #3
0
    def test_rule_enforcement_is_created_on_exception_3(self):
        # 1. Exception in payload_lookup.get_value
        rule_enforcement_dbs = list(RuleEnforcement.get_all())
        self.assertEqual(rule_enforcement_dbs, [])

        self._setup_sample_trigger("st2.test.trigger4")
        rule_4_db = self._setup_sample_rule(RULE_4)
        rules = [rule_4_db]
        trigger_instance = container_utils.create_trigger_instance(
            "dummy_pack_1.st2.test.trigger4",
            {"k1": "t2_p_v", "k2": "v2"},
            date_utils.get_datetime_utc_now(),
        )
        trigger = get_trigger_db_by_ref(trigger_instance.trigger)

        rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
        matching_rules = rules_matcher.get_matching_rules()
        self.assertEqual(matching_rules, [])
        self.assertEqual(len(matching_rules), 0)

        rule_enforcement_dbs = list(RuleEnforcement.get_all())
        self.assertEqual(len(rule_enforcement_dbs), 1)

        expected_failure = (
            'Failed to match rule "yoyohoneysingh.st2.test.rule4" against trigger '
            'instance "%s": There might be a problem with the criteria in rule '
            "yoyohoneysingh.st2.test.rule4: exception in equals"
            % (str(trigger_instance.id))
        )
        self.assertEqual(rule_enforcement_dbs[0].failure_reason, expected_failure)
        self.assertEqual(
            rule_enforcement_dbs[0].trigger_instance_id, str(trigger_instance.id)
        )
        self.assertEqual(rule_enforcement_dbs[0].rule["id"], str(rule_4_db.id))
        self.assertEqual(rule_enforcement_dbs[0].status, RULE_ENFORCEMENT_STATUS_FAILED)
Exemple #4
0
    def test_rule_enforcement_is_created_on_exception_3(self):
        # 1. Exception in payload_lookup.get_value
        rule_enforcement_dbs = list(RuleEnforcement.get_all())
        self.assertEqual(rule_enforcement_dbs, [])

        self._setup_sample_trigger('st2.test.trigger4')
        rule_4_db = self._setup_sample_rule(RULE_4)
        rules = [rule_4_db]
        trigger_instance = container_utils.create_trigger_instance(
            'dummy_pack_1.st2.test.trigger4',
            {'k1': 't2_p_v', 'k2': 'v2'},
            date_utils.get_datetime_utc_now()
        )
        trigger = get_trigger_db_by_ref(trigger_instance.trigger)

        rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
        matching_rules = rules_matcher.get_matching_rules()
        self.assertEqual(matching_rules, [])
        self.assertEqual(len(matching_rules), 0)

        rule_enforcement_dbs = list(RuleEnforcement.get_all())
        self.assertEqual(len(rule_enforcement_dbs), 1)

        expected_failure = ('Failed to match rule "yoyohoneysingh.st2.test.rule4" against trigger '
                            'instance "%s": There might be a problem with the criteria in rule '
                            'yoyohoneysingh.st2.test.rule4: exception in equals' %
                            (str(trigger_instance.id)))
        self.assertEqual(rule_enforcement_dbs[0].failure_reason, expected_failure)
        self.assertEqual(rule_enforcement_dbs[0].trigger_instance_id, str(trigger_instance.id))
        self.assertEqual(rule_enforcement_dbs[0].rule['id'], str(rule_4_db.id))
        self.assertEqual(rule_enforcement_dbs[0].status, RULE_ENFORCEMENT_STATUS_FAILED)
Exemple #5
0
    def process(self, instance):
        trigger = instance['trigger']
        payload = instance['payload']

        trigger_instance = None
        try:
            trigger_instance = container_utils.create_trigger_instance(
                trigger,
                payload or {},
                date_utils.get_datetime_utc_now(),
                raise_on_no_trigger=True)
        except:
            # We got a trigger ref but we were unable to create a trigger instance.
            # This could be because a trigger object wasn't found in db for the ref.
            LOG.exception('Failed to create trigger_instance %s.', instance)
            return

        if trigger_instance:
            try:
                self.rules_engine.handle_trigger_instance(trigger_instance)
            except:
                # This could be a large message but at least in case of an exception
                # we get to see more context.
                # Beyond this point code cannot really handle the exception anyway so
                # eating up the exception.
                LOG.exception('Failed to handle trigger_instance %s.',
                              instance)
                return
Exemple #6
0
    def process(self, instance):
        trigger = instance['trigger']
        payload = instance['payload']

        trigger_instance = None
        try:
            trigger_instance = container_utils.create_trigger_instance(
                trigger,
                payload or {},
                date_utils.get_datetime_utc_now(),
                raise_on_no_trigger=True)
        except:
            # We got a trigger ref but we were unable to create a trigger instance.
            # This could be because a trigger object wasn't found in db for the ref.
            LOG.exception('Failed to create trigger_instance %s.', instance)
            return

        if trigger_instance:
            try:
                self.rules_engine.handle_trigger_instance(trigger_instance)
            except:
                # This could be a large message but at least in case of an exception
                # we get to see more context.
                # Beyond this point code cannot really handle the exception anyway so
                # eating up the exception.
                LOG.exception('Failed to handle trigger_instance %s.', instance)
                return
Exemple #7
0
    def test_trigger_instance_payload_with_special_values(self):
        # Test a rule where TriggerInstance payload contains a dot (".") and $
        self._setup_sample_trigger("st2.test.trigger1")
        self._setup_sample_trigger("st2.test.trigger2")
        rule_db_1 = self._setup_sample_rule(RULE_1)
        rule_db_2 = self._setup_sample_rule(RULE_2)
        rule_db_3 = self._setup_sample_rule(RULE_3)
        rules = [rule_db_1, rule_db_2, rule_db_3]
        trigger_instance = container_utils.create_trigger_instance(
            "dummy_pack_1.st2.test.trigger2",
            {
                "k1": "t1_p_v",
                "k2.k2": "v2",
                "k3.more.nested.deep": "some.value",
                "k4.even.more.nested$": "foo",
                "yep$aaa": "b",
            },
            date_utils.get_datetime_utc_now(),
        )

        trigger = get_trigger_db_by_ref(trigger_instance.trigger)
        rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
        matching_rules = rules_matcher.get_matching_rules()
        self.assertIsNotNone(matching_rules)
        self.assertEqual(len(matching_rules), 1)
Exemple #8
0
 def test_handle_trigger_instance_no_rules(self):
     trigger_instance = container_utils.create_trigger_instance(
         "dummy_pack_1.st2.test.trigger3",
         {"k1": "t1_p_v", "k2": "v2"},
         date_utils.get_datetime_utc_now(),
     )
     rules_engine = RulesEngine()
     rules_engine.handle_trigger_instance(trigger_instance)  # should not throw.
Exemple #9
0
    def dispatch(self, trigger, payload=None):
        """
        """
        trigger_instance = container_utils.create_trigger_instance(
            trigger, payload or {}, datetime.datetime.utcnow())

        if trigger_instance:
            self.rules_engine.handle_trigger_instance(trigger_instance)
Exemple #10
0
 def test_handle_trigger_instance_no_rules(self):
     trigger_instance = container_utils.create_trigger_instance(
         'dummy_pack_1.st2.test.trigger3',
         {'k1': 't1_p_v', 'k2': 'v2'},
         date_utils.get_datetime_utc_now()
     )
     rules_engine = RulesEngine()
     rules_engine.handle_trigger_instance(trigger_instance)  # should not throw.
 def test_handle_trigger_instance_no_rules(self):
     trigger_instance = container_utils.create_trigger_instance(
         'dummy_pack_1.st2.test.trigger3',
         {'k1': 't1_p_v', 'k2': 'v2'},
         date_utils.get_datetime_utc_now()
     )
     rules_engine = RulesEngine()
     rules_engine.handle_trigger_instance(trigger_instance)  # should not throw.
Exemple #12
0
 def test_get_matching_rules_filters_disabled_rules(self):
     trigger_instance = container_utils.create_trigger_instance(
         "dummy_pack_1.st2.test.trigger1", {"k1": "t1_p_v", "k2": "v2"}, date_utils.get_datetime_utc_now()
     )
     rules_engine = RulesEngine()
     matching_rules = rules_engine.get_matching_rules_for_trigger(trigger_instance)
     expected_rules = ["st2.test.rule2"]
     for rule in matching_rules:
         self.assertTrue(rule.name in expected_rules)
Exemple #13
0
 def test_create_trigger_instance_for_trigger_with_params(self):
     trigger = {"type": "dummy_pack_1.st2.test.trigger4", "parameters": {"url": "sample"}}
     payload = {"k1": "t1_p_v", "k2": "v2", "k3": "v3"}
     occurrence_time = date_utils.get_datetime_utc_now()
     trigger_instance = container_utils.create_trigger_instance(
         trigger=trigger, payload=payload, occurrence_time=occurrence_time
     )
     self.assertTrue(trigger_instance)
     self.assertEqual(trigger_instance.trigger, trigger["type"])
     self.assertEqual(trigger_instance.payload, payload)
 def test_get_matching_rules_filters_disabled_rules(self):
     trigger_instance = container_utils.create_trigger_instance(
         'dummy_pack_1.st2.test.trigger1',
         {'k1': 't1_p_v', 'k2': 'v2'}, date_utils.get_datetime_utc_now()
     )
     rules_engine = RulesEngine()
     matching_rules = rules_engine.get_matching_rules_for_trigger(trigger_instance)
     expected_rules = ['st2.test.rule2']
     for rule in matching_rules:
         self.assertTrue(rule.name in expected_rules)
Exemple #15
0
 def test_get_matching_rules_filters_disabled_rules(self):
     trigger_instance = container_utils.create_trigger_instance(
         'dummy_pack_1.st2.test.trigger1',
         {'k1': 't1_p_v', 'k2': 'v2'}, date_utils.get_datetime_utc_now()
     )
     rules_engine = RulesEngine()
     matching_rules = rules_engine.get_matching_rules_for_trigger(trigger_instance)
     expected_rules = ['st2.test.rule2']
     for rule in matching_rules:
         self.assertTrue(rule.name in expected_rules)
Exemple #16
0
 def test_create_trigger_instance_for_trigger_with_params(self):
     trigger = {'type': 'dummy_pack_1.st2.test.trigger4', 'parameters': {'url': 'sample'}}
     payload = {'k1': 't1_p_v', 'k2': 'v2', 'k3': 'v3'}
     occurrence_time = date_utils.get_datetime_utc_now()
     trigger_instance = container_utils.create_trigger_instance(trigger=trigger,
                                                                payload=payload,
                                                                occurrence_time=occurrence_time)
     self.assertTrue(trigger_instance)
     self.assertEqual(trigger_instance.trigger, trigger['type'])
     self.assertEqual(trigger_instance.payload, payload)
 def test_create_trigger_instance_for_trigger_with_params(self):
     trigger = {'type': 'dummy_pack_1.st2.test.trigger4', 'parameters': {'url': 'sample'}}
     payload = {'k1': 't1_p_v', 'k2': 'v2', 'k3': 'v3'}
     occurrence_time = date_utils.get_datetime_utc_now()
     trigger_instance = container_utils.create_trigger_instance(trigger=trigger,
                                                                payload=payload,
                                                                occurrence_time=occurrence_time)
     self.assertTrue(trigger_instance)
     self.assertEqual(trigger_instance.trigger, trigger['type'])
     self.assertEqual(trigger_instance.payload, payload)
Exemple #18
0
 def test_backstop_ignore(self):
     trigger_instance = container_utils.create_trigger_instance(
         self.models['triggers']['trigger1.yaml'].ref, {'k1': 'v1'},
         date_utils.get_datetime_utc_now())
     trigger = self.models['triggers']['trigger1.yaml']
     rules = [rule for rule in six.itervalues(self.models['rules'])]
     rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
     matching_rules = rules_matcher.get_matching_rules()
     self.assertEqual(len(matching_rules), 1)
     self.assertEqual(matching_rules[0].id,
                      self.models['rules']['success.yaml'].id)
Exemple #19
0
 def test_get_matching_rules_filters_disabled_rules(self):
     trigger_instance = container_utils.create_trigger_instance(
         "dummy_pack_1.st2.test.trigger1",
         {"k1": "t1_p_v", "k2": "v2"},
         date_utils.get_datetime_utc_now(),
     )
     rules_engine = RulesEngine()
     matching_rules = rules_engine.get_matching_rules_for_trigger(trigger_instance)
     expected_rules = ["st2.test.rule2"]
     for rule in matching_rules:
         self.assertIn(rule.name, expected_rules)
Exemple #20
0
    def test_handle_trigger_instances(self):
        trigger_instance_1 = container_utils.create_trigger_instance(
            "dummy_pack_1.st2.test.trigger1", {"k1": "t1_p_v", "k2": "v2"}, date_utils.get_datetime_utc_now()
        )

        trigger_instance_2 = container_utils.create_trigger_instance(
            "dummy_pack_1.st2.test.trigger1",
            {"k1": "t1_p_v", "k2": "v2", "k3": "v3"},
            date_utils.get_datetime_utc_now(),
        )

        trigger_instance_3 = container_utils.create_trigger_instance(
            "dummy_pack_1.st2.test.trigger2",
            {"k1": "t1_p_v", "k2": "v2", "k3": "v3"},
            date_utils.get_datetime_utc_now(),
        )
        instances = [trigger_instance_1, trigger_instance_2, trigger_instance_3]
        rules_engine = RulesEngine()
        for instance in instances:
            rules_engine.handle_trigger_instance(instance)
Exemple #21
0
 def test_backstop_ignore(self):
     trigger_instance = container_utils.create_trigger_instance(
         self.models['triggers']['trigger1.yaml'].ref,
         {'k1': 'v1'},
         date_utils.get_datetime_utc_now()
     )
     trigger = self.models['triggers']['trigger1.yaml']
     rules = [rule for rule in six.itervalues(self.models['rules'])]
     rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
     matching_rules = rules_matcher.get_matching_rules()
     self.assertEqual(len(matching_rules), 1)
     self.assertEqual(matching_rules[0].id, self.models['rules']['success.yaml'].id)
Exemple #22
0
 def test_backstop_ignore(self):
     trigger_instance = container_utils.create_trigger_instance(
         self.models["triggers"]["trigger1.yaml"].ref,
         {"k1": "v1"},
         date_utils.get_datetime_utc_now(),
     )
     trigger = self.models["triggers"]["trigger1.yaml"]
     rules = [rule for rule in six.itervalues(self.models["rules"])]
     rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
     matching_rules = rules_matcher.get_matching_rules()
     self.assertEqual(len(matching_rules), 1)
     self.assertEqual(matching_rules[0].id, self.models["rules"]["success.yaml"].id)
Exemple #23
0
 def test_get_matching_rules(self):
     self._setup_sample_trigger('st2.test.trigger1')
     trigger_instance = container_utils.create_trigger_instance(
         'dummy_pack_1.st2.test.trigger1', {
             'k1': 't1_p_v',
             'k2': 'v2'
         }, date_utils.get_datetime_utc_now())
     trigger = get_trigger_db_by_ref(trigger_instance.trigger)
     rules = self._get_sample_rules()
     rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
     matching_rules = rules_matcher.get_matching_rules()
     self.assertTrue(matching_rules is not None)
     self.assertEqual(len(matching_rules), 1)
Exemple #24
0
    def test_handle_trigger_instances(self):
        trigger_instance_1 = container_utils.create_trigger_instance(
            "dummy_pack_1.st2.test.trigger1",
            {"k1": "t1_p_v", "k2": "v2"},
            date_utils.get_datetime_utc_now(),
        )

        trigger_instance_2 = container_utils.create_trigger_instance(
            "dummy_pack_1.st2.test.trigger1",
            {"k1": "t1_p_v", "k2": "v2", "k3": "v3"},
            date_utils.get_datetime_utc_now(),
        )

        trigger_instance_3 = container_utils.create_trigger_instance(
            "dummy_pack_1.st2.test.trigger2",
            {"k1": "t1_p_v", "k2": "v2", "k3": "v3"},
            date_utils.get_datetime_utc_now(),
        )
        instances = [trigger_instance_1, trigger_instance_2, trigger_instance_3]
        rules_engine = RulesEngine()
        for instance in instances:
            rules_engine.handle_trigger_instance(instance)
Exemple #25
0
    def test_handle_trigger_instances(self):
        trigger_instance_1 = container_utils.create_trigger_instance(
            'dummy_pack_1.st2.test.trigger1',
            {'k1': 't1_p_v', 'k2': 'v2'},
            date_utils.get_datetime_utc_now()
        )

        trigger_instance_2 = container_utils.create_trigger_instance(
            'dummy_pack_1.st2.test.trigger1',
            {'k1': 't1_p_v', 'k2': 'v2', 'k3': 'v3'},
            date_utils.get_datetime_utc_now()
        )

        trigger_instance_3 = container_utils.create_trigger_instance(
            'dummy_pack_1.st2.test.trigger2',
            {'k1': 't1_p_v', 'k2': 'v2', 'k3': 'v3'},
            date_utils.get_datetime_utc_now()
        )
        instances = [trigger_instance_1, trigger_instance_2, trigger_instance_3]
        rules_engine = RulesEngine()
        for instance in instances:
            rules_engine.handle_trigger_instance(instance)
Exemple #26
0
 def test_create_trigger_instance_for_trigger_with_params(self):
     trigger = {
         "type": "dummy_pack_1.st2.test.trigger4",
         "parameters": {"url": "sample"},
     }
     payload = {"k1": "t1_p_v", "k2": "v2", "k3": "v3"}
     occurrence_time = date_utils.get_datetime_utc_now()
     trigger_instance = container_utils.create_trigger_instance(
         trigger=trigger, payload=payload, occurrence_time=occurrence_time
     )
     self.assertTrue(trigger_instance)
     self.assertEqual(trigger_instance.trigger, trigger["type"])
     self.assertEqual(trigger_instance.payload, payload)
 def test_get_matching_rules(self):
     self._setup_sample_trigger('st2.test.trigger1')
     trigger_instance = container_utils.create_trigger_instance(
         'dummy_pack_1.st2.test.trigger1',
         {'k1': 't1_p_v', 'k2': 'v2'},
         datetime.datetime.utcnow()
     )
     trigger = get_trigger_db_by_ref(trigger_instance.trigger)
     rules = self._get_sample_rules()
     rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
     matching_rules = rules_matcher.get_matching_rules()
     self.assertTrue(matching_rules is not None)
     self.assertEqual(len(matching_rules), 1)
 def test_trigger_instance_payload_with_special_values(self):
     # Test a rule where TriggerInstance payload contains a dot (".") and $
     self._setup_sample_trigger('st2.test.trigger2')
     trigger_instance = container_utils.create_trigger_instance(
         'dummy_pack_1.st2.test.trigger2',
         {'k1': 't1_p_v', 'k2.k2': 'v2', 'k3.more.nested.deep': 'some.value',
          'k4.even.more.nested$': 'foo', 'yep$aaa': 'b'},
         date_utils.get_datetime_utc_now()
     )
     trigger = get_trigger_db_by_ref(trigger_instance.trigger)
     rules = self._get_sample_rules()
     rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
     matching_rules = rules_matcher.get_matching_rules()
     self.assertTrue(matching_rules is not None)
     self.assertEqual(len(matching_rules), 1)
Exemple #29
0
 def test_trigger_instance_payload_with_special_values(self):
     # Test a rule where TriggerInstance payload contains a dot (".") and $
     self._setup_sample_trigger('st2.test.trigger2')
     trigger_instance = container_utils.create_trigger_instance(
         'dummy_pack_1.st2.test.trigger2',
         {'k1': 't1_p_v', 'k2.k2': 'v2', 'k3.more.nested.deep': 'some.value',
          'k4.even.more.nested$': 'foo', 'yep$aaa': 'b'},
         date_utils.get_datetime_utc_now()
     )
     trigger = get_trigger_db_by_ref(trigger_instance.trigger)
     rules = self._get_sample_rules()
     rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
     matching_rules = rules_matcher.get_matching_rules()
     self.assertTrue(matching_rules is not None)
     self.assertEqual(len(matching_rules), 1)
Exemple #30
0
    def process(self, instance):
        trigger = instance['trigger']
        payload = instance['payload']

        try:
            trigger_instance = container_utils.create_trigger_instance(
                trigger, payload or {}, date_utils.get_datetime_utc_now())

            if trigger_instance:
                self.rules_engine.handle_trigger_instance(trigger_instance)
        except:
            # This could be a large message but at least in case of an exception
            # we get to see more context.
            # Beyond this point code cannot really handle the exception anyway so
            # eating up the exception.
            LOG.exception('Failed to handle trigger_instance %s.', instance)
Exemple #31
0
    def pre_ack_process(self, message):
        '''
        TriggerInstance from message is create prior to acknowledging the message. This
        gets us a way to not acknowledge messages.
        '''
        trigger = message['trigger']
        payload = message['payload']

        # Accomodate for not being able to create a TrigegrInstance if a TriggerDB
        # is not found.
        trigger_instance = container_utils.create_trigger_instance(
            trigger,
            payload or {},
            date_utils.get_datetime_utc_now(),
            raise_on_no_trigger=True)

        return self._compose_pre_ack_process_response(trigger_instance, message)
Exemple #32
0
    def test_get_matching_rules(self):
        self._setup_sample_trigger("st2.test.trigger1")
        rule_db_1 = self._setup_sample_rule(RULE_1)
        rule_db_2 = self._setup_sample_rule(RULE_2)
        rule_db_3 = self._setup_sample_rule(RULE_3)
        rules = [rule_db_1, rule_db_2, rule_db_3]
        trigger_instance = container_utils.create_trigger_instance(
            "dummy_pack_1.st2.test.trigger1",
            {"k1": "t1_p_v", "k2": "v2"},
            date_utils.get_datetime_utc_now(),
        )

        trigger = get_trigger_db_by_ref(trigger_instance.trigger)
        rules_matcher = RulesMatcher(trigger_instance, trigger, rules)
        matching_rules = rules_matcher.get_matching_rules()
        self.assertIsNotNone(matching_rules)
        self.assertEqual(len(matching_rules), 1)
Exemple #33
0
    def pre_ack_process(self, message):
        '''
        TriggerInstance from message is create prior to acknowledging the message. This
        gets us a way to not acknowledge messages.
        '''
        trigger = message['trigger']
        payload = message['payload']

        # Accomodate for not being able to create a TrigegrInstance if a TriggerDB
        # is not found.
        trigger_instance = container_utils.create_trigger_instance(
            trigger,
            payload or {},
            date_utils.get_datetime_utc_now(),
            raise_on_no_trigger=True)

        return self._compose_pre_ack_process_response(trigger_instance, message)
Exemple #34
0
    def process(self, instance):
        trigger = instance['trigger']
        payload = instance['payload']

        try:
            trigger_instance = container_utils.create_trigger_instance(
                trigger,
                payload or {},
                datetime.datetime.utcnow())

            if trigger_instance:
                self.rules_engine.handle_trigger_instance(trigger_instance)
        except:
            # This could be a large message but at least in case of an exception
            # we get to see more context.
            # Beyond this point code cannot really handle the exception anyway so
            # eating up the exception.
            LOG.exception('Failed to handle trigger_instance %s.', instance)
Exemple #35
0
    def process(self, instance):
        trigger = instance['trigger']
        payload = instance['payload']

        trigger_instance = None
        try:
            trigger_instance = container_utils.create_trigger_instance(
                trigger,
                payload or {},
                date_utils.get_datetime_utc_now(),
                raise_on_no_trigger=True)
        except:
            # We got a trigger ref but we were unable to create a trigger instance.
            # This could be because a trigger object wasn't found in db for the ref.
            LOG.exception('Failed to create trigger_instance %s.', instance)
            return

        if trigger_instance:
            try:
                # Use trace_context from the instance and if not found create a new context
                # and use the trigger_instance.id as trace_tag.
                trace_context = instance.get(TRACE_CONTEXT, None)
                if not trace_context:
                    trace_context = {
                        TRACE_ID:
                        'trigger_instance-%s' % str(trigger_instance.id)
                    }
                # add a trace or update an existing trace with trigger_instance
                trace_service.add_or_update_given_trace_context(
                    trace_context=trace_context,
                    trigger_instances=[
                        trace_service.get_trace_component_for_trigger_instance(
                            trigger_instance)
                    ])
                self.rules_engine.handle_trigger_instance(trigger_instance)
            except:
                # This could be a large message but at least in case of an exception
                # we get to see more context.
                # Beyond this point code cannot really handle the exception anyway so
                # eating up the exception.
                LOG.exception('Failed to handle trigger_instance %s.',
                              instance)
                return
Exemple #36
0
    def process(self, instance):
        trigger = instance['trigger']
        payload = instance['payload']

        trigger_instance = None
        try:
            trigger_instance = container_utils.create_trigger_instance(
                trigger,
                payload or {},
                date_utils.get_datetime_utc_now(),
                raise_on_no_trigger=True)
        except:
            # We got a trigger ref but we were unable to create a trigger instance.
            # This could be because a trigger object wasn't found in db for the ref.
            LOG.exception('Failed to create trigger_instance %s.', instance)
            return

        if trigger_instance:
            try:
                # Use trace_context from the instance and if not found create a new context
                # and use the trigger_instance.id as trace_tag.
                trace_context = instance.get(TRACE_CONTEXT, None)
                if not trace_context:
                    trace_context = {
                        TRACE_ID: 'trigger_instance-%s' % str(trigger_instance.id)
                    }
                # add a trace or update an existing trace with trigger_instance
                trace_service.add_or_update_given_trace_context(
                    trace_context=trace_context,
                    trigger_instances=[
                        trace_service.get_trace_component_for_trigger_instance(trigger_instance)
                    ])
                self.rules_engine.handle_trigger_instance(trigger_instance)
            except:
                # This could be a large message but at least in case of an exception
                # we get to see more context.
                # Beyond this point code cannot really handle the exception anyway so
                # eating up the exception.
                LOG.exception('Failed to handle trigger_instance %s.', instance)
                return
Exemple #37
0
    def test_create_trigger_instance_success(self):
        # Here we test trigger instance creation using various ways to look up corresponding
        # TriggerDB object
        payload = {}
        occurrence_time = None

        # TriggerDB look up by id
        trigger = {'id': self.trigger_db.id}
        trigger_instance_db = create_trigger_instance(
            trigger=trigger, payload=payload, occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db.trigger, 'pack1.name1')

        # Object doesn't exist (invalid id)
        trigger = {'id': '5776aa2b0640fd2991b15987'}
        trigger_instance_db = create_trigger_instance(
            trigger=trigger, payload=payload, occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db, None)

        # TriggerDB look up by uid
        trigger = {'uid': self.trigger_db.uid}
        trigger_instance_db = create_trigger_instance(
            trigger=trigger, payload=payload, occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db.trigger, 'pack1.name1')

        trigger = {'uid': 'invaliduid'}
        trigger_instance_db = create_trigger_instance(
            trigger=trigger, payload=payload, occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db, None)

        # TriggerDB look up by type and parameters (last resort)
        trigger = {
            'type': 'pack1.name1',
            'parameters': self.trigger_db.parameters
        }
        trigger_instance_db = create_trigger_instance(
            trigger=trigger, payload=payload, occurrence_time=occurrence_time)

        trigger = {'type': 'pack1.name1', 'parameters': {}}
        trigger_instance_db = create_trigger_instance(
            trigger=trigger, payload=payload, occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db, None)
Exemple #38
0
    def test_create_trigger_instance_success(self):
        # Here we test trigger instance creation using various ways to look up corresponding
        # TriggerDB object
        payload = {}
        occurrence_time = None

        # TriggerDB look up by id
        trigger = {'id': self.trigger_db.id}
        trigger_instance_db = create_trigger_instance(trigger=trigger, payload=payload,
                                                      occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db.trigger, 'pack1.name1')

        # Object doesn't exist (invalid id)
        trigger = {'id': '5776aa2b0640fd2991b15987'}
        trigger_instance_db = create_trigger_instance(trigger=trigger, payload=payload,
                                                      occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db, None)

        # TriggerDB look up by uid
        trigger = {'uid': self.trigger_db.uid}
        trigger_instance_db = create_trigger_instance(trigger=trigger, payload=payload,
                                                      occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db.trigger, 'pack1.name1')

        trigger = {'uid': 'invaliduid'}
        trigger_instance_db = create_trigger_instance(trigger=trigger, payload=payload,
                                                      occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db, None)

        # TriggerDB look up by type and parameters (last resort)
        trigger = {'type': 'pack1.name1', 'parameters': self.trigger_db.parameters}
        trigger_instance_db = create_trigger_instance(trigger=trigger, payload=payload,
                                                      occurrence_time=occurrence_time)

        trigger = {'type': 'pack1.name1', 'parameters': {}}
        trigger_instance_db = create_trigger_instance(trigger=trigger, payload=payload,
                                                      occurrence_time=occurrence_time)
        self.assertEqual(trigger_instance_db, None)
Exemple #39
0
 def test_create_trigger_instance_invalid_trigger(self):
     trigger_instance = {'name': 'footrigger', 'pack': 'dummy_pack'}
     instance = container_utils.create_trigger_instance(
         trigger_instance, {}, None)
     self.assertTrue(instance is None)
Exemple #40
0
 def test_handle_trigger_instance_no_rules(self):
     trigger_instance = container_utils.create_trigger_instance(
         "dummy_pack_1.st2.test.trigger3", {"k1": "t1_p_v", "k2": "v2"}, date_utils.get_datetime_utc_now()
     )
     rules_engine = RulesEngine()
     rules_engine.handle_trigger_instance(trigger_instance)  # should not throw.
 def test_create_trigger_instance_invalid_trigger(self):
     trigger_instance = {'name': 'footrigger', 'pack': 'dummy_pack'}
     instance = container_utils.create_trigger_instance(trigger_instance, {}, None)
     self.assertTrue(instance is None)
Exemple #42
0
 def test_create_trigger_instance_invalid_trigger(self):
     trigger_instance = 'dummy_pack.footrigger'
     instance = create_trigger_instance(trigger=trigger_instance, payload={},
                                        occurrence_time=None)
     self.assertTrue(instance is None)
Exemple #43
0
    def _do_process_task(self, trigger, payload):
        trigger_instance = container_utils.create_trigger_instance(
            trigger, payload or {}, datetime.datetime.utcnow())

        if trigger_instance:
            self.rules_engine.handle_trigger_instance(trigger_instance)
 def test_create_trigger_instance_invalid_trigger(self):
     trigger_instance = 'dummy_pack.footrigger'
     instance = container_utils.create_trigger_instance(trigger_instance, {}, None)
     self.assertTrue(instance is None)
Exemple #45
0
 def test_create_trigger_instance_invalid_trigger(self):
     trigger_instance = 'dummy_pack.footrigger'
     instance = container_utils.create_trigger_instance(trigger_instance, {}, None)
     self.assertTrue(instance is None)