-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
281 lines (230 loc) · 9.18 KB
/
conftest.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
#-------------------------------------------------------------------------------
#
# Test Core
#
# Copyright (C) 2019-2024, HENSOLDT Cyber GmbH
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# For commercial licensing, contact: [email protected]
#
#-------------------------------------------------------------------------------
import sys, os, subprocess, multiprocessing
import traceback
import time, datetime
import socket, ssl
import re
import pytest
import logs # logs module from the common directory in TA
import board_automation.system_selector
import board_automation.board_automation as ba
#-------------------------------------------------------------------------------
# pytest invokes this as iterator. Everything up to the yield is executed only
# once when the iterator is created. Then this "yields" the handle tuple for
# each iteration. When the iterator is destroyed, the code after the yield runs
# to clean things up.
def start_or_attach_to_test_runner(run_context):
# setup phase
print("")
base_log_dir = run_context.log_dir
for retries in range(4):
if (retries > 0):
sleep_time = 2**retries
print(f'Succesful start not detected. Retrying after {sleep_time} seconds')
time.sleep(sleep_time)
log_dir = base_log_dir
if (retries > 0):
log_dir += f'-retry-{retries}'
# ensure the log dir exists
if not os.path.isdir(log_dir):
os.makedirs(log_dir)
run_context.log_dir = log_dir
test_runner = None
do_retry = True
is_error = False
try:
# Create the test runner and start the board. If this fails, then
# keep retrying, as this can happen sometimes, e.g. with QEMU where
# the system still has some resources locked.
test_runner = board_automation.system_selector.get_test_runner(run_context)
test_runner.start()
# We could start the test system, so there is no point in retrying
# if the test fails.
do_retry = False
# PyTest will receive the test runner from this "callback" for each
# test case. The "system" parameter that the test can pass in the
# call is no longer used, since the system image is passed as
# parameter when the whole test framework is started.
yield (lambda system = None: test_runner)
# If we arrive here, the test runner finished normally. This does
# not mean the test(s) passed successfully, it just means there was
# no fatal problem.
except Exception as e:
is_error = True
print(f'Test_runner exception: {e}')
print(''.join(traceback.format_exception(
etype=type(e),
value=e,
tb=e.__traceback__)))
if test_runner:
try:
test_runner.stop()
except Exception as e:
# failing to stop the test runner is really fatal, there is no
# point in retrying then.
do_retry = False
is_error = True
print(f'Test_runner stop exception: {e}')
print(''.join(traceback.format_exception(
etype=type(e),
value=e,
tb=e.__traceback__)))
if not is_error:
break
if not do_retry:
pytest.exit('test_runner failed')
# We arrive here after the loop, the test runner finished normally. This
# does not mean the test(s) passed successfully, it just means there was no
# fatal problem.
print('test_runner finished' + \
('' if (0 == retries) else f' (after {retries} retries)') )
#-------------------------------------------------------------------------------
def tls_server_proc(port = 8888, timeout = 180):
try:
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain(
certfile=os.path.dirname(__file__) + "/test_tls_api/cert.pem",
keyfile=os.path.dirname(__file__) + "/test_tls_api/key.pem"
)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Allow re-use of socket
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Set *some* timeout
sock.settimeout(timeout)
# Allow any IP
sock.bind(('', port))
# Expect one client
sock.listen(1)
print(f'[TLS] Started TLS server on port {port}')
while True:
conn, addr = sock.accept()
print(f'[TLS] Client connected: {addr}')
stream = context.wrap_socket(conn, server_side=True)
try:
# Simply echo back to sender, receiving blocks of up to 1 KiB
# is sufficient for the current test implementation
data = stream.recv(1024)
print("[TLS] Received data", data)
stream.send(data)
print("[TLS] Sent data back")
except ssl.SSLEOFError:
# This happens during the test of the re-set, where we do a handshake
# and then simply close the socket..
pass
finally:
stream.shutdown(socket.SHUT_RDWR)
stream.close()
finally:
sock.close()
#-------------------------------------------------------------------------------
def start_or_attach_to_mosquitto(run_context):
mosquitto_log_file = os.path.join(run_context.log_dir, "mosquitto_log.txt")
with open(mosquitto_log_file, "w") as f:
try:
subprocess.Popen(
['mosquitto', '-c', '/etc/mosquitto/mosquitto.conf'],
stderr=f,
stdout=f)
except OSError:
# no need to run the tests without the broker
pytest.fail("Couldn't run mosquitto broker!")
#-------------------------------------------------------------------------------
@pytest.fixture(scope="module")
def boot(request):
yield from start_or_attach_to_test_runner(
ba.Run_Context(
request,
boot_mode = ba.BootMode.SEL4_CAMKES,
)
)
#-------------------------------------------------------------------------------
@pytest.fixture(scope="module")
def boot_with_proxy(request):
yield from start_or_attach_to_test_runner(
ba.Run_Context(
request,
boot_mode = ba.BootMode.SEL4_CAMKES,
use_proxy = True,
)
)
#-------------------------------------------------------------------------------
@pytest.fixture(scope="module")
def boot_with_proxy_no_sdcard(request):
yield from start_or_attach_to_test_runner(
ba.Run_Context(
request,
boot_mode = ba.BootMode.SEL4_CAMKES,
use_proxy = True,
sd_card_size = 0,
)
)
#-------------------------------------------------------------------------------
@pytest.fixture(scope="module")
def boot_sel4_native(request):
yield from start_or_attach_to_test_runner(
ba.Run_Context(
request,
boot_mode = ba.BootMode.SEL4_NATIVE,
)
)
#-------------------------------------------------------------------------------
@pytest.fixture(scope="module")
def boot_bare_metal(request):
yield from start_or_attach_to_test_runner(
ba.Run_Context(
request,
boot_mode = ba.BootMode.BARE_METAL,
)
)
#-------------------------------------------------------------------------------
@pytest.fixture(scope="module")
def tls_server():
proc = multiprocessing.Process(target=tls_server_proc)
proc.start()
yield proc
proc.terminate()
#-------------------------------------------------------------------------------
@pytest.fixture(scope="module")
def mosquitto_broker(request):
start_or_attach_to_mosquitto(ba.Run_Context(request))
#-------------------------------------------------------------------------------
def pytest_addoption(parser):
# test image
parser.addoption(
"--system_image",
required=True,
help="location of the system image")
# proxy is an optional parameter, because some tests don't need it
parser.addoption(
"--proxy",
help="<binary_location>[,<connection>]")
# Optional parameter as only some tests need the SD Card.
parser.addoption(
"--sd_card",
default="1048576", # 1 MiB
help="SD card shall be available."
)
parser.addoption(
"--target",
required=True,
help="target platform")
parser.addoption(
"--log_dir",
help="folder where to put the logs")
parser.addoption(
"--resource_dir",
help="folder where the platform resources are located")
parser.addoption(
"--print_logs",
action="store_true",
help="print logs to console")