2023-05-26 11:33:25 +02:00
|
|
|
# Copyright (c) 2023 Nordic Semiconductor ASA
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import os
|
2023-08-04 16:02:14 +02:00
|
|
|
from pathlib import Path
|
2023-05-26 11:33:25 +02:00
|
|
|
from typing import Generator
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from twister_harness.device.qemu_adapter import QemuAdapter
|
|
|
|
from twister_harness.exceptions import TwisterHarnessException
|
|
|
|
from twister_harness.twister_harness_config import DeviceConfig
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name='device')
|
|
|
|
def fixture_device_adapter(tmp_path) -> Generator[QemuAdapter, None, None]:
|
|
|
|
build_dir = tmp_path / 'build_dir'
|
2023-08-04 15:23:34 +02:00
|
|
|
os.mkdir(build_dir)
|
2023-08-04 17:33:39 +02:00
|
|
|
device = QemuAdapter(DeviceConfig(build_dir=build_dir, type='qemu'))
|
2023-05-26 11:33:25 +02:00
|
|
|
try:
|
2023-08-04 15:23:34 +02:00
|
|
|
yield device
|
|
|
|
finally:
|
|
|
|
device.close() # to make sure all running processes are closed
|
2023-05-26 11:33:25 +02:00
|
|
|
|
|
|
|
|
2023-08-04 15:23:34 +02:00
|
|
|
@patch('shutil.which', return_value='west')
|
|
|
|
def test_if_generate_command_creates_proper_command(patched_which, device: QemuAdapter):
|
2023-08-04 16:02:14 +02:00
|
|
|
device.device_config.build_dir = Path('build_dir')
|
2023-08-04 15:23:34 +02:00
|
|
|
device.generate_command()
|
|
|
|
assert device.command == ['west', 'build', '-d', 'build_dir', '-t', 'run']
|
2023-05-26 11:33:25 +02:00
|
|
|
|
|
|
|
|
2023-08-04 15:23:34 +02:00
|
|
|
def test_if_qemu_adapter_runs_without_errors(resources, device: QemuAdapter) -> None:
|
|
|
|
fifo_file_path = str(device.device_config.build_dir / 'qemu-fifo')
|
2023-05-26 11:33:25 +02:00
|
|
|
script_path = resources.joinpath('fifo_mock.py')
|
|
|
|
device.command = ['python', str(script_path), fifo_file_path]
|
2023-08-04 15:23:34 +02:00
|
|
|
device.launch()
|
2023-08-04 16:39:23 +02:00
|
|
|
lines = device.readlines_until(regex='Namespaces are one honking great idea')
|
2023-08-04 15:23:34 +02:00
|
|
|
device.close()
|
2023-05-26 11:33:25 +02:00
|
|
|
assert 'Readability counts.' in lines
|
2023-08-04 15:23:34 +02:00
|
|
|
assert os.path.isfile(device.handler_log_path)
|
|
|
|
with open(device.handler_log_path, 'r') as file:
|
2023-05-26 11:33:25 +02:00
|
|
|
file_lines = [line.strip() for line in file.readlines()]
|
|
|
|
assert file_lines[-2:] == lines[-2:]
|
|
|
|
|
|
|
|
|
2023-08-04 15:23:34 +02:00
|
|
|
def test_if_qemu_adapter_raise_exception_due_to_no_fifo_connection(device: QemuAdapter) -> None:
|
|
|
|
device.base_timeout = 0.3
|
|
|
|
device.command = ['sleep', '1']
|
2023-08-04 16:02:14 +02:00
|
|
|
with pytest.raises(TwisterHarnessException, match='Cannot establish communication with QEMU device.'):
|
2023-08-04 15:23:34 +02:00
|
|
|
device._flash_and_run()
|
|
|
|
device._close_device()
|
2023-08-04 16:02:14 +02:00
|
|
|
assert not os.path.exists(device._fifo_connection._fifo_out_path)
|
|
|
|
assert not os.path.exists(device._fifo_connection._fifo_in_path)
|