def setUp(self): RUNNER_TYPE.id = None RunnerType.add_or_update(RUNNER_TYPE) ACTION.id = None ACTION.runner_type = {'name': RUNNER_TYPE.name} Action.add_or_update(ACTION) TRIGGER.id = None Trigger.add_or_update(TRIGGER)
def create_or_update_trigger_db(trigger): """ Create a new TriggerDB model if one doesn't exist yet or update existing one. :param trigger: Trigger info. :type trigger: ``dict`` """ assert isinstance(trigger, dict) existing_trigger_db = _get_trigger_db(trigger) if existing_trigger_db: is_update = True else: is_update = False trigger_api = TriggerAPI(**trigger) trigger_api.validate() trigger_db = TriggerAPI.to_model(trigger_api) if is_update: trigger_db.id = existing_trigger_db.id trigger_db = Trigger.add_or_update(trigger_db) extra = {'trigger_db': trigger_db} if is_update: LOG.audit('Trigger updated. Trigger.id=%s' % (trigger_db.id), extra=extra) else: LOG.audit('Trigger created. Trigger.id=%s' % (trigger_db.id), extra=extra) return trigger_db
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 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 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_or_update_trigger_db(trigger): """ Create a new TriggerDB model if one doesn't exist yet or update existing one. :param trigger: Trigger info. :type trigger: ``dict`` """ assert isinstance(trigger, dict) existing_trigger_db = _get_trigger_db(trigger) if existing_trigger_db: is_update = True else: is_update = False trigger_api = TriggerAPI(**trigger) trigger_db = TriggerAPI.to_model(trigger_api) if is_update: trigger_db.id = existing_trigger_db.id trigger_db = Trigger.add_or_update(trigger_db) if is_update: LOG.audit('Trigger updated. Trigger=%s', trigger_db) else: LOG.audit('Trigger created. Trigger=%s', trigger_db) return trigger_db
def _create_save_triggertype(): created = TriggerTypeDB() created.pack = 'dummy_pack_1' created.name = 'triggertype-1' created.description = '' created.payload_schema = {} created.parameters_schema = {} 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 _create_save_triggertype(): created = TriggerTypeDB() created.pack = 'dummy_pack_1' created.name = 'triggertype-1' created.description = '' created.payload_schema = {} created.parameters_schema = {} 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 test_triggered_execution(self): docs = { 'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']), 'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']), 'rule': copy.deepcopy(fixture.ARTIFACTS['rule']), 'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance']) } # Trigger an action execution. trigger_type = TriggerType.add_or_update( TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type']))) trigger = Trigger.add_or_update( TriggerAPI.to_model(TriggerAPI(**docs['trigger']))) rule = RuleAPI.to_model(RuleAPI(**docs['rule'])) rule.trigger = reference.get_str_resource_ref_from_model(trigger) rule = Rule.add_or_update(rule) trigger_instance = TriggerInstance.add_or_update( TriggerInstanceAPI.to_model( TriggerInstanceAPI(**docs['trigger_instance']))) enforcer = RuleEnforcer(trigger_instance, rule) enforcer.enforce() # Wait for the action execution to complete and then confirm outcome. liveaction = LiveAction.get( context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(liveaction) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(liveaction.status, LIVEACTION_STATUS_FAILED) execution = self._get_action_execution(liveaction__id=str( liveaction.id), raise_exception=True) self.assertDictEqual(execution.trigger, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(execution.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual( execution.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(execution.rule, vars(RuleAPI.from_model(rule))) action = action_utils.get_action_by_ref(liveaction.action) self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(execution.start_timestamp, liveaction.start_timestamp) self.assertEqual(execution.end_timestamp, liveaction.end_timestamp) self.assertEqual(execution.result, liveaction.result) self.assertEqual(execution.status, liveaction.status) self.assertEqual(execution.context, liveaction.context) self.assertEqual(execution.liveaction['callback'], liveaction.callback) self.assertEqual(execution.liveaction['action'], liveaction.action)
def test_triggered_execution(self): docs = { 'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']), 'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']), 'rule': copy.deepcopy(fixture.ARTIFACTS['rule']), 'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance']) } # Trigger an action execution. trigger_type = TriggerType.add_or_update( TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type']))) trigger = Trigger.add_or_update( TriggerAPI.to_model(TriggerAPI(**docs['trigger']))) rule = RuleAPI.to_model(RuleAPI(**docs['rule'])) rule.trigger = reference.get_str_resource_ref_from_model(trigger) rule = Rule.add_or_update(rule) trigger_instance = TriggerInstance.add_or_update( TriggerInstanceAPI.to_model( TriggerInstanceAPI(**docs['trigger_instance']))) enforcer = RuleEnforcer(trigger_instance, rule) enforcer.enforce() # Wait for the action execution to complete and then confirm outcome. execution = ActionExecution.get( context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(execution) execution = ActionExecution.get_by_id(str(execution.id)) self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED) history = ActionExecutionHistory.get(execution__id=str(execution.id), raise_exception=True) self.assertDictEqual(history.trigger, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(history.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual( history.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(history.rule, vars(RuleAPI.from_model(rule))) action_ref = ResourceReference.from_string_reference( ref=execution.action) action, _ = action_utils.get_action_by_dict({ 'name': action_ref.name, 'pack': action_ref.pack }) self.assertDictEqual(history.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(history.runner, vars(RunnerTypeAPI.from_model(runner))) execution = ActionExecution.get_by_id(str(execution.id)) self.assertDictEqual(history.execution, vars(ActionExecutionAPI.from_model(execution)))
def create_trigger_db(trigger_api): # TODO: This is used only in trigger API controller. We should get rid of this. trigger_ref = ResourceReference.to_string_reference(name=trigger_api.name, pack=trigger_api.pack) trigger_db = get_trigger_db_by_ref(trigger_ref) if not trigger_db: trigger_db = TriggerAPI.to_model(trigger_api) LOG.debug('Verified trigger and formulated TriggerDB=%s', trigger_db) trigger_db = Trigger.add_or_update(trigger_db) return trigger_db
def create_trigger_db(trigger): trigger_api = trigger if isinstance(trigger, dict): trigger_api = TriggerAPI(**trigger) trigger_db = get_trigger_db(trigger_api) if not trigger_db: trigger_db = TriggerAPI.to_model(trigger_api) LOG.debug('verified trigger and formulated TriggerDB=%s', trigger_db) trigger_db = Trigger.add_or_update(trigger_db) return trigger_db
def create_trigger_db(trigger_api): # TODO: This is used only in trigger API controller. We should get rid of this. trigger_ref = ResourceReference.to_string_reference(name=trigger_api.name, pack=trigger_api.pack) trigger_db = get_trigger_db_by_ref(trigger_ref) if not trigger_db: trigger_db = TriggerAPI.to_model(trigger_api) LOG.debug('Verified trigger and formulated TriggerDB=%s', trigger_db) trigger_db = Trigger.add_or_update(trigger_db) return trigger_db
def test_triggered_execution(self): docs = { 'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']), 'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']), 'rule': copy.deepcopy(fixture.ARTIFACTS['rule']), 'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance'])} # Trigger an action execution. trigger_type = TriggerType.add_or_update( TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type']))) trigger = Trigger.add_or_update(TriggerAPI.to_model(TriggerAPI(**docs['trigger']))) rule = RuleAPI.to_model(RuleAPI(**docs['rule'])) rule.trigger = reference.get_str_resource_ref_from_model(trigger) rule = Rule.add_or_update(rule) trigger_instance = TriggerInstance.add_or_update( TriggerInstanceAPI.to_model(TriggerInstanceAPI(**docs['trigger_instance']))) enforcer = RuleEnforcer(trigger_instance, rule) enforcer.enforce() # Wait for the action execution to complete and then confirm outcome. liveaction = LiveAction.get(context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(liveaction) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(liveaction.status, LIVEACTION_STATUS_FAILED) execution = self._get_action_execution(liveaction__id=str(liveaction.id), raise_exception=True) self.assertDictEqual(execution.trigger, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(execution.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual(execution.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(execution.rule, vars(RuleAPI.from_model(rule))) action = action_utils.get_action_by_ref(liveaction.action) self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner))) liveaction = LiveAction.get_by_id(str(liveaction.id)) self.assertEqual(execution.start_timestamp, liveaction.start_timestamp) self.assertEqual(execution.end_timestamp, liveaction.end_timestamp) self.assertEqual(execution.result, liveaction.result) self.assertEqual(execution.status, liveaction.status) self.assertEqual(execution.context, liveaction.context) self.assertEqual(execution.liveaction['callback'], liveaction.callback) self.assertEqual(execution.liveaction['action'], liveaction.action)
def put(self, trigger_id, trigger): trigger_db = TriggerController.__get_by_id(trigger_id) try: if trigger.id is not None and trigger.id is not '' and trigger.id != trigger_id: LOG.warning('Discarding mismatched id=%s found in payload and using uri_id=%s.', trigger.id, trigger_id) trigger_db = TriggerAPI.to_model(trigger) trigger_db.id = trigger_id trigger_db = Trigger.add_or_update(trigger_db) except (ValidationError, ValueError) as e: LOG.exception('Validation failed for trigger data=%s', trigger) abort(http_client.BAD_REQUEST, str(e)) return extra = {'old_trigger_db': trigger, 'new_trigger_db': trigger_db} LOG.audit('Trigger updated. Trigger.id=%s' % (trigger.id), extra=extra) trigger_api = TriggerAPI.from_model(trigger_db) return trigger_api
def test_trigger_crud(self): triggertype = ReactorModelTest._create_save_triggertype() saved = ReactorModelTest._create_save_trigger(triggertype) retrieved = Trigger.get_by_id(saved.id) self.assertEqual(saved.name, retrieved.name, 'Same trigger was not returned.') # test update self.assertEqual(retrieved.description, '') retrieved.description = DUMMY_DESCRIPTION saved = Trigger.add_or_update(retrieved) retrieved = Trigger.get_by_id(saved.id) self.assertEqual(retrieved.description, DUMMY_DESCRIPTION, 'Update to trigger failed.') # cleanup ReactorModelTest._delete([retrieved, triggertype]) try: retrieved = Trigger.get_by_id(saved.id) except ValueError: retrieved = None self.assertIsNone(retrieved, 'managed to retrieve after failure.')
def test_trigger_crud(self): triggertype = ReactorModelTest._create_save_triggertype() saved = ReactorModelTest._create_save_trigger(triggertype) retrieved = Trigger.get_by_id(saved.id) self.assertEqual(saved.name, retrieved.name, 'Same trigger was not returned.') # test update self.assertEqual(retrieved.description, '') retrieved.description = DUMMY_DESCRIPTION saved = Trigger.add_or_update(retrieved) retrieved = Trigger.get_by_id(saved.id) self.assertEqual(retrieved.description, DUMMY_DESCRIPTION, 'Update to trigger failed.') # cleanup ReactorModelTest._delete([retrieved, triggertype]) try: retrieved = Trigger.get_by_id(saved.id) except ValueError: retrieved = None self.assertIsNone(retrieved, 'managed to retrieve after failure.')
def put(self, trigger_id, trigger): trigger_db = TriggerController.__get_by_id(trigger_id) try: if trigger.id is not None and trigger.id is not '' and trigger.id != trigger_id: LOG.warning( 'Discarding mismatched id=%s found in payload and using uri_id=%s.', trigger.id, trigger_id) trigger_db = TriggerAPI.to_model(trigger) trigger_db.id = trigger_id trigger_db = Trigger.add_or_update(trigger_db) except (ValidationError, ValueError) as e: LOG.exception('Validation failed for trigger data=%s', trigger) abort(http_client.BAD_REQUEST, str(e)) return extra = {'old_trigger_db': trigger, 'new_trigger_db': trigger_db} LOG.audit('Trigger updated. Trigger.id=%s' % (trigger.id), extra=extra) trigger_api = TriggerAPI.from_model(trigger_db) return trigger_api
def put(self, trigger_id, trigger): LOG.info('PUT /triggers/ with trigger id=%s and data=%s', trigger_id, trigger) trigger_db = TriggerController.__get_by_id(trigger_id) try: if trigger.id is not None and trigger.id is not '' and trigger.id != trigger_id: LOG.warning('Discarding mismatched id=%s found in payload and using uri_id=%s.', trigger.id, trigger_id) trigger_db = TriggerAPI.to_model(trigger) trigger_db.id = trigger_id trigger_db = Trigger.add_or_update(trigger_db) except (ValidationError, ValueError) as e: LOG.exception('Validation failed for trigger data=%s', trigger) abort(http_client.BAD_REQUEST, str(e)) return LOG.audit('Trigger updated. Trigger=%s and original Trigger=%s.', trigger, trigger_db) trigger_api = TriggerAPI.from_model(trigger_db) LOG.debug('PUT /triggers/ client_result=%s', trigger_api) return trigger_api
def test_triggered_execution(self): docs = { 'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']), 'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']), 'rule': copy.deepcopy(fixture.ARTIFACTS['rule']), 'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance'])} # Trigger an action execution. trigger_type = TriggerType.add_or_update( TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type']))) trigger = Trigger.add_or_update(TriggerAPI.to_model(TriggerAPI(**docs['trigger']))) rule = RuleAPI.to_model(RuleAPI(**docs['rule'])) rule.trigger = reference.get_str_resource_ref_from_model(trigger) rule = Rule.add_or_update(rule) trigger_instance = TriggerInstance.add_or_update( TriggerInstanceAPI.to_model(TriggerInstanceAPI(**docs['trigger_instance']))) enforcer = RuleEnforcer(trigger_instance, rule) enforcer.enforce() # Wait for the action execution to complete and then confirm outcome. execution = ActionExecution.get(context__trigger_instance__id=str(trigger_instance.id)) self.assertIsNotNone(execution) execution = ActionExecution.get_by_id(str(execution.id)) self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED) history = ActionExecutionHistory.get(execution__id=str(execution.id), raise_exception=True) self.assertDictEqual(history.trigger, vars(TriggerAPI.from_model(trigger))) self.assertDictEqual(history.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type))) self.assertDictEqual(history.trigger_instance, vars(TriggerInstanceAPI.from_model(trigger_instance))) self.assertDictEqual(history.rule, vars(RuleAPI.from_model(rule))) action_ref = ResourceReference.from_string_reference(ref=execution.action) action, _ = action_utils.get_action_by_dict( {'name': action_ref.name, 'pack': action_ref.pack}) self.assertDictEqual(history.action, vars(ActionAPI.from_model(action))) runner = RunnerType.get_by_name(action.runner_type['name']) self.assertDictEqual(history.runner, vars(RunnerTypeAPI.from_model(runner))) execution = ActionExecution.get_by_id(str(execution.id)) self.assertDictEqual(history.execution, vars(ActionExecutionAPI.from_model(execution)))
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)