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 get_plugin_files(plugin_dir): files = [] for path in [ cmk_path() + "/web/plugins/" + plugin_dir, cmc_path() + "/web/plugins/" + plugin_dir, cme_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") + os.listdir(cme_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_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_find_debug_code(): scanned = 0 for base_path in [cmk_path(), cmc_path(), cme_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) if [ folder for folder in exclude_folders if folder in file_path ]: continue 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("pprint.pprint("), \ "Found \"print(...)\" call in %s:%d" % \ (file_path, nr+1) assert not l.startswith("pprint("), \ "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
"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
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', list( filter(os.path.exists, [ "%s/%s" % (base_path, dir_path) for base_path in [cmk_path(), cmc_path(), cme_path()] for dir_path in check_paths ]))) 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 if file_path.endswith((".pyc", ".whl", ".tar.gz", ".swp")): continue if os.path.relpath(file_path, cmk_path()) in exclude_files:
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 if file_path.endswith((".pyc", ".whl", ".tar.gz", ".swp")):