forked from RIOT-OS/Release-Specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
401 lines (330 loc) · 11.5 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# pylint: disable=W0613,W0621
"""
Central pytest definitions.
See https://docs.pytest.org/en/stable/fixture.html#conftest-py-sharing-fixture-functions
""" # noqa: E501
import re
import os
import subprocess
import sys
import time
from collections.abc import Iterable
import pytest
from riotctrl.ctrl import RIOTCtrl
import testutils.github
import testutils.pytest
from testutils.iotlab import IoTLABExperiment, DEFAULT_SITE
from testutils.pytest import get_required_envvar
from testutils import ttn
IOTLAB_EXPERIMENT_DURATION = 120
RIOTBASE = os.environ.get('RIOTBASE')
RUNNING_CTRLS = []
RUNNING_EXPERIMENTS = []
def pytest_addoption(parser):
# pylint: disable=C0301
"""
register argparse-style options and ini-style config values, called once at
the beginning of a test run.
See https://docs.pytest.org/en/stable/reference.html#_pytest.hookspec.pytest_addoption
""" # noqa: E501
parser.addoption(
"--local",
action="store_true",
default=False,
help="use local boards",
)
parser.addoption(
"--hide-output",
action="store_true",
default=False,
help="Don't log output of nodes",
)
parser.addoption(
"--boards",
type=testutils.pytest.list_from_string,
help="list of BOARD's or IOTLAB_NODEs for the test",
)
parser.addoption(
"--non-RC",
action="store_true",
default=False,
help="Runs test even if RIOT version under test is not an RC",
)
parser.addoption(
"--self-test",
action="store_true",
default=False,
help="Tests the testutils rather than running the release tests",
)
parser.addoption(
"--log-file-fmt",
nargs="?",
default=None,
type=testutils.pytest.log_file_fmt,
help="Format for the log file name. The available variables are: "
"`module`: The module (=specXX) of the test, "
"`function`: The function (=taskXX) of the test, "
"`node`: Name of the node (on IoT-LAB the URL of the node, "
"locally board name + port), "
"`time`: UNIX timestamp at creation time. "
"If the provided argument is an empty string the format will be "
"'{module}-{function}-{node}-{time}.log' and stored in the "
"current work directory",
)
def pytest_ignore_collect(path, config):
# pylint: disable=C0301
"""
return True to prevent considering this path for collection.
See: https://docs.pytest.org/en/stable/reference.html#_pytest.hookspec.pytest_ignore_collect
""" # noqa: E501
# This is about the --self-test option so I don't agree with pylint here
# pylint: disable=R1705
if config.getoption("--self-test"):
return "testutils" not in str(path)
else:
return "testutils" in str(path)
def pytest_collection_modifyitems(config, items):
# pylint: disable=C0301
"""
called after collection has been performed, may filter or re-order the
items in-place.
See: https://docs.pytest.org/en/stable/reference.html#_pytest.hookspec.pytest_collection_modifyitems
""" # noqa: E501
# --local given by CLI
run_local = config.getoption("--local")
sudo_only_mark = testutils.pytest.check_sudo()
local_only_mark = testutils.pytest.check_local(run_local)
iotlab_creds_mark = testutils.pytest.check_credentials(run_local)
rc_only_mark = testutils.pytest.check_rc(not config.getoption("--non-RC"))
for item in items:
if local_only_mark and "local_only" in item.keywords:
item.add_marker(local_only_mark)
if sudo_only_mark and "sudo_only" in item.keywords:
item.add_marker(sudo_only_mark)
if iotlab_creds_mark and "iotlab_creds" in item.keywords:
item.add_marker(iotlab_creds_mark)
if rc_only_mark and "rc_only" in item.keywords:
item.add_marker(rc_only_mark)
def pytest_configure(config):
plugin = GithubCommentReportPlugin(config)
config.pluginmanager.register(plugin, 'github_comment_report_plugin')
class GithubCommentReportPlugin: # pylint: disable=R0903
def __init__(self, config):
self.config = config
def pytest_runtest_logreport(self, report):
# pylint: disable=W0212
basetemp = self.config._tmp_path_factory.getbasetemp()
testutils.github.update_issue(report, basetemp)
def pytest_keyboard_interrupt(excinfo):
# pylint: disable=C0301
"""
Called on KeyInterrupt
See: https://docs.pytest.org/en/stable/reference.html?highlight=hooks#_pytest.hookspec.pytest_keyboard_interrupt
""" # noqa: E501
for child in RUNNING_CTRLS:
child.stop_term()
for exp in RUNNING_EXPERIMENTS:
exp.stop()
@pytest.fixture
def dev_id(request):
if request.param == "otaa":
return ttn.DEVICE_ID
return ttn.DEVICE_ID_ABP
@pytest.fixture
def appkey():
return get_required_envvar("APPKEY")
@pytest.fixture
def appeui():
return get_required_envvar("APPEUI")
@pytest.fixture
def deveui():
return get_required_envvar("DEVEUI")
@pytest.fixture
def devaddr():
return get_required_envvar("DEVADDR")
@pytest.fixture
def nwkskey():
return get_required_envvar("NWKSKEY")
@pytest.fixture
def appskey():
return get_required_envvar("APPSKEY")
@pytest.fixture
def ttn_client():
with ttn.TTNClient() as client:
yield client
@pytest.fixture
def log_nodes(request):
"""
Show output of nodes
:return: True if output of nodes should be shown, False otherwise
"""
# use reverse, since from outside we most of the time _want_ to log
return not request.config.getoption("--hide-output")
@pytest.fixture
def log_file_fmt(request):
"""
Show output of nodes
:return: True if output of nodes should be shown, False otherwise
"""
# use reverse, since from outside we most of the time _want_ to log
return request.config.getoption("--log-file-fmt")
@pytest.fixture
def local(request):
"""
Use local boards
:return: True if local boards should be used, False otherwise
"""
return request.config.getoption("--local")
@pytest.fixture
def riotbase(request):
"""
RIOT directory to test. Taken from the variable `RIOTBASE`
"""
return os.path.abspath(RIOTBASE)
@pytest.fixture
def boards(request):
"""
String list of boards to use for the test.
Values are used for the RIOT environment variables `IOTLAB_NODE` or `BOARD`
"""
return request.config.getoption("--boards")
@pytest.fixture
def iotlab_site(request):
"""
IoT-LAB site where the nodes are reserved.
If not specified by a test, it's fetched from the IOTLAB_SITE environment
variable or falls back to DEFAULT_SITE.
"""
return getattr(request, "param", os.environ.get("IOTLAB_SITE", DEFAULT_SITE))
def get_namefmt(request):
name_fmt = {}
if request.module:
name_fmt["module"] = request.module.__name__.replace("test_", "")
if request.function:
name_fmt["function"] = request.function.__name__.replace("test_", "")
return name_fmt
@pytest.fixture
def nodes(local, request, boards, iotlab_site):
"""
Provides the nodes for a test as a list of RIOTCtrl objects
"""
ctrls = []
if boards is None:
boards = request.param
only_native = all(b.startswith("native") for b in boards)
for board in boards:
if local or only_native or IoTLABExperiment.valid_board(board):
env = {'BOARD': f'{board}'}
else:
env = {
'BOARD': IoTLABExperiment.board_from_iotlab_node(board),
'IOTLAB_NODE': f'{board}',
}
ctrls.append(RIOTCtrl(env=env))
if local or only_native:
yield ctrls
else:
name_fmt = get_namefmt(request)
# Start IoT-LAB experiment if requested
exp = IoTLABExperiment(
# pylint: disable=C0209
name="RIOT-release-test-{module}-{function}".format(**name_fmt),
ctrls=ctrls,
site=iotlab_site,
)
RUNNING_EXPERIMENTS.append(exp)
exp.start(duration=IOTLAB_EXPERIMENT_DURATION)
yield ctrls
exp.stop()
RUNNING_EXPERIMENTS.remove(exp)
def update_env(node, modules=None, cflags=None, port=None, termflags=None):
node.env['QUIETER'] = '1'
if not isinstance(modules, str) and isinstance(modules, Iterable):
node.env['USEMODULE'] = ' '.join(str(m) for m in modules)
elif modules is not None:
node.env['USEMODULE'] = modules
if "USEMODULE" in node.env and os.environ.get('BUILD_IN_DOCKER', 0) == '1':
# workaround to inject USEMODULE into docker container
# see: https://github.com/RIOT-OS/RIOT/issues/14504
node.env['DOCKER_ENVIRONMENT_CMDLINE'] = (
node.env.get('DOCKER_ENVIRONMENT_CMDLINE', '')
+ f" -e 'USEMODULE={node.env['USEMODULE']}'"
)
if cflags is not None:
node.env['CFLAGS'] = cflags
if port is not None:
node.env['PORT'] = port
if termflags is not None:
node.env['TERMFLAGS'] = termflags
@pytest.fixture
def riot_ctrl(log_nodes, log_file_fmt, nodes, riotbase, request):
"""
Factory to create RIOTCtrl objects from list nodes provided by nodes
fixture
"""
factory_ctrls = []
# pylint: disable=R0913
def ctrl(
nodes_idx,
application_dir,
shell_interaction_cls,
board_type=None,
modules=None,
cflags=None,
port=None,
termflags=None,
):
if board_type is not None:
node = next(n for n in nodes if n.board() == board_type)
else:
node = nodes[nodes_idx]
update_env(node, modules, cflags, port, termflags)
# if the nodes are not logged, there is no sense in logging to a file
# so check if nodes are logged as well as if they should be logged to a
# file
if log_nodes and log_file_fmt:
if node.env.get("IOTLAB_NODE"):
node_name = node.env["IOTLAB_NODE"]
else:
node_port = re.sub(r'\W+', '-', node.env["PORT"])
node_name = f"{node.board()}-{node_port}"
node.env["TERMLOG"] = os.path.join(
os.getcwd(),
log_file_fmt.format(
node=node_name, time=int(time.time()), **get_namefmt(request)
),
)
# need to access private member here isn't possible otherwise sadly :(
# pylint: disable=W0212
node._application_directory = os.path.join(riotbase, application_dir)
node.make_run(
['flash'],
check=True,
stdout=None if log_nodes else subprocess.DEVNULL,
stderr=None if log_nodes else subprocess.DEVNULL,
)
if node.env.get("IOTLAB_NODE"):
# reset to prevent at86rf2xx `ifconfig` issue
time.sleep(1)
node.make_run(
['reset'],
check=True,
stdout=None if log_nodes else subprocess.DEVNULL,
stderr=None if log_nodes else subprocess.DEVNULL,
)
time.sleep(1)
termargs = {}
if log_nodes:
termargs["logfile"] = sys.stdout
RUNNING_CTRLS.append(node)
node.start_term(**termargs)
factory_ctrls.append(node)
return shell_interaction_cls(node)
yield ctrl
for node in factory_ctrls:
try:
node.stop_term()
RUNNING_CTRLS.remove(node)
except RuntimeError:
# Process had to be forced kill, happens with native
pass