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.version as cmk_version # pylint: disable=import-outside-toplevel import cmk.utils.paths # 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.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.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"))
def fake_version_and_paths(): if is_running_as_site_user(): return import _pytest.monkeypatch # type: ignore monkeypatch = _pytest.monkeypatch.MonkeyPatch() tmp_dir = tempfile.mkdtemp(prefix="pytest_cmk_") import cmk # TODO: handle CME case #if is_managed_repo(): # monkeypatch.setattr(cmk, "omd_version", lambda: "%s.cee" % cmk.__version__) #elif is_enterprise_repo(): if is_enterprise_repo(): monkeypatch.setattr(cmk, "omd_version", lambda: "%s.cee" % cmk.__version__) else: monkeypatch.setattr(cmk, "omd_version", lambda: "%s.cre" % cmk.__version__) 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.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.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"))
def _update_with_f12_files(self): paths = [ cmk_path() + "/omd/packages/omd", cmk_path() + "/livestatus", cmk_path() + "/livestatus/api/python", cmk_path() + "/bin", cmk_path() + "/agents/special", cmk_path() + "/agents/plugins", cmk_path() + "/agents", cmk_path() + "/modules", 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 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): print("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 print("Executing .f12 in \"%s\" DONE" % path) sys.stdout.flush()
], [ '.1.3.6.1.2.1.25.2.1.4', '/mnt/ext', '4096', '106746', '101037' ] ]], {}, ), ( [[ ['', '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 testlib.utils import cmk_path # type: ignore[import] 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__])
), Metric( '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 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__])
text="Service is OK", perfdata=[], ) }), "node1": local.LocalSection(errors=[], data={}), } worst = local.cluster_check_local("item", {}, section) best = local.cluster_check_local("item", {"outcome_on_cluster": "best"}, section) assert list(worst) == [ Result(state=State.OK, summary="[node0]: Service is OK"), ] assert list(best) == [ Result(state=State.OK, summary="[node0]: Service is OK"), ] 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 testlib.utils import cmk_path # type: ignore[import] assert not pytest.main([ "--doctest-modules", os.path.join(cmk_path(), "cmk/base/plugins/agent_based/local.py") ]) pytest.main(["-T=unit", "-vvsx", __file__])
def _update_with_f12_files(self): paths = [ cmk_path() + "/omd/packages/omd", cmk_path() + "/livestatus", cmk_path() + "/bin", cmk_path() + "/agents/special", cmk_path() + "/agents/plugins", cmk_path() + "/modules", 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 os.path.exists(cmc_path()) and not self.version.is_raw_edition(): paths += [ cmc_path() + "/bin", cmc_path() + "/modules", cmc_path() + "/cmk/base", cmc_path() + "/cmk", cmc_path() + "/web", cmc_path() + "/alert_handlers", cmc_path() + "/misc", # TODO: To be able to build the core correctly we need to build # python/boost/python-modules/rrdtool first. Skip cmc for the moment here #cmc_path() + "/core", cmc_path() + "/agents", ] if os.path.exists(cme_path()) and self.version.is_managed_edition(): paths += [ cme_path(), cme_path() + "/cmk/base", ] # Prevent build problems of livestatus print("Cleanup git files") assert os.system("sudo git clean -xfd -e .venv") >> 8 == 0 for path in paths: if os.path.exists("%s/.f12" % path): print("Executing .f12 in \"%s\"..." % path) sys.stdout.flush() 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 -x .f12" % (path, self.id)) >> 8 == 0 print("Executing .f12 in \"%s\" DONE" % path) sys.stdout.flush()
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
( { "required_node_status": "offline", "required_subscription_status": "Inactive", }, NODE_DATA, ( Result(state=State.WARN, summary="Status: online (required: offline)"), Result(state=State.WARN, summary="Subscription: active (required: inactive)"), Result(state=State.OK, summary="Version: 6.2-15"), Result(state=State.OK, summary="Hosted VMs: 5x LXC, 4x Qemu"), ), ), ]) def test_check_proxmox_ve_node_info(params, section, expected_results) -> None: results = tuple(check_proxmox_ve_node_info(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 testlib.utils import cmk_path # type: ignore[import] assert not pytest.main([ "--doctest-modules", os.path.join(cmk_path(), "cmk/base/plugins/agent_based/proxmox_ve_node_info.py") ]) pytest.main(["-T=unit", "-vvsx", __file__])
def _change_path_to_repo_path(self, msg): return os.path.relpath(msg.abspath, cmk_path())
#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2 # This file is part of Checkmk (https://checkmk.com). It is subject to the terms and # conditions defined in the file COPYING, which is part of this source code package. import os import subprocess import pytest # type: ignore[import] from testlib.utils import cmk_path PLUGIN = os.path.join(cmk_path(), 'agents', 'plugins', 'mk_errpt.aix') ERRPT_OUTPUT = [ u'IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION', u'8650BE3F 0820122810 I H ent2 ETHERCHANNEL RECOVERY', u'F3846E13 0820122510 P H ent2 ETHERCHANNEL FAILOVER', u'8650BE3F 0820104410 I H ent2 ETHERCHANNEL RECOVERY', u'F3846E13 0820093810 P H ent2 ETHERCHANNEL FAILOVER', u'8650BE3F 0820090910 I H ent2 ETHERCHANNEL RECOVERY', ] STATE_FILE_NAME = "mk_errpt_aix.last_reported" LEGACY_STATE_FILE_NAME = "mk_logwatch_aix.last_reported" def _prepare_mock_errpt(tmp_path, errpt_output): errpt_name = str(tmp_path / 'errpt') errpt_script = ''.join(['#!/bin/sh\n'] + ['echo "%s"\n' % line for line in errpt_output])
( 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 testlib.utils import cmk_path # type: ignore[import] 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__])
( Result( state=State.WARN, summary='Age: 18 hours 39 minutes (warn/crit at 12 hours 0 minutes/1 day 0 hours)', ), Metric('age', 67157.0, levels=(43200.0, 86400.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 0 seconds'), Result(state=State.OK, summary='Name: /tmp/vdump-qemu-115-2020_04_16-22_20_43.vma.lzo'), Result(state=State.OK, summary='Size: 1.00 TiB'), Result(state=State.OK, summary='Bandwidth: 2.20 GB/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 testlib.utils import cmk_path # type: ignore[import] 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__])