Skip to content

Commit e0bd7e7

Browse files
gchwierhenrikbrixandersen
authored andcommitted
twister: Allow sharing hardware platform between variants
Extended hardware map to share a single board between variants. To run tests for different variants on the same board without re-configuring the hardware map file for each variant, one can use a `platform` atribute as a list of names. Signed-off-by: Grzegorz Chwierut <[email protected]>
1 parent b4f3763 commit e0bd7e7

File tree

5 files changed

+75
-32
lines changed

5 files changed

+75
-32
lines changed

doc/develop/test/twister.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,25 @@ using an external J-Link probe. The ``probe_id`` keyword overrides the
12781278
runner: jlink
12791279
serial: null
12801280
1281+
Using Single Board For Multiple Variants
1282+
++++++++++++++++++++++++++++++++++++++++
1283+
1284+
The ``platform`` attribute can be a list of names or a string
1285+
with names separated by spaces. This allows to run tests for
1286+
different platform variants on the same physical board, without
1287+
re-configuring the hardware map file for each variant. For example:
1288+
1289+
.. code-block:: yaml
1290+
1291+
- connected: true
1292+
id: '001234567890'
1293+
platform:
1294+
- nrf5340dk/nrf5340/cpuapp
1295+
- nrf5340dk/nrf5340/cpuapp/ns
1296+
product: J-Link
1297+
runner: nrfjprog
1298+
serial: /dev/ttyACM1
1299+
12811300
Quarantine
12821301
++++++++++
12831302

scripts/pylib/twister/twisterlib/handlers.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import threading
2222
import time
2323

24+
from contextlib import contextmanager
2425
from pathlib import Path
2526
from queue import Queue, Empty
2627
from twisterlib.environment import ZEPHYR_BASE, strip_ansi_sequences
@@ -457,6 +458,17 @@ def monitor_serial(self, ser, halt_event, harness):
457458

458459
log_out_fp.close()
459460

461+
@staticmethod
462+
@contextmanager
463+
def acquire_dut_locks(duts):
464+
try:
465+
for d in duts:
466+
d.lock.acquire()
467+
yield
468+
finally:
469+
for d in duts:
470+
d.lock.release()
471+
460472
def device_is_available(self, instance):
461473
device = instance.platform.name
462474
fixture = instance.testsuite.harness_config.get("fixture")
@@ -474,15 +486,16 @@ def device_is_available(self, instance):
474486

475487
# Select an available DUT with less failures
476488
for d in sorted(duts_found, key=lambda _dut: _dut.failures):
477-
d.lock.acquire()
478-
avail = False
479-
if d.available:
480-
d.available = 0
481-
d.counter_increment()
482-
avail = True
483-
logger.debug(f"Retain DUT:{d.platform}, Id:{d.id}, "
484-
f"counter:{d.counter}, failures:{d.failures}")
485-
d.lock.release()
489+
duts_shared_hw = [_d for _d in self.duts if _d.id == d.id] # get all DUTs with the same id
490+
with self.acquire_dut_locks(duts_shared_hw):
491+
avail = False
492+
if d.available:
493+
for _d in duts_shared_hw:
494+
_d.available = 0
495+
d.counter_increment()
496+
avail = True
497+
logger.debug(f"Retain DUT:{d.platform}, Id:{d.id}, "
498+
f"counter:{d.counter}, failures:{d.failures}")
486499
if avail:
487500
return d
488501

@@ -493,7 +506,10 @@ def make_dut_available(self, dut):
493506
dut.failures_increment()
494507
logger.debug(f"Release DUT:{dut.platform}, Id:{dut.id}, "
495508
f"counter:{dut.counter}, failures:{dut.failures}")
496-
dut.available = 1
509+
duts_shared_hw = [_d for _d in self.duts if _d.id == dut.id] # get all DUTs with the same id
510+
with self.acquire_dut_locks(duts_shared_hw):
511+
for _d in duts_shared_hw:
512+
_d.available = 1
497513

498514
@staticmethod
499515
def run_custom_script(script, timeout):

scripts/pylib/twister/twisterlib/hardwaremap.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -262,36 +262,43 @@ def load(self, map_file):
262262
flash_before = dut.get('flash_before')
263263
if flash_before is None:
264264
flash_before = self.options.flash_before and (not (flash_with_test or serial_pty))
265-
platform = dut.get('platform')
265+
platform = dut.get('platform')
266+
if isinstance(platform, str):
267+
platforms = platform.split()
268+
elif isinstance(platform, list):
269+
platforms = platform
270+
else:
271+
raise ValueError(f"Invalid platform value: {platform}")
266272
id = dut.get('id')
267273
runner = dut.get('runner')
268274
runner_params = dut.get('runner_params')
269275
serial = dut.get('serial')
270276
baud = dut.get('baud', None)
271277
product = dut.get('product')
272278
fixtures = dut.get('fixtures', [])
273-
connected= dut.get('connected') and ((serial or serial_pty) is not None)
279+
connected = dut.get('connected') and ((serial or serial_pty) is not None)
274280
if not connected:
275281
continue
276-
new_dut = DUT(platform=platform,
277-
product=product,
278-
runner=runner,
279-
runner_params=runner_params,
280-
id=id,
281-
serial_pty=serial_pty,
282-
serial=serial,
283-
serial_baud=baud,
284-
connected=connected,
285-
pre_script=pre_script,
286-
flash_before=flash_before,
287-
post_script=post_script,
288-
post_flash_script=post_flash_script,
289-
script_param=script_param,
290-
flash_timeout=flash_timeout,
291-
flash_with_test=flash_with_test)
292-
new_dut.fixtures = fixtures
293-
new_dut.counter = 0
294-
self.duts.append(new_dut)
282+
for plat in platforms:
283+
new_dut = DUT(platform=plat,
284+
product=product,
285+
runner=runner,
286+
runner_params=runner_params,
287+
id=id,
288+
serial_pty=serial_pty,
289+
serial=serial,
290+
serial_baud=baud,
291+
connected=connected,
292+
pre_script=pre_script,
293+
flash_before=flash_before,
294+
post_script=post_script,
295+
post_flash_script=post_flash_script,
296+
script_param=script_param,
297+
flash_timeout=flash_timeout,
298+
flash_with_test=flash_with_test)
299+
new_dut.fixtures = fixtures
300+
new_dut.counter = 0
301+
self.duts.append(new_dut)
295302

296303
def scan(self, persistent=False):
297304
from serial.tools import list_ports

scripts/schemas/twister/hwmap-schema.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sequence:
1616
type: str
1717
required: false
1818
"platform":
19-
type: str
19+
type: any
2020
required: true
2121
"probe_id":
2222
type: str

scripts/tests/twister/test_handlers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,7 @@ def mock_serial(*args, **kwargs):
12751275
dut = DUT()
12761276
dut.available = 0
12771277
dut.failures = 0
1278+
handler.duts = [dut]
12781279

12791280
hardware_baud = 14400
12801281
flash_timeout = 60

0 commit comments

Comments
 (0)