From 8e7cda75cc19a5a62e352eba976085369d96ff3d Mon Sep 17 00:00:00 2001 From: Grzegorz Chwierut Date: Mon, 15 Jul 2024 11:49:15 +0200 Subject: [PATCH] twister: pytest: Move helper methods to pyteste-harness package Moved helper methods from tests/boot/with_mcumgr to pytest-harness package. It can be reused by other tests. Signed-off-by: Grzegorz Chwierut --- .../src/twister_harness/helpers/utils.py | 37 +++++++++++++++++++ .../pytest/test_downgrade_prevention.py | 7 ++-- tests/boot/with_mcumgr/pytest/test_upgrade.py | 9 ++--- tests/boot/with_mcumgr/pytest/utils.py | 25 ------------- 4 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 scripts/pylib/pytest-twister-harness/src/twister_harness/helpers/utils.py diff --git a/scripts/pylib/pytest-twister-harness/src/twister_harness/helpers/utils.py b/scripts/pylib/pytest-twister-harness/src/twister_harness/helpers/utils.py new file mode 100644 index 00000000000..076b5dd1940 --- /dev/null +++ b/scripts/pylib/pytest-twister-harness/src/twister_harness/helpers/utils.py @@ -0,0 +1,37 @@ +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +from __future__ import annotations + +import logging +import re + +from pathlib import Path + + +logger = logging.getLogger(__name__) + + +def find_in_config(config_file: Path | str, config_key: str) -> str: + """Find key in config file""" + 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('"\'') + logger.debug('Not found key: %s' % config_key) + 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) diff --git a/tests/boot/with_mcumgr/pytest/test_downgrade_prevention.py b/tests/boot/with_mcumgr/pytest/test_downgrade_prevention.py index b7b00a90e81..622850f7656 100755 --- a/tests/boot/with_mcumgr/pytest/test_downgrade_prevention.py +++ b/tests/boot/with_mcumgr/pytest/test_downgrade_prevention.py @@ -7,13 +7,12 @@ import logging from pathlib import Path from twister_harness import DeviceAdapter, Shell, MCUmgr -from utils import ( +from twister_harness.helpers.utils import ( find_in_config, match_lines, - match_no_lines, - check_with_shell_command, - check_with_mcumgr_command, + match_no_lines ) +from utils import check_with_shell_command, check_with_mcumgr_command from test_upgrade import create_signed_image diff --git a/tests/boot/with_mcumgr/pytest/test_upgrade.py b/tests/boot/with_mcumgr/pytest/test_upgrade.py index bda3d6cb531..18b8b0b41dd 100755 --- a/tests/boot/with_mcumgr/pytest/test_upgrade.py +++ b/tests/boot/with_mcumgr/pytest/test_upgrade.py @@ -8,14 +8,13 @@ import logging from pathlib import Path from twister_harness import DeviceAdapter, Shell, MCUmgr -from west_sign_wrapper import west_sign_with_imgtool -from utils import ( +from twister_harness.helpers.utils import ( find_in_config, match_lines, - match_no_lines, - check_with_shell_command, - check_with_mcumgr_command, + match_no_lines ) +from utils import check_with_shell_command, check_with_mcumgr_command +from west_sign_wrapper import west_sign_with_imgtool logger = logging.getLogger(__name__) diff --git a/tests/boot/with_mcumgr/pytest/utils.py b/tests/boot/with_mcumgr/pytest/utils.py index 208bd92d166..ab879f564be 100644 --- a/tests/boot/with_mcumgr/pytest/utils.py +++ b/tests/boot/with_mcumgr/pytest/utils.py @@ -4,9 +4,7 @@ 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 @@ -14,29 +12,6 @@ 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