Example #1
0
 def _create_save_triggertype():
     created = TriggerTypeDB(pack='dummy_pack_1',
                             name='triggertype-1',
                             description='',
                             payload_schema={},
                             parameters_schema={})
     return Trigger.add_or_update(created)
Example #2
0
    def _setup_sample_triggers(self,
                               names=[
                                   'st2.test.trigger1', 'st2.test.trigger2',
                                   'st2.test.trigger3', 'st2.test.trigger4'
                               ]):
        trigger_dbs = []
        for name in names:
            trigtype = None
            try:
                trigtype = TriggerTypeDB(pack='dummy_pack_1',
                                         name=name,
                                         description='',
                                         payload_schema={},
                                         parameters_schema={})
                try:
                    trigtype = TriggerType.get_by_name(name)
                except:
                    trigtype = TriggerType.add_or_update(trigtype)
            except NotUniqueError:
                pass

            created = TriggerDB(pack='dummy_pack_1',
                                name=name,
                                description='',
                                type=trigtype.get_reference().ref)

            if name in ['st2.test.trigger4']:
                created.parameters = {'url': 'sample'}
            else:
                created.parameters = {}

            created = Trigger.add_or_update(created)
            trigger_dbs.append(created)

        return trigger_dbs
Example #3
0
 def _create_save_triggertype():
     created = TriggerTypeDB()
     created.pack = 'dummy_pack_1'
     created.name = 'triggertype-1'
     created.stupid = 'stupid'
     created.description = ''
     created.payload_schema = {}
     created.parameters_schema = {}
     return Trigger.add_or_update(created)
Example #4
0
 def _create_save_triggertype():
     created = TriggerTypeDB(
         pack="dummy_pack_1",
         name="triggertype-1",
         description="",
         payload_schema={},
         parameters_schema={},
     )
     return Trigger.add_or_update(created)
Example #5
0
    def _setup_sample_trigger(self, name):
        trigtype = TriggerTypeDB(name=name,
                                 pack='dummy_pack_1',
                                 payload_schema={},
                                 parameters_schema={})
        TriggerType.add_or_update(trigtype)

        created = TriggerDB(name=name,
                            pack='dummy_pack_1',
                            type=trigtype.get_reference().ref,
                            parameters={})
        Trigger.add_or_update(created)
Example #6
0
    def _setup_sample_trigger(self, name):
        trigtype = TriggerTypeDB()
        trigtype.name = name
        trigtype.pack = 'dummy_pack_1'
        trigtype.description = ''
        trigtype.payload_schema = {}
        trigtype.parameters_schema = {}
        TriggerType.add_or_update(trigtype)

        created = TriggerDB()
        created.name = name
        created.pack = 'dummy_pack_1'
        created.description = ''
        created.type = trigtype.get_reference().ref
        created.parameters = {}
        Trigger.add_or_update(created)
    def test_get_uid(self):
        pack_db = PackDB(ref='ma_pack')
        self.assertEqual(pack_db.get_uid(), 'pack:ma_pack')

        sensor_type_db = SensorTypeDB(name='sname', pack='spack')
        self.assertEqual(sensor_type_db.get_uid(), 'sensor_type:spack:sname')

        action_db = ActionDB(name='aname', pack='apack', runner_type={})
        self.assertEqual(action_db.get_uid(), 'action:apack:aname')

        rule_db = RuleDB(name='rname', pack='rpack')
        self.assertEqual(rule_db.get_uid(), 'rule:rpack:rname')

        trigger_type_db = TriggerTypeDB(name='ttname', pack='ttpack')
        self.assertEqual(trigger_type_db.get_uid(), 'trigger_type:ttpack:ttname')

        trigger_db = TriggerDB(name='tname', pack='tpack')
        self.assertTrue(trigger_db.get_uid().startswith('trigger:tpack:tname:'))

        # Verify that same set of parameters always results in the same hash
        parameters = {'a': 1, 'b': 'unicode', 'c': [1, 2, 3], 'd': {'g': 1, 'h': 2}}
        paramers_hash = json.dumps(parameters, sort_keys=True)
        paramers_hash = hashlib.md5(paramers_hash).hexdigest()

        parameters = {'a': 1, 'b': 'unicode', 'c': [1, 2, 3], 'd': {'g': 1, 'h': 2}}
        trigger_db = TriggerDB(name='tname', pack='tpack', parameters=parameters)
        self.assertEqual(trigger_db.get_uid(), 'trigger:tpack:tname:%s' % (paramers_hash))

        parameters = {'c': [1, 2, 3], 'b': u'unicode', 'd': {'h': 2, 'g': 1}, 'a': 1}
        trigger_db = TriggerDB(name='tname', pack='tpack', parameters=parameters)
        self.assertEqual(trigger_db.get_uid(), 'trigger:tpack:tname:%s' % (paramers_hash))

        parameters = {'b': u'unicode', 'c': [1, 2, 3], 'd': {'h': 2, 'g': 1}, 'a': 1}
        trigger_db = TriggerDB(name='tname', pack='tpack', parameters=parameters)
        self.assertEqual(trigger_db.get_uid(), 'trigger:tpack:tname:%s' % (paramers_hash))

        parameters = OrderedDict({'c': [1, 2, 3], 'b': u'unicode', 'd': {'h': 2, 'g': 1}, 'a': 1})
        trigger_db = TriggerDB(name='tname', pack='tpack', parameters=parameters)
        self.assertEqual(trigger_db.get_uid(), 'trigger:tpack:tname:%s' % (paramers_hash))

        policy_type_db = PolicyTypeDB(resource_type='action', name='concurrency')
        self.assertEqual(policy_type_db.get_uid(), 'policy_type:action:concurrency')

        policy_db = PolicyDB(pack='dummy', name='policy1')
        self.assertEqual(policy_db.get_uid(), 'policy:dummy:policy1')
Example #8
0
    def test_get_uid(self):
        pack_db = PackDB(ref='ma_pack')
        self.assertEqual(pack_db.get_uid(), 'pack:ma_pack')

        sensor_type_db = SensorTypeDB(name='sname', pack='spack')
        self.assertEqual(sensor_type_db.get_uid(), 'sensor_type:spack:sname')

        action_db = ActionDB(name='aname', pack='apack', runner_type={})
        self.assertEqual(action_db.get_uid(), 'action:apack:aname')

        rule_db = RuleDB(name='rname', pack='rpack')
        self.assertEqual(rule_db.get_uid(), 'rule:rpack:rname')

        trigger_type_db = TriggerTypeDB(name='ttname', pack='ttpack')
        self.assertEqual(trigger_type_db.get_uid(), 'trigger_type:ttpack:ttname')

        trigger_db = TriggerDB(name='tname', pack='tpack')
        self.assertTrue(trigger_db.get_uid().startswith('trigger:tpack:tname:'))
Example #9
0
    def _setup_sample_triggers(
        self,
        names=[
            "st2.test.trigger1",
            "st2.test.trigger2",
            "st2.test.trigger3",
            "st2.test.trigger4",
        ],
    ):
        trigger_dbs = []
        for name in names:
            trigtype = None
            try:
                trigtype = TriggerTypeDB(
                    pack="dummy_pack_1",
                    name=name,
                    description="",
                    payload_schema={},
                    parameters_schema={},
                )
                try:
                    trigtype = TriggerType.get_by_name(name)
                except:
                    trigtype = TriggerType.add_or_update(trigtype)
            except NotUniqueError:
                pass

            created = TriggerDB(
                pack="dummy_pack_1",
                name=name,
                description="",
                type=trigtype.get_reference().ref,
            )

            if name in ["st2.test.trigger4"]:
                created.parameters = {"url": "sample"}
            else:
                created.parameters = {}

            created = Trigger.add_or_update(created)
            trigger_dbs.append(created)

        return trigger_dbs
Example #10
0
}

WEBHOOK_DATA = {
    'value_str': 'test string 1',
    'value_int': 987654,
}

# 1. Trigger which references a system webhook trigger type
DUMMY_TRIGGER_DB = TriggerDB(name='pr-merged', pack='git')
DUMMY_TRIGGER_DB.type = list(WEBHOOK_TRIGGER_TYPES.keys())[0]

DUMMY_TRIGGER_API = TriggerAPI.from_model(DUMMY_TRIGGER_DB)
DUMMY_TRIGGER_DICT = vars(DUMMY_TRIGGER_API)

# 2. Custom TriggerType object
DUMMY_TRIGGER_TYPE_DB = TriggerTypeDB(name='pr-merged', pack='git')
DUMMY_TRIGGER_TYPE_DB.payload_schema = {
    'type': 'object',
    'properties': {
        'body': {
            'properties': {
                'value_str': {
                    'type': 'string',
                    'required': True
                },
                'value_int': {
                    'type': 'integer',
                    'required': True
                }
            }
        }
Example #11
0
}

WEBHOOK_DATA = {
    "value_str": "test string 1",
    "value_int": 987654,
}

# 1. Trigger which references a system webhook trigger type
DUMMY_TRIGGER_DB = TriggerDB(name="pr-merged", pack="git")
DUMMY_TRIGGER_DB.type = list(WEBHOOK_TRIGGER_TYPES.keys())[0]

DUMMY_TRIGGER_API = TriggerAPI.from_model(DUMMY_TRIGGER_DB)
DUMMY_TRIGGER_DICT = vars(DUMMY_TRIGGER_API)

# 2. Custom TriggerType object
DUMMY_TRIGGER_TYPE_DB = TriggerTypeDB(name="pr-merged", pack="git")
DUMMY_TRIGGER_TYPE_DB.payload_schema = {
    "type": "object",
    "properties": {
        "body": {
            "properties": {
                "value_str": {
                    "type": "string",
                    "required": True
                },
                "value_int": {
                    "type": "integer",
                    "required": True
                },
            }
        }
Example #12
0
    def test_get_uid(self):
        pack_db = PackDB(ref="ma_pack")
        self.assertEqual(pack_db.get_uid(), "pack:ma_pack")
        self.assertTrue(pack_db.has_valid_uid())

        sensor_type_db = SensorTypeDB(name="sname", pack="spack")
        self.assertEqual(sensor_type_db.get_uid(), "sensor_type:spack:sname")
        self.assertTrue(sensor_type_db.has_valid_uid())

        action_db = ActionDB(name="aname", pack="apack", runner_type={})
        self.assertEqual(action_db.get_uid(), "action:apack:aname")
        self.assertTrue(action_db.has_valid_uid())

        rule_db = RuleDB(name="rname", pack="rpack")
        self.assertEqual(rule_db.get_uid(), "rule:rpack:rname")
        self.assertTrue(rule_db.has_valid_uid())

        trigger_type_db = TriggerTypeDB(name="ttname", pack="ttpack")
        self.assertEqual(trigger_type_db.get_uid(), "trigger_type:ttpack:ttname")
        self.assertTrue(trigger_type_db.has_valid_uid())

        trigger_db = TriggerDB(name="tname", pack="tpack")
        self.assertTrue(trigger_db.get_uid().startswith("trigger:tpack:tname:"))

        # Verify that same set of parameters always results in the same hash
        parameters = {"a": 1, "b": "unicode", "c": [1, 2, 3], "d": {"g": 1, "h": 2}}
        paramers_hash = json.dumps(parameters, sort_keys=True)
        paramers_hash = hashlib.md5(paramers_hash.encode()).hexdigest()

        parameters = {"a": 1, "b": "unicode", "c": [1, 2, 3], "d": {"g": 1, "h": 2}}
        trigger_db = TriggerDB(name="tname", pack="tpack", parameters=parameters)
        self.assertEqual(
            trigger_db.get_uid(), "trigger:tpack:tname:%s" % (paramers_hash)
        )
        self.assertTrue(trigger_db.has_valid_uid())

        parameters = {"c": [1, 2, 3], "b": "unicode", "d": {"h": 2, "g": 1}, "a": 1}
        trigger_db = TriggerDB(name="tname", pack="tpack", parameters=parameters)
        self.assertEqual(
            trigger_db.get_uid(), "trigger:tpack:tname:%s" % (paramers_hash)
        )
        self.assertTrue(trigger_db.has_valid_uid())

        parameters = {"b": "unicode", "c": [1, 2, 3], "d": {"h": 2, "g": 1}, "a": 1}
        trigger_db = TriggerDB(name="tname", pack="tpack", parameters=parameters)
        self.assertEqual(
            trigger_db.get_uid(), "trigger:tpack:tname:%s" % (paramers_hash)
        )
        self.assertTrue(trigger_db.has_valid_uid())

        parameters = OrderedDict(
            {"c": [1, 2, 3], "b": "unicode", "d": {"h": 2, "g": 1}, "a": 1}
        )
        trigger_db = TriggerDB(name="tname", pack="tpack", parameters=parameters)
        self.assertEqual(
            trigger_db.get_uid(), "trigger:tpack:tname:%s" % (paramers_hash)
        )
        self.assertTrue(trigger_db.has_valid_uid())

        policy_type_db = PolicyTypeDB(resource_type="action", name="concurrency")
        self.assertEqual(policy_type_db.get_uid(), "policy_type:action:concurrency")
        self.assertTrue(policy_type_db.has_valid_uid())

        policy_db = PolicyDB(pack="dummy", name="policy1")
        self.assertEqual(policy_db.get_uid(), "policy:dummy:policy1")

        api_key_db = ApiKeyDB(key_hash="valid")
        self.assertEqual(api_key_db.get_uid(), "api_key:valid")
        self.assertTrue(api_key_db.has_valid_uid())

        api_key_db = ApiKeyDB()
        self.assertEqual(api_key_db.get_uid(), "api_key:")
        self.assertFalse(api_key_db.has_valid_uid())