-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_executor.py
268 lines (208 loc) · 8.93 KB
/
test_executor.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
# mypy: no-strict-optional
"""Test basic executor functionality."""
import gc
import shlex
import signal
from subprocess import check_output
import uuid
from unittest import mock
from typing import Union, List
import pytest
from mirakuru import Executor
from mirakuru.base import SimpleExecutor
from mirakuru.exceptions import ProcessExitedWithError, TimeoutExpired
from tests import SAMPLE_DAEMON_PATH, ps_aux
from tests.retry import retry
SLEEP_300 = "sleep 300"
@pytest.mark.parametrize("command", (SLEEP_300, SLEEP_300.split()))
def test_running_process(command: Union[str, List[str]]) -> None:
"""Start process and shuts it down."""
executor = SimpleExecutor(command)
executor.start()
assert executor.running() is True
executor.stop()
assert executor.running() is False
# check proper __str__ and __repr__ rendering:
assert "SimpleExecutor" in repr(executor)
assert SLEEP_300 in str(executor)
@pytest.mark.parametrize("command", (SLEEP_300, SLEEP_300.split()))
def test_command(command: Union[str, List[str]]) -> None:
"""Check that the command and command parts are equivalent."""
executor = SimpleExecutor(command)
assert executor.command == SLEEP_300
assert executor.command_parts == SLEEP_300.split()
def test_custom_signal_stop() -> None:
"""Start process and shuts it down using signal SIGQUIT."""
executor = SimpleExecutor(SLEEP_300, stop_signal=signal.SIGQUIT)
executor.start()
assert executor.running() is True
executor.stop()
assert executor.running() is False
def test_stop_custom_signal_stop() -> None:
"""Start process and shuts it down using signal SIGQUIT passed to stop."""
executor = SimpleExecutor(SLEEP_300)
executor.start()
assert executor.running() is True
executor.stop(stop_signal=signal.SIGQUIT)
assert executor.running() is False
def test_stop_custom_exit_signal_stop() -> None:
"""Start process and expect it to finish with custom signal."""
executor = SimpleExecutor("false", shell=True)
executor.start()
# false exits instant, so there should not be a process to stop
retry(
lambda: executor.stop(
stop_signal=signal.SIGQUIT, expected_returncode=-3
)
)
assert executor.running() is False
def test_stop_custom_exit_signal_context() -> None:
"""Start process and expect custom exit signal in context manager."""
with SimpleExecutor(
"false", expected_returncode=-3, shell=True
) as executor:
executor.stop(stop_signal=signal.SIGQUIT)
assert executor.running() is False
def test_running_context() -> None:
"""Start process and shuts it down."""
executor = SimpleExecutor(SLEEP_300)
with executor:
assert executor.running() is True
assert executor.running() is False
def test_executor_in_context_only() -> None:
"""Start process and shuts it down only in context."""
with SimpleExecutor(SLEEP_300) as executor:
assert executor.running() is True
def test_context_stopped() -> None:
"""Start for context, and shuts it for nested context."""
executor = SimpleExecutor(SLEEP_300)
with executor:
assert executor.running() is True
with executor.stopped():
assert executor.running() is False
assert executor.running() is True
assert executor.running() is False
ECHO_FOOBAR = 'echo "foobar"'
@pytest.mark.parametrize("command", (ECHO_FOOBAR, shlex.split(ECHO_FOOBAR)))
def test_process_output(command: Union[str, List[str]]) -> None:
"""Start process, check output and shut it down."""
executor = SimpleExecutor(command)
executor.start()
assert executor.output().read() == "foobar\n"
executor.stop()
@pytest.mark.parametrize("command", (ECHO_FOOBAR, shlex.split(ECHO_FOOBAR)))
def test_process_output_shell(command: Union[str, List[str]]) -> None:
"""Start process, check output and shut it down with shell set to True."""
executor = SimpleExecutor(command, shell=True)
executor.start()
assert executor.output().read().strip() == "foobar"
executor.stop()
def test_start_check_executor() -> None:
"""Validate Executor base class having NotImplemented methods."""
executor = Executor(SLEEP_300)
with pytest.raises(NotImplementedError):
executor.pre_start_check()
with pytest.raises(NotImplementedError):
executor.after_start_check()
def test_stopping_not_yet_running_executor() -> None:
"""
Test if SimpleExecutor can be stopped even it was never running.
We must make sure that it's possible to call .stop() and SimpleExecutor
will not raise any exception and .start() can be called afterwards.
"""
executor = SimpleExecutor(SLEEP_300)
executor.stop()
executor.start()
assert executor.running() is True
executor.stop()
def test_forgotten_stop() -> None:
"""
Test if SimpleExecutor subprocess is killed after an instance is deleted.
Existence can end because of context scope end or by calling 'del'.
If someone forgot to stop() or kill() subprocess it should be killed
by default on instance cleanup.
"""
mark = str(uuid.uuid1())
# We cannot simply do `sleep 300 #<our-uuid>` in a shell because in that
# case bash (default shell on some systems) does `execve` without cloning
# itself - that means there will be no process with commandline like:
# '/bin/sh -c sleep 300 && true #<our-uuid>' - instead that process would
# get substituted with 'sleep 300' and the marked commandline would be
# overwritten.
# Injecting some flow control (`&&`) forces bash to fork properly.
marked_command = f"sleep 300 && true #{mark!s}"
executor = SimpleExecutor(marked_command, shell=True)
executor.start()
assert executor.running() is True
assert mark in ps_aux(), "The test process should be running."
del executor
gc.collect() # to force 'del' immediate effect
assert (
mark not in ps_aux()
), "The test process should not be running at this point."
def test_executor_raises_if_process_exits_with_error() -> None:
"""
Test process exit detection.
If the process exits with an error while checks are being polled, executor
should raise an exception.
"""
error_code = 12
failing_executor = Executor(
["bash", "-c", f"exit {error_code!s}"], timeout=5
)
failing_executor.pre_start_check = mock.Mock( # type: ignore
return_value=False
)
# After-start check will keep returning False to let the process terminate.
failing_executor.after_start_check = mock.Mock( # type: ignore
return_value=False
)
with pytest.raises(ProcessExitedWithError) as exc:
failing_executor.start()
assert exc.value.exit_code == 12
error_msg = f"exited with a non-zero code: {error_code!s}"
assert error_msg in str(exc.value)
# Pre-start check should have been called - after-start check might or
# might not have been called - depending on the timing.
assert failing_executor.pre_start_check.called is True
def test_executor_ignores_processes_exiting_with_0() -> None:
"""
Test process exit detection.
Subprocess exiting with zero should be tolerated in order to support
double-forking applications.
"""
# We execute a process that will return zero. In order to give the process
# enough time to return we keep the polling loop spinning for a second.
executor = Executor(["bash", "-c", "exit 0"], timeout=1.0)
executor.pre_start_check = mock.Mock(return_value=False) # type: ignore
executor.after_start_check = mock.Mock(return_value=False) # type: ignore
with pytest.raises(TimeoutExpired):
# We keep the post-checks spinning forever so it eventually times out.
executor.start()
# Both checks should have been called.
assert executor.pre_start_check.called is True
assert executor.after_start_check.called is True
def test_executor_methods_returning_self() -> None:
"""Test if SimpleExecutor lets to chain start, stop and kill methods."""
executor = SimpleExecutor(SLEEP_300).start().stop().kill().stop()
assert not executor.running()
# Check if context manager returns executor to use it in 'as' phrase:
with SimpleExecutor(SLEEP_300) as executor:
assert executor.running()
with SimpleExecutor(SLEEP_300).start().stopped() as executor:
assert not executor.running()
assert SimpleExecutor(SLEEP_300).start().stop().output
def test_mirakuru_cleanup() -> None:
"""Test if cleanup_subprocesses is fired correctly on python exit."""
cmd = f"""
python -c 'from mirakuru import SimpleExecutor;
from time import sleep;
import gc;
gc.disable();
ex = SimpleExecutor(
("python", "{SAMPLE_DAEMON_PATH}")).start();
sleep(1);
'
"""
check_output(shlex.split(cmd.replace("\n", "")))
assert SAMPLE_DAEMON_PATH not in ps_aux()