Skip to content
This repository was archived by the owner on Aug 13, 2025. It is now read-only.

Commit a12673f

Browse files
committed
Run system tests inside temporary directories
For every pytest module, create a copy of its system test directory and run the test from that directory. This makes it easier to clean up afterwards and makes it less error-prone when re-running (failed) tests. Configure the logger to capture the module's log output into a separate file available from the temporary directory. This is quite convenient for exploring failures. Cases where temporary directory should be kept are handled in a follow-up commits.
1 parent ca77d0d commit a12673f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

bin/tests/system/conftest.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ def control_port():
5555
import logging
5656
from pathlib import Path
5757
import re
58+
import shutil
5859
import subprocess
60+
import tempfile
5961
import time
6062
from typing import Any, Dict, List, Optional
6163

@@ -64,6 +66,7 @@ def control_port():
6466

6567
# ----------------------- Globals definition -----------------------------
6668

69+
LOG_FORMAT = "%(asctime)s %(levelname)7s:%(name)s %(message)s"
6770
XDIST_WORKER = os.environ.get("PYTEST_XDIST_WORKER", "")
6871
FILE_DIR = os.path.abspath(Path(__file__).parent)
6972
ENV_RE = re.compile("([^=]+)=(.*)")
@@ -217,6 +220,48 @@ def logger(system_test_name):
217220
"""Logging facility specific to this test."""
218221
return logging.getLogger(system_test_name)
219222

223+
@pytest.fixture(scope="module")
224+
def system_test_dir(request, env, system_test_name, logger):
225+
"""
226+
Temporary directory for executing the test.
227+
228+
This fixture is responsible for creating (and potentially removing) a
229+
copy of the system test directory which is used as a temporary
230+
directory for the test execution.
231+
232+
FUTURE: This removes the need to have clean.sh scripts.
233+
"""
234+
235+
# Create a temporary directory with a copy of the original system test dir contents
236+
system_test_root = Path(f"{env['TOP_BUILDDIR']}/bin/tests/system")
237+
testdir = Path(
238+
tempfile.mkdtemp(prefix=f"{system_test_name}_tmp_", dir=system_test_root)
239+
)
240+
shutil.rmtree(testdir)
241+
shutil.copytree(system_test_root / system_test_name, testdir)
242+
243+
# Configure logger to write to a file inside the temporary test directory
244+
logger.handlers.clear()
245+
logger.setLevel(logging.DEBUG)
246+
handler = logging.FileHandler(testdir / "pytest.log.txt", mode="w")
247+
formatter = logging.Formatter(LOG_FORMAT)
248+
handler.setFormatter(formatter)
249+
logger.addHandler(handler)
250+
251+
# System tests are meant to be executed from their directory - switch to it.
252+
old_cwd = os.getcwd()
253+
os.chdir(testdir)
254+
logger.debug("changed workdir to: %s", testdir)
255+
try:
256+
yield testdir # other fixtures / tests will execute here
257+
finally:
258+
os.chdir(old_cwd)
259+
logger.debug("changed workdir to: %s", old_cwd)
260+
261+
# cases when tempdir should be kept are handled in follow-up commits
262+
logger.debug("deleting temporary directory")
263+
shutil.rmtree(testdir)
264+
220265
def _run_script( # pylint: disable=too-many-arguments
221266
env,
222267
logger,

0 commit comments

Comments
 (0)