def get_webhook_trigger(name, url): trigger = TriggerDB(name=name, pack="test") trigger.type = list(WEBHOOK_TRIGGER_TYPES.keys())[0] trigger.parameters = {"url": url} return trigger
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': 2, 'c': [1, 2, 3], 'd': { 'g': 1, 'h': 2 }, 'b': u'unicode' } paramers_hash = json.dumps(parameters, sort_keys=True) paramers_hash = hashlib.md5(paramers_hash).hexdigest() parameters = { 'a': 1, 'b': 2, 'c': [1, 2, 3], 'b': u'unicode', '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))
ST2_WEBHOOK = { "trigger": "git.pr-merged", "payload": { "value_str": "string!", "value_int": 12345 }, } 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
# See the License for the specific language governing permissions and # limitations under the License. import bson import copy import mock from st2common.models.db.action import ActionDB from st2common.models.db.rule import RuleDB, ActionExecutionSpecDB from st2common.models.db.trigger import TriggerDB, TriggerInstanceDB from st2common.util import reference from st2common.util import date as date_utils from st2reactor.rules.filter import RuleFilter from st2tests import DbTestCase MOCK_TRIGGER = TriggerDB() MOCK_TRIGGER.id = bson.ObjectId() MOCK_TRIGGER.name = 'trigger-test.name' MOCK_TRIGGER.pack = 'dummy_pack_1' MOCK_TRIGGER.type = 'system.test' MOCK_TRIGGER_INSTANCE = TriggerInstanceDB() MOCK_TRIGGER_INSTANCE.id = bson.ObjectId() MOCK_TRIGGER_INSTANCE.trigger = MOCK_TRIGGER.get_reference().ref MOCK_TRIGGER_INSTANCE.payload = { 'p1': 'v1', 'p2': 'preYYYpost', 'bool': True, 'int': 1, 'float': 0.8 }
def setUpClass(cls): super(ReferenceTest, cls).setUpClass() trigger = TriggerDB(pack='dummy_pack_1', name='trigger-1') cls.__model = Trigger.add_or_update(trigger) cls.__ref = {'id': str(cls.__model.id), 'name': cls.__model.name}
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())
ST2_WEBHOOK = { 'trigger': 'git.pr-merged', 'payload': { 'value_str': 'string!', 'value_int': 12345 } } 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
# See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from st2common.exceptions.triggers import TriggerDoesNotExistException from st2common.models.api.rule import RuleAPI from st2common.models.system.common import ResourceReference from st2common.models.db.trigger import TriggerDB from st2common.persistence.trigger import (Trigger, TriggerType) import st2common.services.triggers as trigger_service from st2tests.base import CleanDbTestCase from st2tests.fixturesloader import FixturesLoader MOCK_TRIGGER = TriggerDB(pack='dummy_pack_1', name='trigger-test.name', parameters={}, type='dummy_pack_1.trigger-type-test.name') class TriggerServiceTests(CleanDbTestCase): def test_create_trigger_db_from_rule(self): test_fixtures = { 'rules': ['cron_timer_rule_1.yaml', 'cron_timer_rule_3.yaml'] } loader = FixturesLoader() fixtures = loader.load_fixtures(fixtures_pack='generic', fixtures_dict=test_fixtures) rules = fixtures['rules'] trigger_db_ret_1 = trigger_service.create_trigger_db_from_rule( RuleAPI(**rules['cron_timer_rule_1.yaml']))
def test_get_trigger_db_given_type_and_params(self): # Add dummy triggers trigger_1 = TriggerDB(pack='testpack', name='testtrigger1', type='testpack.testtrigger1') trigger_2 = TriggerDB(pack='testpack', name='testtrigger2', type='testpack.testtrigger2') trigger_3 = TriggerDB(pack='testpack', name='testtrigger3', type='testpack.testtrigger3') trigger_4 = TriggerDB(pack='testpack', name='testtrigger4', type='testpack.testtrigger4', parameters={'ponies': 'unicorn'}) Trigger.add_or_update(trigger_1) Trigger.add_or_update(trigger_2) Trigger.add_or_update(trigger_3) Trigger.add_or_update(trigger_4) # Trigger with no parameters, parameters={} in db trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_1.type, parameters={}) self.assertEqual(trigger_db, trigger_1) trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_1.type, parameters=None) self.assertEqual(trigger_db, trigger_1) trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_1.type, parameters={'fo': 'bar'}) self.assertEqual(trigger_db, None) # Trigger with no parameters, no parameters attribute in the db trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_2.type, parameters={}) self.assertEqual(trigger_db, trigger_2) trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_2.type, parameters=None) self.assertEqual(trigger_db, trigger_2) trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_2.type, parameters={'fo': 'bar'}) self.assertEqual(trigger_db, None) trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_3.type, parameters={}) self.assertEqual(trigger_db, trigger_3) trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_3.type, parameters=None) self.assertEqual(trigger_db, trigger_3) # Trigger with parameters trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_4.type, parameters=trigger_4.parameters) self.assertEqual(trigger_db, trigger_4) trigger_db = trigger_service.get_trigger_db_given_type_and_params( type=trigger_4.type, parameters=None) self.assertEqual(trigger_db, None)
from __future__ import absolute_import import bson import copy import mock from st2common.models.db.action import ActionDB from st2common.models.db.rule import RuleDB, ActionExecutionSpecDB from st2common.models.db.trigger import TriggerDB, TriggerInstanceDB from st2common.util import reference from st2common.util import date as date_utils from st2reactor.rules.filter import RuleFilter from st2tests import DbTestCase MOCK_TRIGGER = TriggerDB(pack='dummy_pack_1', name='trigger-test.name', type='system.test') MOCK_TRIGGER_INSTANCE = TriggerInstanceDB( trigger=MOCK_TRIGGER.get_reference().ref, occurrence_time=date_utils.get_datetime_utc_now(), payload={ 'p1': 'v1', 'p2': 'preYYYpost', 'bool': True, 'int': 1, 'float':
def get_webhook_trigger(name, url): trigger = TriggerDB(name=name, pack='test') trigger.type = WEBHOOK_TRIGGER_TYPES.keys()[0] trigger.parameters = {'url': url} return trigger
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': 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)) self.assertTrue(trigger_db.has_valid_uid()) 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)) self.assertTrue(trigger_db.has_valid_uid()) 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)) 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())
def setUpClass(cls): super(ReferenceTest, cls).setUpClass() trigger = TriggerDB(pack="dummy_pack_1", name="trigger-1") cls.__model = Trigger.add_or_update(trigger) cls.__ref = {"id": str(cls.__model.id), "name": cls.__model.name}
from __future__ import absolute_import import bson import copy import mock from st2common.models.db.action import ActionDB from st2common.models.db.rule import RuleDB, ActionExecutionSpecDB from st2common.models.db.trigger import TriggerDB, TriggerInstanceDB from st2common.util import reference from st2common.util import date as date_utils from st2reactor.rules.filter import RuleFilter from st2tests import DbTestCase MOCK_TRIGGER = TriggerDB(pack="dummy_pack_1", name="trigger-test.name", type="system.test") MOCK_TRIGGER_INSTANCE = TriggerInstanceDB( trigger=MOCK_TRIGGER.get_reference().ref, occurrence_time=date_utils.get_datetime_utc_now(), payload={ "p1": "v1", "p2": "preYYYpost", "bool": True, "int": 1, "float":