mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-09 20:41:14 +00:00
114
conftest.py
114
conftest.py
@@ -19,21 +19,16 @@ if os.path.join(os.path.dirname(__file__), 'tools', 'ci', 'python_packages') not
|
||||
|
||||
import glob
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import time
|
||||
import typing as t
|
||||
import zipfile
|
||||
from copy import deepcopy
|
||||
from telnetlib import Telnet
|
||||
from urllib.parse import quote
|
||||
|
||||
import common_test_methods # noqa: F401
|
||||
import gitlab_api
|
||||
import pexpect
|
||||
import pytest
|
||||
import requests
|
||||
import yaml
|
||||
@@ -58,8 +53,6 @@ from idf_pytest.plugin import IdfPytestEmbedded
|
||||
from idf_pytest.utils import format_case_id
|
||||
from pytest_embedded.plugin import multi_dut_argument
|
||||
from pytest_embedded.plugin import multi_dut_fixture
|
||||
from pytest_embedded.utils import to_bytes
|
||||
from pytest_embedded.utils import to_str
|
||||
from pytest_embedded_idf.dut import IdfDut
|
||||
from pytest_embedded_idf.unity_tester import CaseTester
|
||||
|
||||
@@ -155,113 +148,6 @@ class BuildReportDownloader(AppDownloader):
|
||||
super().download_app(app_build_path, artifact_type)
|
||||
|
||||
|
||||
class OpenOCD:
|
||||
def __init__(self, dut: 'IdfDut'):
|
||||
self.MAX_RETRIES = 3
|
||||
self.RETRY_DELAY = 1
|
||||
self.TELNET_PORT = 4444
|
||||
self.dut = dut
|
||||
self.telnet: t.Optional[Telnet] = None
|
||||
self.log_file = os.path.join(self.dut.logdir, 'ocd.txt')
|
||||
self.proc: t.Optional[pexpect.spawn] = None
|
||||
|
||||
def __enter__(self) -> t.Optional['OpenOCD']:
|
||||
return self.run()
|
||||
|
||||
def __exit__(self, exception_type: t.Any, exception_value: t.Any, exception_traceback: t.Any) -> None:
|
||||
self.kill()
|
||||
|
||||
def run(self) -> t.Optional['OpenOCD']:
|
||||
desc_path = os.path.join(self.dut.app.binary_path, 'project_description.json')
|
||||
|
||||
try:
|
||||
with open(desc_path, 'r') as f:
|
||||
project_desc = json.load(f)
|
||||
except FileNotFoundError:
|
||||
logging.error('Project description file not found at %s', desc_path)
|
||||
raise
|
||||
|
||||
openocd_scripts = os.getenv('OPENOCD_SCRIPTS')
|
||||
if not openocd_scripts:
|
||||
raise RuntimeError('OPENOCD_SCRIPTS environment variable is not set.')
|
||||
|
||||
debug_args = project_desc.get('debug_arguments_openocd')
|
||||
if not debug_args:
|
||||
raise KeyError("'debug_arguments_openocd' key is missing in project_description.json")
|
||||
|
||||
# For debug purposes, make the value '4'
|
||||
ocd_env = os.environ.copy()
|
||||
ocd_env['LIBUSB_DEBUG'] = '1'
|
||||
|
||||
for _ in range(1, self.MAX_RETRIES + 1):
|
||||
try:
|
||||
self.proc = pexpect.spawn(
|
||||
command='openocd',
|
||||
args=['-s', openocd_scripts] + debug_args.split(),
|
||||
timeout=5,
|
||||
encoding='utf-8',
|
||||
codec_errors='ignore',
|
||||
env=ocd_env,
|
||||
)
|
||||
if self.proc and self.proc.isalive():
|
||||
self.proc.expect_exact('Info : Listening on port 3333 for gdb connections', timeout=5)
|
||||
self.connect_telnet()
|
||||
self.write('log_output {}'.format(self.log_file))
|
||||
return self
|
||||
except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT, ConnectionRefusedError) as e:
|
||||
logging.error('Error running OpenOCD: %s', str(e))
|
||||
self.kill()
|
||||
time.sleep(self.RETRY_DELAY)
|
||||
|
||||
raise RuntimeError('Failed to run OpenOCD after %d attempts.', self.MAX_RETRIES)
|
||||
|
||||
def connect_telnet(self) -> None:
|
||||
for attempt in range(1, self.MAX_RETRIES + 1):
|
||||
try:
|
||||
self.telnet = Telnet('127.0.0.1', self.TELNET_PORT, 5)
|
||||
break
|
||||
except ConnectionRefusedError as e:
|
||||
logging.error('Error telnet connection: %s in attempt:%d', e, attempt)
|
||||
time.sleep(1)
|
||||
else:
|
||||
raise ConnectionRefusedError
|
||||
|
||||
def write(self, s: str) -> t.Any:
|
||||
if self.telnet is None:
|
||||
logging.error('Telnet connection is not established.')
|
||||
return ''
|
||||
resp = self.telnet.read_very_eager()
|
||||
self.telnet.write(to_bytes(s, '\n'))
|
||||
resp += self.telnet.read_until(b'>')
|
||||
return to_str(resp)
|
||||
|
||||
def apptrace_wait_stop(self, timeout: int = 30) -> None:
|
||||
stopped = False
|
||||
end_before = time.time() + timeout
|
||||
while not stopped:
|
||||
cmd_out = self.write('esp apptrace status')
|
||||
for line in cmd_out.splitlines():
|
||||
if line.startswith('Tracing is STOPPED.'):
|
||||
stopped = True
|
||||
break
|
||||
if not stopped and time.time() > end_before:
|
||||
raise pexpect.TIMEOUT('Failed to wait for apptrace stop!')
|
||||
time.sleep(1)
|
||||
|
||||
def kill(self) -> None:
|
||||
# Check if the process is still running
|
||||
if self.proc and self.proc.isalive():
|
||||
self.proc.terminate()
|
||||
self.proc.kill(signal.SIGKILL)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def openocd(dut: IdfDut) -> OpenOCD:
|
||||
if isinstance(dut, tuple):
|
||||
raise ValueError('Multi-DUT support is not implemented yet')
|
||||
return OpenOCD(dut)
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def app_downloader(pipeline_id: t.Optional[str]) -> t.Optional[AppDownloader]:
|
||||
if not pipeline_id:
|
||||
|
Reference in New Issue
Block a user