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>
This commit is contained in:
Grzegorz Chwierut 2024-07-15 11:49:15 +02:00 committed by Carles Cufí
commit 8e7cda75cc
4 changed files with 44 additions and 34 deletions

View file

@ -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)

View file

@ -7,13 +7,12 @@ import logging
from pathlib import Path from pathlib import Path
from twister_harness import DeviceAdapter, Shell, MCUmgr from twister_harness import DeviceAdapter, Shell, MCUmgr
from utils import ( from twister_harness.helpers.utils import (
find_in_config, find_in_config,
match_lines, match_lines,
match_no_lines, match_no_lines
check_with_shell_command,
check_with_mcumgr_command,
) )
from utils import check_with_shell_command, check_with_mcumgr_command
from test_upgrade import create_signed_image from test_upgrade import create_signed_image

View file

@ -8,14 +8,13 @@ import logging
from pathlib import Path from pathlib import Path
from twister_harness import DeviceAdapter, Shell, MCUmgr from twister_harness import DeviceAdapter, Shell, MCUmgr
from west_sign_wrapper import west_sign_with_imgtool from twister_harness.helpers.utils import (
from utils import (
find_in_config, find_in_config,
match_lines, match_lines,
match_no_lines, match_no_lines
check_with_shell_command,
check_with_mcumgr_command,
) )
from utils import check_with_shell_command, check_with_mcumgr_command
from west_sign_wrapper import west_sign_with_imgtool
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View file

@ -4,9 +4,7 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
import re
from pathlib import Path
from twister_harness import Shell, MCUmgr from twister_harness import Shell, MCUmgr
from twister_harness.helpers.shell import ShellMCUbootCommandParsed from twister_harness.helpers.shell import ShellMCUbootCommandParsed
@ -14,29 +12,6 @@ from twister_harness.helpers.shell import ShellMCUbootCommandParsed
logger = logging.getLogger(__name__) 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: 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')) mcuboot_areas = ShellMCUbootCommandParsed.create_from_cmd_output(shell.exec_command('mcuboot'))
assert mcuboot_areas.areas[0].version == version assert mcuboot_areas.areas[0].version == version