Esempio n. 1
0
def get_undeclared_dependencies() -> Iterable[ImportName]:
    """Iterate over imported dependencies which could not be found in the Pipfile"""
    imported_libs = get_imported_libs(Path(repo_path()) / "cmk")
    pipfile_libs = get_pipfile_libs(Path(repo_path()))
    declared_libs = set(chain.from_iterable(pipfile_libs.values()))

    yield from imported_libs - declared_libs
Esempio n. 2
0
def get_unused_dependencies() -> Iterable[PackageName]:
    """Iterate over declared dependencies which are not imported"""
    imported_libs = get_imported_libs(Path(repo_path()))
    pipfile_libs = get_pipfile_libs(Path(repo_path()))
    for packagename, import_names in pipfile_libs.items():
        if set(import_names).isdisjoint(imported_libs):
            yield packagename
Esempio n. 3
0
def _compile_check_and_inventory_plugins(pylint_test_dir):

    for idx, f_name in enumerate(pylint_cmk.check_files(repo_path() + "/checks")):
        with stand_alone_template(pylint_test_dir + "/cmk_checks_%s.py" % idx) as file_handle:
            pylint_cmk.add_file(file_handle, f_name)

    with stand_alone_template(pylint_test_dir + "/cmk_checks.py") as file_handle:
        pylint_cmk.add_file(file_handle, repo_path() + "/cmk/base/inventory_plugins.py")
        for path in pylint_cmk.check_files(repo_path() + "/inventory"):
            pylint_cmk.add_file(file_handle, path)
def test_calculate_data_for_prediction(cfg_setup, utcdate, timezone, params):

    period_info = prediction._PREDICTION_PERIODS[params["period"]]
    with on_time(utcdate, timezone):
        now = int(time.time())
        assert callable(period_info.groupby)
        timegroup = period_info.groupby(now)[0]

        time_windows = prediction._time_slices(
            now, int(params["horizon"] * 86400), period_info, timegroup
        )

    hostname, service_description, dsname = HostName("test-prediction"), "CPU load", "load15"
    rrd_datacolumn = cmk.utils.prediction.rrd_datacolum(
        hostname, service_description, dsname, "MAX"
    )
    data_for_pred = prediction._calculate_data_for_prediction(time_windows, rrd_datacolumn)

    expected_reference = _load_expected_result(
        "%s/tests/integration/cmk/base/test-files/%s/%s" % (repo_path(), timezone, timegroup)
    )

    assert isinstance(expected_reference, dict)
    assert sorted(asdict(data_for_pred)) == sorted(expected_reference)
    for key in expected_reference:
        if key == "points":
            for cal, ref in zip(data_for_pred.points, expected_reference["points"]):
                assert cal == pytest.approx(ref, rel=1e-12, abs=1e-12)
        else:
            assert getattr(data_for_pred, key) == expected_reference[key]
Esempio n. 5
0
def test_paths_in_omd_and_opt_root(monkeypatch):

    omd_root = "/omd/sites/dingeling"
    with monkeypatch.context() as m:
        m.setitem(os.environ, "OMD_ROOT", omd_root)
        test_paths = import_module("%s/cmk/utils/paths.py" % repo_path())
        _check_paths(omd_root, test_paths.__dict__)
Esempio n. 6
0
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
Esempio n. 7
0
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(),
    ]
    files = pylint_args + check_files

    print(
        f"Running pylint in '{base_path}' with: {subprocess.list2cmdline(cmd)}"
        f" [{len(files)} files omitted]"
    )
    exit_code = subprocess.run(cmd + files, shell=False, cwd=base_path, check=False).returncode
    print(f"Finished with exit code: {exit_code}")

    return exit_code
Esempio n. 8
0
def _git_repos():
    # This ensures that we can also work with git-worktrees. For this, the original git repository
    # needs to be mapped into the container as well.
    repo_path = testlib.repo_path()
    git_entry = os.path.join(repo_path, ".git")
    repos = {
        # To get access to the test scripts and for updating the version from
        # the current git checkout. Will also be used for updating the image with
        # the current git state
        repo_path: {
            "bind": "/git-lowerdir",
            "mode": "ro",
        },
    }
    if os.path.isfile(git_entry):  # if not, it's a directory
        with open(git_entry, "r") as f:
            real_path = f.read()
            real_path = real_path[8:]  # skip "gitdir: "
            real_path = real_path.split("/.git")[0]  # cut off .git/...

        repos[real_path] = {
            "bind": real_path,
            "mode": "ro",
        }

    return repos
Esempio n. 9
0
def _get_import_names_from_pipfile() -> List[str]:

    parsed_pipfile = Pipfile.load(filename=repo_path() + "/Pipfile")
    import_names = []
    for dist_name in parsed_pipfile.data["default"].keys():
        import_names.extend(_get_import_names_from_dist_name(dist_name))
    assert import_names
    return import_names
Esempio n. 10
0
def _get_files_to_check(pylint_test_dir):
    # Add the compiled files for things that are no modules yet
    Path(pylint_test_dir + "/__init__.py").touch()
    _compile_check_and_inventory_plugins(pylint_test_dir)

    # Not checking compiled check, inventory, bakery plugins with Python 3
    files = [pylint_test_dir]

    p = subprocess.Popen(
        ["%s/scripts/find-python-files" % repo_path()],
        stdout=subprocess.PIPE,
        encoding="utf-8",
        shell=False,
        close_fds=True,
    )
    stdout = p.communicate()[0]

    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/"):
            continue

        # TODO: We should also test them...
        if (
            rel_path == "werk"
            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)

    return files
Esempio n. 11
0
def test_pylint(pylint_test_dir, capsys):
    with capsys.disabled():
        print("\n")
        retcode = subprocess.call("python -m pylint --version".split(), shell=False)
        print()
        assert not retcode

    exit_code = pylint_cmk.run_pylint(repo_path(), _get_files_to_check(pylint_test_dir))
    assert exit_code == 0, "PyLint found an error"
Esempio n. 12
0
def test_all_deployment_packages_pinned() -> None:
    parsed_pipfile = Pipfile.load(filename=repo_path() + "/Pipfile")
    unpinned_packages = [
        f"'{n}'" for n, v in parsed_pipfile.data["default"].items() if v == "*"
    ]
    assert not unpinned_packages, (
        "The following packages are not pinned: %s. "
        "For the sake of reproducibility, all deployment packages must be pinned to a version!"
    ) % " ,".join(unpinned_packages)
def cfg_setup_fixture(request, web, site: Site):
    hostname = "test-prediction"

    # Enforce use of the pre-created RRD file from the git. The restart of the core
    # is needed to make it renew it's internal RRD file cache
    site.makedirs("var/check_mk/rrd/test-prediction")
    with open(site.path("var/check_mk/rrd/test-prediction/CPU_load.rrd"), "wb") as f:
        f.write(
            Path(
                repo_path(), "tests", "integration", "cmk", "base", "test-files", "CPU_load.rrd"
            ).read_bytes()
        )

    site.write_text_file(
        "var/check_mk/rrd/test-prediction/CPU_load.info",
        Path(
            repo_path(), "tests", "integration", "cmk", "base", "test-files", "CPU_load.info"
        ).read_text(),
    )

    site.restart_core()

    create_linux_test_host(request, site, "test-prediction")

    site.write_text_file(
        "etc/check_mk/conf.d/linux_test_host_%s_cpu_load.mk" % hostname,
        """
globals().setdefault('custom_checks', [])

custom_checks = [
    ( {'service_description': u'CPU load', 'has_perfdata': True}, [], ALL_HOSTS, {} ),
] + custom_checks
""",
    )

    site.activate_changes_and_wait_for_core_reload()

    yield

    # Cleanup
    site.delete_file("etc/check_mk/conf.d/linux_test_host_%s_cpu_load.mk" % hostname)
    site.activate_changes_and_wait_for_core_reload()
    site.delete_dir("var/check_mk/rrd")
Esempio n. 14
0
def test_check_plugin_header(plugin_path: str):
    for plugin in Path(testlib.repo_path(), plugin_path).iterdir():
        if plugin.name.startswith("."):
            # .f12
            continue
        with plugin.open() as handle:
            shebang = handle.readline().strip()

        assert shebang == "#!/usr/bin/env python3", (
            f"Plugin '{plugin.name}' has wrong shebang '{shebang}'", )
Esempio n. 15
0
def cfg_setup_fixture(request, web, site: Site):  # noqa: F811 # pylint: disable=redefined-outer-name
    hostname = "test-prediction"

    # Enforce use of the pre-created RRD file from the git. The restart of the core
    # is needed to make it renew it's internal RRD file cache
    site.makedirs("var/check_mk/rrd/test-prediction")
    with open(site.path("var/check_mk/rrd/test-prediction/CPU_load.rrd"),
              "wb") as f:
        f.write(
            open(
                "%s/tests/integration/cmk/base/test-files/CPU_load.rrd" %
                repo_path(), "rb").read())

    site.write_text_file(
        "var/check_mk/rrd/test-prediction/CPU_load.info",
        open("%s/tests/integration/cmk/base/test-files/CPU_load.info" %
             repo_path()).read(),
    )

    site.restart_core()

    create_linux_test_host(request, web, site, "test-prediction")

    site.write_text_file(
        "etc/check_mk/conf.d/linux_test_host_%s_cpu_load.mk" % hostname,
        """
globals().setdefault('custom_checks', [])

custom_checks = [
    ( {'service_description': u'CPU load', 'has_perfdata': True}, [], ALL_HOSTS, {} ),
] + custom_checks
""",
    )

    web.activate_changes()

    yield

    # Cleanup
    site.delete_file("etc/check_mk/conf.d/linux_test_host_%s_cpu_load.mk" %
                     hostname)
    site.delete_dir("var/check_mk/rrd")
Esempio n. 16
0
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(testlib.repo_path())
        ty = repo_rel_path.parts[1]
        if ty not in test_types:
            if not isinstance(item, DoctestItem):
                raise Exception("Test in %s not TYPE marked: %r (%r)" % (repo_rel_path, item, ty))

        item.add_marker(pytest.mark.type.with_args(ty))
Esempio n. 17
0
def test_pipfile_lock_up_to_date(loaded_pipfile):
    lockfile_hash = _get_lockfile_hash(Path(repo_path(), "Pipfile.lock"))
    assert loaded_pipfile.hash == lockfile_hash
Esempio n. 18
0
from pathlib import Path

import pytest
import requests
import requests.exceptions

import tests.testlib as testlib
from tests.testlib.utils import (
    cmk_path,
    get_cmk_download_credentials,
    get_cmk_download_credentials_file,
)

import docker  # type: ignore[import]

build_path = os.path.join(testlib.repo_path(), "docker")
image_prefix = "docker-tests"
branch_name = os.environ.get("BRANCH", "master")

logger = logging.getLogger()


def build_version():
    return testlib.CMKVersion(
        version_spec=os.environ.get("VERSION", testlib.CMKVersion.DAILY),
        edition=os.environ.get("EDITION", testlib.CMKVersion.CEE),
        branch=branch_name,
    )


@pytest.fixture(scope="session")
Esempio n. 19
0
def load_pipfile():
    return Pipfile.load(filename=repo_path() + "/Pipfile")