Esempio n. 1
0
    def setUp(self):
        # Create and test an action object
        a = Action()
        assert a.env == {}
        assert a.timeout == 10
        assert a.exit_status == 3

        time_hacker.set_real_time()
Esempio n. 2
0
    def setUp(self):
        """
        For each test load and check the configuration
        :return: None
        """
        self.print_header()
        self.setup_with_file('./cfg/cfg_escalations.cfg')
        assert self.conf_is_correct

        # Our scheduler
        self._sched = self.schedulers['scheduler-master'].sched

        # Our broker
        self._broker = self._sched.brokers['broker-master']

        # No error messages
        assert len(self.configuration_errors) == 0
        # No warning messages
        assert len(self.configuration_warnings) == 0

        time_hacker.set_real_time()
Esempio n. 3
0
    def test_module_manager(self):
        """
        Test if the module manager manages correctly all the modules
        :return:
        """
        self.print_header()
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        time_hacker.set_real_time()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'web-services',
            'module_types': 'web-services',
            'python_name': 'alignak_module_ws',
            # Set Arbiter address as empty to not poll the Arbiter else the test will fail!
            'alignak_host': '',
            'alignak_port': 7770,
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('receiver', None)

        # Load and initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        # Loading module logs
        self.assert_any_log_match(re.escape(
            "Importing Python module 'alignak_module_ws' for web-services..."
        ))
        self.assert_any_log_match(re.escape(
            "Module properties: {'daemons': ['receiver'], 'phases': ['running'], "
            "'type': 'web-services', 'external': True}"
        ))
        self.assert_any_log_match(re.escape(
            "Imported 'alignak_module_ws' for web-services"
        ))
        self.assert_any_log_match(re.escape(
            "Loaded Python module 'alignak_module_ws' (web-services)"
        ))
        self.assert_any_log_match(re.escape(
            "Give an instance of alignak_module_ws for alias: web-services"
        ))

        my_module = self.modulemanager.instances[0]

        # Get list of not external modules
        self.assertListEqual([], self.modulemanager.get_internal_instances())
        for phase in ['configuration', 'late_configuration', 'running', 'retention']:
            self.assertListEqual([], self.modulemanager.get_internal_instances(phase))

        # Get list of external modules
        self.assertListEqual([my_module], self.modulemanager.get_external_instances())
        for phase in ['configuration', 'late_configuration', 'retention']:
            self.assertListEqual([], self.modulemanager.get_external_instances(phase))
        for phase in ['running']:
            self.assertListEqual([my_module], self.modulemanager.get_external_instances(phase))

        # Clear logs
        self.clear_logs()

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module logs
        self.assert_log_match("Trying to initialize module: web-services", 0)
        self.assert_log_match("Starting external module web-services", 1)
        self.assert_log_match("Starting external process for module web-services", 2)
        self.assert_log_match("web-services is now started", 3)

        # Check alive
        self.assertIsNotNone(my_module.process)
        self.assertTrue(my_module.process.is_alive())

        # Clear logs
        self.clear_logs()

        # Kill the external module (normal stop is .stop_process)
        my_module.kill()
        time.sleep(0.1)
        self.assert_log_match("Killing external module", 0)
        self.assert_log_match("External module killed", 1)

        # Should be dead (not normally stopped...) but we still know a process for this module!
        self.assertIsNotNone(my_module.process)

        # Nothing special ...
        self.modulemanager.check_alive_instances()
        self.assert_log_match("The external module web-services died unexpectedly!", 2)
        self.assert_log_match("Setting the module web-services to restart", 3)

        # Try to restart the dead modules
        self.modulemanager.try_to_restart_deads()
        self.assert_log_match("Trying to initialize module: web-services", 4)

        # In fact it's too early, so it won't do it
        # The module instance is still dead
        self.assertFalse(my_module.process.is_alive())

        # So we lie, on the restart tries ...
        my_module.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()
        self.assert_log_match("Trying to initialize module: web-services", 5)

        # The module instance is now alive again
        self.assertTrue(my_module.process.is_alive())
        self.assert_log_match("I'm stopping module 'web-services'", 6)
        self.assert_log_match("Starting external process for module web-services", 7)
        self.assert_log_match("web-services is now started", 8)

        # There is nothing else to restart in the module manager
        self.assertEqual([], self.modulemanager.to_restart)

        # Clear logs
        self.clear_logs()

        # Now we look for time restart so we kill it again
        my_module.kill()
        time.sleep(0.2)
        self.assertFalse(my_module.process.is_alive())
        self.assert_log_match("Killing external module", 0)
        self.assert_log_match("External module killed", 1)

        # Should be too early
        self.modulemanager.check_alive_instances()
        self.assert_log_match("The external module web-services died unexpectedly!", 2)
        self.assert_log_match("Setting the module web-services to restart", 3)

        self.modulemanager.try_to_restart_deads()
        self.assert_log_match("Trying to initialize module: web-services", 4)

        # In fact it's too early, so it won't do it
        # The module instance is still dead
        self.assertFalse(my_module.process.is_alive())

        # So we lie, on the restart tries ...
        my_module.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()
        self.assert_log_match("Trying to initialize module: web-services", 5)

        # The module instance is now alive again
        self.assertTrue(my_module.process.is_alive())
        self.assert_log_match("I'm stopping module 'web-services'", 6)
        self.assert_log_match("Starting external process for module web-services", 7)
        self.assert_log_match("web-services is now started", 8)

        # And we clear all now
        self.modulemanager.stop_all()
        # Stopping module logs

        self.assert_log_match("Request external process to stop for web-services", 9)
        self.assert_log_match(re.escape("I'm stopping module 'web-services' (pid="), 10)
        self.assert_log_match(
            re.escape("'web-services' is still alive after normal kill, I help it to die"), 11
        )
        self.assert_log_match("Killing external module ", 12)
        self.assert_log_match("External module killed", 13)
        self.assert_log_match("External process stopped.", 14)
    def test_module_manager_arbiter(self):
        """
        Test if the module manager manages correctly all the modules
        :return:
        """
        self.print_header()
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        time_hacker.set_real_time()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'SnmpBoosterArbiter',
            'module_types': 'checks',
            'python_name': 'alignak_module_snmp_booster.snmpbooster_arbiter',
            'loaded_by': 'arbiter',
            'datasource': './cfg/genDevConfig/example.ini',
            'db_host': 'localhost',
            'db_port': 6379
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('arbiter', None)

        # Clear logs
        self.clear_logs()

        # Load and initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        # Loading module logs
        self.assert_log_match(
            re.escape(
                "Importing Python module 'alignak_module_snmp_booster.snmpbooster_arbiter' for "
                "SnmpBoosterArbiter..."), 0)
        self.assert_log_match(
            re.escape(
                "Module properties: {'daemons': ['arbiter'], 'phases': ['running', "
                "'late_configuration'], 'type': 'snmp_booster', 'external': False, "
                "'worker_capable': True}"), 1)
        self.assert_log_match(
            re.escape(
                "Imported 'alignak_module_snmp_booster.snmpbooster_arbiter' for SnmpBoosterArbiter"
            ), 2)
        self.assert_log_match(
            re.escape(
                "Loaded Python module 'alignak_module_snmp_booster.snmpbooster_arbiter' "
                "(SnmpBoosterArbiter)"), 3)
        self.assert_log_match(
            re.escape(
                "Give an instance of alignak_module_snmp_booster.snmpbooster_arbiter "
                "for alias: SnmpBoosterArbiter"), 4)

        self.assert_log_match(
            re.escape(
                "[SnmpBooster] [code 0101] Loading SNMP Booster module for plugin SnmpBoosterArbiter"
            ), 5)
        self.assert_log_match(
            re.escape(
                "[SnmpBooster] [code 0902] Reading input configuration file: "
                "./cfg/genDevConfig/example.ini"), 6)

        # Starting internal module logs
        self.assert_log_match(
            re.escape("Trying to initialize module: SnmpBoosterArbiter"), 7)
        self.assert_log_match(
            re.escape(
                "[SnmpBooster] [code 1101] Initialization of the SNMP Booster 2.0.0"
            ), 8)

        my_module = self.modulemanager.instances[0]

        # Get list of not external modules
        self.assertListEqual([my_module],
                             self.modulemanager.get_internal_instances())
        for phase in ['configuration', 'retention']:
            self.assertListEqual(
                [], self.modulemanager.get_internal_instances(phase))
        for phase in ['late_configuration', 'running']:
            self.assertListEqual(
                [my_module], self.modulemanager.get_internal_instances(phase))

        # Get list of external modules
        self.assertListEqual([], self.modulemanager.get_external_instances())
        for phase in [
                'configuration', 'late_configuration', 'running', 'retention'
        ]:
            self.assertListEqual(
                [], self.modulemanager.get_external_instances(phase))

        # Clear logs
        self.clear_logs()

        # Nothing special ...
        self.modulemanager.check_alive_instances()

        # And we clear all now
        self.modulemanager.stop_all()
Esempio n. 5
0
 def setUp(self):
     self.setup_with_file([])
     time_hacker.set_real_time()
Esempio n. 6
0
    def test_module_manager(self):
        """
        Test if the module manager manages correctly all the modules
        :return:
        """
        self.print_header()
        self.setup_with_file('cfg/cfg_default.cfg')
        self.assertTrue(self.conf_is_correct)

        time_hacker.set_real_time()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'nrpe-booster',
            'module_types': 'nrpe-booster',
            'python_name': 'alignak_module_nrpe_booster'
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('poller', None)

        # Clear logs
        self.clear_logs()

        # Load and initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        # Loading module logs
        self.assert_log_match(
            re.escape(
                "Importing Python module 'alignak_module_nrpe_booster' for nrpe-booster..."
            ), 0)
        self.assert_log_match(
            re.escape(
                "Module properties: {'daemons': ['poller'], 'phases': ['running'], "
                "'type': 'nrpe_poller', 'external': False, 'worker_capable': True}"
            ), 1)
        self.assert_log_match(
            re.escape(
                "Imported 'alignak_module_nrpe_booster' for nrpe-booster"), 2)
        self.assert_log_match(
            re.escape(
                "Loaded Python module 'alignak_module_nrpe_booster' (nrpe-booster)"
            ), 3)
        self.assert_log_match(
            re.escape(
                "Give an instance of alignak_module_nrpe_booster for alias: nrpe-booster"
            ), 4)

        # Starting internal module logs
        self.assert_log_match("Trying to initialize module: nrpe-booster", 5)
        self.assert_log_match("Initialization of the NRPE poller module", 6)

        my_module = self.modulemanager.instances[0]

        # Get list of not external modules
        self.assertListEqual([my_module],
                             self.modulemanager.get_internal_instances())
        for phase in ['configuration', 'late_configuration', 'retention']:
            self.assertListEqual(
                [], self.modulemanager.get_internal_instances(phase))
        for phase in ['running']:
            self.assertListEqual(
                [my_module], self.modulemanager.get_internal_instances(phase))

        # Get list of external modules
        self.assertListEqual([], self.modulemanager.get_external_instances())
        for phase in [
                'configuration', 'late_configuration', 'running', 'retention'
        ]:
            self.assertListEqual(
                [], self.modulemanager.get_external_instances(phase))

        # Clear logs
        self.clear_logs()

        # Nothing special ...
        self.modulemanager.check_alive_instances()

        # And we clear all now
        self.modulemanager.stop_all()
        # Stopping module logs

        self.assert_log_match("Ending the NRPE poller module", 0)
Esempio n. 7
0
 def setUp(self):
     self.setup_with_file([])
     time_hacker.set_real_time()
Esempio n. 8
0
    def test_modulemanager(self):
        """ Module manager manages its modules

        Test if the module manager manages correctly all the modules
        :return:
        """
        self.print_header()
        self.setup_with_file('cfg/cfg_default_with_modules.cfg')
        assert self.conf_is_correct

        time_hacker.set_real_time()

        # Create an Alignak module
        mod = Module({
            'module_alias': 'mod-example',
            'module_types': 'example',
            'python_name': 'alignak_module_example'
        })

        # Create the modules manager for a daemon type
        self.modulemanager = ModulesManager('receiver', None)

        # Load an initialize the modules:
        #  - load python module
        #  - get module properties and instances
        self.modulemanager.load_and_init([mod])

        # Loading module logs
        self.assert_any_log_match(
            re.escape(
                "Importing Python module 'alignak_module_example' for Example..."
            ))
        self.assert_any_log_match(
            re.escape(
                "Module properties: {'daemons': ['arbiter', 'broker', 'scheduler', 'poller', "
                "'receiver', 'reactionner'], 'phases': ['configuration', 'late_configuration', "
                "'running', 'retention'], 'type': 'example', 'external': True}"
            ))
        self.assert_any_log_match(
            re.escape("Imported 'alignak_module_example' for Example"))
        self.assert_any_log_match(
            re.escape(
                "Give an instance of alignak_module_example for alias: Example"
            ))
        self.assert_any_log_match(
            re.escape("I correctly loaded my modules: [Example]"))

        my_module = self.modulemanager.instances[0]
        assert my_module.is_external

        # Get list of not external modules
        assert [] == self.modulemanager.get_internal_instances()
        for phase in [
                'configuration', 'late_configuration', 'running', 'retention'
        ]:
            assert [] == self.modulemanager.get_internal_instances(phase)

        # Get list of external modules
        assert [my_module] == self.modulemanager.get_external_instances()
        for phase in [
                'configuration', 'late_configuration', 'running', 'retention'
        ]:
            assert [my_module
                    ] == self.modulemanager.get_external_instances(phase)

        # Start external modules
        self.modulemanager.start_external_instances()

        # Starting external module logs
        self.assert_any_log_match(
            re.escape("Starting external module mod-example"))
        self.assert_any_log_match(
            re.escape("Starting external process for module mod-example"))
        self.assert_any_log_match(
            re.escape("mod-example is now started (pid="))

        # Check alive
        assert my_module.process is not None
        assert my_module.process.is_alive()

        # Kill the external module (normal stop is .stop_process)
        my_module.kill()
        time.sleep(0.1)
        # Should be dead (not normally stopped...) but we still know a process for this module!
        assert my_module.process is not None

        # Stopping module logs
        self.assert_any_log_match(re.escape("Killing external module "))
        self.assert_any_log_match(re.escape("External module killed"))

        # Nothing special ...
        self.modulemanager.check_alive_instances()

        # Try to restart the dead modules
        self.modulemanager.try_to_restart_deads()

        # In fact it's too early, so it won't do it

        # Here the inst should still be dead
        assert not my_module.process.is_alive()

        # So we lie
        my_module.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()

        # In fact it's too early, so it won't do it

        # Here the inst should be alive again
        assert my_module.process.is_alive()

        # should be nothing more in to_restart of
        # the module manager
        assert [] == self.modulemanager.to_restart

        # Now we look for time restart so we kill it again
        my_module.kill()
        time.sleep(0.2)
        assert not my_module.process.is_alive()

        # Should be too early
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()
        assert not my_module.process.is_alive()
        # We lie for the test again
        my_module.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()

        # Here the inst should be alive again
        assert my_module.process.is_alive()

        # And we clear all now
        self.modulemanager.stop_all()
        # Stopping module logs
        self.assert_any_log_match(re.escape("I'm stopping module "))