Exemple #1
0
def _scss_files() -> Set[Path]:
    return set(
        chain(
            Path(cmk_path(), "web", "htdocs", "themes").glob("**/*.scss"),
            Path(cmk_path(), "enterprise", "web", "htdocs", "themes").glob("**/*.scss"),
        )
    )
Exemple #2
0
def test_all_agents_tested():
    agents = {
        os.path.basename(os.path.splitext(agent_file)[0])
        for agent_file in glob("%s/cmk/special_agents/agent_*.py" % cmk_path())
    }
    untested = agents - set(REQUIRED_ARGUMENTS) - NOT_TESTED_YET
    assert not untested, "Please add test cases for special agents %s" % untested
Exemple #3
0
def _git_tag_exists(tag):
    return (subprocess.Popen(
        ["git", "rev-list", tag],
        stdout=open(os.devnull, "w"),
        stderr=subprocess.STDOUT,
        cwd=testlib.cmk_path(),
    ).wait() == 0)
Exemple #4
0
def test_write_precompiled_werks(tmp_path, monkeypatch):
    tmp_dir = str(tmp_path)

    all_werks = cmk.utils.werks.load_raw_files(Path(testlib.cmk_path()) / ".werks")
    cre_werks = {w["id"]: w for w in all_werks.values() if w["edition"] == "cre"}
    cee_werks = {w["id"]: w for w in all_werks.values() if w["edition"] == "cee"}
    cme_werks = {w["id"]: w for w in all_werks.values() if w["edition"] == "cme"}

    assert len(cre_werks) > 1000
    assert [w for w in cre_werks.keys() if 9000 <= w < 10000] == []
    cmk.utils.werks.write_precompiled_werks(Path(tmp_dir) / "werks", cre_werks)

    assert len(cee_werks) > 700
    cmk.utils.werks.write_precompiled_werks(Path(tmp_dir) / "werks-enterprise", cee_werks)

    assert len(cme_werks) > 5
    cmk.utils.werks.write_precompiled_werks(Path(tmp_dir) / "werks-managed", cme_werks)

    monkeypatch.setattr(cmk.utils.werks, "_compiled_werks_dir", lambda: Path(tmp_dir))
    werks_loaded = cmk.utils.werks.load()

    merged_werks = cre_werks
    merged_werks.update(cee_werks)
    merged_werks.update(cme_werks)

    assert merged_werks == werks_loaded
Exemple #5
0
def add_file(f, path):
    relpath = os.path.relpath(os.path.realpath(path), cmk_path())
    f.write("# -*- encoding: utf-8 -*-")
    f.write("#\n")
    f.write("# ORIG-FILE: " + relpath + "\n")
    f.write("#\n")
    f.write("\n")
    f.write(Path(path).read_text())
Exemple #6
0
def test_permissions() -> None:
    for pattern, check_func, excludes in _PERMISSIONS:
        git_dir = Path(cmk_path())
        for f in git_dir.glob(pattern):
            if not f.is_file():
                continue
            if f.name in excludes or f.name in _GLOBAL_EXCLUDES:
                continue
            assert check_func(f), "%s has wrong permissions (%r)" % (f, check_func)
def python_container(request, docker_client):
    c = docker_client.containers.run(
        image="shimizukawa/python-all",
        command=["python2.5", "-c", "import time; time.sleep(9999)"],
        detach=True,
        volumes={
            testlib.cmk_path(): {"bind": "/cmk", "mode": "ro"},
        },
    )
    yield c
    c.remove(force=True)
Exemple #8
0
def _werks_in_git_tag(tag: str):
    werks_in_tag = (subprocess.check_output(
        [b"git", b"ls-tree", b"-r", b"--name-only",
         tag.encode(), b".werks"],
        cwd=testlib.cmk_path().encode(),
    ).decode().split("\n"))

    # Populate the map of all tags a werk is in
    for werk_file in werks_in_tag:
        try:
            werk_id = int(os.path.basename(werk_file))
        except ValueError:
            continue
        _werk_to_git_tag[werk_id].append(tag)

    return werks_in_tag
class CMKModuleLayerChecker(BaseChecker):
    __implements__ = IAstroidChecker

    name = "cmk-module-layer-violation"
    msgs = {
        "C8410": ("Import of %r not allowed in module %r",
                  "cmk-module-layer-violation", "whoop?"),
    }

    # This doesn't change during a pylint run, so let's save a realpath() call per import.
    cmk_path_cached = cmk_path() + "/"

    @utils.check_messages("cmk-module-layer-violation")
    def visit_import(self, node: Import) -> None:
        for name, _ in node.names:
            self._check_import(node, ModuleName(name))

    @utils.check_messages("cmk-module-layer-violation")
    def visit_importfrom(self, node: ImportFrom) -> None:
        # handle 'from . import foo, bar'
        imported = [node.modname
                    ] if node.modname else [n for n, _ in node.names]
        for modname in imported:
            self._check_import(
                node,
                ModuleName(modname)
                if node.level is None else _get_absolute_importee(
                    root_name=node.root().name,
                    modname=modname,
                    level=node.level,
                    is_package=_is_package(node),
                ),
            )

    def _check_import(self, node: Statement, imported: ModuleName) -> None:
        # We only care about imports of our own modules.
        if not imported.startswith("cmk"):
            return

        # We use paths relative to our project root, but not for our "pasting magic".
        absolute_path: str = node.root().file
        importing_path = ModulePath(
            removeprefix(absolute_path, self.cmk_path_cached))

        # Tests are allowed to import anyting.
        if str(importing_path).startswith("tests/"):
            return

        importing = self._get_module_name_of_files(importing_path)
        if not self._is_import_allowed(importing_path, importing, imported):
            self.add_message("cmk-module-layer-violation",
                             node=node,
                             args=(imported, importing))

    @staticmethod
    def _get_module_name_of_files(importing_path: ModulePath) -> ModuleName:
        # Due to our symlinks and pasting magic, astroid gets confused, so we need to compute the
        # real module name from the file path of the module.
        parts = importing_path.split("/")
        parts[-1] = removesuffix(parts[-1], ".py")
        # Emacs' flycheck stores files to be checked in a temporary file with a prefix.
        parts[-1] = removeprefix(parts[-1], "flycheck_")
        # Strip CEE/CME/CPE prefix, we use symlink magic to combine editions. :-P
        if parts[:2] in (["enterprise", "cmk"], ["managed",
                                                 "cmk"], ["plus", "cmk"]):
            parts = parts[1:]
        # Pretend that the combined checks and inventory/bakery plugins live below cmk.base.
        if len(parts) >= 2 and parts[-2].startswith("cmk_pylint_"):
            parts = ["cmk", "base", parts[-1]]
        # For all modules which don't live below cmk after mangling, just assume a toplevel module.
        if parts[0] != "cmk":
            parts = [parts[-1]]
        return ModuleName(".".join(parts))

    def _is_import_allowed(self, importing_path: ModulePath,
                           importing: ModuleName,
                           imported: ModuleName) -> bool:
        for component, component_specific_checker in _COMPONENTS:
            if not self._is_part_of_component(importing, importing_path,
                                              component):
                continue

            return component_specific_checker(
                imported=imported,
                component=component,
            )

        # the rest (matched no component)
        return _is_allowed_import(imported)

    @staticmethod
    def _is_part_of_component(importing: ModuleName,
                              importing_path: ModulePath,
                              component: Component) -> bool:
        if _in_component(importing, component):
            return True

        explicit_component = _EXPLICIT_FILE_TO_COMPONENT.get(importing_path)
        if explicit_component is not None:
            return explicit_component == component

        # The check and bakery plugins are all compiled together by tests/pylint/test_pylint.py.
        # They clearly belong to the cmk.base component.
        if component == Component("cmk.base") and importing.startswith(
                "cmk_pylint"):
            return True

        if component == Component(
                "cmk.notification_plugins") and importing_path.startswith(
                    "notifications/"):
            return True

        if component == Component(
                "cmk.special_agents") and importing_path.startswith(
                    "agents/special/"):
            return True

        return False
Exemple #10
0
 def _change_path_to_repo_path(self, msg):
     return os.path.relpath(msg.abspath, cmk_path())
def _get_python_plugins():
    return [
        "agents/plugins/%s" % p.name
        for p in Path(testlib.cmk_path(), "agents", "plugins").iterdir()
        if is_python_file(str(p), "python") or is_python_file(str(p), "python3")
    ]
Exemple #12
0
def locale_base_dir():
    return Path("%s/locale" % cmk_path())
Exemple #13
0
def precompiled_werks(tmp_path, monkeypatch):
    all_werks = cmk.utils.werks.load_raw_files(Path(testlib.cmk_path()) / ".werks")
    cmk.utils.werks.write_precompiled_werks(tmp_path / "werks", all_werks)
    monkeypatch.setattr(cmk.utils.werks, "_compiled_werks_dir", lambda: tmp_path)
def _files_to_scan():
    to_scan = [Path(cmk_path(), "web", "app", "index.wsgi")]
    for matched_file in Path(cmk_path(), "cmk", "gui").glob("**/*.py"):
        if matched_file.is_file():
            to_scan.append(matched_file)
    return to_scan
    # At the moment it's not possible to display attributes and table
    # below same node.
    assert filtered_node.count_entries() == 9

    assert filtered_node.get_node(["path", "to", "nta", "nt"]) is None
    assert filtered_node.get_node(["path", "to", "nta", "na"]) is None

    assert filtered_node.get_node(["path", "to", "another",
                                   "node1"]) is not None
    assert filtered_node.get_node(["path", "to", "another",
                                   "node2"]) is not None


# Tests with real host data

TEST_DIR = "%s/tests/unit/cmk/utils/structured_data/tree_test_data" % cmk_path(
)

TEST_DATA_STORE = StructuredDataStore(Path(TEST_DIR))

tree_name_old_addresses_arrays_memory = HostName(
    "tree_old_addresses_arrays_memory")
tree_name_old_addresses = HostName("tree_old_addresses")
tree_name_old_arrays = HostName("tree_old_arrays")
tree_name_old_interfaces = HostName("tree_old_interfaces")
tree_name_old_memory = HostName("tree_old_memory")
tree_name_old_heute = HostName("tree_old_heute")

tree_name_new_addresses_arrays_memory = HostName(
    "tree_new_addresses_arrays_memory")
tree_name_new_addresses = HostName("tree_new_addresses")
tree_name_new_arrays = HostName("tree_new_arrays")