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

Commit e1935ec

Browse files
committed
Obtain env vars from conf.sh in pytest runner
The commands executed by pytest during a system test need to have the same environment variables set as if they were executed by the run.sh shell script. It was decided that for the moment, legacy way of executing system tests with run.sh should be kept, which complicates things a bit. In order to avoid duplicating the required variables in both conf.sh and pytest, it was decided to use the existing conf.sh as the only authoritative place for the variables. It is necessary to process the environment variables from conf.sh right when conftest.py is loaded, since they might be needed right away (e.g. to test for feature support during test collection). This solution is a bit hacky and is only meant to be used during the transitory phase when both pytest and the legacy run.sh are both supported. In the future, a superior pytest-only solution should be used. For discussion of other options, refer to https://gitlab.isc.org/isc-projects/bind9/-/merge_requests/6809#note_318889
1 parent d9507f8 commit e1935ec

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

bin/tests/system/conftest.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,44 @@ def control_port():
4949
# don't branch the code.
5050

5151
if os.getenv("LEGACY_TEST_RUNNER", "0") == "0":
52-
pass # will be implemented in followup commits
52+
import logging
53+
from pathlib import Path
54+
import re
55+
import subprocess
56+
57+
# ----------------------- Globals definition -----------------------------
58+
59+
FILE_DIR = os.path.abspath(Path(__file__).parent)
60+
ENV_RE = re.compile("([^=]+)=(.*)")
61+
62+
# ---------------------- Module initialization ---------------------------
63+
64+
def parse_env(env_text):
65+
"""Parse the POSIX env format into Python dictionary."""
66+
out = {}
67+
for line in env_text.splitlines():
68+
match = ENV_RE.match(line)
69+
if match:
70+
out[match.groups()[0]] = match.groups()[1]
71+
return out
72+
73+
def get_env(cmd):
74+
try:
75+
proc = subprocess.run(
76+
[cmd],
77+
shell=True,
78+
check=True,
79+
cwd=FILE_DIR,
80+
stdout=subprocess.PIPE,
81+
)
82+
except subprocess.CalledProcessError as exc:
83+
logging.error("failed to get shell env: %s", exc)
84+
raise exc
85+
env_text = proc.stdout.decode("utf-8")
86+
return parse_env(env_text)
87+
88+
# Read common environment variables for running tests from conf.sh.
89+
# FUTURE: Remove conf.sh entirely and define all variables in pytest only.
90+
CONF_ENV = get_env(". ./conf.sh && env")
91+
os.environ.update(CONF_ENV)
92+
logging.debug("conf.sh env: %s", CONF_ENV)

0 commit comments

Comments
 (0)