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
|
import pytest
from ansible.playbook.play_context import PlayContext
from openshift_health_check import ActionModule, resolve_checks
from openshift_health_check import copy_remote_file_to_dir, write_result_to_output_dir, write_to_output_file
from openshift_checks import OpenShiftCheckException, FileToSave
def fake_check(name='fake_check', tags=None, is_active=True, run_return=None, run_exception=None,
run_logs=None, run_files=None, changed=False, get_var_return=None):
"""Returns a new class that is compatible with OpenShiftCheck for testing."""
_name, _tags = name, tags
class FakeCheck(object):
name = _name
tags = _tags or []
def __init__(self, **_):
self.changed = False
self.failures = []
self.logs = run_logs or []
self.files_to_save = run_files or []
def is_active(self):
if isinstance(is_active, Exception):
raise is_active
return is_active
def run(self):
self.changed = changed
if run_exception is not None:
raise run_exception
return run_return
def get_var(*args, **_):
return get_var_return
def register_failure(self, exc):
self.failures.append(OpenShiftCheckException(str(exc)))
return
return FakeCheck
# Fixtures
@pytest.fixture
def plugin():
task = FakeTask('openshift_health_check', {'checks': ['fake_check']})
plugin = ActionModule(task, None, PlayContext(), None, None, None)
return plugin
class FakeTask(object):
def __init__(self, action, args):
self.action = action
self.args = args
self.async = 0
@pytest.fixture
def task_vars():
return dict(openshift=dict(), ansible_host='unit-test-host')
# Assertion helpers
def failed(result, msg_has=None):
if msg_has is not None:
assert 'msg' in result
for term in msg_has:
assert term.lower() in result['msg'].lower()
return result.get('failed', False)
def changed(result):
return result.get('changed', False)
# tests whether task is skipped, not individual checks
def skipped(result):
return result.get('skipped', False)
# Tests
@pytest.mark.parametrize('task_vars', [
None,
{},
])
def test_action_plugin_missing_openshift_facts(plugin, task_vars, monkeypatch):
monkeypatch.setattr(plugin, 'load_known_checks', lambda *_: {})
monkeypatch.setattr('openshift_health_check.resolve_checks', lambda *args: ['fake_check'])
result = plugin.run(tmp=None, task_vars=task_vars)
assert failed(result, msg_has=['openshift_facts'])
def test_action_plugin_cannot_load_checks_with_the_same_name(plugin, task_vars, monkeypatch):
FakeCheck1 = fake_check('duplicate_name')
FakeCheck2 = fake_check('duplicate_name')
checks = [FakeCheck1, FakeCheck2]
monkeypatch.setattr('openshift_checks.OpenShiftCheck.subclasses', classmethod(lambda cls: checks))
result = plugin.run(tmp=None, task_vars=task_vars)
assert failed(result, msg_has=['duplicate', 'duplicate_name', 'FakeCheck'])
@pytest.mark.parametrize('is_active, skipped_reason', [
(False, "Not active for this host"),
(Exception("borked"), "exception"),
])
def test_action_plugin_skip_non_active_checks(is_active, skipped_reason, plugin, task_vars, monkeypatch):
checks = [fake_check(is_active=is_active)]
monkeypatch.setattr('openshift_checks.OpenShiftCheck.subclasses', classmethod(lambda cls: checks))
result = plugin.run(tmp=None, task_vars=task_vars)
assert result['checks']['fake_check'].get('skipped')
assert skipped_reason in result['checks']['fake_check'].get('skipped_reason')
assert not failed(result)
assert not changed(result)
assert not skipped(result)
@pytest.mark.parametrize('to_disable', [
'fake_check',
['fake_check', 'spam'],
'*,spam,eggs',
])
def test_action_plugin_skip_disabled_checks(to_disable, plugin, task_vars, monkeypatch):
checks = [fake_check('fake_check', is_active=True)]
monkeypatch.setattr('openshift_checks.OpenShiftCheck.subclasses', classmethod(lambda cls: checks))
task_vars['openshift_disable_check'] = to_disable
result = plugin.run(tmp=None, task_vars=task_vars)
assert result['checks']['fake_check'] == dict(skipped=True, skipped_reason="Disabled by user request")
assert not failed(result)
assert not changed(result)
assert not skipped(result)
def test_action_plugin_run_list_checks(monkeypatch):
task = FakeTask('openshift_health_check', {'checks': []})
plugin = ActionModule(task, None, PlayContext(), None, None, None)
monkeypatch.setattr(plugin, 'load_known_checks', lambda *_: {})
result = plugin.run()
assert failed(result, msg_has="Available checks")
assert not changed(result)
assert not skipped(result)
def test_action_plugin_run_check_ok(plugin, task_vars, monkeypatch):
check_return_value = {'ok': 'test'}
check_class = fake_check(run_return=check_return_value, run_files=[None])
monkeypatch.setattr(plugin, 'load_known_checks', lambda *_: {'fake_check': check_class()})
monkeypatch.setattr('openshift_health_check.resolve_checks', lambda *args: ['fake_check'])
result = plugin.run(tmp=None, task_vars=task_vars)
assert result['checks']['fake_check'] == check_return_value
assert not failed(result)
assert not changed(result)
assert not skipped(result)
def test_action_plugin_run_check_changed(plugin, task_vars, monkeypatch):
check_return_value = {'ok': 'test'}
check_class = fake_check(run_return=check_return_value, changed=True)
monkeypatch.setattr(plugin, 'load_known_checks', lambda *_: {'fake_check': check_class()})
monkeypatch.setattr('openshift_health_check.resolve_checks', lambda *args: ['fake_check'])
result = plugin.run(tmp=None, task_vars=task_vars)
assert result['checks']['fake_check'] == check_return_value
assert changed(result['checks']['fake_check'])
assert not failed(result)
assert changed(result)
assert not skipped(result)
def test_action_plugin_run_check_fail(plugin, task_vars, monkeypatch):
check_return_value = {'failed': True, 'msg': 'this is a failure'}
check_class = fake_check(run_return=check_return_value)
monkeypatch.setattr(plugin, 'load_known_checks', lambda *_: {'fake_check': check_class()})
monkeypatch.setattr('openshift_health_check.resolve_checks', lambda *args: ['fake_check'])
result = plugin.run(tmp=None, task_vars=task_vars)
assert result['checks']['fake_check'] == check_return_value
assert failed(result, msg_has=['failed'])
assert not changed(result)
assert not skipped(result)
@pytest.mark.parametrize('exc_class, expect_traceback', [
(OpenShiftCheckException, False),
(Exception, True),
])
def test_action_plugin_run_check_exception(plugin, task_vars, exc_class, expect_traceback, monkeypatch):
exception_msg = 'fake check has an exception'
run_exception = exc_class(exception_msg)
check_class = fake_check(run_exception=run_exception, changed=True)
monkeypatch.setattr(plugin, 'load_known_checks', lambda *_: {'fake_check': check_class()})
monkeypatch.setattr('openshift_health_check.resolve_checks', lambda *args: ['fake_check'])
result = plugin.run(tmp=None, task_vars=task_vars)
assert failed(result['checks']['fake_check'], msg_has=exception_msg)
assert expect_traceback == ("Traceback" in result['checks']['fake_check']['msg'])
assert failed(result, msg_has=['failed'])
assert changed(result['checks']['fake_check'])
assert changed(result)
assert not skipped(result)
def test_action_plugin_run_check_output_dir(plugin, task_vars, tmpdir, monkeypatch):
check_class = fake_check(
run_return={},
run_logs=[('thing', 'note')],
run_files=[
FileToSave('save.file', 'contents', None),
FileToSave('save.file', 'duplicate', None),
FileToSave('copy.file', None, 'foo'), # note: copy runs execute_module => exception
],
)
task_vars['openshift_checks_output_dir'] = str(tmpdir)
check_class.get_var = lambda self, name, **_: task_vars.get(name)
monkeypatch.setattr(plugin, 'load_known_checks', lambda *_: {'fake_check': check_class()})
monkeypatch.setattr('openshift_health_check.resolve_checks', lambda *args: ['fake_check'])
plugin.run(tmp=None, task_vars=task_vars)
assert any(path.basename == task_vars['ansible_host'] for path in tmpdir.listdir())
assert any(path.basename == 'fake_check.log.json' for path in tmpdir.visit())
assert any(path.basename == 'save.file' for path in tmpdir.visit())
assert any(path.basename == 'save.file.2' for path in tmpdir.visit())
def test_action_plugin_resolve_checks_exception(plugin, task_vars, monkeypatch):
monkeypatch.setattr(plugin, 'load_known_checks', lambda *_: {})
result = plugin.run(tmp=None, task_vars=task_vars)
assert failed(result, msg_has=['unknown', 'name'])
assert not changed(result)
assert not skipped(result)
@pytest.mark.parametrize('names,all_checks,expected', [
([], [], set()),
(
['a', 'b'],
[
fake_check('a'),
fake_check('b'),
],
set(['a', 'b']),
),
(
['a', 'b', '@group'],
[
fake_check('from_group_1', ['group', 'another_group']),
fake_check('not_in_group', ['another_group']),
fake_check('from_group_2', ['preflight', 'group']),
fake_check('a'),
fake_check('b'),
],
set(['a', 'b', 'from_group_1', 'from_group_2']),
),
])
def test_resolve_checks_ok(names, all_checks, expected):
assert resolve_checks(names, all_checks) == expected
@pytest.mark.parametrize('names,all_checks,words_in_exception', [
(
['testA', 'testB'],
[],
['check', 'name', 'testA', 'testB'],
),
(
['@group'],
[],
['tag', 'name', 'group'],
),
(
['testA', 'testB', '@group'],
[],
['check', 'name', 'testA', 'testB', 'tag', 'group'],
),
(
['testA', 'testB', '@group'],
[
fake_check('from_group_1', ['group', 'another_group']),
fake_check('not_in_group', ['another_group']),
fake_check('from_group_2', ['preflight', 'group']),
],
['check', 'name', 'testA', 'testB'],
),
])
def test_resolve_checks_failure(names, all_checks, words_in_exception):
with pytest.raises(Exception) as excinfo:
resolve_checks(names, all_checks)
for word in words_in_exception:
assert word in str(excinfo.value)
@pytest.mark.parametrize('give_output_dir, result, expect_file', [
(False, None, False),
(True, dict(content="c3BhbQo=", encoding="base64"), True),
(True, dict(content="encoding error", encoding="base64"), False),
(True, dict(content="spam", no_encoding=None), True),
(True, dict(failed=True, msg="could not slurp"), False),
])
def test_copy_remote_file_to_dir(give_output_dir, result, expect_file, tmpdir):
check = fake_check()()
check.execute_module = lambda *args, **_: result
copy_remote_file_to_dir(check, "remote_file", str(tmpdir) if give_output_dir else "", "local_file")
assert expect_file == any(path.basename == "local_file" for path in tmpdir.listdir())
def test_write_to_output_exceptions(tmpdir, monkeypatch, capsys):
class Spam(object):
def __str__(self):
raise Exception("break str")
test = {1: object(), 2: Spam()}
test[3] = test
write_result_to_output_dir(str(tmpdir), test)
assert "Error writing" in test["output_files"]
output_dir = tmpdir.join("eggs")
output_dir.write("spam") # so now it's not a dir
write_to_output_file(str(output_dir), "somefile", "somedata")
assert "Could not write" in capsys.readouterr()[1]
monkeypatch.setattr("openshift_health_check.prepare_output_dir", lambda *_: False)
write_result_to_output_dir(str(tmpdir), test)
assert "Error creating" in test["output_files"]
|