forked from Checkmk/checkmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
469 lines (376 loc) · 16.3 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env python3
# 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 abc
import datetime
import fcntl
import importlib.machinery
import importlib.util
import os
import subprocess
import sys
import tempfile
import time
from collections.abc import Callable, Collection, Iterator, Mapping
from contextlib import contextmanager
from pathlib import Path
from types import ModuleType
from typing import Any, Final
import freezegun
import pytest
import urllib3
from tests.testlib.compare_html import compare_html
from tests.testlib.event_console import CMKEventConsole, CMKEventConsoleStatus
from tests.testlib.site import Site, SiteFactory
from tests.testlib.utils import (
add_python_paths,
cmc_path,
cme_path,
cmk_path,
current_branch_name,
get_cmk_download_credentials,
get_standard_linux_agent_output,
is_cloud_repo,
is_enterprise_repo,
is_managed_repo,
is_running_as_site_user,
repo_path,
site_id,
virtualenv_path,
)
from tests.testlib.version import CMKVersion # noqa: F401 # pylint: disable=unused-import
from tests.testlib.web_session import APIError, CMKWebSession
from cmk.utils.type_defs import CheckPluginName, HostName
# Disable insecure requests warning message during SSL testing
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def skip_unwanted_test_types(item: pytest.Item) -> None:
test_type = item.get_closest_marker("type")
if test_type is None:
raise Exception("Test is not TYPE marked: %s" % item)
if not item.config.getoption("-T"):
raise SystemExit("Please specify type of tests to be executed (py.test -T TYPE)")
test_type_name = test_type.args[0]
if test_type_name != item.config.getoption("-T"):
pytest.skip("Not testing type %r" % test_type_name)
_UNPATCHED_PATHS: Final = {
# FIXME :-(
# dropping these makes tests/unit/cmk/gui/watolib/test_config_sync.py fail.
"local_dashboards_dir",
"local_views_dir",
"local_reports_dir",
}
# Some cmk.* code is calling things like cmk_version.is_raw_edition() at import time
# (e.g. cmk/base/default_config/notify.py) for edition specific variable
# defaults. In integration tests we want to use the exact version of the
# site. For unit tests we assume we are in Enterprise Edition context.
def fake_version_and_paths() -> None:
if is_running_as_site_user():
return
from pytest import MonkeyPatch # pylint: disable=import-outside-toplevel
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_cloud_repo():
edition_short = "cce"
elif is_enterprise_repo():
edition_short = "cee"
else:
edition_short = "cre"
monkeypatch.setattr(cmk_version, "orig_omd_version", cmk_version.omd_version, raising=False)
monkeypatch.setattr(
cmk_version, "omd_version", lambda: f"{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_cloud_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_cloud_edition", is_cloud_repo)
original_omd_root = Path(cmk.utils.paths.omd_root)
for name, value in vars(cmk.utils.paths).items():
if name.startswith("_") or not isinstance(value, (str, Path)) or name in _UNPATCHED_PATHS:
continue
try:
monkeypatch.setattr(
f"cmk.utils.paths.{name}",
type(value)(tmp_dir / Path(value).relative_to(original_omd_root)),
)
except ValueError:
pass # path is outside of omd_root
# these use cmk_path
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.check_manpages_dir", "%s/checkman" % cmk_path())
monkeypatch.setattr("cmk.utils.paths.web_dir", "%s/web" % cmk_path())
def import_module_hack(pathname: str) -> ModuleType:
"""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: https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
"""
name = os.path.splitext(os.path.basename(pathname))[0]
location = os.path.join(cmk_path(), pathname)
loader = importlib.machinery.SourceFileLoader(name, location)
spec = importlib.machinery.ModuleSpec(name, loader, origin=location)
spec.has_location = True
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
loader.exec_module(module)
return module
def wait_until(condition: Callable[[], bool], timeout: float = 1, interval: float = 0.1) -> None:
start = time.time()
while time.time() - start < timeout:
if condition():
return # Success. Stop waiting...
time.sleep(interval)
raise Exception("Timeout out waiting for %r to finish (Timeout: %d sec)" % (condition, timeout))
def wait_until_liveproxyd_ready(site: Site, site_ids: Collection[str]) -> None:
def _config_available() -> bool:
return site.file_exists("etc/check_mk/liveproxyd.mk")
wait_until(_config_available, timeout=60, interval=0.5)
# First wait for the site sockets to appear
def _all_sockets_opened() -> bool:
return all(site.file_exists("tmp/run/liveproxy/%s" % s) for s in site_ids)
wait_until(_all_sockets_opened, timeout=60, interval=0.5)
# Then wait for the sites to be ready
def _all_sites_ready() -> bool:
content = site.read_file("var/log/liveproxyd.state")
num_ready = content.count("State: ready")
print("%d sites are ready. Waiting for %d sites to be ready." % (num_ready, len(site_ids)))
return len(site_ids) == num_ready
wait_until(_all_sites_ready, timeout=60, interval=0.5)
class WatchLog:
"""Small helper for integration tests: Watch a sites log file"""
def __init__(self, site: Site, default_timeout: int | None = None) -> None:
self._site = site
self._log_path = site.core_history_log()
self._default_timeout = default_timeout or site.core_history_log_timeout()
self._tail_process: subprocess.Popen[str] | None = None
def __enter__(self) -> "WatchLog":
if not self._site.file_exists(self._log_path):
self._site.write_text_file(self._log_path, "")
self._tail_process = self._site.execute(
["tail", "-f", self._site.path(self._log_path)],
stdout=subprocess.PIPE,
bufsize=1, # line buffered
)
# Make stdout non blocking. Otherwise the timeout handling
# in _check_for_line will not work
assert self._tail_process.stdout is not None
fd = self._tail_process.stdout.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
return self
def __exit__(self, *exc_info):
if self._tail_process is not None:
self._tail_process.terminate()
self._tail_process.wait()
self._tail_process = None
def check_logged(self, match_for: str, timeout: float | None = None) -> None:
if timeout is None:
timeout = self._default_timeout
if not self._check_for_line(match_for, timeout):
raise Exception(
"Did not find %r in %s after %d seconds" % (match_for, self._log_path, timeout)
)
def check_not_logged(self, match_for: str, timeout: float | None = None) -> None:
if timeout is None:
timeout = self._default_timeout
if self._check_for_line(match_for, timeout):
raise Exception(
"Found %r in %s after %d seconds" % (match_for, self._log_path, timeout)
)
def _check_for_line(self, match_for: str, timeout: float) -> bool:
if self._tail_process is None:
raise Exception("no log file")
timeout_at = time.time() + timeout
sys.stdout.write(
"Start checking for matching line %r at %d until %d\n"
% (match_for, time.time(), timeout_at)
)
while time.time() < timeout_at:
# print("read till timeout %0.2f sec left" % (timeout_at - time.time()))
assert self._tail_process.stdout is not None
line = self._tail_process.stdout.readline()
if line:
sys.stdout.write("PROCESS LINE: %r\n" % line)
if match_for in line:
return True
time.sleep(0.1)
sys.stdout.write("Timed out at %d\n" % (time.time()))
return False
def create_linux_test_host(request: pytest.FixtureRequest, site: Site, hostname: str) -> None:
def get_data_source_cache_files(name: str) -> list[str]:
p = site.execute(
["ls", f"tmp/check_mk/data_source_cache/*/{name}"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
output = p.communicate()[0].strip()
if not output:
return []
return output.split(" ")
def finalizer() -> None:
site.openapi.delete_host(hostname)
site.activate_changes_and_wait_for_core_reload()
for path in [
"var/check_mk/agent_output/%s" % hostname,
"etc/check_mk/conf.d/linux_test_host_%s.mk" % hostname,
"tmp/check_mk/status_data/%s" % hostname,
"tmp/check_mk/status_data/%s.gz" % hostname,
"var/check_mk/inventory/%s" % hostname,
"var/check_mk/inventory/%s.gz" % hostname,
"var/check_mk/autochecks/%s.mk" % hostname,
"tmp/check_mk/counters/%s" % hostname,
"tmp/check_mk/cache/%s" % hostname,
] + get_data_source_cache_files(hostname):
if site.file_exists(path):
site.delete_file(path)
request.addfinalizer(finalizer)
site.openapi.create_host(hostname, attributes={"ipaddress": "127.0.0.1"})
site.write_text_file(
"etc/check_mk/conf.d/linux_test_host_%s.mk" % hostname,
f"datasource_programs.append({{'condition': {{'hostname': ['{hostname}']}}, 'value': 'cat ~/var/check_mk/agent_output/<HOST>'}})\n",
)
site.makedirs("var/check_mk/agent_output/")
site.write_text_file(
"var/check_mk/agent_output/%s" % hostname, get_standard_linux_agent_output()
)
# .
# .--Checks--------------------------------------------------------------.
# | ____ _ _ |
# | / ___| |__ ___ ___| | _____ |
# | | | | '_ \ / _ \/ __| |/ / __| |
# | | |___| | | | __/ (__| <\__ \ |
# | \____|_| |_|\___|\___|_|\_\___/ |
# | |
# +----------------------------------------------------------------------+
# | Testing of Checkmk checks |
# '----------------------------------------------------------------------'
class MissingCheckInfoError(KeyError):
pass
class BaseCheck(abc.ABC):
"""Abstract base class for Check and ActiveCheck"""
def __init__(self, name: str) -> None:
self.name = name
# we cant use the current_host context, b/c some tests rely on a persistent
# item state across several calls to run_check
import cmk.checkers.plugin_contexts # pylint: disable=import-outside-toplevel
cmk.checkers.plugin_contexts._hostname = HostName("non-existent-testhost")
class Check(BaseCheck):
def __init__(self, name: str) -> None:
import cmk.base.config as config # pylint: disable=import-outside-toplevel
from cmk.base.api.agent_based import register # pylint: disable=import-outside-toplevel
super().__init__(name)
if self.name not in config.check_info:
raise MissingCheckInfoError(self.name)
self.info = config.check_info[self.name]
self._migrated_plugin = register.get_check_plugin(
CheckPluginName(self.name.replace(".", "_"))
)
def default_parameters(self) -> Mapping[str, Any]:
if self._migrated_plugin:
return self._migrated_plugin.check_default_parameters or {}
return {}
def run_parse(self, info):
parse_func = self.info.get("parse_function")
if not parse_func:
raise MissingCheckInfoError("Check '%s' " % self.name + "has no parse function defined")
return parse_func(info)
def run_discovery(self, info):
disco_func = self.info.get("inventory_function")
if not disco_func:
raise MissingCheckInfoError(
"Check '%s' " % self.name + "has no discovery function defined"
)
return disco_func(info)
def run_check(self, item, params, info):
check_func = self.info.get("check_function")
if not check_func:
raise MissingCheckInfoError("Check '%s' " % self.name + "has no check function defined")
return check_func(item, params, info)
class ActiveCheck(BaseCheck):
def __init__(self, name: str) -> None:
import cmk.base.config as config # pylint: disable=import-outside-toplevel
super().__init__(name)
assert self.name.startswith(
"check_"
), "Specify the full name of the active check, e.g. check_http"
self.info = config.active_check_info[self.name[len("check_") :]]
def run_argument_function(self, params):
return self.info["argument_function"](params)
def run_service_description(self, params):
return self.info["service_description"](params)
def run_generate_icmp_services(self, host_config, params):
yield from self.info["service_generator"](host_config, params)
class SpecialAgent:
def __init__(self, name: str) -> None:
import cmk.base.config as config # pylint: disable=import-outside-toplevel
super().__init__()
self.name = name
assert self.name.startswith(
"agent_"
), "Specify the full name of the active check, e.g. agent_3par"
self.argument_func = config.special_agent_info[self.name[len("agent_") :]]
@contextmanager
def set_timezone(timezone: str) -> Iterator[None]:
if "TZ" not in os.environ:
tz_set = False
old_tz = ""
else:
tz_set = True
old_tz = os.environ["TZ"]
os.environ["TZ"] = timezone
time.tzset()
yield
if not tz_set:
del os.environ["TZ"]
else:
os.environ["TZ"] = old_tz
time.tzset()
@contextmanager
def on_time(utctime: datetime.datetime | str | int | float, timezone: str) -> Iterator[None]:
"""Set the time and timezone for the test"""
if isinstance(utctime, (int, float)):
utctime = datetime.datetime.utcfromtimestamp(utctime)
with set_timezone(timezone), freezegun.freeze_time(utctime):
yield
__all__ = [
"cmc_path",
"cme_path",
"cmk_path",
"add_python_paths",
"create_linux_test_host",
"fake_version_and_paths",
"skip_unwanted_test_types",
"wait_until_liveproxyd_ready",
"wait_until",
"on_time",
"set_timezone",
"Site",
"SiteFactory",
"Check",
"MissingCheckInfoError",
"CMKEventConsole",
"CMKEventConsoleStatus",
"import_module_hack",
"APIError",
"CMKWebSession",
"compare_html",
"current_branch_name",
"get_cmk_download_credentials",
"repo_path",
"site_id",
"virtualenv_path",
]