forked from ansible/ansible-runner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_runner.py
282 lines (232 loc) · 8.37 KB
/
test_runner.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
# -*- coding: utf-8 -*-
import json
import os
import re
import pytest
import six
import sys
from ansible_runner import Runner
from ansible_runner.exceptions import AnsibleRunnerException
from test.utils.common import iterate_timeout
def test_password_prompt(rc):
rc.command = [sys.executable, '-c' 'import time; print(input("Password: "))']
rc.expect_passwords[re.compile(r'Password:\s*?$', re.M)] = '1234'
status, exitcode = Runner(config=rc).run()
assert status == 'successful'
assert exitcode == 0
# stdout file can be subject to a race condition
for _ in iterate_timeout(30.0, 'stdout file to be written with 1234 in it', interval=0.2):
with open(os.path.join(rc.artifact_dir, 'stdout')) as f:
if '1234' in f.read():
break
def test_run_command(rc):
rc.command = ['sleep', '1']
status, exitcode = Runner(config=rc).run()
assert status == 'successful'
assert exitcode == 0
with open(os.path.join(rc.artifact_dir, 'command')) as f:
data = json.load(f)
assert data.get('command') == ['sleep', '1']
assert 'cwd' in data
assert isinstance(data.get('env'), dict)
def test_run_command_with_unicode(rc):
expected = '"utf-8-䉪ቒ칸ⱷ?噂폄蔆㪗輥"'
if six.PY2:
expected = expected.decode('utf-8')
rc.command = ['echo', '"utf-8-䉪ቒ칸ⱷ?噂폄蔆㪗輥"']
rc.envvars = {"䉪ቒ칸": "蔆㪗輥"}
rc.prepare_env()
status, exitcode = Runner(config=rc).run()
assert status == 'successful'
assert exitcode == 0
with open(os.path.join(rc.artifact_dir, 'command')) as f:
data = json.load(f)
assert data.get('command') == ['echo', expected]
assert 'cwd' in data
assert isinstance(data.get('env'), dict)
assert u"䉪ቒ칸" in data.get('env')
def test_run_command_finished_callback(rc, mocker):
finished_callback = mocker.MagicMock()
rc.command = ['sleep', '1']
runner = Runner(config=rc, finished_callback=finished_callback)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
finished_callback.assert_called_with(runner)
def test_run_command_explosive_finished_callback(rc):
def boom(*args):
raise Exception('boom')
rc.command = ['sleep', '1']
runner = Runner(config=rc, finished_callback=boom)
with pytest.raises(Exception):
runner.run()
def test_run_command_explosive_cancel_callback(rc):
def boom(*args):
raise Exception('boom')
rc.command = ['sleep', '1']
runner = Runner(config=rc, cancel_callback=boom)
with pytest.raises(Exception):
runner.run()
def test_run_command_cancel_callback(rc):
def cancel(*args):
return True
rc.command = ['sleep', '1']
runner = Runner(config=rc, cancel_callback=cancel)
status, exitcode = runner.run()
assert status == 'canceled'
assert exitcode == 254
def test_run_command_job_timeout(rc):
rc.command = ['sleep', '1']
rc.job_timeout = 0.0000001
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'timeout'
assert exitcode == 254
def test_run_command_idle_timeout(rc):
rc.command = ['sleep', '1']
rc.idle_timeout = 0.0000001
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'timeout'
assert exitcode == 254
def test_run_command_failed(rc):
rc.command = ['false']
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'failed'
assert exitcode == 1
def test_executable_not_found(rc):
rc.command = ['supercalifragilistic']
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'failed'
assert exitcode == 127
events = list(runner.events)
assert len(events) == 1
assert 'The command was not found or was not executable: supercalifragilistic' in events[0]['stdout'] # noqa
def test_run_command_long_running(rc):
rc.command = ['yes']
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'timeout'
assert exitcode == 254
def test_run_command_long_running_children(rc):
rc.command = ['bash', '-c', "(yes)"]
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'timeout'
assert exitcode == 254
def test_run_command_events_missing(rc):
rc.command = ['sleep', '1']
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
assert list(runner.events) == []
def test_run_command_stdout_missing(rc):
rc.command = ['sleep', '1']
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
os.unlink(os.path.join(runner.config.artifact_dir, 'stdout'))
with pytest.raises(AnsibleRunnerException):
list(runner.stdout)
def test_run_command_no_stats(rc):
rc.command = ['sleep', '1']
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
assert runner.stats is None
def test_run_command_ansible(rc):
rc.module = "debug"
rc.host_pattern = "localhost"
rc.prepare()
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
assert list(runner.events) != []
assert runner.stats != {}
assert list(runner.host_events('localhost')) != []
stdout = runner.stdout
assert stdout.read() != ""
def test_run_command_ansible_event_handler(rc, mocker):
event_handler = mocker.MagicMock()
status_handler = mocker.MagicMock()
rc.module = "debug"
rc.host_pattern = "localhost"
rc.prepare()
runner = Runner(config=rc, event_handler=event_handler, status_handler=status_handler)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
event_handler.assert_called()
status_handler.assert_called()
def test_run_command_ansible_event_handler_failure(rc):
def event_handler(*args):
raise IOError()
rc.module = "debug"
rc.host_pattern = "localhost"
rc.prepare()
runner = Runner(config=rc, event_handler=event_handler)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
def test_run_command_ansible_rotate_artifacts(rc):
rc.module = "debug"
rc.host_pattern = "localhost"
rc.prepare()
rc.rotate_artifacts = 1
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
def test_get_fact_cache(rc):
assert os.path.basename(rc.fact_cache) == 'fact_cache'
rc.module = "setup"
rc.host_pattern = "localhost"
rc.prepare()
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
print(rc.cwd)
assert os.path.exists(os.path.join(rc.artifact_dir, 'fact_cache'))
assert os.path.exists(os.path.join(rc.artifact_dir, 'fact_cache', 'localhost'))
data = runner.get_fact_cache('localhost')
assert data
def test_set_fact_cache(rc):
assert os.path.basename(rc.fact_cache) == 'fact_cache'
rc.module = "debug"
rc.module_args = "var=message"
rc.host_pattern = "localhost"
rc.prepare()
runner = Runner(config=rc)
runner.set_fact_cache('localhost', dict(message='hello there'))
status, exitcode = runner.run()
assert status == 'successful'
assert exitcode == 0
print(rc.cwd)
assert os.path.exists(os.path.join(rc.artifact_dir, 'fact_cache'))
assert os.path.exists(os.path.join(rc.artifact_dir, 'fact_cache', 'localhost'))
data = runner.get_fact_cache('localhost')
assert data['message'] == 'hello there'
def test_set_extra_vars(rc):
rc.module = "debug"
rc.module_args = "var=test_extra_vars"
rc.host_pattern = "localhost"
rc.extra_vars = dict(test_extra_vars='hello there')
rc.prepare()
runner = Runner(config=rc)
status, exitcode = runner.run()
# stdout file can be subject to a race condition
for _ in iterate_timeout(30.0, 'stdout file to be written with "hello there" in it', interval=0.2):
with open(os.path.join(rc.artifact_dir, 'stdout')) as f:
if 'hello there' in f.read():
break