Example #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()

        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)
Example #2
0
    def test_trigger_types_are_registered_on_start(self):
        timer = St2Timer()
        timer._scheduler = Mock()

        # Verify there are no TriggerType in the db when we start
        self.assertItemsEqual(TriggerType.get_all(), [])

        timer.start()

        # Verify TriggerType objects have been created
        trigger_type_dbs = TriggerType.get_all()
        self.assertEqual(len(trigger_type_dbs), len(TIMER_TRIGGER_TYPES))

        timer_trigger_type_refs = TIMER_TRIGGER_TYPES.keys()

        for trigger_type in trigger_type_dbs:
            ref = ResourceReference(pack=trigger_type.pack, name=trigger_type.name).ref
            self.assertTrue(ref in timer_trigger_type_refs)
    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')