-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_error_hardening.py
More file actions
338 lines (213 loc) · 10.2 KB
/
Copy pathtest_error_hardening.py
File metadata and controls
338 lines (213 loc) · 10.2 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
"""Error hardening tests — empty inputs, None, malformed data, huge strings, unicode."""
from __future__ import annotations
import json
import tempfile
import pytest
from traceagent.mcp_server import MCPServer, _coerce_limit
from traceagent.models import Span, SpanStatus, Trace
from traceagent.storage import FileStorage, InMemoryStorage, _validate_limit, _validate_trace_id
from traceagent.tracer import Tracer
# ── Span name validation ─────────────────────────────────────────────────────
def test_span_none_name_raises():
with pytest.raises((ValueError, TypeError)):
Span(name=None) # type: ignore[arg-type]
def test_span_empty_name_raises():
with pytest.raises(ValueError):
Span(name="")
def test_span_whitespace_only_name_raises():
with pytest.raises(ValueError):
Span(name=" \t\n")
def test_span_non_string_name_raises():
with pytest.raises(TypeError):
Span(name=42) # type: ignore[arg-type]
def test_span_huge_name_truncated():
big = "x" * 10_000
span = Span(name=big)
assert len(span.name) <= 512
def test_span_unicode_name_ok():
span = Span(name="操作 — ünïcödé ✓")
assert "ünïcödé" in span.name
def test_span_name_leading_trailing_whitespace_stripped():
span = Span(name=" hello world ")
assert span.name == "hello world"
# ── Span.add_event validation ────────────────────────────────────────────────
def test_add_event_none_name_raises():
span = Span(name="op")
with pytest.raises((ValueError, TypeError)):
span.add_event(None) # type: ignore[arg-type]
def test_add_event_empty_name_raises():
span = Span(name="op")
with pytest.raises(ValueError):
span.add_event("")
def test_add_event_non_dict_attributes_raises():
span = Span(name="op")
with pytest.raises(TypeError):
span.add_event("ev", attributes="not-a-dict") # type: ignore[arg-type]
def test_add_event_none_attributes_ok():
span = Span(name="op")
span.add_event("ev", attributes=None)
assert span.events[0]["attributes"] == {}
def test_add_event_huge_name_truncated():
span = Span(name="op")
span.add_event("x" * 5000)
assert len(span.events[0]["name"]) <= 256
# ── Span.set_attribute validation ────────────────────────────────────────────
def test_set_attribute_none_key_raises():
span = Span(name="op")
with pytest.raises(ValueError):
span.set_attribute(None, "v") # type: ignore[arg-type]
def test_set_attribute_empty_key_raises():
span = Span(name="op")
with pytest.raises(ValueError):
span.set_attribute("", "v")
def test_set_attribute_non_string_key_raises():
span = Span(name="op")
with pytest.raises(TypeError):
span.set_attribute(123, "v") # type: ignore[arg-type]
def test_set_attribute_huge_string_value_truncated():
span = Span(name="op")
span.set_attribute("key", "v" * 10_000)
assert len(span.attributes["key"]) <= 4096
def test_set_attribute_non_string_value_preserved():
span = Span(name="op")
span.set_attribute("count", 42)
assert span.attributes["count"] == 42
def test_set_attribute_unicode_key_and_value():
span = Span(name="op")
span.set_attribute("emoji_🔑", "value_🎉")
assert span.attributes["emoji_🔑"] == "value_🎉"
def test_set_attribute_none_value_ok():
span = Span(name="op")
span.set_attribute("k", None)
assert span.attributes["k"] is None
# ── Span.end with edge cases ─────────────────────────────────────────────────
def test_span_end_huge_error_truncated():
span = Span(name="op")
span.end(error="E" * 100_000)
assert len(span.error) <= 8192
def test_span_end_empty_error_does_not_override_ok():
span = Span(name="op")
span.end(error="")
# empty string is falsy — should not force ERROR status
assert span.status == SpanStatus.OK
# ── Trace.add_span validation ─────────────────────────────────────────────────
def test_trace_add_span_non_span_raises():
trace = Trace(trace_id="t1")
with pytest.raises(TypeError):
trace.add_span("not-a-span") # type: ignore[arg-type]
def test_trace_add_span_none_raises():
trace = Trace(trace_id="t1")
with pytest.raises(TypeError):
trace.add_span(None) # type: ignore[arg-type]
# ── Storage validation helpers ───────────────────────────────────────────────
def test_validate_trace_id_none_raises():
with pytest.raises(ValueError):
_validate_trace_id(None) # type: ignore[arg-type]
def test_validate_trace_id_empty_raises():
with pytest.raises(ValueError):
_validate_trace_id("")
def test_validate_trace_id_non_string_raises():
with pytest.raises(TypeError):
_validate_trace_id(123) # type: ignore[arg-type]
def test_validate_trace_id_huge_truncated():
long_id = "a" * 1000
result = _validate_trace_id(long_id)
assert len(result) <= 128
def test_validate_limit_non_int_raises():
with pytest.raises(TypeError):
_validate_limit("bad") # type: ignore[arg-type]
def test_validate_limit_negative_clamped():
assert _validate_limit(-10) == 0
def test_validate_limit_huge_clamped():
assert _validate_limit(999_999) == 10_000
# ── InMemoryStorage error paths ───────────────────────────────────────────────
def test_inmemory_save_non_span_raises():
store = InMemoryStorage()
with pytest.raises(TypeError):
store.save_span("not-a-span") # type: ignore[arg-type]
def test_inmemory_get_trace_none_raises():
store = InMemoryStorage()
with pytest.raises(ValueError):
store.get_trace(None) # type: ignore[arg-type]
def test_inmemory_get_trace_empty_raises():
store = InMemoryStorage()
with pytest.raises(ValueError):
store.get_trace("")
def test_inmemory_list_traces_bad_limit_raises():
store = InMemoryStorage()
with pytest.raises(TypeError):
store.list_traces(limit="bad") # type: ignore[arg-type]
# ── FileStorage error paths ───────────────────────────────────────────────────
def test_file_storage_none_path_raises():
with pytest.raises(ValueError):
FileStorage(None) # type: ignore[arg-type]
def test_file_storage_save_non_span_raises():
with tempfile.TemporaryDirectory() as d:
store = FileStorage(d)
with pytest.raises(TypeError):
store.save_span("not-a-span") # type: ignore[arg-type]
def test_file_storage_get_trace_empty_raises():
with tempfile.TemporaryDirectory() as d:
store = FileStorage(d)
with pytest.raises(ValueError):
store.get_trace("")
def test_file_storage_skips_corrupt_json(tmp_path):
"""list_traces logs a warning and skips files with invalid JSON."""
store = FileStorage(tmp_path)
# Write a legitimate span first
span = Span(name="ok")
span.end()
store.save_span(span)
# Corrupt a file in the directory
(tmp_path / "corrupt.json").write_text("{invalid json}")
# Should return 1 trace (not raise)
traces = store.list_traces()
assert len(traces) == 1
# ── MCPServer error paths ─────────────────────────────────────────────────────
def test_mcp_empty_tool_name():
server = MCPServer(tracer=Tracer(storage=InMemoryStorage()))
result = json.loads(server.call_tool(""))
assert "error" in result
def test_mcp_none_tool_name():
server = MCPServer(tracer=Tracer(storage=InMemoryStorage()))
result = json.loads(server.call_tool(None)) # type: ignore[arg-type]
assert "error" in result
def test_mcp_get_trace_empty_id():
server = MCPServer(tracer=Tracer(storage=InMemoryStorage()))
result = json.loads(server.call_tool("get_trace", {"trace_id": ""}))
assert "error" in result
def test_mcp_get_trace_missing_key():
"""get_trace with no trace_id in args returns error (not KeyError crash)."""
server = MCPServer(tracer=Tracer(storage=InMemoryStorage()))
result = json.loads(server.call_tool("get_trace", {}))
assert "error" in result
def test_mcp_list_traces_bad_limit():
"""list_traces with a string limit falls back to default gracefully."""
tracer = Tracer(storage=InMemoryStorage())
with tracer.start_span("x"):
pass
server = MCPServer(tracer=tracer)
result = json.loads(server.call_tool("list_traces", {"limit": "bad"}))
# Should succeed with fallback limit, returning traces list
assert "traces" in result
def test_mcp_list_traces_negative_limit():
server = MCPServer(tracer=Tracer(storage=InMemoryStorage()))
result = json.loads(server.call_tool("list_traces", {"limit": -1}))
assert "traces" in result
def test_mcp_call_tool_none_arguments():
server = MCPServer(tracer=Tracer(storage=InMemoryStorage()))
result = json.loads(server.call_tool("get_stats", None))
assert "trace_count" in result
# ── Coerce limit helper ───────────────────────────────────────────────────────
def test_coerce_limit_none_returns_default():
assert _coerce_limit(None) == 20
def test_coerce_limit_string_number():
assert _coerce_limit("10") == 10
def test_coerce_limit_float_truncates():
assert _coerce_limit(5.9) == 5
def test_coerce_limit_non_numeric_string():
assert _coerce_limit("abc") == 20
def test_coerce_limit_negative():
assert _coerce_limit(-5) == 20
def test_coerce_limit_huge_clamped():
assert _coerce_limit(999_999) == 1000