-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cli_subprocess.py
More file actions
172 lines (147 loc) · 4.72 KB
/
Copy pathtest_cli_subprocess.py
File metadata and controls
172 lines (147 loc) · 4.72 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
from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
import yaml
from telemetry_lab.io import load_config
def _cli_env(repo_root: Path) -> dict[str, str]:
env = os.environ.copy()
src_path = str(repo_root / "src")
existing_pythonpath = env.get("PYTHONPATH")
env["PYTHONPATH"] = (
src_path
if not existing_pythonpath
else os.pathsep.join((src_path, existing_pythonpath))
)
return env
def test_readme_summarize_command_runs_as_module() -> None:
repo_root = Path(__file__).resolve().parents[1]
result = subprocess.run(
[
sys.executable,
"-m",
"telemetry_lab.cli",
"summarize",
"--input",
"data/raw/sample_events.jsonl",
],
cwd=repo_root,
env=_cli_env(repo_root),
text=True,
capture_output=True,
timeout=30,
)
assert result.returncode == 0, result.stderr
assert "events: 41" in result.stdout
assert "overall_error_rate: 0.61" in result.stdout
def test_readme_default_run_command_writes_expected_artifacts(tmp_path) -> None:
repo_root = Path(__file__).resolve().parents[1]
config = load_config(repo_root / "configs" / "default.yaml")
output_dir = tmp_path / "processed"
config["input_path"] = str((repo_root / "data" / "raw" / "sample_events.jsonl").resolve())
config["output_dir"] = str(output_dir.resolve())
config_path = tmp_path / "default.yaml"
config_path.write_text(yaml.safe_dump(config, sort_keys=False), encoding="utf-8")
result = subprocess.run(
[
sys.executable,
"-m",
"telemetry_lab.cli",
"run",
"window",
"--config",
str(config_path),
],
cwd=repo_root,
env=_cli_env(repo_root),
text=True,
capture_output=True,
timeout=30,
)
assert result.returncode == 0, result.stderr
assert "[OK] Loaded 41 events" in result.stdout
assert "[OK] Triggered 12 alerts" in result.stdout
assert (output_dir / "features.csv").is_file()
assert (output_dir / "alerts.csv").is_file()
assert (output_dir / "summary.json").is_file()
assert (output_dir / "run_manifest.json").is_file()
assert (output_dir / "event_count_timeline.png").is_file()
def test_plot_command_runs_as_module(tmp_path) -> None:
repo_root = Path(__file__).resolve().parents[1]
output_dir = tmp_path / "plots"
result = subprocess.run(
[
sys.executable,
"-m",
"telemetry_lab.cli",
"plot",
"--features",
"data/processed/features.csv",
"--alerts",
"data/processed/alerts.csv",
"--output-dir",
str(output_dir),
],
cwd=repo_root,
env=_cli_env(repo_root),
text=True,
capture_output=True,
timeout=30,
)
assert result.returncode == 0, result.stderr
assert "[OK] Saved plots to" in result.stdout
assert (output_dir / "event_count_timeline.png").is_file()
assert (output_dir / "error_rate_timeline.png").is_file()
assert (output_dir / "alerts_timeline.png").is_file()
def test_cloud_iam_demo_command_runs_as_module() -> None:
repo_root = Path(__file__).resolve().parents[1]
result = subprocess.run(
[
sys.executable,
"-m",
"telemetry_lab.cli",
"run",
"cloud-iam",
],
cwd=repo_root,
env=_cli_env(repo_root),
text=True,
capture_output=True,
timeout=30,
)
assert result.returncode == 0, result.stderr
assert "[OK] Loaded 14 CloudTrail-like events" in result.stdout
assert "[OK] Built 5 investigation signals" in result.stdout
assert (
repo_root
/ "demos"
/ "cloud-iam-change-investigation-demo"
/ "artifacts"
/ "run_manifest.json"
).is_file()
def test_legacy_cli_module_remains_compatible() -> None:
repo_root = Path(__file__).resolve().parents[1]
result = subprocess.run(
[
sys.executable,
"-m",
"telemetry_window_demo.cli",
"run-cloud-iam-change-demo",
],
cwd=repo_root,
env=_cli_env(repo_root),
text=True,
capture_output=True,
timeout=30,
)
assert result.returncode == 0, result.stderr
assert "[OK] Loaded 14 CloudTrail-like events" in result.stdout
assert "[OK] Built 5 investigation signals" in result.stdout
assert (
repo_root
/ "demos"
/ "cloud-iam-change-investigation-demo"
/ "artifacts"
/ "investigation_report.md"
).is_file()