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