Exemplo n.º 1
0
def get_all_pages() -> ManPages:
    base_dirs = [
        Path(cpe_path(), "checkman"),
        Path(cmk_path(), "checkman"),
    ]
    return {
        name: man_pages.load_man_page(name, base_dirs)
        for name in man_pages.all_man_pages(base_dirs)
    }
Exemplo n.º 2
0
def resolve_image_alias(alias):
    """Resolves given "Docker image alias" using the common `resolve.sh` and returns an image
    name which can be used with `docker run`
    >>> image = resolve_image_alias("IMAGE_CMK_BASE")
    >>> assert image and isinstance(image, str)
    """
    return subprocess.check_output(
        [os.path.join(cmk_path(), "buildscripts/docker_image_aliases/resolve.sh"), alias],
        universal_newlines=True,
    ).split("\n", maxsplit=1)[0]
Exemplo n.º 3
0
def patch_man_page_dir_paths(monkeypatch, tmp_path):
    monkeypatch.setattr(
        man_pages,
        "_get_man_page_dirs",
        lambda: [
            tmp_path,
            Path(cpe_path(), "checkman"),
            Path(cmk_path(), "checkman"),
        ],
    )
Exemplo n.º 4
0
def test_all_man_pages(tmp_path):
    (tmp_path / ".asd").write_text("", encoding="utf-8")
    (tmp_path / "asd~").write_text("", encoding="utf-8")
    (tmp_path / "if").write_text("", encoding="utf-8")

    pages = man_pages.all_man_pages()

    assert len(pages) > 1241
    assert ".asd" not in pages
    assert "asd~" not in pages

    assert pages["if"] == str(tmp_path / "if")
    assert pages["if64"] == "%s/checkman/if64" % cmk_path()
Exemplo n.º 5
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)
Exemplo n.º 6
0
def import_module(pathname):
    """Return the module loaded from `pathname`.

    `pathname` is a path relative to the top-level directory
    of the repository.

    This function loads the module at `pathname` even if it does not have
    the ".py" extension.

    See Also:
        - `https://mail.python.org/pipermail/python-ideas/2014-December/030265.html`.

    """
    modname = os.path.splitext(os.path.basename(pathname))[0]
    modpath = os.path.join(cmk_path(), pathname)

    import importlib  # pylint: disable=import-outside-toplevel

    # TODO: load_module() is deprecated, we should avoid using it.
    # Furthermore, due to some reflection Kung-Fu and typeshed oddities,
    # mypy is confused about its arguments.
    return importlib.machinery.SourceFileLoader(modname, modpath).load_module()  # type: ignore[call-arg] # pylint: disable=no-value-for-parameter,deprecated-method
Exemplo n.º 7
0
def test_man_page_path_only_shipped():
    assert man_pages.man_page_path("if64") == Path(
        cmk_path()) / "checkman" / "if64"
    assert man_pages.man_page_path("not_existant") is None
Exemplo n.º 8
0
                },
            },
            (
                Result(state=state.OK,
                       summary="Usage: 53.17% - 409 MiB of 770 MiB"),
                Metric("mem_used_percent",
                       53.16899356888102,
                       boundaries=(0.0, None)),
            ),
        ),
    ],
)
def test_check_cisco_mem(check_args, expected_result):
    assert list(_idem_check_cisco_mem(value_store={},
                                      **check_args)) == list(expected_result)


if __name__ == "__main__":
    # Please keep these lines - they make TDD easy and have no effect on normal test runs.
    # Just run this file from your IDE and dive into the code.
    import os

    from tests.testlib.utils import cmk_path

    assert not pytest.main([
        "--doctest-modules",
        os.path.join(cmk_path(),
                     "cmk/base/plugins/agent_based/cisco_mem_asa.py"),
    ])
    pytest.main(["-T=unit", "-vvsx", __file__])
Exemplo n.º 9
0
    def _update_with_f12_files(self) -> None:
        paths = [
            cmk_path() + "/omd/packages/omd",
            cmk_path() + "/livestatus/api/python",
            cmk_path() + "/bin",
            cmk_path() + "/agents/special",
            cmk_path() + "/agents/plugins",
            cmk_path() + "/agents/windows/plugins",
            cmk_path() + "/agents",
            cmk_path() + "/cmk/base",
            cmk_path() + "/cmk",
            cmk_path() + "/checks",
            cmk_path() + "/checkman",
            cmk_path() + "/web",
            cmk_path() + "/inventory",
            cmk_path() + "/notifications",
            cmk_path() + "/.werks",
        ]

        if self.version.is_raw_edition():
            # The module is only used in CRE
            paths += [
                cmk_path() + "/livestatus",
            ]

        if os.path.exists(cmc_path()) and not self.version.is_raw_edition():
            paths += [
                cmc_path() + "/bin",
                cmc_path() + "/agents/plugins",
                cmc_path() + "/modules",
                cmc_path() + "/cmk/base",
                cmc_path() + "/cmk",
                cmc_path() + "/web",
                cmc_path() + "/alert_handlers",
                cmc_path() + "/misc",
                cmc_path() + "/core",
                # TODO: Do not invoke the chroot build mechanism here, which is very time
                # consuming when not initialized yet
                # cmc_path() + "/agents",
            ]

        if os.path.exists(cme_path()) and self.version.is_managed_edition():
            paths += [
                cme_path(),
                cme_path() + "/cmk/base",
            ]

        for path in paths:
            if os.path.exists("%s/.f12" % path):
                logger.info('Executing .f12 in "%s"...', path)
                assert (os.system(  # nosec
                    'cd "%s" ; '
                    "sudo PATH=$PATH ONLY_COPY=1 ALL_EDITIONS=0 SITE=%s "
                    "CHROOT_BASE_PATH=$CHROOT_BASE_PATH CHROOT_BUILD_DIR=$CHROOT_BUILD_DIR "
                    "bash .f12" % (path, self.id)) >> 8 == 0)
                logger.info('Executing .f12 in "%s" DONE', path)
                    "fs_used_percent",
                    50.0,
                    levels=(40.0, 90.0),
                    boundaries=(0.0, 100.0),
                ),
                Result(state=State.WARN, summary="50.00% used (1.10 TB of 2.20 TB)"),
            ),
        ),
    ),
)
def test_check_proxmox_ve_disk_usage(params, section, expected_results) -> None:
    results = tuple(check_proxmox_ve_disk_usage(params, section))
    print("\n" + "\n".join(map(str, results)))
    assert results == expected_results


if __name__ == "__main__":
    # Please keep these lines - they make TDD easy and have no effect on normal test runs.
    # Just run this file from your IDE and dive into the code.
    import os

    from tests.testlib.utils import cmk_path

    assert not pytest.main(
        [
            "--doctest-modules",
            os.path.join(cmk_path(), "cmk/base/plugins/agent_based/proxmox_ve_disk_usage.py"),
        ]
    )
    pytest.main(["-T=unit", "-vvsx", __file__])
Exemplo n.º 11
0
def fake_version_and_paths():
    if is_running_as_site_user():
        return

    import _pytest.monkeypatch  # type: ignore # pylint: disable=import-outside-toplevel

    monkeypatch = _pytest.monkeypatch.MonkeyPatch()
    tmp_dir = tempfile.mkdtemp(prefix="pytest_cmk_")

    import cmk.utils.paths  # pylint: disable=import-outside-toplevel
    import cmk.utils.version as cmk_version  # pylint: disable=import-outside-toplevel

    if is_managed_repo():
        edition_short = "cme"
    elif is_plus_repo():
        edition_short = "cpe"
    elif is_enterprise_repo():
        edition_short = "cee"
    else:
        edition_short = "cre"

    monkeypatch.setattr(
        cmk_version, "omd_version", lambda: "%s.%s" %
        (cmk_version.__version__, edition_short))

    # Unit test context: load all available modules
    monkeypatch.setattr(
        cmk_version,
        "is_raw_edition",
        lambda: not (is_enterprise_repo() and is_managed_repo() and
                     is_plus_repo()),
    )
    monkeypatch.setattr(cmk_version, "is_enterprise_edition",
                        is_enterprise_repo)
    monkeypatch.setattr(cmk_version, "is_managed_edition", is_managed_repo)
    monkeypatch.setattr(cmk_version, "is_plus_edition", is_plus_repo)

    monkeypatch.setattr("cmk.utils.paths.agents_dir", "%s/agents" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.checks_dir", "%s/checks" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.notifications_dir",
                        Path(cmk_path()) / "notifications")
    monkeypatch.setattr("cmk.utils.paths.inventory_dir",
                        "%s/inventory" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.inventory_output_dir",
                        os.path.join(tmp_dir, "var/check_mk/inventory"))
    monkeypatch.setattr(
        "cmk.utils.paths.inventory_archive_dir",
        os.path.join(tmp_dir, "var/check_mk/inventory_archive"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.inventory_delta_cache_dir",
        os.path.join(tmp_dir, "var/check_mk/inventory_delta_cache"),
    )
    monkeypatch.setattr("cmk.utils.paths.check_manpages_dir",
                        "%s/checkman" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.web_dir", "%s/web" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.omd_root", Path(tmp_dir))
    monkeypatch.setattr("cmk.utils.paths.tmp_dir",
                        os.path.join(tmp_dir, "tmp/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.counters_dir",
                        os.path.join(tmp_dir, "tmp/check_mk/counters"))
    monkeypatch.setattr("cmk.utils.paths.tcp_cache_dir",
                        os.path.join(tmp_dir, "tmp/check_mk/cache"))
    monkeypatch.setattr("cmk.utils.paths.trusted_ca_file",
                        Path(tmp_dir, "var/ssl/ca-certificates.crt"))
    monkeypatch.setattr(
        "cmk.utils.paths.data_source_cache_dir",
        os.path.join(tmp_dir, "tmp/check_mk/data_source_cache"),
    )
    monkeypatch.setattr("cmk.utils.paths.var_dir",
                        os.path.join(tmp_dir, "var/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.log_dir",
                        os.path.join(tmp_dir, "var/log"))
    monkeypatch.setattr("cmk.utils.paths.core_helper_config_dir",
                        Path(tmp_dir, "var/check_mk/core/helper_config"))
    monkeypatch.setattr("cmk.utils.paths.autochecks_dir",
                        os.path.join(tmp_dir, "var/check_mk/autochecks"))
    monkeypatch.setattr(
        "cmk.utils.paths.precompiled_checks_dir",
        os.path.join(tmp_dir, "var/check_mk/precompiled_checks"),
    )
    monkeypatch.setattr("cmk.utils.paths.crash_dir",
                        Path(cmk.utils.paths.var_dir) / "crashes")
    monkeypatch.setattr("cmk.utils.paths.include_cache_dir",
                        os.path.join(tmp_dir, "tmp/check_mk/check_includes"))
    monkeypatch.setattr("cmk.utils.paths.check_mk_config_dir",
                        os.path.join(tmp_dir, "etc/check_mk/conf.d"))
    monkeypatch.setattr("cmk.utils.paths.main_config_file",
                        os.path.join(tmp_dir, "etc/check_mk/main.mk"))
    monkeypatch.setattr("cmk.utils.paths.default_config_dir",
                        os.path.join(tmp_dir, "etc/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.piggyback_dir",
                        Path(tmp_dir) / "var/check_mk/piggyback")
    monkeypatch.setattr("cmk.utils.paths.piggyback_source_dir",
                        Path(tmp_dir) / "var/check_mk/piggyback_sources")
    monkeypatch.setattr("cmk.utils.paths.htpasswd_file",
                        os.path.join(tmp_dir, "etc/htpasswd"))

    monkeypatch.setattr("cmk.utils.paths.local_share_dir",
                        Path(tmp_dir, "local/share/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.local_checks_dir",
                        Path(tmp_dir, "local/share/check_mk/checks"))
    monkeypatch.setattr(
        "cmk.utils.paths.local_notifications_dir",
        Path(tmp_dir, "local/share/check_mk/notifications"),
    )
    monkeypatch.setattr("cmk.utils.paths.local_inventory_dir",
                        Path(tmp_dir, "local/share/check_mk/inventory"))
    monkeypatch.setattr("cmk.utils.paths.local_check_manpages_dir",
                        Path(tmp_dir, "local/share/check_mk/checkman"))
    monkeypatch.setattr("cmk.utils.paths.local_agents_dir",
                        Path(tmp_dir, "local/share/check_mk/agents"))
    monkeypatch.setattr("cmk.utils.paths.local_web_dir",
                        Path(tmp_dir, "local/share/check_mk/web"))
    monkeypatch.setattr(
        "cmk.utils.paths.local_pnp_templates_dir",
        Path(tmp_dir, "local/share/check_mk/pnp-templates"),
    )
    monkeypatch.setattr("cmk.utils.paths.local_doc_dir",
                        Path(tmp_dir, "local/share/doc/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.local_locale_dir",
                        Path(tmp_dir, "local/share/check_mk/locale"))
    monkeypatch.setattr("cmk.utils.paths.local_bin_dir",
                        Path(tmp_dir, "local/bin"))
    monkeypatch.setattr("cmk.utils.paths.local_lib_dir",
                        Path(tmp_dir, "local/lib"))
    monkeypatch.setattr("cmk.utils.paths.local_gui_plugins_dir",
                        Path(tmp_dir, "local/lib/check_mk/gui/plugins"))
    monkeypatch.setattr("cmk.utils.paths.local_mib_dir",
                        Path(tmp_dir, "local/share/snmp/mibs"))
    monkeypatch.setattr("cmk.utils.paths.diagnostics_dir",
                        Path(tmp_dir).joinpath("var/check_mk/diagnostics"))
    monkeypatch.setattr("cmk.utils.paths.site_config_dir",
                        Path(cmk.utils.paths.var_dir, "site_configs"))
    monkeypatch.setattr("cmk.utils.paths.disabled_packages_dir",
                        Path(cmk.utils.paths.var_dir, "disabled_packages"))
    monkeypatch.setattr(
        "cmk.utils.paths.nagios_objects_file",
        os.path.join(tmp_dir, "etc/nagios/conf.d/check_mk_objects.cfg"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.precompiled_hostchecks_dir",
        os.path.join(tmp_dir, "var/check_mk/precompiled"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.discovered_host_labels_dir",
        Path(tmp_dir, "var/check_mk/discovered_host_labels"),
    )
    monkeypatch.setattr("cmk.utils.paths.profile_dir",
                        Path(cmk.utils.paths.var_dir, "web"))

    # Agent registration paths
    monkeypatch.setattr(
        "cmk.utils.paths.received_outputs_dir",
        Path(cmk.utils.paths.var_dir, "agent-receiver/received-outputs"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.data_source_push_agent_dir",
        Path(cmk.utils.paths.data_source_cache_dir, "push-agent"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths._r4r_base_dir",
        Path(cmk.utils.paths.var_dir, "wato/requests-for-registration"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.r4r_new_dir",
        Path(cmk.utils.paths._r4r_base_dir, "NEW"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.r4r_pending_dir",
        Path(cmk.utils.paths._r4r_base_dir, "PENDING"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.r4r_declined_dir",
        Path(cmk.utils.paths._r4r_base_dir, "DECLINED"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.r4r_declined_bundles_dir",
        Path(cmk.utils.paths._r4r_base_dir, "DECLINED-BUNDLES"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.r4r_ready_dir",
        Path(cmk.utils.paths._r4r_base_dir, "READY"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.r4r_discoverable_dir",
        Path(cmk.utils.paths._r4r_base_dir, "DISCOVERABLE"),
    )
                Result(state=State.OK,
                       summary="Name: /some/file.name.vma.lzo"),
                Result(state=State.OK, summary="Bandwidth: 10.0 kB/s"),
                Metric("backup_avgspeed", 10000.0, boundaries=(0.0, None)),
            ),
        ),
    ),
)
def test_check_proxmox_ve_vm_backup_status(params, section,
                                           expected_results) -> None:
    results = tuple(
        check_proxmox_ve_vm_backup_status(FROZEN_TIME, params, section))
    print("\n" + ",\n".join(map(str, results)))
    assert results == expected_results


if __name__ == "__main__":
    # Please keep these lines - they make TDD easy and have no effect on normal test runs.
    # Just run this file from your IDE and dive into the code.
    import os

    from tests.testlib.utils import cmk_path

    assert not pytest.main([
        "--doctest-modules",
        os.path.join(
            cmk_path(),
            "cmk/base/plugins/agent_based/proxmox_ve_backup_status.py"),
    ])
    pytest.main(["-T=unit", "-vvsx", __file__])
Exemplo n.º 13
0
                Result(state=State.OK, summary="Age: 18 hours 39 minutes"),
                Metric("age", 67157.0, boundaries=(0.0, None)),
                Result(state=State.OK, summary="Time: 2020-04-16 22:20:43"),
                Result(state=State.OK, summary="Duration: 2 minutes 20 seconds"),
                Result(state=State.OK, summary="Name: /some/file.name.vma.lzo"),
                Result(state=State.OK, summary="Bandwidth: 10.0 kB/s"),
            ),
        ),
    ),
)
def test_check_proxmox_ve_vm_backup_status(params, section, expected_results) -> None:
    results = tuple(check_proxmox_ve_vm_backup_status(FROZEN_TIME, params, section))
    print("\n" + ",\n".join(map(str, results)))
    assert results == expected_results


if __name__ == "__main__":
    # Please keep these lines - they make TDD easy and have no effect on normal test runs.
    # Just run this file from your IDE and dive into the code.
    import os

    from tests.testlib.utils import cmk_path

    assert not pytest.main(
        [
            "--doctest-modules",
            os.path.join(cmk_path(), "cmk/base/plugins/agent_based/proxmox_ve_backup_status.py"),
        ]
    )
    pytest.main(["-T=unit", "-vvsx", __file__])
Exemplo n.º 14
0
            [(interfaces.DISCOVERY_DEFAULT_PARAMETERS)],
            section,
        )) == discovery_results)

    for item, par, res in items_params_results:
        assert (list(
            netapp_api_if._check_netapp_api_if(
                item,
                (par),
                section,
                value_store={},
            )) == res)


if __name__ == "__main__":
    # Please keep these lines - they make TDD easy and have no effect on normal test runs.
    # Just run this file from your IDE and dive into the code.
    from pathlib import Path

    from tests.testlib.utils import cmk_path

    assert not pytest.main([
        "-T=unit",
        "-vvsx",
        "--doctest-modules",
        str(
            Path(cmk_path()) /
            "cmk/base/plugins/agent_based/netapp_api_if.py"),
        __file__,
    ])
Exemplo n.º 15
0
            ),
        ],
    )

    assert list(
        local.check_local(
            "", {},
            local.LocalSection(errors=[], data={"": local_result}))) == [
                Result(state=State.OK,
                       summary="Result is computed from two values"),
                Result(state=State.OK, summary="Value 1: 10.00"),
                Metric("value1", 10, levels=(30.0, 50.0)),
                Result(state=State.WARN,
                       summary="Value 2: 20.00 (warn/crit at 20.00/50.00)"),
                Metric("value2", 20, levels=(20, 50), boundaries=(0, 100)),
            ]


if __name__ == "__main__":
    # Please keep these lines - they make TDD easy and have no effect on normal test runs.
    # Just run this file from your IDE and dive into the code.
    import os

    from tests.testlib.utils import cmk_path

    assert not pytest.main([
        "--doctest-modules",
        os.path.join(cmk_path(), "cmk/base/plugins/agent_based/local.py")
    ])
    pytest.main(["-T=unit", "-vvsx", __file__])
Exemplo n.º 16
0
                    ("swap space", 34156818432, 6154354688),
                ],
            },
        ),
        (
            [
                [
                    ["", "Swap space", "1024", "6143996", "144128"],
                    ["", "/", "4096", "125957388", "7714723"],
                ]
            ],
            {},
        ),
    ],
)
def test_hr_mem(string_table, expected_parsed_data):
    assert hr_mem.pre_parse_hr_mem(string_table) == expected_parsed_data


if __name__ == "__main__":
    # Please keep these lines - they make TDD easy and have no effect on normal test runs.
    # Just run this file from your IDE and dive into the code.
    import os

    from tests.testlib.utils import cmk_path

    assert not pytest.main(
        ["--doctest-modules", os.path.join(cmk_path(), "cmk/base/plugins/agent_based/hr_mem.py")]
    )
    pytest.main(["-T=unit", "-vvsx", __file__])
Exemplo n.º 17
0
def fake_version_and_paths():
    if is_running_as_site_user():
        return

    import _pytest.monkeypatch  # type: ignore # pylint: disable=import-outside-toplevel

    monkeypatch = _pytest.monkeypatch.MonkeyPatch()
    tmp_dir = tempfile.mkdtemp(prefix="pytest_cmk_")

    import cmk.utils.paths  # pylint: disable=import-outside-toplevel
    import cmk.utils.version as cmk_version  # pylint: disable=import-outside-toplevel

    if is_managed_repo():
        edition_short = "cme"
    elif is_enterprise_repo():
        edition_short = "cee"
    else:
        edition_short = "cre"

    monkeypatch.setattr(
        cmk_version, "omd_version", lambda: "%s.%s" %
        (cmk_version.__version__, edition_short))

    monkeypatch.setattr("cmk.utils.paths.agents_dir", "%s/agents" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.checks_dir", "%s/checks" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.notifications_dir",
                        Path(cmk_path()) / "notifications")
    monkeypatch.setattr("cmk.utils.paths.inventory_dir",
                        "%s/inventory" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.inventory_output_dir",
                        os.path.join(tmp_dir, "var/check_mk/inventory"))
    monkeypatch.setattr(
        "cmk.utils.paths.inventory_archive_dir",
        os.path.join(tmp_dir, "var/check_mk/inventory_archive"),
    )
    monkeypatch.setattr("cmk.utils.paths.check_manpages_dir",
                        "%s/checkman" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.web_dir", "%s/web" % cmk_path())
    monkeypatch.setattr("cmk.utils.paths.omd_root", tmp_dir)
    monkeypatch.setattr("cmk.utils.paths.tmp_dir",
                        os.path.join(tmp_dir, "tmp/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.counters_dir",
                        os.path.join(tmp_dir, "tmp/check_mk/counters"))
    monkeypatch.setattr("cmk.utils.paths.tcp_cache_dir",
                        os.path.join(tmp_dir, "tmp/check_mk/cache"))
    monkeypatch.setattr("cmk.utils.paths.trusted_ca_file",
                        os.path.join(tmp_dir, "var/ssl/ca-certificates.crt"))
    monkeypatch.setattr(
        "cmk.utils.paths.data_source_cache_dir",
        os.path.join(tmp_dir, "tmp/check_mk/data_source_cache"),
    )
    monkeypatch.setattr("cmk.utils.paths.var_dir",
                        os.path.join(tmp_dir, "var/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.log_dir",
                        os.path.join(tmp_dir, "var/log"))
    monkeypatch.setattr("cmk.utils.paths.core_helper_config_dir",
                        Path(tmp_dir, "var/check_mk/core/helper_config"))
    monkeypatch.setattr("cmk.utils.paths.autochecks_dir",
                        os.path.join(tmp_dir, "var/check_mk/autochecks"))
    monkeypatch.setattr(
        "cmk.utils.paths.precompiled_checks_dir",
        os.path.join(tmp_dir, "var/check_mk/precompiled_checks"),
    )
    monkeypatch.setattr("cmk.utils.paths.crash_dir",
                        Path(cmk.utils.paths.var_dir) / "crashes")
    monkeypatch.setattr("cmk.utils.paths.include_cache_dir",
                        os.path.join(tmp_dir, "tmp/check_mk/check_includes"))
    monkeypatch.setattr("cmk.utils.paths.check_mk_config_dir",
                        os.path.join(tmp_dir, "etc/check_mk/conf.d"))
    monkeypatch.setattr("cmk.utils.paths.main_config_file",
                        os.path.join(tmp_dir, "etc/check_mk/main.mk"))
    monkeypatch.setattr("cmk.utils.paths.default_config_dir",
                        os.path.join(tmp_dir, "etc/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.piggyback_dir",
                        Path(tmp_dir) / "var/check_mk/piggyback")
    monkeypatch.setattr("cmk.utils.paths.piggyback_source_dir",
                        Path(tmp_dir) / "var/check_mk/piggyback_sources")
    monkeypatch.setattr("cmk.utils.paths.htpasswd_file",
                        os.path.join(tmp_dir, "etc/htpasswd"))

    monkeypatch.setattr("cmk.utils.paths.local_share_dir",
                        Path(tmp_dir, "local/share/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.local_checks_dir",
                        Path(tmp_dir, "local/share/check_mk/checks"))
    monkeypatch.setattr(
        "cmk.utils.paths.local_notifications_dir",
        Path(tmp_dir, "local/share/check_mk/notifications"),
    )
    monkeypatch.setattr("cmk.utils.paths.local_inventory_dir",
                        Path(tmp_dir, "local/share/check_mk/inventory"))
    monkeypatch.setattr("cmk.utils.paths.local_check_manpages_dir",
                        Path(tmp_dir, "local/share/check_mk/checkman"))
    monkeypatch.setattr("cmk.utils.paths.local_agents_dir",
                        Path(tmp_dir, "local/share/check_mk/agents"))
    monkeypatch.setattr("cmk.utils.paths.local_web_dir",
                        Path(tmp_dir, "local/share/check_mk/web"))
    monkeypatch.setattr(
        "cmk.utils.paths.local_pnp_templates_dir",
        Path(tmp_dir, "local/share/check_mk/pnp-templates"),
    )
    monkeypatch.setattr("cmk.utils.paths.local_doc_dir",
                        Path(tmp_dir, "local/share/doc/check_mk"))
    monkeypatch.setattr("cmk.utils.paths.local_locale_dir",
                        Path(tmp_dir, "local/share/check_mk/locale"))
    monkeypatch.setattr("cmk.utils.paths.local_bin_dir",
                        Path(tmp_dir, "local/bin"))
    monkeypatch.setattr("cmk.utils.paths.local_lib_dir",
                        Path(tmp_dir, "local/lib"))
    monkeypatch.setattr("cmk.utils.paths.local_mib_dir",
                        Path(tmp_dir, "local/share/snmp/mibs"))
    monkeypatch.setattr("cmk.utils.paths.diagnostics_dir",
                        Path(tmp_dir).joinpath("var/check_mk/diagnostics"))
    monkeypatch.setattr("cmk.utils.paths.site_config_dir",
                        Path(cmk.utils.paths.var_dir, "site_configs"))
    monkeypatch.setattr("cmk.utils.paths.disabled_packages_dir",
                        Path(cmk.utils.paths.var_dir, "disabled_packages"))
    monkeypatch.setattr(
        "cmk.utils.paths.nagios_objects_file",
        os.path.join(tmp_dir, "etc/nagios/conf.d/check_mk_objects.cfg"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.precompiled_hostchecks_dir",
        os.path.join(tmp_dir, "var/check_mk/precompiled"),
    )
    monkeypatch.setattr(
        "cmk.utils.paths.discovered_host_labels_dir",
        Path(tmp_dir, "var/check_mk/discovered_host_labels"),
    )
    monkeypatch.setattr("cmk.utils.paths.profile_dir",
                        Path(cmk.utils.paths.var_dir, "web"))
Exemplo n.º 18
0
def test_parse_backup_logs(logfile, expected_results,
                           expected_exception) -> None:
    file_path = Path(os.path.dirname(__file__)) / "proxmox_ve-files" / logfile
    log = ({
        "n": i,
        "t": line
    } for i, line in enumerate(file_path.open().readlines()))
    with ExitStack() as exit_stack:
        if expected_exception:
            exit_stack.enter_context(pytest.raises(expected_exception))
        results = collect_vm_backup_info([
            BackupTask({},
                       log,
                       strict=True,
                       dump_logs=False,
                       dump_erroneous_logs=False)
        ])
        assert results == expected_results


if __name__ == "__main__":
    # Please keep these lines - they make TDD easy and have no effect on normal test runs.
    # Just run this file from your IDE and dive into the code.
    from tests.testlib.utils import cmk_path

    assert not pytest.main([
        "--doctest-modules",
        os.path.join(cmk_path(), "cmk/special_agents/agent_proxmox_ve.py"),
    ])
    pytest.main(["-T=unit", "-vvsx", __file__])