def _setup_sample_triggers(self, names=['st2.test.trigger1', 'st2.test.trigger2', 'st2.test.trigger3']): 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 created.parameters = {} created = Trigger.add_or_update(created) trigger_dbs.append(created) return trigger_dbs
def setUpClass(cls): super(ReferenceTest, cls).setUpClass() trigger = TriggerDB() trigger.name = 'trigger-1' trigger.pack = 'dummy_pack_1' cls.__model = Trigger.add_or_update(trigger) cls.__ref = {'id': str(cls.__model.id), 'name': cls.__model.name}
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 _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 setUpClass(cls): super(ReferenceTest, cls).setUpClass() trigger = TriggerDB() trigger.name = 'trigger-1' trigger.pack = 'dummy_pack_1' cls.__model = Trigger.add_or_update(trigger) cls.__ref = {'id': str(cls.__model.id), 'name': cls.__model.name}
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 _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 evaluate(self): """ Evaluate trigger instance against the rule. :return: ``True`` if the rule matches, ``False`` otherwise. :rtype: ``boolean`` """ rule_db = self._get_rule_db_from_file(file_path=self._rule_file_path) trigger_instance_db = \ self._get_trigger_instance_db_from_file(file_path=self._trigger_instance_file_path) trigger_ref = ResourceReference.from_string_reference(trigger_instance_db['trigger']) trigger_db = TriggerDB() trigger_db.pack = trigger_ref.pack trigger_db.name = trigger_ref.name trigger_db.type = trigger_ref.ref matcher = RulesMatcher(trigger_instance=trigger_instance_db, trigger=trigger_db, rules=[rule_db]) matching_rules = matcher.get_matching_rules() return len(matching_rules) >= 1
def evaluate(self): """ Evaluate trigger instance against the rule. :return: ``True`` if the rule matches, ``False`` otherwise. :rtype: ``boolean`` """ rule_db = self._get_rule_db_from_file(file_path=self._rule_file_path) trigger_instance_db = \ self._get_trigger_instance_db_from_file(file_path=self._trigger_instance_file_path) trigger_ref = ResourceReference.from_string_reference( trigger_instance_db['trigger']) trigger_db = TriggerDB() trigger_db.pack = trigger_ref.pack trigger_db.name = trigger_ref.name trigger_db.type = trigger_ref.ref matcher = RulesMatcher(trigger_instance=trigger_instance_db, trigger=trigger_db, rules=[rule_db]) matching_rules = matcher.get_matching_rules() return len(matching_rules) >= 1
from st2common.persistence.reactor import Trigger, TriggerType from st2common.models.db.reactor import TriggerDB, TriggerTypeDB from st2common.transport.publishers import PoolPublisher import st2reactor.container.utils as container_utils from st2tests.base import DbTestCase MOCK_TRIGGER_TYPE = TriggerTypeDB() 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_info = {} 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(DbTestCase): @mock.patch.object(TriggerType, 'query', mock.MagicMock(return_value=[MOCK_TRIGGER_TYPE])) @mock.patch.object(Trigger, 'get_by_name', mock.MagicMock(return_value=MOCK_TRIGGER)) @mock.patch.object(TriggerType, 'add_or_update') def test_add_trigger(self, mock_add_handler): mock_add_handler.return_value = MOCK_TRIGGER_TYPE container_utils.add_trigger_models(pack='dummy_pack_1',
import datetime import mock import unittest2 from st2common.models.db.reactor import TriggerDB, TriggerInstanceDB, \ RuleDB, ActionExecutionSpecDB from st2common.models.db.action import ActionDB, ActionExecutionDB import st2common.services.action as action_service from st2common.util import reference from st2reactor.rules.enforcer import RuleEnforcer import st2tests.config as tests_config MOCK_TRIGGER = TriggerDB() MOCK_TRIGGER.id = 'trigger-test.id' MOCK_TRIGGER.name = 'trigger-test.name' MOCK_TRIGGER.pack = 'dummypack1' MOCK_TRIGGER_INSTANCE = TriggerInstanceDB() MOCK_TRIGGER_INSTANCE.id = 'triggerinstance-test' MOCK_TRIGGER_INSTANCE.trigger = reference.get_ref_from_model(MOCK_TRIGGER) MOCK_TRIGGER_INSTANCE.payload = {} MOCK_TRIGGER_INSTANCE.occurrence_time = datetime.datetime.utcnow() MOCK_ACTION = ActionDB() MOCK_ACTION.id = 'action-test-1.id' MOCK_ACTION.name = 'action-test-1.name' MOCK_ACTION_EXECUTION = ActionExecutionDB() MOCK_ACTION_EXECUTION.id = 'actionexec-test-1.id' MOCK_ACTION_EXECUTION.name = 'actionexec-test-1.name'
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)
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)