Esempio n. 1
0
def fake_sendmail_fixture(site: Site):
    site.write_text_file(
        "local/bin/sendmail", "#!/bin/bash\n" "set -e\n" 'echo "sendmail called with: $@"\n'
    )
    os.chmod(site.path("local/bin/sendmail"), 0o775)
    yield
    site.delete_file("local/bin/sendmail")
Esempio n. 2
0
def test_load_dashboard_plugin(request, site: Site):
    plugin_path = "local/lib/check_mk/gui/plugins/dashboard/test_plugin.py"

    def cleanup():
        site.delete_file(plugin_path)

    request.addfinalizer(cleanup)

    assert not site.file_exists("tmp/dashboard_test")

    site.write_text_file(
        plugin_path,
        """
with open("%s", "w") as f:
    f.write("ding")
""" % site.path("tmp/dashboard_test"),
    )

    # Reload site apache to trigger the reload of our plugin
    site.omd("reload", "apache")

    def file_created():
        return site.file_exists("tmp/dashboard_test")

    # We need to wait some time for apache to initialize our application
    wait_until(file_created, timeout=60, interval=1)
Esempio n. 3
0
def test_simple_check_mkevents_call(site: Site, args):
    p = site.execute(
        ["./check_mkevents"] + args + ["somehost"],
        stdout=subprocess.PIPE,
        cwd=site.path("lib/nagios/plugins"),
    )
    output = p.stdout.read() if p.stdout else "<NO STDOUT>"
    assert output == "OK - no events for somehost\n"
    assert p.wait() == 0
def _execute_as_site_user(site: Site, args):
    env_vars = {
        "VERSION": site.version.version_spec,
        "EDITION": site.version.edition(),
        "REUSE": "1" if site.reuse else "0",
        "BRANCH": site.version._branch,
    }
    for varname in [
            "WORKSPACE",
            "PYTEST_ADDOPTS",
            "BANDIT_OUTPUT_ARGS",
            "SHELLCHECK_OUTPUT_ARGS",
            "PYLINT_ARGS",
            "CI",
    ]:
        if varname in os.environ:
            env_vars[varname] = os.environ[varname]

    env_var_str = " ".join(
        ["%s=%s" % (k, pipes.quote(v)) for k, v in env_vars.items()]) + " "

    cmd_parts = [
        "python3",
        site.path("local/bin/pytest"),
        "-p",
        "no:cov",
        "--log-cli-level=DEBUG",
        "--log-cli-format=%(asctime)s %(levelname)s %(message)s",
        "--junitxml",
        site.path("junit.xml"),
        "-T",
        "integration",
    ] + args

    cmd = "cd %s && " % pipes.quote(cmk_path())
    cmd += env_var_str + subprocess.list2cmdline(cmd_parts)
    args = ["/usr/bin/sudo", "--", "/bin/su", "-l", site.id, "-c", cmd]
    logger.info("Executing: %r", subprocess.list2cmdline(args))
    return subprocess.call(args, stderr=subprocess.STDOUT)
Esempio n. 5
0
def test_export_msi_file(site: Site, tmp_path):
    msi_file = _get_msi_file_path(site=site)

    out_dir = tmp_path / "idts"
    bin_path = site.path("bin/")
    try:
        out_dir.mkdir()
        for entry in ["File", "Property", "Component"]:
            msi_engine.export_msi_file(bin_path, entry, str(msi_file),
                                       str(out_dir))
            f = out_dir / (entry + ".idt")
            assert f.exists(), "Ups for [{}] {}".format(entry, f)
    finally:
        if out_dir.exists():
            shutil.rmtree(str(out_dir))
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. 7
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")
def initial_state_fixture(site: Site, scenario):
    # Before each test: Set to initial state: Both UP
    site.send_host_check_result("notify-test-child", 0, "UP")
    site.send_host_check_result("notify-test-parent", 0, "UP")

    # Before each test: Clear logs
    if scenario.core == "cmc":
        # The command is processed asynchronously -> Wait for completion
        inode_before = os.stat(site.path("var/check_mk/core/history")).st_ino
        site.live.command("[%d] ROTATE_LOGFILE" % time.time())

        def rotated_log():
            try:
                return inode_before != os.stat(site.path("var/check_mk/core/history")).st_ino
            except OSError as e:
                if e.errno == errno.ENOENT:
                    return False
                raise e

        wait_until(rotated_log, timeout=10)
    else:
        site.delete_file("var/nagios/nagios.log")
Esempio n. 9
0
def test_load_openapi_plugin(site: Site, test_script: str) -> None:
    assert (subprocess.check_output(
        ["python3", site.path(test_script)],
        encoding="utf-8").rstrip() == "True")
Esempio n. 10
0
def test_load_legacy_config_plugin(site: Site, test_script: str) -> None:
    assert (subprocess.check_output(
        ["python3", site.path(test_script)],
        encoding="utf-8").rstrip() == "legacy")
Esempio n. 11
0
def _get_msi_file_path(site: Site):
    msi_path = Path(site.path(MSI_LOCATION))
    return msi_path / "check_mk_agent.msi"
Esempio n. 12
0
def test_files(site: Site, test_file):
    msi_path = Path(site.path(MSI_LOCATION))
    assert Path(msi_path / test_file).exists(), "path: '{}' file: '{}'".format(
        msi_path, test_file)
Esempio n. 13
0
def test_executables(site: Site, executable):
    bin_path = Path(site.path("bin"))
    assert Path(bin_path / executable).exists(), "path: '{}' exe: '{}'".format(
        bin_path, executable)
Esempio n. 14
0
def test_monitoring_plugins(site: Site, plugin):
    plugin_path = site.path(os.path.join("lib/nagios/plugins", plugin))
    assert os.path.exists(plugin_path)
    assert os.access(plugin_path, os.X_OK)
Esempio n. 15
0
def _get_msi_file_path_not_signed(site: Site):
    msi_path = Path(site.path(MSI_LOCATION))
    return msi_path / msi_engine.AGENT_MSI_FILE
Esempio n. 16
0
def fixture_test_dir(site: Site):
    site.makedirs("protobuf")
    yield Path(site.path("protobuf"))