zephyr/tests/boot/with_mcumgr/pytest/utils.py
Grzegorz Chwierut ae336f69c3 tests: mcuboot: pytest: Add image swap test with mcumgr
Added application based on SMP Server Sample. Application is built together
with MCUboot using sysbuild and is flashed onto device in one step.
Tests are automated with pytest - new harness of Twister.
The image for upgrade is prepared using west sign command
then is uploaded by mcumgr into device and tested.
Automated scenarios to test upgrade (image upload, test, revert, confirm),
to test downgrade prevention mechanism and to test upgrade with image,
that is signed with an invalid key.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2023-09-27 15:43:56 +02:00

51 lines
1.7 KiB
Python

# Copyright (c) 2023 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import logging
import re
from pathlib import Path
from twister_harness import Shell, MCUmgr
from twister_harness.helpers.shell import ShellMCUbootCommandParsed
logger = logging.getLogger(__name__)
def find_in_config(config_file: Path | str, config_key: str) -> str:
re_key = re.compile(rf'{config_key}=(.+)')
with open(config_file) as f:
lines = f.readlines()
for line in lines:
if m := re_key.match(line):
logger.debug('Found matching key: %s' % line.strip())
return m.group(1).strip('"\'')
return ''
def match_lines(output_lines: list[str], searched_lines: list[str]) -> None:
"""Check all lines exist in the output"""
for sl in searched_lines:
assert any(sl in line for line in output_lines)
def match_no_lines(output_lines: list[str], searched_lines: list[str]) -> None:
"""Check lines not found in the output"""
for sl in searched_lines:
assert all(sl not in line for line in output_lines)
def check_with_shell_command(shell: Shell, version: str, swap_type: str | None = None) -> None:
mcuboot_areas = ShellMCUbootCommandParsed.create_from_cmd_output(shell.exec_command('mcuboot'))
assert mcuboot_areas.areas[0].version == version
if swap_type:
assert mcuboot_areas.areas[0].swap_type == swap_type
def check_with_mcumgr_command(mcumgr: MCUmgr, version: str) -> None:
image_list = mcumgr.get_image_list()
# version displayed by MCUmgr does not print +0 and changes + to '.' for non-zero values
assert image_list[0].version == version.replace('+0', '').replace('+', '.')