Source code for tests.utils.session_utils

#
# SPDX-FileCopyrightText: <text>Copyright 2026 Arm Limited and/or its
# affiliates <open-source-office@arm.com></text>
#
# SPDX-License-Identifier: MIT

import pexpect
import pytest
from tests.utils.common_utils import CommonUtils

[docs] logger = CommonUtils.get_logger(__name__)
[docs] def expect_pattern_live( session: pexpect.spawn, pattern: str, timeout: int, msg: str, level: str = "info", ) -> bool: """ Wait for a specific pattern to appear in a live pexpect session. :param session: Active pexpect session instance. :param pattern: Pattern to wait for in the session output. :param timeout: Maximum time (in seconds) to wait. :param msg: Log message to emit if a timeout occurs. :param level: Logging level to use. :returns: True if the pattern is matched within the timeout """ try: session.expect(pattern, timeout=timeout) return True except pexpect.TIMEOUT: getattr(logger, level, logger.info)(msg) return False
[docs] def fvp_power_cycle_and_reconnect( platform_base_obj, platform_bundle, boot_wait_seconds: int = 0 ): """ Power cycle the FVP instance and restore console connectivity. :param platform_base_obj: Initialized :class:`AutoTestPlatformBase`. :param platform_bundle: Initialized driver bundle. :param boot_wait_seconds: Optional delay in seconds :returns: None """ mgr = platform_bundle.manager platform_bundle.driver.reset(boot_wait_seconds) if callable(getattr(platform_bundle, "login_primary", None)): platform_bundle.login_primary( mgr, platform_base_obj.config_data, )
[docs] def login_and_wait_for_shell( session: pexpect.spawn, login_prompt: str, shell_prompt: str, ) -> None: """ Complete login sequence and wait for root shell prompt. :param login_prompt: login prompt pattern :param shell_prompt: shell prompt pattern :returns: None """ logger.info("Checking for login prompt") status = expect_pattern_live( session, login_prompt, timeout=600, msg="login prompt not detected", level="error", ) if not status: pytest.fail("Unable to detect the login prompt") logger.info("Sending root") session.sendline("root") logger.info("Waiting for shell prompt") status = expect_pattern_live( session, f"{shell_prompt}#", timeout=300, msg="shell prompt not detected", level="error", ) if not status: pytest.fail("Unable to detect the shell prompt")