/
test_pylint.py
executable file
·175 lines (142 loc) · 5.34 KB
/
test_pylint.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
# encoding: utf-8
# pylint: disable=redefined-outer-name
from __future__ import print_function
import os
import sys
import tempfile
import shutil
import pytest # type: ignore
from testlib import (
cmk_path,
repo_path,
is_enterprise_repo,
is_managed_repo,
)
import testlib.pylint_cmk as pylint_cmk
@pytest.fixture(scope="function")
def pylint_test_dir():
base_path = os.environ.get("WORKDIR")
if base_path:
base_path += "/" + os.path.basename(sys.argv[0])
if not os.path.exists(base_path):
os.makedirs(base_path)
else:
base_path = None
test_dir = tempfile.mkdtemp(prefix="cmk_pylint_", dir=base_path)
print("Prepare check in %s ..." % test_dir)
yield test_dir
#
# Cleanup code
#
print("Cleanup pylint test dir %s ..." % test_dir)
shutil.rmtree(test_dir)
def test_pylint(pylint_test_dir):
# Only specify the path to python packages or modules here
if sys.version_info[0] >= 3:
modules_or_packages = []
else:
modules_or_packages = [
"omd/packages/omd/omdlib",
"livestatus/api/python/livestatus.py",
"cmk_base",
"cmk",
"web/app/index.wsgi",
]
if is_enterprise_repo():
modules_or_packages += [
# TODO: Check if this kind of "overlay" really works.
# TODO: Why do we have e.g. a symlink cmk_base/cee -> enterprise/cmk_base/cee?
"enterprise/cmk_base/automations/cee.py",
"enterprise/cmk_base/cee",
"enterprise/cmk_base/default_config/cee.py",
"enterprise/cmk_base/modes/cee.py",
# TODO: Funny links there, see above.
"enterprise/cmk/cee",
"enterprise/cmk/gui/cee",
]
if is_managed_repo():
modules_or_packages += [
"managed/cmk_base/default_config/cme.py",
"managed/cmk/gui/cme",
]
# Add the compiled files for things that are no modules yet
open(pylint_test_dir + "/__init__.py", "w")
_compile_check_and_inventory_plugins(pylint_test_dir)
if is_enterprise_repo():
_compile_bakery_plugins(pylint_test_dir)
# Not checking compiled check, inventory, bakery plugins with Python 3
if sys.version_info[0] == 2:
modules_or_packages += [
pylint_test_dir,
]
# We use our own search logic to find scripts without python extension
search_paths = [
"omd/packages/omd.bin",
"bin",
"notifications",
"agents/plugins",
"agents/special",
"active_checks",
]
if is_enterprise_repo():
search_paths += [
"enterprise/agents/plugins",
"enterprise/bin",
"enterprise/misc",
]
for path in search_paths:
abs_path = cmk_path() + "/" + path
for fname in pylint_cmk.get_pylint_files(abs_path, "*"):
modules_or_packages.append(path + "/" + fname)
exit_code = pylint_cmk.run_pylint(cmk_path(), modules_or_packages)
assert exit_code == 0, "PyLint found an error"
def _compile_check_and_inventory_plugins(pylint_test_dir):
with open(pylint_test_dir + "/cmk_checks.py", "w") as f:
# Fake data structures where checks register (See cmk_base/checks.py)
f.write("""
# -*- encoding: utf-8 -*-
check_info = {}
check_includes = {}
precompile_params = {}
check_default_levels = {}
factory_settings = {}
check_config_variables = []
snmp_info = {}
snmp_scan_functions = {}
active_check_info = {}
special_agent_info = {}
inv_info = {} # Inventory plugins
inv_export = {} # Inventory export hooks
def inv_tree_list(path):
return inv_tree(path, [])
def inv_tree(path, default_value=None):
if default_value is not None:
node = default_value
else:
node = {}
return node
""")
# add the modules
# These pylint warnings are incompatible with our "concatenation technology".
f.write(
"# pylint: disable=reimported,ungrouped-imports,wrong-import-order,wrong-import-position,redefined-outer-name\n"
)
pylint_cmk.add_file(f, repo_path() + "/cmk_base/check_api.py")
pylint_cmk.add_file(f, repo_path() + "/cmk_base/inventory_plugins.py")
# Now add the checks
for path in pylint_cmk.check_files(repo_path() + "/checks"):
pylint_cmk.add_file(f, path)
# Now add the inventory plugins
for path in pylint_cmk.check_files(repo_path() + "/inventory"):
pylint_cmk.add_file(f, path)
def _compile_bakery_plugins(pylint_test_dir):
with open(pylint_test_dir + "/cmk_bakery_plugins.py", "w") as f:
pylint_cmk.add_file(
f,
os.path.realpath(
os.path.join(cmk_path(), "enterprise/cmk_base/cee/agent_bakery_plugins.py")))
# This pylint warning is incompatible with our "concatenation technology".
f.write("# pylint: disable=reimported,wrong-import-order,wrong-import-position\n")
# Also add bakery plugins
for path in pylint_cmk.check_files(os.path.join(cmk_path(), "enterprise/agents/bakery")):
pylint_cmk.add_file(f, path)