Browse Source

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 <grzegorz.chwierut@nordicsemi.no>
pull/76670/head
Grzegorz Chwierut 12 months ago committed by Carles Cufí
parent
commit
8e7cda75cc
  1. 37
      scripts/pylib/pytest-twister-harness/src/twister_harness/helpers/utils.py
  2. 7
      tests/boot/with_mcumgr/pytest/test_downgrade_prevention.py
  3. 9
      tests/boot/with_mcumgr/pytest/test_upgrade.py
  4. 25
      tests/boot/with_mcumgr/pytest/utils.py

37
scripts/pylib/pytest-twister-harness/src/twister_harness/helpers/utils.py

@ -0,0 +1,37 @@ @@ -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)

7
tests/boot/with_mcumgr/pytest/test_downgrade_prevention.py

@ -7,13 +7,12 @@ import logging @@ -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

9
tests/boot/with_mcumgr/pytest/test_upgrade.py

@ -8,14 +8,13 @@ import logging @@ -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__)

25
tests/boot/with_mcumgr/pytest/utils.py

@ -4,9 +4,7 @@ @@ -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 @@ -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

Loading…
Cancel
Save