Exemplo n.º 1
0
    def test_existing_rules_are_loaded_on_start(self):
        # Assert that we dispatch message for every existing Trigger object
        St2Timer._handle_create_trigger = mock.Mock()

        timer = St2Timer()
        timer._scheduler = mock.Mock()
        timer._trigger_watcher.run = mock.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(id=bson.ObjectId(), 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)
Exemplo n.º 2
0
    def test_create_or_update_trigger_db_simple_triggers(self):
        test_fixtures = {
            'triggertypes': ['triggertype1.yaml']
        }
        loader = FixturesLoader()
        fixtures = loader.save_fixtures_to_db(fixtures_pack='generic', fixtures_dict=test_fixtures)
        triggertypes = fixtures['triggertypes']
        trigger_type_ref = ResourceReference.to_string_reference(
            name=triggertypes['triggertype1.yaml']['name'],
            pack=triggertypes['triggertype1.yaml']['pack'])

        trigger = {
            'name': triggertypes['triggertype1.yaml']['name'],
            'pack': triggertypes['triggertype1.yaml']['pack'],
            'type': trigger_type_ref
        }
        trigger_service.create_or_update_trigger_db(trigger)
        triggers = Trigger.get_all()
        self.assertTrue(len(triggers) == 1, 'Only one trigger should be created.')
        self.assertTrue(triggers[0]['name'] == triggertypes['triggertype1.yaml']['name'])

        # Try adding duplicate
        trigger_service.create_or_update_trigger_db(trigger)
        triggers = Trigger.get_all()
        self.assertTrue(len(triggers) == 1, 'Only one trigger should be present.')
        self.assertTrue(triggers[0]['name'] == triggertypes['triggertype1.yaml']['name'])
    def test_register_rules(self):
        # Verify DB is empty at the beginning
        self.assertEqual(len(Rule.get_all()), 0)
        self.assertEqual(len(Trigger.get_all()), 0)

        registrar = RulesRegistrar()
        registrar.register_from_packs(base_dirs=[PACKS_DIR])

        # Verify modeles are created
        rule_dbs = Rule.get_all()
        trigger_dbs = Trigger.get_all()
        self.assertEqual(len(rule_dbs), 2)
        self.assertEqual(len(trigger_dbs), 1)

        self.assertEqual(rule_dbs[0].name, 'sample.with_the_same_timer')
        self.assertEqual(rule_dbs[1].name, 'sample.with_timer')
        self.assertTrue(trigger_dbs[0].name is not None)

        # Verify second register call updates existing models
        registrar.register_from_packs(base_dirs=[PACKS_DIR])

        rule_dbs = Rule.get_all()
        trigger_dbs = Trigger.get_all()
        self.assertEqual(len(rule_dbs), 2)
        self.assertEqual(len(trigger_dbs), 1)
Exemplo n.º 4
0
    def _setup_sample_trigger(self, name):
        trigtype = TriggerTypeDB(name=name, pack='dummy_pack_1', payload_schema={},
                                 parameters_schema={})
        TriggerType.add_or_update(trigtype)

        created = TriggerDB(name=name, pack='dummy_pack_1', type=trigtype.get_reference().ref,
                            parameters={})
        Trigger.add_or_update(created)
Exemplo n.º 5
0
def cleanup_trigger_db_for_rule(rule_db):
    # rule.trigger is actually trigger_db ref.
    existing_trigger_db = get_trigger_db_by_ref(rule_db.trigger)
    if not existing_trigger_db or not existing_trigger_db.parameters:
        # nothing to be done here so moving on.
        LOG.debug('ref_count decrement for %s not required.', existing_trigger_db)
        return
    Trigger.update(existing_trigger_db, dec__ref_count=1)
    Trigger.delete_if_unreferenced(existing_trigger_db)
Exemplo n.º 6
0
def get_trigger_db_given_type_and_params(type=None, parameters=None):
    try:
        parameters = parameters or {}
        trigger_dbs = Trigger.query(type=type, parameters=parameters)

        trigger_db = trigger_dbs[0] if len(trigger_dbs) > 0 else None

        # NOTE: This is a work-around which we might be able to remove once we upgrade
        # pymongo and mongoengine
        # Work around for cron-timer when in some scenarios finding an object fails when Python
        # value types are unicode :/
        is_cron_trigger = type == CRON_TIMER_TRIGGER_REF
        has_parameters = bool(parameters)

        if not trigger_db and six.PY2 and is_cron_trigger and has_parameters:
            non_unicode_literal_parameters = {}
            for key, value in six.iteritems(parameters):
                key = key.encode("utf-8")

                if isinstance(value, six.text_type):
                    # We only encode unicode to str
                    value = value.encode("utf-8")

                non_unicode_literal_parameters[key] = value
            parameters = non_unicode_literal_parameters

            trigger_dbs = Trigger.query(type=type, parameters=non_unicode_literal_parameters).no_cache()

            # Note: We need to directly access the object, using len or accessing the query set
            # twice won't work - there seems to bug a bug with cursor where accessing it twice
            # will throw an exception
            try:
                trigger_db = trigger_dbs[0]
            except IndexError:
                trigger_db = None

        if not parameters and not trigger_db:
            # We need to do double query because some TriggeDB objects without
            # parameters have "parameters" attribute stored in the db and others
            # don't
            trigger_db = Trigger.query(type=type, parameters=None).first()

        return trigger_db
    except StackStormDBObjectNotFoundError as e:
        LOG.debug(
            'Database lookup for type="%s" parameters="%s" resulted ' + "in exception : %s.",
            type,
            parameters,
            e,
            exc_info=True,
        )
        return None
Exemplo n.º 7
0
def increment_trigger_ref_count(rule_api):
    """
    Given the rule figures out the TriggerType with parameter and increments
    reference count on the appropriate Trigger.

    :param rule_api: Rule object used to infer the Trigger.
    :type rule_api: ``RuleApi``
    """
    trigger_dict = _get_trigger_dict_given_rule(rule_api)

    # Special reference counting for trigger with parameters.
    if trigger_dict.get('parameters', None):
        trigger_db = _get_trigger_db(trigger_dict)
        Trigger.update(trigger_db, inc__ref_count=1)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    def _delete_shadow_trigger(triggertype_db):
        # shadow Trigger's have the same name as the shadowed TriggerType.
        triggertype_ref = ResourceReference(name=triggertype_db.name, pack=triggertype_db.pack)
        trigger_db = TriggerService.get_trigger_db_by_ref(triggertype_ref.ref)
        if not trigger_db:
            LOG.warn('No shadow trigger found for %s. Will skip delete.', triggertype_db)
            return
        try:
            Trigger.delete(trigger_db)
        except Exception:
            LOG.exception('Database delete encountered exception during delete of id="%s". ',
                          trigger_db.id)

        extra = {'trigger_db': trigger_db}
        LOG.audit('Trigger deleted. Trigger.id=%s' % (trigger_db.id), extra=extra)
Exemplo n.º 10
0
    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
Exemplo n.º 11
0
    def get_all(self, exclude_attributes=None, include_attributes=None, sort=None, offset=0,
                limit=None, requester_user=None, **raw_filters):
        """
            List all triggerinstances.

            Handles requests:
                GET /triggerinstances/
        """
        # If trigger_type filter is provided, filter based on the TriggerType via Trigger object
        trigger_type_ref = raw_filters.get('trigger_type', None)

        if trigger_type_ref:
            # 1. Retrieve TriggerType object id which match this trigger_type ref
            trigger_dbs = Trigger.query(type=trigger_type_ref,
                                        only_fields=['ref', 'name', 'pack', 'type'])
            trigger_refs = [trigger_db.ref for trigger_db in trigger_dbs]
            raw_filters['trigger'] = trigger_refs

        if trigger_type_ref and len(raw_filters.get('trigger', [])) == 0:
            # Empty list means trigger_type_ref filter was provided, but we matched no Triggers so
            # we should return back empty result
            return []

        trigger_instances = self._get_trigger_instances(exclude_fields=exclude_attributes,
                                                        include_fields=include_attributes,
                                                        sort=sort,
                                                        offset=offset,
                                                        limit=limit,
                                                        raw_filters=raw_filters,
                                                        requester_user=requester_user)
        return trigger_instances
Exemplo n.º 12
0
    def test_register_triggers_from_pack(self):
        base_path = get_fixtures_base_path()
        pack_dir = os.path.join(base_path, 'dummy_pack_1')

        trigger_type_dbs = TriggerType.get_all()
        self.assertEqual(len(trigger_type_dbs), 0)

        count = triggers_registrar.register_triggers(pack_dir=pack_dir)
        self.assertEqual(count, 2)

        # Verify TriggerTypeDB and corresponding TriggerDB objects have been created
        trigger_type_dbs = TriggerType.get_all()
        trigger_dbs = Trigger.get_all()
        self.assertEqual(len(trigger_type_dbs), 2)
        self.assertEqual(len(trigger_dbs), 2)

        self.assertEqual(trigger_type_dbs[0].name, 'event_handler')
        self.assertEqual(trigger_type_dbs[0].pack, 'dummy_pack_1')
        self.assertEqual(trigger_dbs[0].name, 'event_handler')
        self.assertEqual(trigger_dbs[0].pack, 'dummy_pack_1')
        self.assertEqual(trigger_dbs[0].type, 'dummy_pack_1.event_handler')

        self.assertEqual(trigger_type_dbs[1].name, 'head_sha_monitor')
        self.assertEqual(trigger_type_dbs[1].pack, 'dummy_pack_1')
        self.assertEqual(trigger_type_dbs[1].payload_schema['type'], 'object')
Exemplo n.º 13
0
    def setUp(self):
        super(ContainerUtilsTest, self).setUp()

        # Insert mock TriggerDB
        trigger_db = TriggerDB(name='name1', pack='pack1', type='type1',
                               parameters={'a': 1, 'b': '2', 'c': 'foo'})
        self.trigger_db = Trigger.add_or_update(trigger_db)
Exemplo n.º 14
0
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
Exemplo n.º 15
0
    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
Exemplo n.º 16
0
    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)
Exemplo n.º 17
0
 def test_trigger_lookup(self):
     triggertype = ReactorModelTest._create_save_triggertype()
     saved = ReactorModelTest._create_save_trigger(triggertype)
     retrievedtriggers = Trigger.query(name=saved.name)
     self.assertEqual(1, len(retrievedtriggers), "No triggers found.")
     for retrievedtrigger in retrievedtriggers:
         self.assertEqual(saved.id, retrievedtrigger.id, "Incorrect trigger returned.")
     ReactorModelTest._delete([saved, triggertype])
Exemplo n.º 18
0
 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)
Exemplo n.º 19
0
 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}
Exemplo n.º 20
0
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
Exemplo n.º 21
0
def get_trigger_db_given_type_and_params(type=None, parameters=None):
    try:
        parameters = parameters or {}

        trigger_db = Trigger.query(type=type,
                                   parameters=parameters).first()

        if not parameters and not trigger_db:
            # We need to do double query because some TriggeDB objects without
            # parameters have "parameters" attribute stored in the db and others
            # don't
            trigger_db = Trigger.query(type=type, parameters=None).first()

        return trigger_db
    except ValueError as e:
        LOG.debug('Database lookup for type="%s" parameters="%s" resulted ' +
                  'in exception : %s.', type, parameters, e, exc_info=True)
        return None
Exemplo n.º 22
0
 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 StackStormDBObjectNotFoundError:
         retrieved = None
     self.assertIsNone(retrieved, "managed to retrieve after failure.")
Exemplo n.º 23
0
 def _create_save_triggertype():
     created = TriggerTypeDB()
     created.pack = 'dummy_pack_1'
     created.name = 'triggertype-1'
     created.stupid = 'stupid'
     created.description = ''
     created.payload_schema = {}
     created.parameters_schema = {}
     return Trigger.add_or_update(created)
Exemplo n.º 24
0
    def get_all(self, requester_user=None):
        """
            List all triggers.

            Handles requests:
                GET /triggers/
        """
        trigger_dbs = Trigger.get_all()
        trigger_apis = [TriggerAPI.from_model(trigger_db) for trigger_db in trigger_dbs]
        return trigger_apis
Exemplo n.º 25
0
    def delete(self, trigger_id):
        """
            Delete a trigger.

            Handles requests:
                DELETE /triggers/1
        """
        LOG.info('DELETE /triggers/ with id=%s', trigger_id)
        trigger_db = TriggerController.__get_by_id(trigger_id)
        try:
            Trigger.delete(trigger_db)
        except Exception as e:
            LOG.exception('Database delete encountered exception during delete of id="%s". ',
                          trigger_id)
            abort(http_client.INTERNAL_SERVER_ERROR, str(e))
            return

        extra = {'trigger_db': trigger_db}
        LOG.audit('Trigger deleted. Trigger.id=%s' % (trigger_db.id), extra=extra)
Exemplo n.º 26
0
def get_trigger_db_by_ref(ref):
    """
    Returns the trigger object from db given a string ref.

    :param ref: Reference to the trigger db object.
    :type ref: ``str``

    :rtype trigger_type: ``object``
    """
    return Trigger.get_by_ref(ref)
Exemplo n.º 27
0
 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.')
Exemplo n.º 28
0
    def test_ref_count_trigger_increment(self):
        post_resp = self.__do_post(self.RULE_1)
        rule_1_id = self.__get_rule_id(post_resp)
        self.assertEqual(post_resp.status_int, http_client.CREATED)
        # ref_count is not served over API. Likely a choice that will prove unwise.
        triggers = Trigger.get_all(**{'type': post_resp.json['trigger']['type']})
        self.assertEqual(len(triggers), 1, 'Exactly 1 should exist')
        self.assertEqual(triggers[0].ref_count, 1, 'ref_count should be 1')

        # different rule same params
        rule_2 = copy.copy(self.RULE_1)
        rule_2['name'] = rule_2['name'] + '-2'
        post_resp = self.__do_post(rule_2)
        rule_2_id = self.__get_rule_id(post_resp)
        self.assertEqual(post_resp.status_int, http_client.CREATED)
        triggers = Trigger.get_all(**{'type': post_resp.json['trigger']['type']})
        self.assertEqual(len(triggers), 1, 'Exactly 1 should exist')
        self.assertEqual(triggers[0].ref_count, 2, 'ref_count should be 1')

        self.__do_delete(rule_1_id)
        self.__do_delete(rule_2_id)
Exemplo n.º 29
0
    def test_trigger_cleanup(self):
        post_resp = self.__do_post(self.RULE_1)
        rule_1_id = self.__get_rule_id(post_resp)
        self.assertEqual(post_resp.status_int, http_client.CREATED)

        rule_2 = copy.copy(self.RULE_1)
        rule_2['name'] = rule_2['name'] + '-2'
        post_resp = self.__do_post(rule_2)
        rule_2_id = self.__get_rule_id(post_resp)
        self.assertEqual(post_resp.status_int, http_client.CREATED)

        triggers = Trigger.get_all(**{'type': post_resp.json['trigger']['type']})
        self.assertEqual(len(triggers), 1, 'Exactly 1 should exist')
        self.assertEqual(triggers[0].ref_count, 2, 'ref_count should be 1')

        self.__do_delete(rule_1_id)
        self.__do_delete(rule_2_id)

        # validate cleanup
        triggers = Trigger.get_all(**{'type': post_resp.json['trigger']['type']})
        self.assertEqual(len(triggers), 0, 'Exactly 1 should exist')
Exemplo n.º 30
0
    def test_register_all_triggers(self):
        trigger_type_dbs = TriggerType.get_all()
        self.assertEqual(len(trigger_type_dbs), 0)

        packs_base_path = get_fixtures_base_path()
        count = triggers_registrar.register_triggers(packs_base_paths=[packs_base_path])
        self.assertEqual(count, 3)

        # Verify TriggerTypeDB and corresponding TriggerDB objects have been created
        trigger_type_dbs = TriggerType.get_all()
        trigger_dbs = Trigger.get_all()
        self.assertEqual(len(trigger_type_dbs), 3)
        self.assertEqual(len(trigger_dbs), 3)
Exemplo n.º 31
0
    def test_register_sensors(self):
        # Verify DB is empty at the beginning
        self.assertEqual(len(SensorType.get_all()), 0)
        self.assertEqual(len(TriggerType.get_all()), 0)
        self.assertEqual(len(Trigger.get_all()), 0)

        registrar = SensorsRegistrar()
        registrar.register_from_packs(base_dirs=[PACKS_DIR])

        # Verify objects have been created
        sensor_dbs = SensorType.get_all()
        trigger_type_dbs = TriggerType.get_all()
        trigger_dbs = Trigger.get_all()

        self.assertEqual(len(sensor_dbs), 2)
        self.assertEqual(len(trigger_type_dbs), 2)
        self.assertEqual(len(trigger_dbs), 2)

        self.assertEqual(sensor_dbs[0].name, 'TestSensor')
        self.assertEqual(sensor_dbs[0].poll_interval, 10)
        self.assertTrue(sensor_dbs[0].enabled)
        self.assertEqual(sensor_dbs[0].metadata_file,
                         'sensors/test_sensor_1.yaml')

        self.assertEqual(sensor_dbs[1].name, 'TestSensorDisabled')
        self.assertEqual(sensor_dbs[1].poll_interval, 10)
        self.assertFalse(sensor_dbs[1].enabled)
        self.assertEqual(sensor_dbs[1].metadata_file,
                         'sensors/test_sensor_2.yaml')

        self.assertEqual(trigger_type_dbs[0].name, 'trigger_type_1')
        self.assertEqual(trigger_type_dbs[0].pack, 'pack_with_sensor')
        self.assertEqual(len(trigger_type_dbs[0].tags), 0)
        self.assertEqual(trigger_type_dbs[1].name, 'trigger_type_2')
        self.assertEqual(trigger_type_dbs[1].pack, 'pack_with_sensor')
        self.assertEqual(len(trigger_type_dbs[1].tags), 2)
        self.assertEqual(trigger_type_dbs[1].tags[0].name, 'tag1name')
        self.assertEqual(trigger_type_dbs[1].tags[0].value, 'tag1 value')

        # Triggered which are registered via sensors have metadata_file pointing to the sensor
        # definition file
        self.assertEqual(trigger_type_dbs[0].metadata_file,
                         'sensors/test_sensor_1.yaml')
        self.assertEqual(trigger_type_dbs[1].metadata_file,
                         'sensors/test_sensor_1.yaml')

        # Verify second call to registration doesn't create a duplicate objects
        registrar.register_from_packs(base_dirs=[PACKS_DIR])

        sensor_dbs = SensorType.get_all()
        trigger_type_dbs = TriggerType.get_all()
        trigger_dbs = Trigger.get_all()

        self.assertEqual(len(sensor_dbs), 2)
        self.assertEqual(len(trigger_type_dbs), 2)
        self.assertEqual(len(trigger_dbs), 2)

        self.assertEqual(sensor_dbs[0].name, 'TestSensor')
        self.assertEqual(sensor_dbs[0].poll_interval, 10)

        self.assertEqual(trigger_type_dbs[0].name, 'trigger_type_1')
        self.assertEqual(trigger_type_dbs[0].pack, 'pack_with_sensor')
        self.assertEqual(trigger_type_dbs[1].name, 'trigger_type_2')
        self.assertEqual(trigger_type_dbs[1].pack, 'pack_with_sensor')

        # Verify sensor and trigger data is updated on registration
        original_load = registrar._meta_loader.load

        def mock_load(*args, **kwargs):
            # Update poll_interval and trigger_type_2 description
            data = original_load(*args, **kwargs)
            data['poll_interval'] = 50
            data['trigger_types'][1]['description'] = 'test 2'
            return data

        registrar._meta_loader.load = mock_load

        registrar.register_from_packs(base_dirs=[PACKS_DIR])

        sensor_dbs = SensorType.get_all()
        trigger_type_dbs = TriggerType.get_all()
        trigger_dbs = Trigger.get_all()

        self.assertEqual(len(sensor_dbs), 2)
        self.assertEqual(len(trigger_type_dbs), 2)
        self.assertEqual(len(trigger_dbs), 2)

        self.assertEqual(sensor_dbs[0].name, 'TestSensor')
        self.assertEqual(sensor_dbs[0].poll_interval, 50)

        self.assertEqual(trigger_type_dbs[0].name, 'trigger_type_1')
        self.assertEqual(trigger_type_dbs[0].pack, 'pack_with_sensor')
        self.assertEqual(trigger_type_dbs[1].name, 'trigger_type_2')
        self.assertEqual(trigger_type_dbs[1].pack, 'pack_with_sensor')
        self.assertEqual(trigger_type_dbs[1].description, 'test 2')
 def _update_trigger_ref_count(self, trigger_db, ref_count):
     """
     Non-publishing ref_count update to a TriggerDB.
     """
     trigger_db.ref_count = ref_count
     Trigger.add_or_update(trigger_db, publish=False, dispatch_trigger=False)
Exemplo n.º 33
0
 def __get_by_id(trigger_id):
     try:
         return Trigger.get_by_id(trigger_id)
     except (ValueError, ValidationError):
         LOG.exception('Database lookup for id="%s" resulted in exception.', trigger_id)
         abort(http_client.NOT_FOUND)
Exemplo n.º 34
0
 def __get_by_name(trigger_name):
     try:
         return [Trigger.get_by_name(trigger_name)]
     except ValueError as e:
         LOG.debug('Database lookup for name="%s" resulted in exception : %s.', trigger_name, e)
         return []
Exemplo n.º 35
0
 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}
Exemplo n.º 36
0
    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"])))
        trace_service.add_or_update_given_trace_context(
            trace_context={"trace_tag": "test_triggered_execution_trace"},
            trigger_instances=[str(trigger_instance.id)],
        )
        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 = self._wait_on_status(
            liveaction, action_constants.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)
        # NOTE: Timestamp of liveaction and execution may be a bit different, depending on how long
        # it takes to persist each object in the database
        self.assertEqual(
            execution.end_timestamp.replace(microsecond=0),
            liveaction.end_timestamp.replace(microsecond=0),
        )
        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)
Exemplo n.º 37
0
    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)
 def tearDownClass(cls):
     Trigger.delete(cls.__model)
     super(ReferenceTest, cls).tearDownClass()
Exemplo n.º 39
0
    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'])))
        trace_service.add_or_update_given_trace_context(
            trace_context={'trace_tag': 'test_triggered_execution_trace'},
            trigger_instances=[str(trigger_instance.id)])
        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,
                         action_constants.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)
Exemplo n.º 40
0
 def _load_triggers_from_db(self):
     for trigger_type in self._trigger_types:
         for trigger in Trigger.query(type=trigger_type):
             LOG.debug('Found existing trigger: %s in db.' % trigger)
             self._handlers[publishers.CREATE_RK](trigger)
Exemplo n.º 41
0
 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}