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() trigtype.pack = "dummy_pack_1" trigtype.name = name trigtype.description = "" trigtype.payload_schema = {} trigtype.parameters_schema = {} try: trigtype = TriggerType.get_by_name(name) except: trigtype = TriggerType.add_or_update(trigtype) except NotUniqueError: pass created = TriggerDB() created.name = name created.pack = "dummy_pack_1" created.description = "" created.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
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
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
def _create_save_trigger(triggertype): created = TriggerDB() created.name = 'trigger-1' created.pack = 'dummy_pack_1' created.description = '' created.type = triggertype.get_reference().ref created.parameters = {} return Trigger.add_or_update(created)
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
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 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 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_trigger_db_given_type_and_params(self): # Add dummy triggers trigger_1 = TriggerDB() trigger_1.pack = 'testpack' trigger_1.name = 'testtrigger1' trigger_1.type = 'testpack.testtrigger1' trigger_1.parameters = {} trigger_2 = TriggerDB() trigger_2.pack = 'testpack' trigger_2.name = 'testtrigger2' trigger_2.type = 'testpack.testtrigger2' trigger_2.parameters = None trigger_3 = TriggerDB() trigger_3.pack = 'testpack' trigger_3.name = 'testtrigger3' trigger_3.type = 'testpack.testtrigger3' trigger_4 = TriggerDB() trigger_4.pack = 'testpack' trigger_4.name = 'testtrigger4' trigger_4.type = 'testpack.testtrigger4' trigger_4.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 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() MOCK_TRIGGER.id = 'trigger-test.id' MOCK_TRIGGER.name = 'trigger-test.name' MOCK_TRIGGER.pack = 'dummy_pack_1' MOCK_TRIGGER.parameters = {} MOCK_TRIGGER.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']))
import mock from st2common.models.db.trigger import TriggerDB from st2common.transport.publishers import PoolPublisher import st2reactor.container.utils as container_utils from st2tests.base import CleanDbTestCase MOCK_TRIGGER_TYPE = {} MOCK_TRIGGER_TYPE['id'] = 'trigger-type-test.id' MOCK_TRIGGER_TYPE['name'] = 'trigger-type-test.name' MOCK_TRIGGER_TYPE['pack'] = 'dummy_pack_1' MOCK_TRIGGER_TYPE['parameters_schema'] = {} MOCK_TRIGGER_TYPE['payload_schema'] = {} MOCK_TRIGGER = TriggerDB() MOCK_TRIGGER.id = 'trigger-test.id' MOCK_TRIGGER.name = 'trigger-test.name' MOCK_TRIGGER.pack = 'dummy_pack_1' MOCK_TRIGGER.parameters = {} MOCK_TRIGGER.type = 'dummy_pack_1.trigger-type-test.name' @mock.patch.object(PoolPublisher, 'publish', mock.MagicMock()) class ContainerUtilsTest(CleanDbTestCase): 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)
def test_get_trigger_db_given_type_and_params(self): # Add dummy triggers trigger_1 = TriggerDB() trigger_1.pack = 'testpack' trigger_1.name = 'testtrigger1' trigger_1.type = 'testpack.testtrigger1' trigger_1.parameters = {} trigger_2 = TriggerDB() trigger_2.pack = 'testpack' trigger_2.name = 'testtrigger2' trigger_2.type = 'testpack.testtrigger2' trigger_2.parameters = None trigger_3 = TriggerDB() trigger_3.pack = 'testpack' trigger_3.name = 'testtrigger3' trigger_3.type = 'testpack.testtrigger3' trigger_4 = TriggerDB() trigger_4.pack = 'testpack' trigger_4.name = 'testtrigger4' trigger_4.type = 'testpack.testtrigger4' trigger_4.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)