def _compile_bakery_plugins(pylint_test_dir): with open(pylint_test_dir + "/cmk_bakery_plugins.py", "w") as f: pylint_cmk.add_file( f, os.path.realpath(os.path.join(cmc_path(), "cmk_base/cee/agent_bakery_plugins.py"))) # This pylint warning is incompatible with our "concatenation technology". f.write("# pylint: disable=reimported,wrong-import-order,wrong-import-position\n") # Also add bakery plugins for path in pylint_cmk.check_files(os.path.join(cmc_path(), "agents/bakery")): pylint_cmk.add_file(f, path)
def test_find_debug_code(): scanned = 0 for base_path in [ cmk_path(), cmc_path() ]: for dir_path in check_paths: path = "%s/%s" % (base_path, dir_path) if not os.path.exists(path): continue for dirpath, dirnames, filenames in os.walk(path): scanned += 1 for filename in filenames: file_path = "%s/%s" % (dirpath, filename) for nr, line in enumerate(open(file_path)): if nr == 0 and ("bash" in line or "php" in line): break # skip non python files l = line.lstrip() assert not l.startswith("print("), \ "Found \"print(...)\" call in %s:%d" % \ (file_path, nr+1) assert not l.startswith("print "), \ "Found \"print ...\" call in %s:%d" % \ (file_path, nr+1) assert scanned > 0
def test_pylint_checks(): base_path = pylint_cmk.get_test_dir() f = file(base_path + "/cmk-checks.py", "w") # add the modules for path in pylint_cmk.ordered_module_files(): pylint_cmk.add_file(f, path) # Now add the checks for path in pylint_cmk.check_files(repo_path() + "/checks"): pylint_cmk.add_file(f, path) # Also add inventory plugins for path in pylint_cmk.check_files(repo_path() + "/inventory"): pylint_cmk.add_file(f, path) # Also add bakery plugins for path in pylint_cmk.check_files(cmc_path() + "/agents/bakery"): pylint_cmk.add_file(f, path) f.close() exit_code = pylint_cmk.run_pylint(base_path, cleanup_test_dir=True) assert exit_code == 0, "PyLint found an error in checks, inventory " \ "or agent bakery plugins"
def test_precompiled_file_ages(): newest_source_file, newest_source_time = find_newest_source_file() for filename in precompiled_files: path = "%s/agents/windows/%s" % (cmc_path(), filename) commit_time = last_commit_time(path) assert commit_time >= newest_source_time, \ "%s is older than source code file %s" % (path, newest_source_file)
def ordered_module_files(): ordered_modules = [ cmk_path() + "/modules/check_mk_base.py", cmk_path() + "/modules/check_mk.py", cmk_path() + "/modules/config.py", cmk_path() + "/modules/discovery.py", cmk_path() + "/modules/snmp.py", cmk_path() + "/modules/notify.py", cmk_path() + "/modules/events.py", cmk_path() + "/modules/nagios.py", cmk_path() + "/modules/automation.py", cmk_path() + "/modules/inventory.py", cmc_path() + "/modules/real_time_checks.py", cmc_path() + "/modules/alert_handling.py", cmc_path() + "/modules/keepalive.py", cmc_path() + "/modules/cmc.py", cmc_path() + "/modules/inline_snmp.py", cmc_path() + "/modules/agent_bakery.py", cmc_path() + "/modules/rrd.py", cme_path() + "/modules/managed.py", ] modules = ordered_modules # Add modules which are not specified above for path in module_files(): if path not in modules: modules.append(path) return modules
def module_files(): modules = [] for base_path in [cmk_path() + "/modules", cmc_path() + "/modules"]: modules += [ base_path + "/" + f for f in os.listdir(base_path) if not f.startswith(".") ] return sorted(modules)
def get_plugin_files(plugin_dir): files = [] for path in [ cmk_path() + "/web/plugins/" + plugin_dir, cmc_path() + "/web/plugins/" + plugin_dir ]: if os.path.exists(path): files += [ (f, path) for f in os.listdir(path) ] return sorted(files)
def get_plugin_files(plugin_dir): files = [] for path in [ cmk_path() + "/web/plugins/" + plugin_dir, cmc_path() + "/web/plugins/" + plugin_dir ]: if os.path.exists(path): files += [(f, path) for f in os.listdir(path)] return sorted(files)
def get_web_plugin_dirs(): plugin_dirs = sorted(list(set(os.listdir(cmk_path() + "/web/plugins") + os.listdir(cmc_path() + "/web/plugins")))) # icons are included from a plugin of views module. Move to the end to # make them be imported after the views plugins. Same for perfometers. plugin_dirs.remove("icons") plugin_dirs.append("icons") plugin_dirs.remove("perfometer") plugin_dirs.append("perfometer") return plugin_dirs
def test_pylint_misc(): search_paths = [ cmk_path() + "/cmk_base", cmc_path() + "/cmk_base", cmk_path() + "/lib", cmk_path() + "/bin", cmk_path() + "/notifications", cmk_path() + "/agents/plugins", cmk_path() + "/active_checks", cmc_path() + "/agents/plugins", cmc_path() + "/bin", cmc_path() + "/misc", ] checked, worst = 0, 0 for path in search_paths: worst = max(worst, pylint_cmk.run_pylint(path)) checked += 1 assert checked > 0, "Did not find a file to check!" assert worst == 0, "At least one issue found"
def get_web_plugin_dirs(): plugin_dirs = sorted( list( set( os.listdir(cmk_path() + "/web/plugins") + os.listdir(cmc_path() + "/web/plugins")))) # icons are included from a plugin of views module. Move to the end to # make them be imported after the views plugins. Same for perfometers. plugin_dirs.remove("icons") plugin_dirs.append("icons") plugin_dirs.remove("perfometer") plugin_dirs.append("perfometer") return plugin_dirs
def test_find_debug_code(): scanned = 0 for base_path in [cmk_path(), cmc_path()]: for dirpath, dirnames, filenames in os.walk("%s/web" % base_path): scanned += 1 for filename in filenames: path = "%s/%s" % (dirpath, filename) for line in open(path): l = line.lstrip() assert not l.startswith("html.debug("), \ "Found \"html.debug(...)\" call" assert scanned > 0
def test_find_debug_code(): scanned = 0 for base_path in [ cmk_path(), cmc_path() ]: for dirpath, dirnames, filenames in os.walk("%s/web" % base_path): scanned += 1 for filename in filenames: path = "%s/%s" % (dirpath, filename) for line in open(path): l = line.lstrip() assert not l.startswith("html.debug("), \ "Found \"html.debug(...)\" call" assert scanned > 0
def test_pylint_web(): base_path = pylint_cmk.get_test_dir() # Make compiled files import eachother by default sys.path.insert(0, base_path) modules = glob.glob(cmk_path() + "/web/htdocs/*.py") \ + glob.glob(cmc_path() + "/web/htdocs/*.py") \ + glob.glob(cme_path() + "/web/htdocs/*.py") for module in modules: print("Copy %s to test directory" % module) f = open(base_path + "/" + os.path.basename(module), "w") pylint_cmk.add_file(f, module) f.close() # Move the whole plugins code to their modules, then # run pylint only on the modules for plugin_dir in get_web_plugin_dirs(): files = get_plugin_files(plugin_dir) for plugin_file, plugin_base in files: plugin_path = plugin_base + "/" + plugin_file if plugin_file.startswith('.'): continue elif plugin_dir in ["icons", "perfometer"]: module_name = "views" elif plugin_dir == "pages": module_name = "modules" else: module_name = plugin_dir print("[%s] add %s" % (module_name, plugin_path)) module = file(base_path + "/" + module_name + ".py", "a") pylint_cmk.add_file(module, plugin_path) module.close() exit_code = pylint_cmk.run_pylint(base_path, cleanup_test_dir=True) assert exit_code == 0, "PyLint found an error in the web code"
def test_pylint_web(): base_path = pylint_cmk.get_test_dir() # Make compiled files import eachother by default sys.path.insert(0, base_path) modules = glob.glob(cmk_path() + "/web/htdocs/*.py") \ + glob.glob(cmc_path() + "/web/htdocs/*.py") for module in modules: print("Copy %s to test directory" % module) f = open(base_path + "/" + os.path.basename(module), "w") pylint_cmk.add_file(f, module) f.close() # Move the whole plugins code to their modules, then # run pylint only on the modules for plugin_dir in get_web_plugin_dirs(): files = get_plugin_files(plugin_dir) for plugin_file, plugin_base in files: plugin_path = plugin_base +"/"+plugin_file if plugin_file.startswith('.'): continue elif plugin_dir in ["icons","perfometer"]: module_name = "views" elif plugin_dir == "pages": module_name = "modules" else: module_name = plugin_dir print("[%s] add %s" % (module_name, plugin_path)) module = file(base_path + "/" + module_name + ".py", "a") pylint_cmk.add_file(module, plugin_path) module.close() exit_code = pylint_cmk.run_pylint(base_path, cleanup_test_dir=True) assert exit_code == 0, "PyLint found an error in the web code"
"line", [' print "hello Word"', 'print("variable")', ' pprint(dict)', ' pprint.pprint(list)']) def test_find_debugs(line): assert find_debugs(line) @pytest.mark.parametrize("line", ['sys.stdout.write("message")', '# print(variable)']) def test_find_debugs_false(line): assert find_debugs(line) is None @pytest.mark.parametrize( 'path', [ p # for base_path in [cmk_path(), cmc_path(), cme_path()] # for dir_path in check_paths # for p in ["%s/%s" % (base_path, dir_path)] if os.path.exists(p) ], ) def test_find_debug_code(path): scanned = 0 for dirpath, _, filenames in os.walk(path): scanned += 1 for filename in filenames: file_path = "%s/%s" % (dirpath, filename) if [folder for folder in exclude_folders if folder in file_path]: continue
def find_newest_source_file(): path = "%s/agents/plugins/cmk-update-agent" % cmc_path() return path, last_commit_time(path)
def test_precompiled_files_present(): for filename in precompiled_files: path = "%s/agents/windows/%s" % (cmc_path(), filename) assert os.path.exists(path)