-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
155 lines (120 loc) · 5.22 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
#
# Copyright (c) 2022 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
import pytest
import yaml
import logging
from contextlib import ExitStack
from targettest.devkit import Devkit, discover_dks, halt_unused
from targettest.provision import (register_dk, get_dk_list,
FlashedDevice, RPCDevice, TestDevice)
LOGGER = logging.getLogger(__name__)
def pytest_addoption(parser):
parser.addoption("--no-flash", action="store_true",
help='Skip the erase/flash cycle')
# Note: has to be called with `-s` option so the test session can prompt the
# user to reset the device(s) manually.
parser.addoption("--no-emu", action="store_true",
help='Don\'t connect to jlink emulator, has to be called with `-s`. \
This allows the use of a debugger during the test run.')
parser.addoption("--devconf", action="store",
help='Use a static device configuration (see sample_devconf.yml)')
parser.addoption("--dut-family", action="store",
help='specify a device (nrf52, nrf53) family for the DUT.')
parser.addoption("--tester-family", action="store",
help='specify a device (nrf52, nrf53) family for the Tester.')
@pytest.fixture(scope="session", autouse=True)
def devkits(request):
# Don't discover devices if devconf was specified on cli
devconf = request.config.getoption("--devconf")
if devconf is not None:
return
LOGGER.info(f'Discovering devices...')
devkits = discover_dks()
LOGGER.info(f'Available devices: {[devkit.segger_id for devkit in devkits]}')
for devkit in devkits:
register_dk(devkit)
def get_device_by_name(devices, name):
for dev in devices:
if dev['name'] == name:
return dev
return None
def get_board_by_family(family: str):
if family.upper() == 'NRF52':
return 'nrf52840dk_nrf52840'
else:
return 'nrf5340dk_nrf5340_cpuapp'
@pytest.fixture(scope="class")
def flasheddevices(request):
flash = not request.config.getoption("--no-flash")
emu = not request.config.getoption("--no-emu")
devconf = request.config.getoption("--devconf")
dut_family = request.config.getoption("--dut-family")
tester_family = request.config.getoption("--tester-family")
# Select the devices families
if dut_family is None:
dut_family = 'nrf53'
if tester_family is None:
tester_family = 'nrf53'
# Select the actual devices
dut_id = None
tester_id = None
if devconf is not None:
LOGGER.info(f'Using devconf: {devconf}')
with open(devconf, 'r') as stream:
# Assuming only one config per devconf file
parsed = yaml.safe_load(stream)
config = parsed['configurations'][0]
devices = parsed['devices']
dut_name = config['dut_' + dut_family]
dut_id = get_device_by_name(devices, dut_name)['segger']
register_dk(Devkit(dut_id, dut_family, dut_name))
assert dut_id, 'DUT not found in configuration'
tester_name = config['tester_' + tester_family]
tester_id = get_device_by_name(devices, tester_name)['segger']
register_dk(Devkit(tester_id, tester_family, tester_name))
assert tester_id, 'Tester not found in configuration'
LOGGER.info(f'DUT: {dut_id} Tester: {tester_id}')
# ExitStack is equivalent to multiple nested `with` statements, but is more readable
with ExitStack() as stack:
dut_dk = stack.enter_context(
FlashedDevice(request,
name='DUT',
family=dut_family,
id=dut_id,
board=get_board_by_family(dut_family),
flash_device=flash,
emu=emu))
tester_dk = stack.enter_context(
FlashedDevice(request,
name='Tester',
family=tester_family,
id=tester_id,
board=get_board_by_family(tester_family),
flash_device=flash,
emu=emu))
devices = {'dut_dk': dut_dk, 'tester_dk': tester_dk}
halt_unused(get_dk_list())
yield devices
LOGGER.debug('closing DK APIs')
@pytest.fixture()
def testdevices(flasheddevices):
with ExitStack() as stack:
try:
dut_dk = flasheddevices['dut_dk']
tester_dk = flasheddevices['tester_dk']
LOGGER.debug(f'opening DUT rpc {dut_dk.segger_id}')
dut_rpc = stack.enter_context(RPCDevice(dut_dk))
dut = TestDevice(dut_dk, dut_rpc)
LOGGER.debug(f'opening Tester rpc {tester_dk.segger_id}')
tester_rpc = stack.enter_context(RPCDevice(tester_dk))
tester = TestDevice(tester_dk, tester_rpc)
devices = {'dut': dut, 'tester': tester}
LOGGER.info(f'Test devices: {devices}')
yield devices
finally:
LOGGER.info(f'[{dut_dk.segger_id}] DUT logs:\n{dut_dk.log}')
LOGGER.info(f'[{tester_dk.segger_id}] Tester logs:\n{tester_dk.log}')
LOGGER.debug('closing RPC channels')