-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtest_runtime.py
226 lines (195 loc) · 5.71 KB
/
test_runtime.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
"""
Module with tests for general CLI runtime
"""
import logging
import subprocess
import time
from pathlib import Path
import pytest
from pytest_check import check
from . import utils
from .settings import SETTINGS
LOGGER = logging.getLogger(__name__)
DEFAULT_CMD_TIMEOUT = SETTINGS["cmd_timeout"]
DEFAULT_STARTUP_DELAY = SETTINGS["default_startup_delay"]
DISABLED_RULES_EVENT_DELAY = SETTINGS["disabled_rules_event_delay"]
@pytest.mark.e2e
@pytest.mark.parametrize(
"rulebook, inventory, event_delay, expected_rc",
[
pytest.param(
"hello_events_with_var.yml",
utils.DEFAULT_INVENTORY,
"0.1",
0,
id="successful_rc",
),
pytest.param(
"actions/test_run_playbook.yml",
Path("nonexistent.yml"),
"0.1",
1,
id="failed_rc_wrong_inventory",
),
pytest.param(
"hello_events_with_var.yml",
utils.DEFAULT_INVENTORY,
"notafloat",
1,
id="failed_rc_bad_src_config",
),
pytest.param(
"malformed_rulebook.yml",
utils.DEFAULT_INVENTORY,
"0.1",
1,
id="failed_rc_rulebook_validation",
),
],
)
def test_program_return_code(
update_environment, rulebook, inventory, event_delay, expected_rc
):
"""
Execute a rulebook with various potential failure
scenarios and validate that the return code is correct
"""
env = update_environment({"EVENT_DELAY": event_delay})
rulebook = utils.BASE_DATA_PATH / f"rulebooks/{rulebook}"
cmd = utils.Command(
rulebook=rulebook, envvars="EVENT_DELAY", inventory=inventory
)
LOGGER.info(f"Running command: {cmd}")
result = subprocess.run(
cmd,
timeout=DEFAULT_CMD_TIMEOUT,
cwd=utils.BASE_DATA_PATH,
env=env,
)
assert result.returncode == expected_rc
@pytest.mark.e2e
def test_disabled_rules(update_environment):
"""
Execute a rulebook that has disabled rules and
ensure they do not execute
"""
rulebook = utils.BASE_DATA_PATH / "rulebooks/test_disabled_rules.yml"
env = update_environment(
{
"DEFAULT_STARTUP_DELAY": str(DEFAULT_STARTUP_DELAY),
"DISABLED_RULES_EVENT_DELAY": str(DISABLED_RULES_EVENT_DELAY),
}
)
cmd = utils.Command(
rulebook=rulebook,
envvars="DEFAULT_STARTUP_DELAY,DISABLED_RULES_EVENT_DELAY",
)
LOGGER.info(f"Running command: {cmd}")
result = subprocess.run(
cmd,
timeout=DEFAULT_CMD_TIMEOUT,
capture_output=True,
cwd=utils.BASE_DATA_PATH,
text=True,
env=env,
)
with check:
assert (
"Enabled rule fired correctly" in result.stdout
), "Enabled rule failed to fire"
with check:
assert (
"Disabled rule should not have fired" not in result.stdout
), "A disabled rule fired unexpectedly"
@pytest.mark.e2e
@pytest.mark.parametrize(
"shutdown_delay",
[
pytest.param(
None,
id="default_timeout",
),
pytest.param(
5,
id="custom_timeout",
),
],
)
def test_terminate_process_source_end(update_environment, shutdown_delay):
"""
Execute a rulebook and ensure ansible-rulebook terminates correctly
after an event source plugin ends
"""
if shutdown_delay:
env = update_environment({"EDA_SHUTDOWN_DELAY": str(shutdown_delay)})
rulebook = utils.BASE_DATA_PATH / "rulebooks/test_process_source_end.yml"
cmd = utils.Command(rulebook=rulebook, execution_strategy="parallel")
LOGGER.info(f"Running command: {cmd}")
result = subprocess.run(
cmd,
timeout=DEFAULT_CMD_TIMEOUT,
capture_output=True,
cwd=utils.BASE_DATA_PATH,
text=True,
env=env if shutdown_delay else None,
)
assert result.returncode == 0
assert not result.stderr
with check:
assert (
len(
[
line
for line in result.stdout.splitlines()
if '"msg": "Sleeping..."' in line
]
)
== 1
), "long-running playbook failed to fire"
with check:
assert (
len(
[
line
for line in result.stdout.splitlines()
if "Parallel action triggered successfully" in line
]
)
== 3
), "a parallel action failed to fire"
with check:
assert (
len(
[
line
for line in result.stdout.splitlines()
if '"msg": "Rise and shine..."' in line
]
)
== 1
), "long-running playbook failed to finish"
@pytest.mark.e2e
def test_terminate_process_sigint():
"""
Execute a rulebook and ensure ansible-rulebook terminates correctly
after a SIGINT is issued
"""
rulebook = utils.BASE_DATA_PATH / "rulebooks/test_process_sigint.yml"
cmd = utils.Command(rulebook=rulebook)
LOGGER.info(f"Running command: {cmd}")
process = subprocess.Popen(
cmd,
cwd=utils.BASE_DATA_PATH,
text=True,
stdout=subprocess.PIPE,
)
start = time.time()
while line := process.stdout.readline():
if "'action': 'long_loop'" in line:
process.send_signal(subprocess.signal.SIGINT)
process.wait()
break
time.sleep(0.1)
if time.time() - start > DEFAULT_CMD_TIMEOUT:
process.kill()
assert process.returncode == 130