def _get_files_to_check(pylint_test_dir): p = subprocess.Popen( ["%s/scripts/find-python-files" % repo_path(), str(sys.version_info[0])], stdout=subprocess.PIPE, shell=False, close_fds=True, ) stdout = p.communicate()[0] files = [] for fname in stdout.splitlines(): # Thin out these excludes some day... rel_path = fname[len(repo_path()) + 1:] # Can currently not be checked alone. Are compiled together below if rel_path.startswith("checks/") or \ rel_path.startswith("inventory/") or \ rel_path.startswith("agents/bakery/") or \ rel_path.startswith("enterprise/agents/bakery/"): continue # TODO: We should also test them... if rel_path == "werk" \ or rel_path.startswith("tests/") \ or rel_path.startswith("scripts/") \ or rel_path.startswith("agents/wnx/integration/"): continue # TODO: disable random, not that important stuff if rel_path.startswith("agents/windows/it/") \ or rel_path.startswith("agents/windows/msibuild/") \ or rel_path.startswith("doc/") \ or rel_path.startswith("livestatus/api/python/example") \ or rel_path.startswith("livestatus/api/python/make_"): continue files.append(fname) # Add the compiled files for things that are no modules yet open(pylint_test_dir + "/__init__.py", "w") _compile_check_and_inventory_plugins(pylint_test_dir) if is_enterprise_repo(): _compile_bakery_plugins(pylint_test_dir) # Not checking compiled check, inventory, bakery plugins with Python 3 if sys.version_info[0] == 3: files += [ pylint_test_dir, ] return files
def _compile_bakery_plugins(pylint_test_dir): with open(pylint_test_dir + "/cmk_bakery_plugins.py", "w") as f: # This pylint warning is incompatible with our "concatenation technology". f.write("# pylint: disable=reimported,wrong-import-order,wrong-import-position\n") add_file( f, os.path.realpath( os.path.join(repo_path(), "enterprise/cmk/base/cee/agent_bakery_plugins.py"))) # Also add bakery plugins for path in check_files(os.path.join(repo_path(), "enterprise/agents/bakery")): add_file(f, path)
def create_linux_test_host(request, web, site, hostname): def finalizer(): web.delete_host(hostname) web.activate_changes() for path in [ "var/check_mk/agent_output/%s" % hostname, "etc/check_mk/conf.d/linux_test_host_%s.mk" % hostname, "tmp/check_mk/status_data/%s" % hostname, "tmp/check_mk/status_data/%s.gz" % hostname, "var/check_mk/inventory/%s" % hostname, "var/check_mk/inventory/%s.gz" % hostname, ]: if os.path.exists(path): site.delete_file(path) request.addfinalizer(finalizer) web.add_host(hostname, attributes={"ipaddress": "127.0.0.1"}) site.write_file( "etc/check_mk/conf.d/linux_test_host_%s.mk" % hostname, "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ['%s']))\n" % hostname) site.makedirs("var/check_mk/agent_output/") site.write_file( "var/check_mk/agent_output/%s" % hostname, open("%s/tests/integration/cmk/base/test-files/linux-agent-output" % repo_path()).read())
def run_pylint(base_path, check_files): args = os.environ.get("PYLINT_ARGS", "") if args: pylint_args = args.split(" ") else: pylint_args = [] pylint_cfg = repo_path() + "/.pylintrc" cmd = [ "python", "-m", "pylint", "--rcfile", pylint_cfg, "--jobs=%d" % num_jobs_to_use(), ] + pylint_args + check_files print("Running pylint in '%s' with: %s" % (base_path, subprocess.list2cmdline(cmd))) p = subprocess.Popen(cmd, shell=False, cwd=base_path) exit_code = p.wait() print("Finished with exit code: %d" % exit_code) return exit_code
def _compile_check_and_inventory_plugins(pylint_test_dir): with open(pylint_test_dir + "/cmk_checks.py", "w") as f: # Fake data structures where checks register (See cmk/base/checks.py) f.write(""" # -*- encoding: utf-8 -*- check_info = {} check_includes = {} precompile_params = {} check_default_levels = {} factory_settings = {} check_config_variables = [] snmp_info = {} snmp_scan_functions = {} active_check_info = {} special_agent_info = {} inv_info = {} # Inventory plugins inv_export = {} # Inventory export hooks def inv_tree_list(path): return inv_tree(path, []) def inv_tree(path, default_value=None): if default_value is not None: node = default_value else: node = {} return node """) # add the modules # These pylint warnings are incompatible with our "concatenation technology". f.write( "# pylint: disable=reimported,ungrouped-imports,wrong-import-order,wrong-import-position,redefined-outer-name\n" ) add_file(f, repo_path() + "/cmk/base/check_api.py") add_file(f, repo_path() + "/cmk/base/inventory_plugins.py") # Now add the checks for path in check_files(repo_path() + "/checks"): add_file(f, path) # Now add the inventory plugins for path in check_files(repo_path() + "/inventory"): add_file(f, path)
def pytest_collection_modifyitems(items): """Mark collected test types based on their location""" for item in items: type_marker = item.get_closest_marker("type") if type_marker and type_marker.args: continue # Do not modify manually set marks file_path = Path("%s" % item.reportinfo()[0]) repo_rel_path = file_path.relative_to(repo_path()) ty = repo_rel_path.parts[1] if ty not in test_types: raise Exception("Test in %s not TYPE marked: %r (%r)" % (repo_rel_path, item, ty)) item.add_marker(pytest.mark.type.with_args(ty))
def test_pylint(pylint_test_dir): exit_code = run_pylint(repo_path(), _get_files_to_check(pylint_test_dir)) assert exit_code == 0, "PyLint found an error"