-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
174 lines (138 loc) · 5.38 KB
/
Copy pathgenerator.py
File metadata and controls
174 lines (138 loc) · 5.38 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""Core test data generator module.
Reads handlers.yaml and provides a unified API for generating
test result files for different handler types.
"""
import os
from pathlib import Path
from typing import Dict, List, Optional
import yaml
# Project root (two levels up from tests/data_generator)
PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
HANDLERS_YAML = PROJECT_ROOT / "handlers.yaml"
def load_handlers_registry() -> List[Dict]:
"""Load handler definitions from handlers.yaml.
Returns:
List of handler definitions, each with 'name', 'title', 'class_import',
'result_ext', and optional 'handler_params'.
"""
if not HANDLERS_YAML.exists():
raise FileNotFoundError(
f"handlers.yaml not found at {HANDLERS_YAML}. "
"This file is required for test data generation."
)
with open(HANDLERS_YAML, "r", encoding="utf-8") as f:
data = yaml.safe_load(f)
if not data or "handlers" not in data:
raise ValueError("handlers.yaml must contain a 'handlers' key")
return data["handlers"]
def get_supported_handlers() -> List[str]:
"""Return list of handler names from handlers.yaml.
Returns:
List of handler names (e.g., ['junit', 'gatling', 'zaproxy'])
"""
handlers = load_handlers_registry()
return [h["name"] for h in handlers]
def generate_handler_file(
handler_name: str,
output_path: Path,
test_status: str = "passed",
**kwargs
) -> Path:
"""Generate a test result file for the specified handler.
Args:
handler_name: Name of handler (e.g., 'junit', 'zaproxy', 'gatling')
output_path: Path where the result file should be written
test_status: Test outcome - 'passed', 'failed', or 'mixed' (default: 'passed')
**kwargs: Handler-specific parameters (e.g., num_tests, duration_ms)
Returns:
Path to the generated file
Raises:
ValueError: If handler_name is not supported
RuntimeError: If handler generator fails
"""
handlers = load_handlers_registry()
handler_def = next((h for h in handlers if h["name"] == handler_name), None)
if not handler_def:
supported = [h["name"] for h in handlers]
raise ValueError(
f"Handler '{handler_name}' not found in handlers.yaml. "
f"Supported handlers: {', '.join(supported)}"
)
# Import handler-specific generator dynamically
# Map handler names to generator module names
handler_to_module = {
"zaproxy": "zap",
"junit": "junit",
"gatling": "gatling",
"locust": "locust",
}
module_name = handler_to_module.get(handler_name, handler_name)
try:
from . import handlers as handler_module
generator_name = f"{module_name}_generator"
generator = getattr(handler_module, generator_name)
except (ImportError, AttributeError) as e:
raise RuntimeError(
f"Could not import generator for '{handler_name}'. "
f"Expected module: tests.data_generator.handlers.{generator_name}"
) from e
# Call handler-specific generator
try:
generator.generate(
output_path=output_path,
test_status=test_status,
handler_def=handler_def,
**kwargs
)
except Exception as e:
raise RuntimeError(
f"Failed to generate test data for handler '{handler_name}': {e}"
) from e
# Ensure generated file is world-readable so other users/processes can read test data
try:
if output_path.exists():
output_path.chmod(0o644)
except Exception:
# Non-fatal: permission change may not be supported on all platforms
pass
return output_path
def generate_all_handler_files(
output_dir: Path,
test_status: str = "passed",
filename_pattern: str = "{handler}.{ext}"
) -> Dict[str, Path]:
"""Generate test result files for all supported handlers.
Args:
output_dir: Directory where files should be written
test_status: Test outcome - 'passed', 'failed', or 'mixed'
filename_pattern: Pattern for output filenames (variables: {handler}, {ext})
Returns:
Dict mapping handler name to generated file path
Example:
>>> files = generate_all_handler_files(Path("tests/e2e/data"))
>>> files
{'junit': Path('tests/e2e/data/junit.xml'),
'gatling': Path('tests/e2e/data/gatling.log'),
'zaproxy': Path('tests/e2e/data/zaproxy.xml')}
"""
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
try:
# Ensure directory is accessible so files within can be read by other users
output_dir.chmod(0o755)
except Exception:
pass
handlers = load_handlers_registry()
generated_files = {}
for handler_def in handlers:
handler_name = handler_def["name"]
result_ext = handler_def.get("result_ext", "txt")
filename = filename_pattern.format(handler=handler_name, ext=result_ext)
output_path = output_dir / filename
generate_handler_file(
handler_name=handler_name,
output_path=output_path,
test_status=test_status
)
generated_files[handler_name] = output_path
return generated_files