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 test_existing_rules_are_loaded_on_start(self): # Assert that we dispatch message for every existing Trigger object St2Timer._handle_create_trigger = Mock() timer = St2Timer() timer._scheduler = Mock() timer._trigger_watcher.run = Mock() # Verify there are no Trigger and TriggerType in the db wh:w self.assertItemsEqual(Trigger.get_all(), []) self.assertItemsEqual(TriggerType.get_all(), []) # Add a dummy timer Trigger object type = TIMER_TRIGGER_TYPES.keys()[0] parameters = {'unit': 'seconds', 'delta': 1000} trigger_db = TriggerDB(name='test_trigger_1', pack='dummy', type=type, parameters=parameters) trigger_db = Trigger.add_or_update(trigger_db) # Verify object has been added self.assertEqual(len(Trigger.get_all()), 1) timer.start() timer._trigger_watcher._load_thread.wait() # Verify handlers are called timer._handle_create_trigger.assert_called_with(trigger_db)
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 _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 _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 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
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)
import mock import six from tests import FunctionalTest from st2api.controllers.v1.webhooks import WebhooksController from st2common.constants.triggers import WEBHOOK_TRIGGER_TYPES from st2common.models.db.reactor import TriggerDB from st2common.transport.reactor import TriggerInstancePublisher http_client = six.moves.http_client WEBHOOK_1 = {'action': 'closed', 'pull_request': {'merged': True}} ST2_WEBHOOK = {'trigger': 'foo.bar', 'payload': {'ponies': 'unicorns'}} DUMMY_TRIGGER = TriggerDB(name='pr-merged', pack='git') DUMMY_TRIGGER.type = WEBHOOK_TRIGGER_TYPES.keys()[0] class TestTriggerTypeController(FunctionalTest): @mock.patch.object(TriggerInstancePublisher, 'publish_trigger', mock.MagicMock(return_value=True)) @mock.patch.object(WebhooksController, '_is_valid_hook', mock.MagicMock(return_value=True)) @mock.patch.object(WebhooksController, '_get_trigger_for_hook', mock.MagicMock(return_value=DUMMY_TRIGGER)) def test_post(self): post_resp = self.__do_post('git', WEBHOOK_1, expect_errors=False) self.assertEqual(post_resp.status_int, http_client.ACCEPTED) @mock.patch.object(TriggerInstancePublisher, 'publish_trigger',
http_client = six.moves.http_client WEBHOOK_1 = { 'action': 'closed', 'pull_request': { 'merged': True } } ST2_WEBHOOK = { 'trigger': 'foo.bar', 'payload': {'ponies': 'unicorns'} } DUMMY_TRIGGER = TriggerDB(name='pr-merged', pack='git') DUMMY_TRIGGER.type = WEBHOOK_TRIGGER_TYPES.keys()[0] class TestTriggerTypeController(FunctionalTest): @mock.patch.object(TriggerInstancePublisher, 'publish_trigger', mock.MagicMock( return_value=True)) @mock.patch.object(WebhooksController, '_is_valid_hook', mock.MagicMock( return_value=True)) @mock.patch.object(WebhooksController, '_get_trigger_for_hook', mock.MagicMock( return_value=DUMMY_TRIGGER)) def test_post(self): post_resp = self.__do_post('git', WEBHOOK_1, expect_errors=False) self.assertEqual(post_resp.status_int, http_client.ACCEPTED)
import mock 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
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import bson import copy import datetime import mock from st2common.models.db.reactor import TriggerDB, TriggerInstanceDB, \ RuleDB, ActionExecutionSpecDB from st2common.models.db.action import ActionDB from st2common.util import reference 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', 'bool': True, 'int': 1, 'float': 0.8 } MOCK_TRIGGER_INSTANCE.occurrence_time = datetime.datetime.utcnow()
# limitations under the License. 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()
from st2api.controllers.v1.webhooks import WebhooksController from st2common.constants.triggers import GENERIC_WEBHOOK_TRIGGER_REF from st2common.models.db.reactor import TriggerDB from st2common.transport.reactor import TriggerInstancePublisher http_client = six.moves.http_client WEBHOOK_1 = { 'action': 'closed', 'pull_request': { 'merged': True } } DUMMY_TRIGGER = TriggerDB(name='pr-merged', pack='git') DUMMY_TRIGGER.type = GENERIC_WEBHOOK_TRIGGER_REF class TestTriggerTypeController(FunctionalTest): @mock.patch.object(TriggerInstancePublisher, 'publish_trigger', mock.MagicMock( return_value=True)) @mock.patch.object(WebhooksController, '_is_valid_hook', mock.MagicMock( return_value=True)) @mock.patch.object(WebhooksController, '_get_trigger_for_hook', mock.MagicMock( return_value=DUMMY_TRIGGER)) def test_post(self): post_resp = self.__do_post('git', WEBHOOK_1, expect_errors=False) self.assertEqual(post_resp.status_int, http_client.ACCEPTED)
# See the License for the specific language governing permissions and # limitations under the License. import bson import copy import datetime import mock from st2common.models.db.reactor import TriggerDB, TriggerInstanceDB, \ RuleDB, ActionExecutionSpecDB from st2common.models.db.action import ActionDB from st2common.util import reference 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'} MOCK_TRIGGER_INSTANCE.occurrence_time = datetime.datetime.utcnow() MOCK_ACTION = ActionDB() MOCK_ACTION.id = bson.ObjectId() MOCK_ACTION.name = 'action-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)
import mock 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):
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)