Skip to content

Commit 6d2e83c

Browse files
feat(xtest): --scenario and --instance flags for conftest (DSPX-3302)
Adds two new pytest CLI options so xtest can be driven by a scenarios.yaml and run against a specific otdf-local instance. --scenario PATH When set, defaults --sdks-encrypt, --sdks-decrypt, and --containers from the scenario's `sdks` and `suite` blocks. Options explicitly passed on the CLI always override. --instance NAME Propagated to OTDF_LOCAL_INSTANCE_NAME so child `otdf-local` invocations within the test see the same instance the scenario expects. If otdf-sdk-mgr is not installed (minimal pytest environments), the --scenario flag silently no-ops via an ImportError guard. The flag shape is invariant either way so CI configs don't fork. This is the consumer side of the PR 3 / scenario-driven flow: the authoritative entry point remains `otdf-local scenario run <path>`, which sets these flags for you; this PR lets pytest accept them directly when running scenario-aware sessions outside the wrapper. Refs: https://virtru.atlassian.net/browse/DSPX-3302 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent c69afd6 commit 6d2e83c

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

xtest/conftest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ def is_a(v: str) -> typing.Any:
6262

6363
def pytest_addoption(parser: pytest.Parser):
6464
"""Add custom CLI options for pytest."""
65+
parser.addoption(
66+
"--scenario",
67+
help="path to scenarios.yaml; --sdks-encrypt/--sdks-decrypt/--containers default from it",
68+
type=Path,
69+
)
70+
parser.addoption(
71+
"--instance",
72+
help="otdf-local instance name; sets OTDF_LOCAL_INSTANCE_NAME for child tooling",
73+
type=str,
74+
)
6575
parser.addoption(
6676
"--audit-log-dir",
6777
help="directory to write audit logs on test failure (default: tmp/audit-logs)",
@@ -109,6 +119,40 @@ def pytest_addoption(parser: pytest.Parser):
109119
)
110120

111121

122+
def pytest_configure(config: pytest.Config) -> None:
123+
"""Apply --scenario defaults and --instance env-var threading.
124+
125+
When `--scenario PATH` is given, missing `--sdks-encrypt`, `--sdks-decrypt`,
126+
and `--containers` options are populated from the scenario file. Options
127+
explicitly passed on the CLI always win. `--instance NAME` is propagated
128+
via `OTDF_LOCAL_INSTANCE_NAME` so any child `otdf-local` invocation sees
129+
the same instance.
130+
"""
131+
import os
132+
133+
instance = config.getoption("--instance")
134+
if instance:
135+
os.environ["OTDF_LOCAL_INSTANCE_NAME"] = instance
136+
137+
scenario_path = config.getoption("--scenario")
138+
if not scenario_path:
139+
return
140+
try:
141+
from otdf_sdk_mgr.schema import load_scenario
142+
except ImportError:
143+
# otdf-sdk-mgr may not be installed in a minimal pytest env.
144+
return
145+
scenario = load_scenario(scenario_path)
146+
if not config.getoption("--sdks-encrypt") and scenario.sdks.encrypt:
147+
config.option.sdks_encrypt = " ".join(scenario.sdks.encrypt.keys())
148+
if not config.getoption("--sdks-decrypt") and scenario.sdks.decrypt:
149+
config.option.sdks_decrypt = " ".join(scenario.sdks.decrypt.keys())
150+
if not config.getoption("--containers") and scenario.suite.containers:
151+
config.option.containers = scenario.suite.containers
152+
if not instance and scenario.instance.metadata.name:
153+
os.environ["OTDF_LOCAL_INSTANCE_NAME"] = scenario.instance.metadata.name
154+
155+
112156
def pytest_generate_tests(metafunc: pytest.Metafunc):
113157
"""Dynamically parametrize test functions based on CLI options.
114158

0 commit comments

Comments
 (0)