def test_create_or_update_trigger_db_simple_triggers(self): test_fixtures = {'triggertypes': ['triggertype1.json']} 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.json']['name'], pack=triggertypes['triggertype1.json']['pack']) trigger = { 'name': triggertypes['triggertype1.json']['name'], 'pack': triggertypes['triggertype1.json']['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.json']['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.json']['name'])
def test_create_or_update_trigger_db_simple_triggers(self): test_fixtures = { 'triggertypes': ['triggertype1.json'] } 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.json']['name'], pack=triggertypes['triggertype1.json']['pack']) trigger = { 'name': triggertypes['triggertype1.json']['name'], 'pack': triggertypes['triggertype1.json']['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.json']['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.json']['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_rules_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_rules_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)
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 get_all(self, **kw): """ List all triggers. Handles requests: GET /triggers/ """ trigger_dbs = Trigger.get_all(**kw) trigger_apis = [TriggerAPI.from_model(trigger_db) for trigger_db in trigger_dbs] return trigger_apis
def get_all(self, **kw): """ List all triggers. Handles requests: GET /triggers/ """ LOG.info('GET all /triggers/ with filters=%s', kw) trigger_dbs = Trigger.get_all(**kw) trigger_apis = [TriggerAPI.from_model(trigger_db) for trigger_db in trigger_dbs] return trigger_apis
def get_all(self, **kw): """ List all triggers. Handles requests: GET /triggers/ """ trigger_dbs = Trigger.get_all(**kw) trigger_apis = [ TriggerAPI.from_model(trigger_db) for trigger_db in trigger_dbs ] return trigger_apis
def test_add_trigger_type_no_params(self): # Trigger type with no params should create a trigger with same name as trigger type. trig_type = { 'name': 'myawesometriggertype', 'pack': 'dummy_pack_1', 'description': 'Words cannot describe how awesome I am.', 'parameters_schema': {}, 'payload_schema': {} } trigtype_dbs = container_utils.add_trigger_models(trigger_types=[trig_type]) trigger_type, trigger = trigtype_dbs[0] trigtype_db = TriggerType.get_by_id(trigger_type.id) self.assertEqual(trigtype_db.pack, 'dummy_pack_1') self.assertEqual(trigtype_db.name, trig_type.get('name')) self.assertTrue(trigger is not None) self.assertEqual(trigger.name, trigtype_db.name) # Add duplicate trigtype_dbs = container_utils.add_trigger_models(trigger_types=[trig_type]) triggers = Trigger.get_all() self.assertTrue(len(triggers) == 1)
def run_sensors(self, sensors_dict): LOG.info('Setting up container to run %d sensors.', len(sensors_dict)) container_service = ContainerService() sensors_to_run = [] # TODO: Once the API registration is in place, query DB for available # sensors here # TODO: Use trigger_types and description from sensors metadata for filename, sensors in six.iteritems(sensors_dict): for sensor_class in sensors: sensor_class_kwargs = {} class_name = sensor_class.__name__ # System sensors which are not located inside a content pack # don't and can't have custom config associated with them pack = getattr(sensor_class, 'pack', None) if pack: # TODO: Don't parse the same config multiple times when we # are referring to sensors from the same pack pack = validate_pack_name(name=pack) config_parser = ContentPackConfigParser(pack_name=pack) config = config_parser.get_sensor_config(sensor_file_path=filename) if config: sensor_class_kwargs['config'] = config.config LOG.info('Using config "%s" for sensor "%s"' % (config.file_path, class_name)) else: LOG.info('No config found for sensor "%s"' % (class_name)) sensor_class_kwargs['config'] = {} else: pack = SYSTEM_PACK_NAME try: sensor = sensor_class(container_service=container_service, **sensor_class_kwargs) except Exception as e: LOG.warning('Unable to create instance for sensor %s in file %s. Exception: %s', sensor_class, filename, e, exc_info=True) continue try: trigger_types = sensor.get_trigger_types() if not trigger_types: trigger_type_dbs = [] LOG.warning('No trigger type registered by sensor %s in file %s', sensor_class, filename) else: assert isinstance(trigger_types, (list, tuple)) trigger_type_dbs = container_utils.add_trigger_models( pack=pack, trigger_types=trigger_types) except TriggerTypeRegistrationException as e: LOG.warning('Unable to register trigger type for sensor %s in file %s.' + ' Exception: %s', sensor_class, filename, e, exc_info=True) continue # Populate sensors dict trigger_type_refs = [] for trigger_type_db, _ in trigger_type_dbs: ref_obj = trigger_type_db.get_reference() trigger_type_ref = ref_obj.ref self._trigger_sensors[trigger_type_ref] = sensor trigger_type_refs.append(trigger_type_ref) # Register sensor type in the DB sensor_obj = { 'filename': os.path.abspath(filename), 'name': class_name, 'class_name': class_name, 'trigger_types': trigger_type_refs } container_utils.add_sensor_model(pack=pack, sensor=sensor_obj) # Add good sensor to the run list sensors_to_run.append(sensor) for trigger in Trigger.get_all(): self._create_handler(trigger=trigger) self._trigger_watcher.start() LOG.info('Watcher started.') LOG.info('(PID:%s) SensorContainer started.', os.getpid()) sensor_container = SensorContainer(sensor_instances=sensors_to_run) try: exit_code = sensor_container.run() LOG.info('(PID:%s) SensorContainer stopped. Reason - run ended.', os.getpid()) return exit_code except (KeyboardInterrupt, SystemExit): LOG.info('(PID:%s) SensorContainer stopped. Reason - %s', os.getpid(), sys.exc_info()[0].__name__) return 0 finally: self._trigger_watcher.stop()
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_sensors_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[1].name, 'TestSensorDisabled') self.assertEqual(sensor_dbs[1].poll_interval, 10) self.assertFalse(sensor_dbs[1].enabled) 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 second call to registration doesn't create a duplicate objects registrar.register_sensors_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_sensors_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')