-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathconftest.py
More file actions
110 lines (87 loc) · 3.59 KB
/
conftest.py
File metadata and controls
110 lines (87 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import re
import pytest
from pathlib import Path
from typing import Tuple, List
from tools.report_generator import generate_report
from tools.k8s_resources_collector import collect_k8s_resources, get_namespace
def pytest_addoption(parser):
"""Add custom command line option for test suite file"""
parser.addoption(
"--test-suite",
action="store",
default=None,
help="Name of the test suite file (will look for run-{name}.csv)",
)
parser.addoption(
"--test-regex",
action="store",
default=None,
help="Run tests matching the given regex pattern",
)
parser.addoption(
"--collect-k8s-resources",
action="store_true",
default=False,
help="Enable collection of K8s resources on test failure",
)
def get_bash_tests(test_suite: str = "") -> List[Tuple[str, Path]]:
"""Get bash test scripts from file or all directories"""
current_dir = Path(__file__).parent
bash_tests: List[Tuple[str, Path]] = []
if test_suite:
file_path = current_dir / f"run-{test_suite}.csv"
if not file_path.exists():
raise FileNotFoundError(f"Test suite file not found: {file_path}")
with open(file_path, "r", encoding="utf-8") as f:
test_names = [line.strip() for line in f if line.strip()]
else:
test_names = [d.name for d in current_dir.iterdir() if d.is_dir()]
for test_name in test_names:
test_dir = current_dir / test_name
run_script = test_dir / "run"
if run_script.exists():
bash_tests.append((test_name, run_script))
return bash_tests
def pytest_generate_tests(metafunc):
"""Generate tests dynamically with regex filtering"""
if "test_name" in metafunc.fixturenames and "script_path" in metafunc.fixturenames:
test_suite = metafunc.config.getoption("--test-suite")
test_regex = metafunc.config.getoption("--test-regex")
bash_tests = get_bash_tests(test_suite)
if test_regex:
try:
pattern = re.compile(test_regex)
filtered_tests = [
(name, path) for name, path in bash_tests if pattern.search(name)
]
bash_tests = filtered_tests
print(f"\nFiltered to {len(bash_tests)} test(s) matching regex '{test_regex}':")
for name, _ in bash_tests:
print(f" - {name}")
except re.error as e:
pytest.exit(f"Invalid regex pattern '{test_regex}': {e}")
metafunc.parametrize(
"test_name,script_path", bash_tests, ids=[name for name, _ in bash_tests]
)
def pytest_html_report_title(report):
report.title = "PSMDB E2E Test Report"
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if report.when == "call" and report.failed:
try:
namespace = get_namespace(str(report.longrepr))
html_report = generate_report(namespace)
if not hasattr(report, "extras"):
report.extras = []
report.extras.extend(html_report)
collect_resources = item.config.getoption("--collect-k8s-resources")
if collect_resources:
collect_k8s_resources(
namespace=namespace,
custom_resources=["psmdb", "psmdb-backup", "psmdb-restore"],
output_dir=f"e2e-tests/reports/{namespace}",
)
except Exception as e:
print(f"Error adding K8s info: {e}")