-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cli_errors.py
More file actions
355 lines (293 loc) · 11 KB
/
Copy pathtest_cli_errors.py
File metadata and controls
355 lines (293 loc) · 11 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from __future__ import annotations
import pytest
import yaml
from telemetry_lab.cli import main
def test_main_reports_config_errors_without_traceback(tmp_path, capsys) -> None:
config_path = tmp_path / "missing-input-path.yaml"
config_path.write_text(
yaml.safe_dump({"output_dir": str(tmp_path / "processed")}),
encoding="utf-8",
)
with pytest.raises(SystemExit) as excinfo:
main(["run", "--config", str(config_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "input_path" in stderr
assert "Traceback" not in stderr
def test_main_reports_missing_config_without_traceback(tmp_path, capsys) -> None:
config_path = tmp_path / "missing.yaml"
with pytest.raises(SystemExit) as excinfo:
main(["run", "--config", str(config_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Config file not found" in stderr
assert "Traceback" not in stderr
def test_main_reports_unknown_run_config_field_without_traceback(
tmp_path,
capsys,
) -> None:
config_path = tmp_path / "typo.yaml"
config_path.write_text(
yaml.safe_dump(
{
"input": "events.csv",
"output_dir": str(tmp_path / "processed"),
}
),
encoding="utf-8",
)
with pytest.raises(SystemExit) as excinfo:
main(["run", "--config", str(config_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Unknown config field" in stderr
assert "input" in stderr
assert "Traceback" not in stderr
def test_main_reports_invalid_yaml_config_without_traceback(tmp_path, capsys) -> None:
config_path = tmp_path / "broken.yaml"
config_path.write_text("input_path: [\n", encoding="utf-8")
with pytest.raises(SystemExit) as excinfo:
main(["run", "--config", str(config_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Invalid YAML config" in stderr
assert "Traceback" not in stderr
def test_main_reports_directory_input_without_traceback(tmp_path, capsys) -> None:
config_path = tmp_path / "directory-input.yaml"
config_path.write_text(
yaml.safe_dump(
{
"input_path": str(tmp_path),
"output_dir": str(tmp_path / "processed"),
}
),
encoding="utf-8",
)
with pytest.raises(SystemExit) as excinfo:
main(["run", "--config", str(config_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Input file path is not a file" in stderr
assert "Traceback" not in stderr
def test_main_reports_file_output_dir_without_traceback(tmp_path, capsys) -> None:
input_path = tmp_path / "events.csv"
input_path.write_text(
"timestamp,event_type,source,target,status\n"
"2026-03-10T10:00:00Z,login_success,user_a,auth,ok\n",
encoding="utf-8",
)
output_path = tmp_path / "processed"
output_path.write_text("not a directory\n", encoding="utf-8")
config_path = tmp_path / "file-output-dir.yaml"
config_path.write_text(
yaml.safe_dump(
{
"input_path": str(input_path),
"output_dir": str(output_path),
}
),
encoding="utf-8",
)
with pytest.raises(SystemExit) as excinfo:
main(["run", "--config", str(config_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Output directory path is not a directory" in stderr
assert "Traceback" not in stderr
def test_main_reports_directory_output_artifact_without_traceback(tmp_path, capsys) -> None:
input_path = tmp_path / "events.csv"
input_path.write_text(
"timestamp,event_type,source,target,status\n"
"2026-03-10T10:00:00Z,login_success,user_a,auth,ok\n",
encoding="utf-8",
)
output_dir = tmp_path / "processed"
(output_dir / "features.csv").mkdir(parents=True)
config_path = tmp_path / "directory-output-artifact.yaml"
config_path.write_text(
yaml.safe_dump(
{
"input_path": str(input_path),
"output_dir": str(output_dir),
}
),
encoding="utf-8",
)
with pytest.raises(SystemExit) as excinfo:
main(["run", "--config", str(config_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Output file path is a directory" in stderr
assert "features.csv" in stderr
assert "Traceback" not in stderr
def test_main_reports_bad_summarize_timestamp_column_without_traceback(
tmp_path,
capsys,
) -> None:
input_path = tmp_path / "events.csv"
input_path.write_text(
"event_type,source,target,status\n"
"2026-03-10T10:00:00Z,user_a,auth,ok\n",
encoding="utf-8",
)
with pytest.raises(SystemExit) as excinfo:
main(
[
"summarize",
"--input",
str(input_path),
"--timestamp-col",
"event_type",
]
)
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "timestamp-col" in stderr
assert "event_type" in stderr
assert "Traceback" not in stderr
def test_main_reports_bad_plot_feature_table_without_traceback(tmp_path, capsys) -> None:
features_path = tmp_path / "features.csv"
features_path.write_text(
"window_start,window_end,error_rate\n"
"2026-03-10T10:00:00Z,2026-03-10T10:01:00Z,0.25\n",
encoding="utf-8",
)
with pytest.raises(SystemExit) as excinfo:
main(["plot", "--features", str(features_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "event_count" in stderr
assert "Traceback" not in stderr
def test_main_reports_bad_plot_feature_value_without_traceback(tmp_path, capsys) -> None:
features_path = tmp_path / "features.csv"
features_path.write_text(
"window_start,window_end,event_count,error_rate\n"
"2026-03-10T10:00:00Z,2026-03-10T10:01:00Z,not-a-count,0.25\n",
encoding="utf-8",
)
with pytest.raises(SystemExit) as excinfo:
main(["plot", "--features", str(features_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Invalid numeric values" in stderr
assert "event_count" in stderr
assert "Traceback" not in stderr
def test_main_reports_missing_default_alert_table_without_traceback(tmp_path, capsys) -> None:
features_path = tmp_path / "features.csv"
features_path.write_text(
"window_start,window_end,event_count,error_rate\n"
"2026-03-10T10:00:00Z,2026-03-10T10:01:00Z,10,0.25\n",
encoding="utf-8",
)
with pytest.raises(SystemExit) as excinfo:
main(["plot", "--features", str(features_path)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Alert table not found" in stderr
assert "Traceback" not in stderr
def test_main_reports_file_plot_output_dir_without_traceback(tmp_path, capsys) -> None:
features_path = tmp_path / "features.csv"
features_path.write_text(
"window_start,window_end,event_count,error_rate\n"
"2026-03-10T10:00:00Z,2026-03-10T10:01:00Z,10,0.25\n",
encoding="utf-8",
)
alerts_path = tmp_path / "alerts.csv"
alerts_path.write_text(
"alert_time,window_start,window_end,rule_name,severity\n"
"2026-03-10T10:01:00Z,2026-03-10T10:00:00Z,"
"2026-03-10T10:01:00Z,high_error_rate,medium\n",
encoding="utf-8",
)
output_path = tmp_path / "plots"
output_path.write_text("not a directory\n", encoding="utf-8")
with pytest.raises(SystemExit) as excinfo:
main(
[
"plot",
"--features",
str(features_path),
"--alerts",
str(alerts_path),
"--output-dir",
str(output_path),
]
)
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Output directory path is not a directory" in stderr
assert "Traceback" not in stderr
@pytest.mark.parametrize(
"command",
[
"run-ai-demo",
"run-rule-dedup-demo",
"run-config-change-demo",
"run-cloud-iam-change-demo",
],
)
def test_main_reports_file_demo_root_without_traceback(command, tmp_path, capsys) -> None:
demo_root = tmp_path / "demo-root"
demo_root.write_text("not a directory\n", encoding="utf-8")
with pytest.raises(SystemExit) as excinfo:
main([command, "--demo-root", str(demo_root)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Demo root path is not a directory" in stderr
assert "Traceback" not in stderr
def test_main_reports_missing_demo_root_without_traceback(tmp_path, capsys) -> None:
demo_root = tmp_path / "missing-demo-root"
with pytest.raises(SystemExit) as excinfo:
main(["run-ai-demo", "--demo-root", str(demo_root)])
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Demo root not found" in stderr
assert "Traceback" not in stderr
def test_main_reports_directory_plot_artifact_without_traceback(tmp_path, capsys) -> None:
features_path = tmp_path / "features.csv"
features_path.write_text(
"window_start,window_end,event_count,error_rate\n"
"2026-03-10T10:00:00Z,2026-03-10T10:01:00Z,10,0.25\n",
encoding="utf-8",
)
alerts_path = tmp_path / "alerts.csv"
alerts_path.write_text(
"alert_time,window_start,window_end,rule_name,severity\n"
"2026-03-10T10:01:00Z,2026-03-10T10:00:00Z,"
"2026-03-10T10:01:00Z,high_error_rate,medium\n",
encoding="utf-8",
)
output_dir = tmp_path / "plots"
(output_dir / "event_count_timeline.png").mkdir(parents=True)
with pytest.raises(SystemExit) as excinfo:
main(
[
"plot",
"--features",
str(features_path),
"--alerts",
str(alerts_path),
"--output-dir",
str(output_dir),
]
)
assert excinfo.value.code == 1
stderr = capsys.readouterr().err
assert stderr.startswith("error: ")
assert "Output file path is a directory" in stderr
assert "event_count_timeline.png" in stderr
assert "Traceback" not in stderr