/
test_checks.py
353 lines (280 loc) · 14.2 KB
/
test_checks.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
#!/usr/bin/env python3
# -*- 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 re
from pathlib import Path
import pytest # type: ignore[import]
import testlib # type: ignore[import]
from testlib.base import Scenario # type: ignore[import]
import cmk.utils.paths
import cmk.base.config as config
import cmk.base.check_utils
import cmk.base.check_api as check_api
def _search_deprecated_api_feature(check_file_path, deprecated_pattern):
try:
with check_file_path.open() as handle:
return [
"%s:%d:%s" % (check_file_path.name, line_no, repr(line.strip()))
for line_no, line in enumerate(handle, 1)
if re.search(deprecated_pattern, line.strip())
]
except UnicodeDecodeError as exc:
return ["%s:-1:Unable to reade file: %s" % (check_file_path.name, exc)]
@pytest.mark.parametrize("deprecated_pattern", [
r"\bservice_description\(",
r"\bOID_BIN\b",
r"\bOID_STRING\b",
r"\bOID_END_BIN\b",
r"\bOID_END_OCTET_STRING\b",
r"\ball_matching_hosts\b",
r"\bbinstring_to_int\b",
r"\bcheck_type\b",
r"\bcore_state_names\b",
r"\bget_http_proxy\b",
r"\bhosttags_match_taglist\b",
r"\bin_extraconf_hostlist\b",
r"\bis_cmc\b",
r"\bnagios_illegal_chars\b",
r"\bquote_shell_string\b",
r"\btags_of_host\b",
])
def test_deprecated_api_features(deprecated_pattern):
check_files = Path(cmk.utils.paths.checks_dir).glob("*")
check_files = (p for p in check_files if p.suffix not in (".swp",))
with_deprecated_feature = [
finding #
for check_file_path in check_files #
for finding in _search_deprecated_api_feature(check_file_path, deprecated_pattern)
]
assert not with_deprecated_feature, "Found %d deprecated API name '%r' usages:\n%s" % (
len(with_deprecated_feature),
deprecated_pattern,
"\n".join(with_deprecated_feature),
)
def _search_from_imports(check_file_path):
with open(check_file_path) as f_:
return [
"%s:%d:%s" % (Path(check_file_path).stem, line_no, repr(line.strip()))
for line_no, line in enumerate(f_.readlines(), 1)
if re.search(r'from\s.*\simport\s', line.strip())
]
def test_imports_in_checks():
check_files = config.get_plugin_paths(cmk.utils.paths.checks_dir)
with_from_imports = [
finding #
for check_file_path in check_files #
for finding in _search_from_imports(check_file_path)
]
assert not with_from_imports, "Found %d from-imports:\n%s" % (len(with_from_imports),
"\n".join(with_from_imports))
def test_is_snmp_check():
config.load_all_checks(check_api.get_check_api_context)
assert cmk.base.check_utils.is_snmp_check("xxx") is False
assert cmk.base.check_utils.is_snmp_check("uptime") is False
assert cmk.base.check_utils.is_snmp_check("uptime") is False
assert cmk.base.check_utils.is_snmp_check("snmp_uptime") is True
assert cmk.base.check_utils.is_snmp_check("mem") is False
assert cmk.base.check_utils.is_snmp_check("mem.linux") is False
assert cmk.base.check_utils.is_snmp_check("mem.ding") is False
assert cmk.base.check_utils.is_snmp_check("apc_humidity") is True
assert cmk.base.check_utils.is_snmp_check("brocade.power") is True
assert cmk.base.check_utils.is_snmp_check("brocade.fan") is True
assert cmk.base.check_utils.is_snmp_check("brocade.xy") is True
assert cmk.base.check_utils.is_snmp_check("brocade") is True
# ########### Management board checks
def _check_plugins():
return {
"tcp_check_mgmt_only": "mgmt_only",
"tcp_check_host_precedence": "host_precedence",
"tcp_check_host_only": "host_only",
"snmp_check_mgmt_only": "mgmt_only",
"snmp_check_host_precedence": "host_precedence",
"snmp_check_host_only": "host_only",
}
@pytest.fixture()
def patch_mgmt_board_plugins(monkeypatch):
monkeypatch.setattr(config, "get_management_board_precedence", lambda c, _: _check_plugins()[c])
monkeypatch.setattr(cmk.base.check_utils, "is_snmp_check", lambda c: c.startswith("snmp_"))
# ########### Unknown check plugins
@pytest.mark.usefixtures("patch_mgmt_board_plugins")
@pytest.mark.parametrize("for_discovery,result", [
(False, []),
(True, []),
])
def test_filter_by_management_board_unknown_check_plugins(monkeypatch, for_discovery, result):
ts = Scenario()
ts.add_host("this_host")
ts.apply(monkeypatch)
found_check_plugins = set(_check_plugins())
monkeypatch.setattr(config, "check_info", [])
assert config.filter_by_management_board("this_host",
found_check_plugins,
False,
for_discovery=for_discovery) == set(result)
# ########### TCP host
@pytest.mark.usefixtures("patch_mgmt_board_plugins")
@pytest.mark.parametrize("for_discovery,result", [
(False, ["tcp_check_host_precedence", "tcp_check_host_only"]),
(True, ["tcp_check_host_precedence", "tcp_check_host_only"]),
])
def test_filter_by_management_board_TCP_host_without_mgmt_board(monkeypatch, for_discovery, result):
ts = Scenario()
ts.add_host("this_host")
ts.apply(monkeypatch)
found_check_plugins = set(c for c in _check_plugins() if c.startswith("tcp_"))
monkeypatch.setattr(config, "check_info", found_check_plugins)
assert config.filter_by_management_board("this_host",
found_check_plugins,
False,
for_discovery=for_discovery) == set(result)
# ########### SNMP host
@pytest.mark.usefixtures("patch_mgmt_board_plugins")
@pytest.mark.parametrize("for_discovery,result", [
(False, ["snmp_check_host_precedence", "snmp_check_host_only"]),
(True, ["snmp_check_host_precedence", "snmp_check_host_only"]),
])
def test_filter_by_management_board_SNMP_host_without_mgmt_board(monkeypatch, for_discovery,
result):
ts = Scenario()
ts.add_host("this_host", tags={"snmp_ds": "snmp-v1", "agent": "no-agent"})
ts.apply(monkeypatch)
found_check_plugins = set(c for c in _check_plugins() if c.startswith("snmp_"))
monkeypatch.setattr(config, "check_info", found_check_plugins)
assert config.filter_by_management_board("this_host",
found_check_plugins,
False,
for_discovery=for_discovery) == set(result)
# ########### Dual host
@pytest.mark.usefixtures("patch_mgmt_board_plugins")
@pytest.mark.parametrize("for_discovery,result", [
(False, [
"tcp_check_host_precedence", "tcp_check_host_only", "snmp_check_host_precedence",
"snmp_check_host_only"
]),
(True, [
"tcp_check_host_precedence", "tcp_check_host_only", "snmp_check_host_precedence",
"snmp_check_host_only"
]),
])
def test_filter_by_management_board_dual_host_without_mgmt_board(monkeypatch, for_discovery,
result):
ts = Scenario()
ts.add_host("this_host", tags={"snmp_ds": "snmp-v1", "agent": "cmk-agent"})
ts.apply(monkeypatch)
found_check_plugins = set(_check_plugins())
monkeypatch.setattr(config, "check_info", found_check_plugins)
assert config.filter_by_management_board("this_host",
found_check_plugins,
False,
for_discovery=for_discovery) == set(result)
# ########### TCP host + SNMP Management Board
@pytest.mark.usefixtures("patch_mgmt_board_plugins")
@pytest.mark.parametrize("for_discovery,host_result,mgmt_board_result", [
(False, ["tcp_check_host_precedence", "tcp_check_host_only"
], ["snmp_check_mgmt_only", "snmp_check_host_precedence", "snmp_check_host_only"]),
(True, ["tcp_check_host_precedence", "tcp_check_host_only"
], ["snmp_check_mgmt_only", "snmp_check_host_precedence"]),
])
def test_filter_by_management_board_TCP_host_with_SNMP_mgmt_board(monkeypatch, for_discovery,
host_result, mgmt_board_result):
ts = Scenario()
ts.add_host("this_host", tags={
"agent": "cmk-agent",
})
config_cache = ts.apply(monkeypatch)
h = config_cache.get_host_config("this_host")
h.has_management_board = True
found_check_plugins = set(c for c in _check_plugins() if c.startswith("tcp_"))
monkeypatch.setattr(config, "check_info", found_check_plugins)
assert config.filter_by_management_board("this_host",
found_check_plugins,
False,
for_discovery=for_discovery) == set(host_result)
found_check_plugins = set(c for c in _check_plugins() if c.startswith("snmp_"))
monkeypatch.setattr(config, "check_info", found_check_plugins)
assert config.filter_by_management_board("this_host",
found_check_plugins,
True,
for_discovery=for_discovery) == set(mgmt_board_result)
# ########### SNMP host + SNMP Management Board
@pytest.mark.usefixtures("patch_mgmt_board_plugins")
@pytest.mark.parametrize("for_discovery,host_result,mgmt_board_result", [
(False, ["snmp_check_host_only", "snmp_check_host_precedence"], ["snmp_check_mgmt_only"]),
(True, ["snmp_check_host_only", "snmp_check_host_precedence"], ["snmp_check_mgmt_only"]),
])
def test_filter_by_management_board_SNMP_host_with_SNMP_mgmt_board(monkeypatch, for_discovery,
host_result, mgmt_board_result):
ts = Scenario()
ts.add_host("this_host", tags={"snmp_ds": "snmp-v1", "agent": "no-agent"})
config_cache = ts.apply(monkeypatch)
h = config_cache.get_host_config("this_host")
h.has_management_board = True
found_check_plugins = set(c for c in _check_plugins() if c.startswith("snmp_"))
monkeypatch.setattr(config, "check_info", found_check_plugins)
assert config.filter_by_management_board("this_host",
found_check_plugins,
False,
for_discovery=for_discovery) == set(host_result)
found_check_plugins = set(c for c in _check_plugins() if c.startswith("snmp_"))
monkeypatch.setattr(config, "check_info", found_check_plugins)
assert config.filter_by_management_board("this_host",
found_check_plugins,
True,
for_discovery=for_discovery) == set(mgmt_board_result)
# ########### Dual host + SNMP Management Board
@pytest.mark.usefixtures("patch_mgmt_board_plugins")
@pytest.mark.parametrize("for_discovery,host_result,mgmt_board_result", [
(False, [
"tcp_check_host_precedence", "tcp_check_host_only", "snmp_check_host_only",
"snmp_check_host_precedence"
], ["snmp_check_mgmt_only"]),
(True, [
"tcp_check_host_precedence", "tcp_check_host_only", "snmp_check_host_only",
"snmp_check_host_precedence"
], ["snmp_check_mgmt_only"]),
])
def test_filter_by_management_board_dual_host_with_SNMP_mgmt_board(monkeypatch, for_discovery,
host_result, mgmt_board_result):
ts = Scenario()
ts.add_host("this_host", tags={"snmp_ds": "snmp-v1", "agent": "cmk-agent"})
config_cache = ts.apply(monkeypatch)
h = config_cache.get_host_config("this_host")
h.has_management_board = True
found_check_plugins = set(_check_plugins())
monkeypatch.setattr(config, "check_info", found_check_plugins)
assert config.filter_by_management_board("this_host",
found_check_plugins,
False,
for_discovery=for_discovery) == set(host_result)
found_check_plugins = set(c for c in _check_plugins() if c.startswith("snmp_"))
monkeypatch.setattr(config, "check_info", found_check_plugins)
assert config.filter_by_management_board("this_host",
found_check_plugins,
True,
for_discovery=for_discovery) == set(mgmt_board_result)
def test_check_plugin_header():
for checkfile in Path(testlib.repo_path()).joinpath(Path('checks')).iterdir():
if checkfile.name.startswith("."):
# .f12
continue
with checkfile.open() as f:
shebang = f.readline().strip()
encoding_header = f.readline().strip()
assert shebang == "#!/usr/bin/env python3", "Check plugin '%s' has wrong shebang '%s'" % (
checkfile.name, shebang)
assert encoding_header == "# -*- coding: utf-8 -*-", "Check plugin '%s' has wrong encoding header '%s'" % (
checkfile.name, encoding_header)
def test_inventory_plugin_header():
for inventory_pluginfile in Path(testlib.repo_path()).joinpath(Path('inventory')).iterdir():
if inventory_pluginfile.name.startswith("."):
# .f12
continue
with inventory_pluginfile.open() as f:
shebang = f.readline().strip()
encoding_header = f.readline().strip()
assert shebang == "#!/usr/bin/env python3", "Inventory plugin '%s' has wrong shebang '%s'" % (
inventory_pluginfile.name, shebang)
assert encoding_header == "# -*- coding: utf-8 -*-", "Inventory plugin '%s' has wrong encoding header '%s'" % (
inventory_pluginfile.name, encoding_header)