Example #1
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)
Example #2
0
class AlignakTest(unittest.TestCase):

    time_hacker = TimeHacker()

    if sys.version_info < (2, 7):
        def assertRegex(self, *args, **kwargs):
            return self.assertRegexpMatches(*args, **kwargs)

    def setUp(self):
        self.setup_with_file(['etc/alignak_1r_1h_1s.cfg'], add_default=False)

    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)
            # only load if the module_dir is reallyexisting, so was set explicitly
            # in the test configuration
            if os.path.exists(getattr(self.conf, 'modules_dir', '')):
                arbdaemon.modules_dir = self.conf.modules_dir
                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.modules_manager.set_modules(arb.modules)
                    arbdaemon.do_load_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.modules_dir = modules_dir
        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()

    def add(self, b):
        if isinstance(b, Brok):
            self.broks[b._id] = b
            return
        if isinstance(b, ExternalCommand):
            self.sched.run_external_command(b.cmd_line)

    def fake_check(self, ref, exit_status, output="OK"):
        #print "fake", ref
        now = time.time()
        ref.schedule(force=True)
        # now checks are schedule and we get them in
        # the action queue
        #check = ref.actions.pop()
        check = ref.checks_in_progress[0]
        self.sched.add(check)  # check is now in sched.checks[]

        # Allows to force check scheduling without setting its status nor
        # output. Useful for manual business rules rescheduling, for instance.
        if exit_status is None:
            return

        # fake execution
        check.check_time = now

        # and lie about when we will launch it because
        # if not, the schedule call for ref
        # will not really reschedule it because there
        # is a valid value in the future
        ref.next_chk = now - 0.5

        check.get_outputs(output, 9000)
        check.exit_status = exit_status
        check.execution_time = 0.001
        check.status = 'waitconsume'
        self.sched.waiting_results.append(check)


    def scheduler_loop(self, count, reflist, do_sleep=False, sleep_time=61, verbose=True):
        for ref in reflist:
            (obj, exit_status, output) = ref
            obj.checks_in_progress = []
        for loop in range(1, count + 1):
            if verbose is True:
                print "processing check", loop
            for ref in reflist:
                (obj, exit_status, output) = ref
                obj.update_in_checking()
                self.fake_check(obj, exit_status, output)
            self.sched.manage_internal_checks()

            self.sched.consume_results()
            self.sched.get_new_actions()
            self.sched.get_new_broks()
            self.sched.scatter_master_notifications()
            self.worker_loop(verbose)
            for ref in reflist:
                (obj, exit_status, output) = ref
                obj.checks_in_progress = []
            self.sched.update_downtimes_and_comments()
            #time.sleep(ref.retry_interval * 60 + 1)
            if do_sleep:
                time.sleep(sleep_time)


    def worker_loop(self, verbose=True):
        self.sched.delete_zombie_checks()
        self.sched.delete_zombie_actions()
        checks = self.sched.get_to_run_checks(True, False, worker_name='tester')
        actions = self.sched.get_to_run_checks(False, True, worker_name='tester')
        #print "------------ worker loop checks ----------------"
        #print checks
        #print "------------ worker loop actions ----------------"
        if verbose is True:
            self.show_actions()
        #print "------------ worker loop new ----------------"
        for a in actions:
            a.status = 'inpoller'
            a.check_time = time.time()
            a.exit_status = 0
            self.sched.put_results(a)
        if verbose is True:
            self.show_actions()
        #print "------------ worker loop end ----------------"


    def show_logs(self):
        print "--- logs <<<----------------------------------"
        if hasattr(self, "sched"):
            broks = self.sched.broks
        else:
            broks = self.broks
        for brok in sorted(broks.values(), lambda x, y: x._id - y._id):
            if brok.type == 'log':
                brok.prepare()
                safe_print("LOG: ", brok.data['log'])

        print "--- logs >>>----------------------------------"


    def show_actions(self):
        print "--- actions <<<----------------------------------"
        if hasattr(self, "sched"):
            actions = self.sched.actions
        else:
            actions = self.actions
        for a in sorted(actions.values(), lambda x, y: x._id - y._id):
            if a.is_a == 'notification':
                if a.ref.my_type == "host":
                    ref = "host: %s" % a.ref.get_name()
                else:
                    ref = "host: %s svc: %s" % (a.ref.host.get_name(), a.ref.get_name())
                print "NOTIFICATION %d %s %s %s %s" % (a._id, ref, a.type, time.asctime(time.localtime(a.t_to_go)), a.status)
            elif a.is_a == 'eventhandler':
                print "EVENTHANDLER:", a
        print "--- actions >>>----------------------------------"


    def show_and_clear_logs(self):
        self.show_logs()
        self.clear_logs()


    def show_and_clear_actions(self):
        self.show_actions()
        self.clear_actions()


    def count_logs(self):
        if hasattr(self, "sched"):
            broks = self.sched.broks
        else:
            broks = self.broks
        return len([b for b in broks.values() if b.type == 'log'])


    def count_actions(self):
        if hasattr(self, "sched"):
            actions = self.sched.actions
        else:
            actions = self.actions
        return len(actions.values())


    def clear_logs(self):
        if hasattr(self, "sched"):
            broks = self.sched.broks
        else:
            broks = self.broks
        id_to_del = []
        for b in broks.values():
            if b.type == 'log':
                id_to_del.append(b._id)
        for id in id_to_del:
            del broks[id]


    def clear_actions(self):
        if hasattr(self, "sched"):
            self.sched.actions = {}
        else:
            self.actions = {}


    def assert_log_match(self, index, pattern, no_match=False):
        # log messages are counted 1...n, so index=1 for the first message
        if not no_match:
            self.assertGreaterEqual(self.count_logs(), index)
        regex = re.compile(pattern)
        lognum = 1
        broks = sorted(self.sched.broks.values(), key=lambda x: x._id)
        for brok in broks:
            if brok.type == 'log':
                brok.prepare()
                if index == lognum:
                    if re.search(regex, brok.data['log']):
                        return
                lognum += 1
        self.assertTrue(no_match, "%s found a matched log line in broks :\n"
                               "index=%s pattern=%r\n"
                               "broks_logs=[[[\n%s\n]]]" % (
            '*HAVE*' if no_match else 'Not',
            index, pattern, '\n'.join(
                '\t%s=%s' % (idx, b.strip())
                for idx, b in enumerate(
                    (b.data['log'] for b in broks if b.type == 'log'),
                    1)
            )
        ))

    def _any_log_match(self, pattern, assert_not):
        regex = re.compile(pattern)
        broks = getattr(self, 'sched', self).broks
        broks = sorted(broks.values(), lambda x, y: x._id - y._id)
        for brok in broks:
            if brok.type == 'log':
                brok.prepare()
                if re.search(regex, brok.data['log']):
                    self.assertTrue(not assert_not,
                                    "Found matching log line:\n"
                                    "pattern = %r\nbrok log = %r" % (pattern, brok.data['log'])
                    )
                    return
        logs = [brok.data['log'] for brok in broks if brok.type == 'log']
        self.assertTrue(assert_not,
            "No matching log line found:\n"
            "pattern = %r\n" "logs broks = %r" % (pattern, logs)
        )

    def assert_any_log_match(self, pattern):
        self._any_log_match(pattern, assert_not=False)

    def assert_no_log_match(self, pattern):
        self._any_log_match(pattern, assert_not=True)


    def get_log_match(self, pattern):
        regex = re.compile(pattern)
        res = []
        for brok in sorted(self.sched.broks.values(), lambda x, y: x._id - y._id):
            if brok.type == 'log':
                if re.search(regex, brok.data['log']):
                    res.append(brok.data['log'])
        return res

    def print_header(self):
        print "\n" + "#" * 80 + "\n" + "#" + " " * 78 + "#"
        print "#" + string.center(self.id(), 78) + "#"
        print "#" + " " * 78 + "#\n" + "#" * 80 + "\n"

    def xtest_conf_is_correct(self):
        self.print_header()
        self.assertTrue(self.conf.conf_is_correct)
Example #3
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)
            # only load if the module_dir is reallyexisting, so was set explicitly
            # in the test configuration
            if os.path.exists(getattr(self.conf, 'modules_dir', '')):
                arbdaemon.modules_dir = self.conf.modules_dir
                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.modules_manager.set_modules(arb.modules)
                    arbdaemon.do_load_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.modules_dir = modules_dir
        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()
 class Arbiter(object):
     conf = Config()
     pidfile = '/tmp/arbiter.pid'
Example #5
0
 def setUp(self):
     from alignak.objects.config import Config
     self.item = Config()
 class Arbiter(object):
     conf = Config()
    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)
Example #8
0
class TestEndParsingType(unittest.TestCase):
    def check_object_property(self, obj, prop):
        if prop in (
                'realm',  # Realm
                'check_period',  # CheckPeriod
                'check_command',  # CommandCall
                'event_handler',  # CommandCall
                'notification_period',  # Timeperiod
                'service_notification_period',  # Timeperiod
                'host_notification_period',  # Timeperiod
        ):
            # currently not supported / handled or badly parsed / decoded properties values..
            # TODO: consider to also handles them properly ..
            return
        value = getattr(obj, prop, None)
        if value is not None:
            obj_expected_type = self.map_type(obj.properties[prop])
            self.assertIsInstance(
                value, obj_expected_type,
                "The %s attr/property of %s object isn't a %s: %s, value=%r" %
                (prop, obj, obj_expected_type, value.__class__, value))

    def map_type(self, obj):
        # TODO: Replace all basestring with unicode when done in property.default attribute
        # TODO: Fix ToGuessProp as it may be a list.

        if isinstance(obj, ListProp):
            return list

        if isinstance(obj, StringProp):
            return basestring

        if isinstance(obj, UnusedProp):
            return basestring

        if isinstance(obj, BoolProp):
            return bool

        if isinstance(obj, IntegerProp):
            return int

        if isinstance(obj, FloatProp):
            return float

        if isinstance(obj, CharProp):
            return basestring

        if isinstance(obj, DictProp):
            return dict

        if isinstance(obj, AddrProp):
            return basestring

        if isinstance(obj, ToGuessProp):
            return basestring

    def print_header(self):
        print "\n" + "#" * 80 + "\n" + "#" + " " * 78 + "#"
        print "#" + string.center(self.id(), 78) + "#"
        print "#" + " " * 78 + "#\n" + "#" * 80 + "\n"

    def add(self, b):
        if isinstance(b, Brok):
            self.broks[b._id] = b
            return
        if isinstance(b, ExternalCommand):
            self.sched.run_external_command(b.cmd_line)

    def check_objects_from(self, container):
        self.assertIsInstance(container, Items)
        for obj in container:
            for prop in obj.properties:
                self.check_object_property(obj, prop)

    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)
def main():
    """
    Main function
    """
    # Store list of errors found
    errors_found = []

    # Get command line parameters
    args = docopt(__doc__, version='0.1.0')

    # Define here the path of the cfg files
    cfg = args['<cfg_file>']
    print ("Configuration to load: %s" % cfg)

    # Define here the url of the backend
    backend_url = args['--backend']
    print ("Backend URL: %s", backend_url)

    # Delete all objects in backend ?
    destroy_backend_data = args['--delete']
    print ("Delete existing backend data: %s" % destroy_backend_data)

    username = args['--username']
    password = args['--password']
    print ("Backend login with credentials: %s/%s" % (username, password))

    def check_mapping(items, mapping):
        """
        Check if elements are found ...
        """
        response = {
            'all_found': True,
            'data': items
        }
        new_list = []
        for item in items:
            if item in mapping:
                new_list.append(mapping[item])
            else:
                response['all_found'] = False
        if response['all_found']:
            response['data'] = new_list
        return response

    # Get flat files configuration
    alconfig = Config()
    buf = alconfig.read_config(cfg)
    print ("Configuration: %s" % (buf))
    conf = alconfig.read_config_buf(buf)
    print ("Configuration: %s" % (conf))

    print("~~~~~~~~~~~~~~~~~~~~~~ First authentication to delete previous data ~~~~~~~~~~~~~~")
    # Backend authentication with token generation
    headers = {'Content-Type': 'application/json'}
    payload = {'username': username, 'password': password, 'action': 'generate'}
    backend = Backend(backend_url)
    backend.login(username, password)

    if backend.token is None:
        print("Can't authenticated")
        exit()
    print("~~~~~~~~~~~~~~~~~~~~~~ Authenticated ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

    # Destroy data in backend if defined
    if destroy_backend_data:
        headers = {'Content-Type': 'application/json'}
        backend.delete('command', headers)
        backend.delete('timeperiod', headers)
        backend.delete('hostgroup', headers)
        backend.delete('hostdependency', headers)
        backend.delete('servicedependency', headers)
        backend.delete('serviceextinfo', headers)
        backend.delete('trigger', headers)
        # backend.delete('contact', headers)
        backend.delete('contactgroup', headers)
        backend.delete('contactrestrictrole', headers)
        backend.delete('escalation', headers)
        backend.delete('host', headers)
        backend.delete('hostextinfo', headers)
        backend.delete('hostescalation', headers)
        backend.delete('servicegroup', headers)
        backend.delete('service', headers)
        backend.delete('serviceescalation', headers)
        backend.delete('livestate', headers)
        backend.delete('livesynthesis', headers)
        print("~~~~~~~~~~~~~~~~~~~~~~ Data destroyed ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Order of objects + fields to update post add
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #
    # COMMAND
    #    command.use
    # TIMEPERIOD
    #    timeperiod.use
    # HOSTGROUP
    #    hostgroup.hostgroup_members
    # HOSTDEPENDENCY
    #    hostdependency.use
    # SERVICEDEPENDENCY
    #    servicedependency.use
    # SERVICEEXTINFO
    #    serviceextinfo.use
    # TRIGGER
    #    trigger.use
    # CONTACT
    #    contact.use
    # CONTACTGROUP
    #    contact.contactgroups / contactgroup.contactgroup_members
    # CONTACTRESTRICTROLE
    #
    # ESCALATION
    #    escalation.use
    # HOST
    #    hostgroup.members / host.use / host.parents
    # HOSTEXTINFO
    #    hostextinfo.use
    # HOSTESCALATION
    #    hostescalation.use
    # SERVICEGROUP
    #    servicegroup.servicegroup_members
    # SERVICE
    #    service.use
    # SERVICEESCALATION
    #    serviceescalation.use
    #

    def update_types(source, schema):
        """
        Update elements types
        """
        for prop in source:
            source[prop] = ''.join(source[prop])
            if prop in schema:
                # get type
                if schema[prop]['type'] == 'boolean':
                    if source[prop] == '1':
                        source[prop] = True
                    else:
                        source[prop] = False
                elif schema[prop]['type'] == 'list':
                    source[prop] = source[prop].split(',')
                elif schema[prop]['type'] == 'integer':
                    source[prop] = int(source[prop])
        return source

    def update_later(later, inserted, ressource, field, schema):
        """
        Update field of ressource hav link with other ressources (objectid in backend)

        :param later:
        :type later: dict
        :param inserted:
        :type inserted: dict
        :param ressource: ressource name (command, contact, host...)
        :type ressource: str
        :param field: field of ressource to update
        :type field: str
        :return: None
        """
        def get_template(ressource, value):
            """

            :param ressource:
            :param value: value of use
            :return:
            """
            if isinstance(value, basestring):
                value = value.split()
            for template_value in reversed(value):
                template_value = template_value.strip()
                if template_value not in template[ressource]:
                    errors_found.append("# Undeclared template: %s for %s" %
                                        (template_value, ressource))
                    continue
                print ("Template: %s - %s" % (template_value, template[ressource][template_value]))
                if 'use' in template[ressource][template_value]:
                    get_template(ressource, template[ressource][template_value]['use'])
                for key, val in iteritems(template[ressource][template_value]):
                    if key not in ['register', 'name', 'use']:
                        data[key] = val
                    elif key == 'name':
                        if val not in inserted[ressource]:
                            errors_found.append("# Unknown resource %s for %s in %s" %
                                                (val, key, inserted[ressource]))
                        else:
                            data['use'].append(inserted[ressource][val])

        headers = {'Content-Type': 'application/json'}
        for (index, item) in iteritems(later[ressource][field]):
            if field == 'use':
                data = {'use': []}
                get_template(ressource, item['value'])
                # data = update_types(data, schema)
                use_data = []
                for template_id in reversed(data['use']):
                    use_data.append(template_id)
                data['use'] = use_data
            else:
                if item['type'] == 'simple':
                    data = {field: inserted[item['ressource']][item['value']]}
                elif item['type'] == 'list':
                    data = {field: []}
                    for val in item['value']:
                        val = val.strip()
                        if val not in inserted[item['ressource']]:
                            errors_found.append("# Unknown %s: %s for %s" % (item['ressource'],
                                                                             val, ressource))
                        else:
                            data[field].append(inserted[item['ressource']][val])

            headers['If-Match'] = item['_etag']
            resp = backend.patch(''.join([ressource, '/', index]), data, headers, True)
            if '_status' in resp:
                if resp['_status'] == 'ERR':
                    raise ValueError(resp['_issues'])
                elif resp['_status'] == 'OK':
                    for (ind, it) in iteritems(later[ressource]):
                        if index in later[ressource][ind]:
                            later[ressource][ind][index]['_etag'] = resp['_etag']

    def manage_ressource(r_name, inserted, later, data_later, id_name, schema):
        """

        data_later = [{'field': 'use', 'type': 'simple|list', 'ressource': 'command'}]

        :param r_name:
        :param inserted:
        :param later:
        :param data_later:
        :return:
        """
        if r_name not in inserted:
            inserted[r_name] = {}
        if r_name not in template:
            template[r_name] = {}
        if r_name not in later:
            later[r_name] = {}
        for k, values in enumerate(data_later):
            if values['field'] not in later[r_name]:
                later[r_name][values['field']] = {}
        if r_name in conf:
            for item in conf[r_name]:
                later_tmp = {}
                headers = {
                    'Content-Type': 'application/json',
                }
                if 'imported_from' in item:
                    del item['imported_from']
                for p in item:
                    item[p] = item[p][0]
                # Hack for check_command_args
                if r_name in ['host', 'service']:
                    if 'check_command' in item:
                        commands = item['check_command'].split('!', 1)
                        item['check_command'] = commands[0]
                        if len(commands) == 2:
                            item['check_command_args'] = commands[1]

                # convert type (boolean, integer...)
                item = update_types(item, schema['schema'])
                for k, values in enumerate(data_later):
                    # {'field': 'hostgroups', 'type': 'list', 'ressource': 'hostgroup'},
                    if values['field'] in item:
                        if values['type'] == 'simple':
                            if values['now'] \
                                    and values['ressource'] in inserted \
                                    and item[values['field']] in inserted[values['ressource']]:
                                item[values['field']] = inserted[
                                    values['ressource']
                                ][item[values['field']]]
                            else:
                                later_tmp[values['field']] = item[values['field']]
                                del item[values['field']]
                        elif values['type'] == 'list':
                            add = True
                            objectsid = []
                            if values['now']:
                                if isinstance(item[values['field']], basestring):
                                    item[values['field']] = item[values['field']].split()
                                for keylist, vallist in enumerate(item[values['field']]):
                                    vallist = vallist.strip()
                                    if values['ressource'] in inserted:
                                        if vallist in inserted[values['ressource']]:
                                            objectsid.append(inserted[values['ressource']][vallist])
                                    else:
                                        add = False
                            else:
                                add = False
                            if add:
                                item[values['field']] = objectsid
                            else:
                                later_tmp[values['field']] = item[values['field']]
                                del item[values['field']]

                # special case of timeperiod
                if r_name == 'timeperiod':
                    fields = ['imported_from', 'use', 'name', 'definition_order', 'register',
                              'timeperiod_name', 'alias', 'dateranges', 'exclude', 'is_active']
                    item['dateranges'] = []
                    prop_to_del = []
                    for prop in item:
                        if prop not in fields:
                            item['dateranges'].append({prop: item[prop]})
                            prop_to_del.append(prop)
                    for prop in prop_to_del:
                        del item[prop]
                # if template add to template
                if 'register' in item:
                    if not item['register']:
                        if 'name' in item:
                            template[r_name][item['name']] = item.copy()
                        else:
                            print("***** Missing name property in template: %s" % item)
                            if 'service_description' in item:
                                item['name'] = item['service_description']
                                template[r_name][item['name']] = item.copy()
                            elif 'host_name' in item:
                                item['name'] = item['host_name']
                                template[r_name][item['name']] = item.copy()
                print("before_post")
                print("%s : %s:" % (r_name, item))
                try:
                    response = backend.post(r_name, item, headers)
                except BackendException as e:
                    print("***** Exception: %s" % str(e))
                    if "_issues" in e.response:
                        print("ERROR: %s" % e.response['_issues'])
                else:
                    print("POST response : %s:" % (response))
                    if id_name in item:
                        inserted[r_name][item[id_name]] = response['_id']
                    else:
                        inserted[r_name][item['name']] = response['_id']
                    for k, values in enumerate(data_later):
                        if values['field'] in later_tmp:
                            later[r_name][values['field']][response['_id']] = {
                                'type': values['type'],
                                'ressource': values['ressource'],
                                'value': later_tmp[values['field']],
                                '_etag': response['_etag']
                            }

    later = {}
    inserted = {}
    template = {}

    print("~~~~~~~~~~~~~~~~~~~~~~ add commands ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [{'field': 'use', 'type': 'list', 'ressource': 'command', 'now': False}]
    schema = command.get_schema()
    manage_ressource('command', inserted, later, data_later, 'command_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post commands ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'command', 'use', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add timeperiods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [{'field': 'use', 'type': 'list', 'ressource': 'timeperiod', 'now': False}]
    schema = timeperiod.get_schema()
    manage_ressource('timeperiod', inserted, later, data_later, 'timeperiod_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post timeperiods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'timeperiod', 'use', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add hostgroups ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [
        {
            'field': 'members', 'type': 'list', 'ressource': 'host', 'now': False
        },
        {
            'field': 'hostgroup_members', 'type': 'list', 'ressource': 'hostgroup', 'now': False
        }
    ]
    schema = hostgroup.get_schema()
    manage_ressource('hostgroup', inserted, later, data_later, 'hostgroup_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post hostgroups ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'hostgroup', 'hostgroup_members', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add hostdependency ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [{'field': 'use', 'type': 'list', 'ressource': 'hostdependency', 'now': False}]
    schema = hostdependency.get_schema()
    manage_ressource('hostdependency', inserted, later, data_later, 'name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post hostdependency ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'hostdependency', 'use', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add servicedependency ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [{'field': 'use', 'type': 'list', 'ressource': 'servicedependency', 'now': False}]
    schema = servicedependency.get_schema()
    manage_ressource('servicedependency', inserted, later, data_later, 'name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post servicedependency ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'servicedependency', 'use', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add serviceextinfo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [{'field': 'use', 'type': 'list', 'ressource': 'serviceextinfo', 'now': False}]
    schema = serviceextinfo.get_schema()
    manage_ressource('serviceextinfo', inserted, later, data_later, 'name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post serviceextinfo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'serviceextinfo', 'use', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add trigger ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [{'field': 'use', 'type': 'list', 'ressource': 'trigger', 'now': False}]
    schema = trigger.get_schema()
    manage_ressource('trigger', inserted, later, data_later, 'trigger_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post trigger ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'trigger', 'use', schema)

    # print("~~~~~~~~~~~~~~~~~~~~~~ add contact ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    # data_later = [
    # {'field': 'use', 'type': 'simple', 'ressource': 'contact'},
    # {'field': 'contactgroups', 'type': 'list', 'ressource': 'contactgroup'},
    # {'field': 'host_notification_period', 'type': 'simple', 'ressource': 'timeperiod'},
    # {'field': 'service_notification_period', 'type': 'simple', 'ressource': 'timeperiod'},
    # {'field': 'host_notification_commands', 'type': 'list', 'ressource': 'command'},
    # {'field': 'service_notification_commands', 'type': 'list', 'ressource': 'command'}
    # ]
    # schema = contact.get_schema()
    # manage_ressource('contact', inserted, later, data_later, 'contact_name', schema)
    # print("~~~~~~~~~~~~~~~~~~~~~~ post contact ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    # update_later(later, inserted, 'contact', 'use', schema)
    # update_later(later, inserted, 'contact', 'host_notification_period', schema)
    # update_later(later, inserted, 'contact', 'service_notification_period', schema)
    # update_later(later, inserted, 'contact', 'host_notification_commands', schema)
    # update_later(later, inserted, 'contact', 'service_notification_commands', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add contactgroup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [
        {'field': 'members', 'type': 'list', 'ressource': 'contact', 'now': False},
        {'field': 'contactgroup_members', 'type': 'list', 'ressource': 'contactgroup', 'now': False}
    ]
    schema = contactgroup.get_schema()
    manage_ressource('contactgroup', inserted, later, data_later, 'contactgroup_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post contactgroup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    # update_later(later, inserted, 'contactgroup', 'members', schema)
    update_later(later, inserted, 'contactgroup', 'contactgroup_members', schema)
    # update_later(later, inserted, 'contact', 'contactgroups', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add contactrestrictrole ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [
        {'field': 'contact', 'type': 'simple', 'ressource': 'contact', 'now': False}
    ]
    schema = contactrestrictrole.get_schema()
    manage_ressource('contactrestrictrole', inserted, later, data_later, 'contact', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post contactrestrictrole ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    # update_later(later, inserted, 'contactrestrictrole', 'contact', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add escalation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [
        {'field': 'use', 'type': 'list', 'ressource': 'escalation', 'now': False},
        {'field': 'contacts', 'type': 'list', 'ressource': 'contact', 'now': False},
        {'field': 'contact_groups', 'type': 'list', 'ressource': 'contactgroup', 'now': True}
    ]
    schema = escalation.get_schema()
    manage_ressource('escalation', inserted, later, data_later, 'escalation_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post escalation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'escalation', 'use', schema)
    # update_later(later, inserted, 'escalation', 'contacts', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add host ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [
        {'field': 'use', 'type': 'list', 'ressource': 'host', 'now': False},
        {'field': 'parents', 'type': 'list', 'ressource': 'host', 'now': False},
        {'field': 'hostgroups', 'type': 'list', 'ressource': 'hostgroup', 'now': True},
        {'field': 'check_command', 'type': 'simple', 'ressource': 'command', 'now': True},
        {'field': 'check_period', 'type': 'simple', 'ressource': 'timeperiod', 'now': True},
        {'field': 'contacts', 'type': 'list', 'ressource': 'contact', 'now': False},
        {'field': 'contact_groups', 'type': 'list', 'ressource': 'contactgroup', 'now': True},
        {'field': 'notification_period', 'type': 'simple', 'ressource': 'timeperiod', 'now': True},
        {'field': 'escalations', 'type': 'list', 'ressource': 'escalation', 'now': True}
    ]
    schema = host.get_schema()
    manage_ressource('host', inserted, later, data_later, 'host_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post host ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'host', 'use', schema)
    update_later(later, inserted, 'host', 'parents', schema)
    # update_later(later, inserted, 'host', 'contacts', schema)
    update_later(later, inserted, 'hostgroup', 'members', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add hostextinfo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [{'field': 'use', 'type': 'list', 'ressource': 'hostextinfo', 'now': False}]
    schema = hostextinfo.get_schema()
    manage_ressource('hostextinfo', inserted, later, data_later, 'host_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post hostextinfo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'hostextinfo', 'use', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add hostescalation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [{'field': 'use', 'type': 'list', 'ressource': 'hostescalation', 'now': False},
                  {'field': 'contacts', 'type': 'list', 'ressource': 'contact', 'now': False},
                  {'field': 'contact_groups', 'type': 'list', 'ressource': 'contactgroup',
                   'now': True}]
    schema = hostescalation.get_schema()
    manage_ressource('hostescalation', inserted, later, data_later, 'host_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post hostescalation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'hostescalation', 'use', schema)
    # update_later(later, inserted, 'hostescalation', 'contacts', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add servicegroups ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [
        {'field': 'members', 'type': 'list', 'ressource': 'service', 'now': False},
        {'field': 'servicegroup_members', 'type': 'list', 'ressource': 'servicegroup', 'now': False}
    ]
    schema = servicegroup.get_schema()
    manage_ressource('servicegroup', inserted, later, data_later, 'servicegroup_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post servicegroups ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'servicegroup', 'servicegroup_members', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add service ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [
        {'field': 'use', 'type': 'list', 'ressource': 'service', 'now': False},
        {'field': 'host_name', 'type': 'simple', 'ressource': 'host', 'now': True},
        {'field': 'servicegroups', 'type': 'list', 'ressource': 'servicegroup', 'now': True},
        {'field': 'check_command', 'type': 'simple', 'ressource': 'command', 'now': True},
        {'field': 'check_period', 'type': 'simple', 'ressource': 'timeperiod', 'now': True},
        {'field': 'notification_period', 'type': 'simple', 'ressource': 'timeperiod', 'now': True},
        {'field': 'contacts', 'type': 'list', 'ressource': 'contact', 'now': False},
        {'field': 'contact_groups', 'type': 'list', 'ressource': 'contactgroup', 'now': True},
        {'field': 'escalations', 'type': 'list', 'ressource': 'escalation', 'now': True},
        {'field': 'maintenance_period', 'type': 'simple', 'ressource': 'timeperiod', 'now': True},
        {'field': 'service_dependencies', 'type': 'list', 'ressource': 'service', 'now': True}
    ]
    schema = service.get_schema()
    manage_ressource('service', inserted, later, data_later, 'service_description', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post service ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'service', 'use', schema)
    # update_later(later, inserted, 'service', 'contacts', schema)
    update_later(later, inserted, 'servicegroup', 'members', schema)

    print("~~~~~~~~~~~~~~~~~~~~~~ add serviceescalation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    data_later = [
        {'field': 'use', 'type': 'list', 'ressource': 'serviceescalation', 'now': False},
        {'field': 'contacts', 'type': 'list', 'ressource': 'contact', 'now': False},
        {'field': 'contact_groups', 'type': 'list', 'ressource': 'contactgroup', 'now': True}
    ]
    schema = serviceescalation.get_schema()
    manage_ressource('serviceescalation', inserted, later, data_later, 'host_name', schema)
    print("~~~~~~~~~~~~~~~~~~~~~~ post serviceescalation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    update_later(later, inserted, 'serviceescalation', 'use', schema)
    # update_later(later, inserted, 'serviceescalation', 'contacts', schema)

    # print all errors found
    print('################################## errors report ##################################')
    for error in errors_found:
        print(error)
    print('###################################################################################')
    response = {
        'all_found': True,
        'data': items
    }
    new_list = []
    for item in items:
        if item in mapping:
            new_list.append(mapping[item])
        else:
            response['all_found'] = False
    if response['all_found']:
        response['data'] = new_list
    return response


alconfig = Config()

file_list = ('cfg/hosts.cfg', 'cfg/commands.cfg', 'cfg/contacts.cfg', 'cfg/hostgroups.cfg',
             'cfg/services.cfg', 'cfg/servicetemplates.cfg', 'cfg/timeperiods.cfg')

buf = alconfig.read_config(file_list)
conf = alconfig.read_config_buf(buf)

# Destroy data in backend if defined
if destroy_backend_data:
    method_delete(''.join([backend_url, 'command']))
    method_delete(''.join([backend_url, 'host']))
    method_delete(''.join([backend_url, 'hostgroup']))
    method_delete(''.join([backend_url, 'service']))
    method_delete(''.join([backend_url, 'contact']))
    method_delete(''.join([backend_url, 'timeperiod']))
Example #11
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)
Example #12
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()
Example #13
0
class AlignakTest(unittest.TestCase):

    time_hacker = TimeHacker()

    if sys.version_info < (2, 7):
        def assertRegex(self, *args, **kwargs):
            return self.assertRegexpMatches(*args, **kwargs)

    def setUp(self):
        self.setup_with_file(['etc/alignak_1r_1h_1s.cfg'], add_default=False)

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

    def add(self, b):
        if isinstance(b, Brok):
            self.broks[b._id] = b
            return
        if isinstance(b, ExternalCommand):
            self.sched.run_external_command(b.cmd_line)

    def fake_check(self, ref, exit_status, output="OK"):
        #print "fake", ref
        now = time.time()
        ref.schedule(force=True)
        # now checks are schedule and we get them in
        # the action queue
        #check = ref.actions.pop()
        check = ref.checks_in_progress[0]
        self.sched.add(check)  # check is now in sched.checks[]

        # Allows to force check scheduling without setting its status nor
        # output. Useful for manual business rules rescheduling, for instance.
        if exit_status is None:
            return

        # fake execution
        check.check_time = now

        # and lie about when we will launch it because
        # if not, the schedule call for ref
        # will not really reschedule it because there
        # is a valid value in the future
        ref.next_chk = now - 0.5

        check.get_outputs(output, 9000)
        check.exit_status = exit_status
        check.execution_time = 0.001
        check.status = 'waitconsume'
        self.sched.waiting_results.append(check)


    def scheduler_loop(self, count, reflist, do_sleep=False, sleep_time=61, verbose=True):
        for ref in reflist:
            (obj, exit_status, output) = ref
            obj.checks_in_progress = []
        for loop in range(1, count + 1):
            if verbose is True:
                print "processing check", loop
            for ref in reflist:
                (obj, exit_status, output) = ref
                obj.update_in_checking()
                self.fake_check(obj, exit_status, output)
            self.sched.manage_internal_checks()

            self.sched.consume_results()
            self.sched.get_new_actions()
            self.sched.get_new_broks()
            self.sched.scatter_master_notifications()
            self.worker_loop(verbose)
            for ref in reflist:
                (obj, exit_status, output) = ref
                obj.checks_in_progress = []
            self.sched.update_downtimes_and_comments()
            #time.sleep(ref.retry_interval * 60 + 1)
            if do_sleep:
                time.sleep(sleep_time)


    def worker_loop(self, verbose=True):
        self.sched.delete_zombie_checks()
        self.sched.delete_zombie_actions()
        checks = self.sched.get_to_run_checks(True, False, worker_name='tester')
        actions = self.sched.get_to_run_checks(False, True, worker_name='tester')
        #print "------------ worker loop checks ----------------"
        #print checks
        #print "------------ worker loop actions ----------------"
        if verbose is True:
            self.show_actions()
        #print "------------ worker loop new ----------------"
        for a in actions:
            a.status = 'inpoller'
            a.check_time = time.time()
            a.exit_status = 0
            self.sched.put_results(a)
        if verbose is True:
            self.show_actions()
        #print "------------ worker loop end ----------------"


    def show_logs(self):
        print "--- logs <<<----------------------------------"
        if hasattr(self, "sched"):
            broks = self.sched.broks
        else:
            broks = self.broks
        for brok in sorted(broks.values(), lambda x, y: x._id - y._id):
            if brok.type == 'log':
                brok.prepare()
                safe_print("LOG: ", brok.data['log'])

        print "--- logs >>>----------------------------------"


    def show_actions(self):
        print "--- actions <<<----------------------------------"
        if hasattr(self, "sched"):
            actions = self.sched.actions
        else:
            actions = self.actions
        for a in sorted(actions.values(), lambda x, y: x._id - y._id):
            if a.is_a == 'notification':
                if a.ref.my_type == "host":
                    ref = "host: %s" % a.ref.get_name()
                else:
                    ref = "host: %s svc: %s" % (a.ref.host.get_name(), a.ref.get_name())
                print "NOTIFICATION %d %s %s %s %s" % (a._id, ref, a.type, time.asctime(time.localtime(a.t_to_go)), a.status)
            elif a.is_a == 'eventhandler':
                print "EVENTHANDLER:", a
        print "--- actions >>>----------------------------------"


    def show_and_clear_logs(self):
        self.show_logs()
        self.clear_logs()


    def show_and_clear_actions(self):
        self.show_actions()
        self.clear_actions()


    def count_logs(self):
        if hasattr(self, "sched"):
            broks = self.sched.broks
        else:
            broks = self.broks
        return len([b for b in broks.values() if b.type == 'log'])


    def count_actions(self):
        if hasattr(self, "sched"):
            actions = self.sched.actions
        else:
            actions = self.actions
        return len(actions.values())


    def clear_logs(self):
        if hasattr(self, "sched"):
            broks = self.sched.broks
        else:
            broks = self.broks
        id_to_del = []
        for b in broks.values():
            if b.type == 'log':
                id_to_del.append(b._id)
        for id in id_to_del:
            del broks[id]


    def clear_actions(self):
        if hasattr(self, "sched"):
            self.sched.actions = {}
        else:
            self.actions = {}


    def assert_log_match(self, index, pattern, no_match=False):
        # log messages are counted 1...n, so index=1 for the first message
        if not no_match:
            self.assertGreaterEqual(self.count_logs(), index)
        regex = re.compile(pattern)
        lognum = 1
        broks = sorted(self.sched.broks.values(), key=lambda x: x._id)
        for brok in broks:
            if brok.type == 'log':
                brok.prepare()
                if index == lognum:
                    if re.search(regex, brok.data['log']):
                        return
                lognum += 1
        self.assertTrue(no_match, "%s found a matched log line in broks :\n"
                               "index=%s pattern=%r\n"
                               "broks_logs=[[[\n%s\n]]]" % (
            '*HAVE*' if no_match else 'Not',
            index, pattern, '\n'.join(
                '\t%s=%s' % (idx, b.strip())
                for idx, b in enumerate(
                    (b.data['log'] for b in broks if b.type == 'log'),
                    1)
            )
        ))

    def _any_log_match(self, pattern, assert_not):
        regex = re.compile(pattern)
        broks = getattr(self, 'sched', self).broks
        broks = sorted(broks.values(), lambda x, y: x._id - y._id)
        for brok in broks:
            if brok.type == 'log':
                brok.prepare()
                if re.search(regex, brok.data['log']):
                    self.assertTrue(not assert_not,
                                    "Found matching log line:\n"
                                    "pattern = %r\nbrok log = %r" % (pattern, brok.data['log'])
                    )
                    return
        logs = [brok.data['log'] for brok in broks if brok.type == 'log']
        self.assertTrue(assert_not,
            "No matching log line found:\n"
            "pattern = %r\n" "logs broks = %r" % (pattern, logs)
        )

    def assert_any_log_match(self, pattern):
        self._any_log_match(pattern, assert_not=False)

    def assert_no_log_match(self, pattern):
        self._any_log_match(pattern, assert_not=True)


    def get_log_match(self, pattern):
        regex = re.compile(pattern)
        res = []
        for brok in sorted(self.sched.broks.values(), lambda x, y: x._id - y._id):
            if brok.type == 'log':
                if re.search(regex, brok.data['log']):
                    res.append(brok.data['log'])
        return res

    def print_header(self):
        print "\n" + "#" * 80 + "\n" + "#" + " " * 78 + "#"
        print "#" + string.center(self.id(), 78) + "#"
        print "#" + " " * 78 + "#\n" + "#" * 80 + "\n"

    def xtest_conf_is_correct(self):
        self.print_header()
        self.assertTrue(self.conf.conf_is_correct)
Example #14
0
 def setUp(self):
     super(TestConfig, self).setUp()
     from alignak.objects.config import Config
     self.item = Config()
Example #15
0
class TestEndParsingType(unittest.TestCase):

    def check_object_property(self, obj, prop):
        if prop in (
                'realm',  # Realm
                'check_period',    # CheckPeriod
                'check_command',  # CommandCall
                'event_handler',  # CommandCall
                'notification_period',  # Timeperiod
                'service_notification_period',  # Timeperiod
                'host_notification_period',  # Timeperiod
        ):
            # currently not supported / handled or badly parsed / decoded properties values..
            # TODO: consider to also handles them properly ..
            return
        value = getattr(obj, prop, None)
        if value is not None:
            obj_expected_type = self.map_type(obj.properties[prop])
            self.assertIsInstance(value, obj_expected_type,
                                  "The %s attr/property of %s object isn't a %s: %s, value=%r" %
                                  (prop, obj, obj_expected_type, value.__class__, value))

    def map_type(self, obj):
        # TODO: Replace all basestring with unicode when done in property.default attribute
        # TODO: Fix ToGuessProp as it may be a list.

        if isinstance(obj, ListProp):
            return list

        if isinstance(obj, StringProp):
            return basestring

        if isinstance(obj, UnusedProp):
            return basestring

        if isinstance(obj, BoolProp):
            return bool

        if isinstance(obj, IntegerProp):
            return int

        if isinstance(obj, FloatProp):
            return float

        if isinstance(obj, CharProp):
            return basestring

        if isinstance(obj, DictProp):
            return dict

        if isinstance(obj, AddrProp):
            return basestring

        if isinstance(obj, ToGuessProp):
            return basestring

    def print_header(self):
        print "\n" + "#" * 80 + "\n" + "#" + " " * 78 + "#"
        print "#" + string.center(self.id(), 78) + "#"
        print "#" + " " * 78 + "#\n" + "#" * 80 + "\n"

    def add(self, b):
        if isinstance(b, Brok):
            self.broks[b._id] = b
            return
        if isinstance(b, ExternalCommand):
            self.sched.run_external_command(b.cmd_line)

    def check_objects_from(self, container):
        self.assertIsInstance(container, Items)
        for obj in container:
            for prop in obj.properties:
                self.check_object_property(obj, prop)

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