Exemplo n.º 1
0
 def setUp(self):
     from alignak.objects.config import Config
     self.item = Config()
Exemplo n.º 2
0
    def test_config_ok(self):
        """Test the object initialization and base features"""
        # ---
        # print("Reference to Config: %s" % sys.getrefcount(Config))
        # mod = importlib.import_module("alignak.objects.config")
        # importlib.reload(mod)
        # #
        # # importlib.reload('alignak.objects.config')
        # print("Reference to Config: %s" % sys.getrefcount(Config))

        # Fresh initialized configuration
        alignak_cfg = Config()
        assert alignak_cfg.magic_hash
        next_instance_id = "Config_%s" % Config._next_id
        # assert str(alignak_cfg) == '<Config Config_1 - unknown />'

        # Another fresh initialized configuration
        alignak_cfg = Config()
        assert alignak_cfg.magic_hash
        # Config instance_id incremented!
        assert next_instance_id == alignak_cfg.instance_id
        # assert str(alignak_cfg) == '<Config Config_2 - unknown />'
        from pprint import pprint
        pprint(alignak_cfg.macros)

        # -----------------------------------------------------------------------------------------
        # Macro part
        # ---
        # Macro list is yet defined but the values are not yet set
        expected_macros = {
            # Main Config objects macros
            'ALIGNAK': 'alignak_name',
            'ALIGNAK_CONFIG': 'alignak_env',
            'ADMINEMAIL': '',
            'ADMINPAGER': '',
            'MAINCONFIGDIR': 'config_base_dir',
            'CONFIGFILES': 'config_files',
            'MAINCONFIGFILE': 'main_config_file',
            'OBJECTCACHEFILE': '',
            'COMMENTDATAFILE': '',
            'TEMPPATH': '',
            'SERVICEPERFDATAFILE': '',
            'RESOURCEFILE': '',
            'COMMANDFILE': '',
            'DOWNTIMEDATAFILE': '',
            'HOSTPERFDATAFILE': '',
            'LOGFILE': '',
            'TEMPFILE': '',
            'RETENTIONDATAFILE': '',
            'STATUSDATAFILE': '',
            'RETENTION_FILE': 'state_retention_file'
        }
        # The 64 "USER" macros.
        for i in range(1, 65):
            expected_macros['USER%d' % i] = '$USER%d$' % i
        assert alignak_cfg.macros == expected_macros

        # After several tests execution the Config object got imported several times and
        # has several python references. The properties object containing the macros is a
        # class object and has thus been updated because some configurations got loaded.
        # Because of this, a pure assertion is only valid when the test is the first one executed!
        compare_macros = {}
        for macro in list(alignak_cfg.macros.items()):
            compare_macros[macro[0]] = macro[1]
            # print(macro)
            # if macro[0] not in [
            #     'DIST', 'DIST_BIN', 'DIST_ETC', 'DIST_LOG', 'DIST_RUN', 'DIST_VAR',
            #     'VAR', 'RUN', 'ETC', 'BIN', 'USER', 'GROUP', 'LIBEXEC', 'LOG',
            #     'NAGIOSPLUGINSDIR', 'PLUGINSDIR', ''
            # ]:
            #     compare_macros[macro[0]] = macro[1]
        assert compare_macros == expected_macros
        assert alignak_cfg.macros == expected_macros

        # # Macro properties are not yet existing!
        # for macro in alignak_cfg.macros:
        #     print("Macro: %s" % macro)
        #     assert getattr(alignak_cfg, '$%s$' % macro, None) is None, \
        #         "Macro: %s property is still existing!" % ('$%s$' % macro)
        # -----------------------------------------------------------------------------------------

        # -----------------------------------------------------------------------------------------
        # Configuration parsing part
        # ---
        # Read and parse the legacy configuration files, do not provide environement file name
        legacy_cfg_files = ['../etc/alignak.cfg']
        raw_objects = alignak_cfg.read_config_buf\
            (alignak_cfg.read_legacy_cfg_files(legacy_cfg_files))
        assert isinstance(raw_objects, dict)
        for daemon_type in [
                'arbiter', 'broker', 'poller', 'reactionner', 'receiver',
                'scheduler'
        ]:
            assert daemon_type in raw_objects
        # Make sure we got all the managed objects type
        for o_type in alignak_cfg.types_creations:
            assert o_type in raw_objects, 'Did not found %s in configuration ojbect' % o_type
        assert alignak_cfg.alignak_env == 'n/a'

        # Same parser that stores the environment files names
        env_filename = '../etc/alignak.ini'
        # It should be a list
        env_filename = [os.path.abspath(env_filename)]
        # Read and parse the legacy configuration files, do not provide environement file name
        raw_objects = alignak_cfg.read_config_buf(
            alignak_cfg.read_legacy_cfg_files(legacy_cfg_files, env_filename))
        assert alignak_cfg.alignak_env == env_filename

        # Same parser that stores a string (not list) environment file name
        # as an absolute file path in a list
        env_filename = '../etc/alignak.ini'
        # Read and parse the legacy configuration files, do not provide environement file name
        raw_objects = alignak_cfg.read_config_buf(
            alignak_cfg.read_legacy_cfg_files(legacy_cfg_files, env_filename))
        assert alignak_cfg.alignak_env == [os.path.abspath(env_filename)]

        # Same parser that stores the environment file name as an absolute file path
        env_filename = '../etc/alignak.ini'
        # Read and parse the legacy configuration files, do not provide environement file name
        raw_objects = alignak_cfg.read_config_buf(
            alignak_cfg.read_legacy_cfg_files(legacy_cfg_files, env_filename))
        assert alignak_cfg.alignak_env == [os.path.abspath(env_filename)]
        # -----------------------------------------------------------------------------------------

        # -----------------------------------------------------------------------------------------
        # Macro part
        # ---
        # The macros defined in the default loaded configuration
        expected_macros.update({
            # 'DIST': '$DIST$',
            # 'DIST_BIN': '$DIST_BIN$',
            # 'DIST_ETC': '$DIST_ETC$',
            # 'DIST_LOG': '$DIST_LOG$',
            # 'DIST_RUN': '$DIST_RUN$',
            # 'DIST_VAR': '$DIST_VAR$',
            'BIN': '$BIN$',
            'ETC': '$ETC$',
            'GROUP': '$GROUP$',
            'LIBEXEC': '$LIBEXEC$',
            'LOG': '$LOG$',
            'NAGIOSPLUGINSDIR': '',
            'PLUGINSDIR': '$',
            'RUN': '$RUN$',
            'USER': '******',
            'USER1': '$NAGIOSPLUGINSDIR$',
            'VAR': '$VAR$'
        })
        assert sorted(alignak_cfg.macros) == sorted(expected_macros)
        assert alignak_cfg.resource_macros_names == []
        # Macro are not existing in the object attributes!
        for macro in alignak_cfg.macros:
            macro = alignak_cfg.macros[macro]
            assert getattr(alignak_cfg, '$%s$' % macro, None) is None, \
                "Macro: %s property is existing as an attribute!" % ('$%s$' % macro)
        # But as an attribute of the properties attribute!
        for macro in alignak_cfg.macros:
            macro = alignak_cfg.macros[macro]
            assert getattr(alignak_cfg.properties, '$%s$' % macro, None) is None, \
                "Macro: %s property is not existing as an attribute of properties!" % ('$%s$' % macro)
 class Arbiter(object):
     conf = Config()
     pidfile = '/tmp/arbiter.pid'
 class Arbiter(object):
     conf = Config()
Exemplo n.º 5
0
    def test_types(self):
        path = 'etc/alignak_1r_1h_1s.cfg'
        time_hacker.set_my_time()
        self.print_header()
        # i am arbiter-like
        self.broks = {}
        self.me = None
        self.log = logger
        self.log.setLevel("INFO")
        self.log.load_obj(self)
        self.config_files = [path]
        self.conf = Config()
        buf = self.conf.read_config(self.config_files)
        raw_objects = self.conf.read_config_buf(buf)
        self.conf.create_objects_for_type(raw_objects, 'arbiter')
        self.conf.create_objects_for_type(raw_objects, 'module')
        self.conf.early_arbiter_linking()
        self.conf.create_objects(raw_objects)
        self.conf.instance_id = 0
        self.conf.instance_name = 'test'
        # Hack push_flavor, that is set by the dispatcher
        self.conf.push_flavor = 0
        self.conf.load_triggers()
        self.conf.linkify_templates()
        self.conf.apply_inheritance()
        self.conf.explode()

        self.conf.apply_implicit_inheritance()
        self.conf.fill_default()
        self.conf.remove_templates()

        self.conf.override_properties()
        self.conf.linkify()
        self.conf.apply_dependencies()
        self.conf.explode_global_conf()
        self.conf.propagate_timezone_option()
        self.conf.create_business_rules()
        self.conf.create_business_rules_dependencies()
        self.conf.is_correct()

        ###############

        for objects in (self.conf.arbiters, self.conf.contacts,
                        self.conf.notificationways, self.conf.hosts):
            self.check_objects_from(objects)

        print "== test Check() =="
        check = Check('OK', 'check_ping', 0, 10.0)
        for prop in check.properties:
            if hasattr(check, prop):
                value = getattr(check, prop)
                # We should get ride of None, maybe use the "neutral" value for type
                if prop not in ['ref']:  # TODO : clean this
                    if value is not None:
                        print("TESTING %s with value %s" % (prop, value))
                        self.assertIsInstance(
                            value, self.map_type(check.properties[prop]))
                    else:
                        print("Skipping %s " % prop)

        print "== test Notification() =="
        notification = Notification()
        for prop in notification.properties:
            if hasattr(notification, prop):
                value = getattr(notification, prop)
                # We should get ride of None, maybe use the "neutral" value for type
                if prop not in ['already_start_escalations'
                                ]:  # TODO : clean this
                    if value is not None:
                        print("TESTING %s with value %s" % (prop, value))
                        self.assertIsInstance(
                            value,
                            self.map_type(notification.properties[prop]))
                    else:
                        print("Skipping %s " % prop)

        print "== test EventHandler() =="
        eventhandler = EventHandler('')
        for prop in eventhandler.properties:
            if hasattr(eventhandler, prop):
                value = getattr(eventhandler, prop)
                # We should get ride of None, maybe use the "neutral" value for type
                if prop not in ['jjjj']:  # TODO : clean this
                    if value is not None:
                        print("TESTING %s with value %s" % (prop, value))
                        self.assertIsInstance(
                            value,
                            self.map_type(eventhandler.properties[prop]))
                    else:
                        print("Skipping %s " % prop)

        print "== test Timeperiod() =="
        timeperiod = Timeperiod()
        for prop in timeperiod.properties:
            if hasattr(timeperiod, prop):
                value = getattr(timeperiod, prop)
                # We should get ride of None, maybe use the "neutral" value for type
                if value is not None:
                    print("TESTING %s with value %s" % (prop, value))
                    self.assertIsInstance(
                        value, self.map_type(timeperiod.properties[prop]))
                else:
                    print("Skipping %s " % prop)

        print "== test Command() =="
        command = Command({})
        for prop in command.properties:
            if hasattr(command, prop):
                value = getattr(command, prop)
                # We should get ride of None, maybe use the "neutral" value for type
                if value is not None:
                    print("TESTING %s with value %s" % (prop, value))
                    self.assertIsInstance(
                        value, self.map_type(command.properties[prop]))
                else:
                    print("Skipping %s " % prop)
Exemplo n.º 6
0
    def setup_with_file(self, paths, add_default=True):
        self.time_hacker.set_my_time()
        self.print_header()
        # i am arbiter-like
        self.broks = {}
        self.me = None
        self.log = logger
        self.log.load_obj(self)
        if not isinstance(paths, list):
            paths = [paths]  # Fix for modules tests
            add_default = False # Don't mix config
        if add_default:
            paths.insert(0, 'etc/alignak_1r_1h_1s.cfg')
        self.config_files = paths
        self.conf = Config()
        buf = self.conf.read_config(self.config_files)
        raw_objects = self.conf.read_config_buf(buf)
        self.conf.create_objects_for_type(raw_objects, 'arbiter')
        self.conf.create_objects_for_type(raw_objects, 'module')
        self.conf.early_arbiter_linking()

        # If we got one arbiter defined here (before default) we should be in a case where
        # the tester want to load/test a module, so we simulate an arbiter daemon
        # and the modules loading phase. As it has its own modulesmanager, should
        # not impact scheduler modules ones, especially we are asking for arbiter type :)
        if len(self.conf.arbiters) == 1:
            arbdaemon = Arbiter([''], [''], False, False, None, None)

            arbdaemon.load_modules_manager()

            # we request the instances without them being *started*
            # (for those that are concerned ("external" modules):
            # we will *start* these instances after we have been daemonized (if requested)
            me = None
            for arb in self.conf.arbiters:
                me = arb
                arbdaemon.do_load_modules(arb.modules)
                arbdaemon.load_modules_configuration_objects(raw_objects)

        self.conf.create_objects(raw_objects)
        self.conf.instance_id = 0
        self.conf.instance_name = 'test'
        # Hack push_flavor, that is set by the dispatcher
        self.conf.push_flavor = 0
        self.conf.load_triggers()
        #import pdb;pdb.set_trace()
        self.conf.linkify_templates()
        #import pdb;pdb.set_trace()
        self.conf.apply_inheritance()
        #import pdb;pdb.set_trace()
        self.conf.explode()
        #print "Aconf.services has %d elements" % len(self.conf.services)
        self.conf.apply_implicit_inheritance()
        self.conf.fill_default()
        self.conf.remove_templates()
        #print "conf.services has %d elements" % len(self.conf.services)
        self.conf.override_properties()
        self.conf.linkify()
        self.conf.apply_dependencies()
        self.conf.explode_global_conf()
        self.conf.propagate_timezone_option()
        self.conf.create_business_rules()
        self.conf.create_business_rules_dependencies()
        self.conf.is_correct()
        if not self.conf.conf_is_correct:
            print "The conf is not correct, I stop here"
            self.conf.dump()
            return
        self.conf.clean()

        self.confs = self.conf.cut_into_parts()
        self.conf.prepare_for_sending()
        self.conf.show_errors()
        self.dispatcher = Dispatcher(self.conf, self.me)

        scheddaemon = Alignak(None, False, False, False, None, None)
        self.scheddaemon = scheddaemon
        self.sched = scheddaemon.sched
        scheddaemon.load_modules_manager()
        # Remember to clean the logs we just created before launching tests
        self.clear_logs()
        m = MacroResolver()
        m.init(self.conf)
        self.sched.load_conf(self.conf)
        e = ExternalCommandManager(self.conf, 'applyer')
        self.sched.external_command = e
        e.load_scheduler(self.sched)
        e2 = ExternalCommandManager(self.conf, 'dispatcher')
        e2.load_arbiter(self)
        self.external_command_dispatcher = e2
        self.sched.conf.accept_passive_unknown_check_results = False

        self.sched.schedule()
Exemplo n.º 7
0
 def setUp(self):
     super(TestConfig, self).setUp()
     from alignak.objects.config import Config
     self.item = Config()