-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathtest_check_agent_server_rest_api_breakage.py
More file actions
647 lines (547 loc) · 21.2 KB
/
test_check_agent_server_rest_api_breakage.py
File metadata and controls
647 lines (547 loc) · 21.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
"""Tests for agent-server REST API breakage check script."""
from __future__ import annotations
import importlib.util
import json
import sys
from pathlib import Path
import pytest
def _load_script_module(name: str):
repo_root = Path(__file__).resolve().parents[2]
script_path = repo_root / ".github" / "scripts" / f"{name}.py"
spec = importlib.util.spec_from_file_location(name, script_path)
assert spec and spec.loader
mod = importlib.util.module_from_spec(spec)
sys.modules[name] = mod
spec.loader.exec_module(mod)
return mod
_prod = _load_script_module("check_agent_server_rest_api_breakage")
_deprecations_prod = _load_script_module("check_deprecations")
_find_deprecation_policy_errors = _prod._find_deprecation_policy_errors
_find_sdk_deprecated_fastapi_routes_in_file = (
_prod._find_sdk_deprecated_fastapi_routes_in_file
)
_get_baseline_version = _prod._get_baseline_version
_normalize_openapi_for_oasdiff = _prod._normalize_openapi_for_oasdiff
_parse_openapi_deprecation_description = _prod._parse_openapi_deprecation_description
_validate_removed_operations = _prod._validate_removed_operations
_rest_route_deprecation_re = _prod.REST_ROUTE_DEPRECATION_RE
_deprecation_check_re = _deprecations_prod.REST_ROUTE_DEPRECATION_RE
def _schema_with_operation(path: str, method: str, operation: dict) -> dict:
return {
"openapi": "3.0.0",
"paths": {
path: {
method: operation,
}
},
}
def test_find_deprecation_policy_errors_requires_openapi_deprecated_flag():
schema = _schema_with_operation(
"/foo",
"get",
{
"description": (
"Deprecated since v1.2.3 and scheduled for removal in v1.5.0."
),
"responses": {},
},
)
assert _find_deprecation_policy_errors(schema) == [
"GET /foo documents deprecation in its description but is not marked "
"deprecated=true in OpenAPI."
]
def test_find_deprecation_policy_errors_accepts_deprecated_operations():
schema = _schema_with_operation(
"/foo",
"get",
{
"deprecated": True,
"description": (
"Deprecated since v1.2.3 and scheduled for removal in v1.5.0."
),
"responses": {},
},
)
assert _find_deprecation_policy_errors(schema) == []
def test_find_deprecation_policy_errors_ignores_non_deprecated_operations():
schema = _schema_with_operation(
"/foo",
"get",
{
"description": "Current endpoint.",
"responses": {},
},
)
assert _find_deprecation_policy_errors(schema) == []
def test_find_sdk_deprecated_fastapi_routes_in_file_flags_direct_import(tmp_path):
repo_root = tmp_path
source = repo_root / "openhands-agent-server" / "openhands" / "agent_server"
source.mkdir(parents=True)
file_path = source / "router.py"
file_path.write_text(
"from openhands.sdk.utils.deprecation import deprecated\n"
"\n"
'@router.get("/foo")\n'
'@deprecated(deprecated_in="1.0.0", removed_in="1.1.0")\n'
"async def foo():\n"
" return {}\n"
)
errors = _find_sdk_deprecated_fastapi_routes_in_file(file_path, repo_root)
assert errors == [
"openhands-agent-server/openhands/agent_server/router.py:5 FastAPI route "
"`foo` uses openhands.sdk.utils.deprecation.deprecated; use the route "
"decorator's deprecated=True flag instead."
]
def test_find_sdk_deprecated_fastapi_routes_in_file_flags_alias_import(tmp_path):
repo_root = tmp_path
source = repo_root / "openhands-agent-server" / "openhands" / "agent_server"
source.mkdir(parents=True)
file_path = source / "router.py"
file_path.write_text(
"import openhands.sdk.utils.deprecation as dep\n"
"\n"
'@router.post("/foo")\n'
'@dep.deprecated(deprecated_in="1.0.0", removed_in="1.1.0")\n'
"async def foo():\n"
" return {}\n"
)
errors = _find_sdk_deprecated_fastapi_routes_in_file(file_path, repo_root)
assert errors == [
"openhands-agent-server/openhands/agent_server/router.py:5 FastAPI route "
"`foo` uses openhands.sdk.utils.deprecation.deprecated; use the route "
"decorator's deprecated=True flag instead."
]
def test_find_sdk_deprecated_fastapi_routes_in_file_ignores_non_route_usage(tmp_path):
repo_root = tmp_path
source = repo_root / "openhands-agent-server" / "openhands" / "agent_server"
source.mkdir(parents=True)
file_path = source / "helpers.py"
file_path.write_text(
"from openhands.sdk.utils.deprecation import deprecated\n"
"\n"
'@deprecated(deprecated_in="1.0.0", removed_in="1.1.0")\n'
"def helper():\n"
" return None\n"
)
assert _find_sdk_deprecated_fastapi_routes_in_file(file_path, repo_root) == []
def test_get_baseline_version_warns_and_returns_none_when_pypi_fails(
monkeypatch, capsys
):
def _raise(_distribution: str) -> dict: # pragma: no cover
raise RuntimeError("boom")
monkeypatch.setattr(_prod, "_fetch_pypi_metadata", _raise)
assert _get_baseline_version("some-dist", "1.0.0") is None
captured = capsys.readouterr()
assert "::warning" in captured.out
assert "Failed to fetch PyPI metadata" in captured.out
def test_rest_deprecation_regex_matches_deprecation_check_regex():
assert _rest_route_deprecation_re.pattern == _deprecation_check_re.pattern
assert _rest_route_deprecation_re.flags == _deprecation_check_re.flags
def test_parse_openapi_deprecation_description_extracts_versions_from_example():
description = (
"Nice description here with more context for API consumers.\n\n"
" Deprecated since v1.14.0 and scheduled for removal in v1.19.0."
)
assert _parse_openapi_deprecation_description(description) == ("1.14.0", "1.19.0")
def test_validate_removed_operations_rejects_malformed_removal_version():
prev_schema = _schema_with_operation(
"/foo",
"get",
{
"deprecated": True,
"description": (
"Nice description here.\n\n"
" Deprecated since v1.14.0 and scheduled for removal in v1.x.0."
),
"responses": {},
},
)
with pytest.raises(SystemExit, match="Invalid semantic version comparison"):
_validate_removed_operations(
[{"path": "/foo", "method": "get", "deprecated": True}],
prev_schema,
"1.19.0",
)
def test_validate_removed_operations_requires_scheduled_removal_version():
prev_schema = _schema_with_operation(
"/foo",
"get",
{
"deprecated": True,
"description": "Deprecated endpoint.",
"responses": {},
},
)
errors = _validate_removed_operations(
[{"path": "/foo", "method": "get", "deprecated": True}],
prev_schema,
"1.19.0",
)
assert errors == [
"Removed GET /foo was marked deprecated in the baseline release, but its "
"OpenAPI description does not declare a scheduled removal version. REST "
"API removals require 5 minor releases of deprecation runway."
]
def test_validate_removed_operations_requires_removal_target_to_be_reached():
prev_schema = _schema_with_operation(
"/foo",
"get",
{
"deprecated": True,
"description": (
"Deprecated since v1.14.0 and scheduled for removal in v1.19.0."
),
"responses": {},
},
)
errors = _validate_removed_operations(
[{"path": "/foo", "method": "get", "deprecated": True}],
prev_schema,
"1.18.0",
)
assert errors == [
"Removed GET /foo before its scheduled removal version v1.19.0 (current "
"version: v1.18.0). REST API removals require 5 minor releases of "
"deprecation runway."
]
def test_validate_removed_operations_allows_scheduled_removal(capsys):
prev_schema = _schema_with_operation(
"/foo",
"get",
{
"deprecated": True,
"description": (
"Deprecated since v1.14.0 and scheduled for removal in v1.19.0."
),
"responses": {},
},
)
errors = _validate_removed_operations(
[{"path": "/foo", "method": "get", "deprecated": True}],
prev_schema,
"1.19.0",
)
assert errors == []
assert "scheduled removal version v1.19.0" in capsys.readouterr().out
def test_main_allows_scheduled_removal_with_documented_target(monkeypatch, capsys):
prev_schema = _schema_with_operation(
"/foo",
"get",
{
"deprecated": True,
"description": (
"Nice description here.\n\n"
" Deprecated since v1.9.0 and scheduled for removal in v1.14.0."
),
"responses": {},
},
)
monkeypatch.setattr(_prod, "_read_version_from_pyproject", lambda _path: "1.14.0")
monkeypatch.setattr(
_prod, "_get_baseline_version", lambda _distribution, _current: "1.13.0"
)
monkeypatch.setattr(_prod, "_find_sdk_deprecated_fastapi_routes", lambda _root: [])
monkeypatch.setattr(_prod, "_generate_current_openapi", lambda: {"paths": {}})
monkeypatch.setattr(_prod, "_find_deprecation_policy_errors", lambda _schema: [])
monkeypatch.setattr(
_prod, "_generate_openapi_for_git_ref", lambda _ref: prev_schema
)
monkeypatch.setattr(_prod, "_normalize_openapi_for_oasdiff", lambda schema: schema)
monkeypatch.setattr(
_prod,
"_run_oasdiff_breakage_check",
lambda _prev, _cur: (
[
{
"id": "removed-operation",
"details": {"path": "/foo", "method": "get", "deprecated": True},
"text": "removed GET /foo",
}
],
1,
),
)
assert _prod.main() == 0
captured = capsys.readouterr()
assert "MINOR version bump" not in captured.out
assert "scheduled removal versions have been reached" in captured.out
def test_main_allows_scheduled_removal_when_baseline_matches_current(
monkeypatch, capsys
):
prev_schema = _schema_with_operation(
"/foo",
"get",
{
"deprecated": True,
"description": (
"Nice description here.\n\n"
" Deprecated since v1.9.0 and scheduled for removal in v1.14.0."
),
"responses": {},
},
)
monkeypatch.setattr(_prod, "_read_version_from_pyproject", lambda _path: "1.14.0")
monkeypatch.setattr(
_prod, "_get_baseline_version", lambda _distribution, _current: "1.14.0"
)
monkeypatch.setattr(_prod, "_find_sdk_deprecated_fastapi_routes", lambda _root: [])
monkeypatch.setattr(_prod, "_generate_current_openapi", lambda: {"paths": {}})
monkeypatch.setattr(_prod, "_find_deprecation_policy_errors", lambda _schema: [])
monkeypatch.setattr(
_prod, "_generate_openapi_for_git_ref", lambda _ref: prev_schema
)
monkeypatch.setattr(_prod, "_normalize_openapi_for_oasdiff", lambda schema: schema)
monkeypatch.setattr(
_prod,
"_run_oasdiff_breakage_check",
lambda _prev, _cur: (
[
{
"id": "removed-operation",
"details": {"path": "/foo", "method": "get", "deprecated": True},
"text": "removed GET /foo",
}
],
1,
),
)
assert _prod.main() == 0
captured = capsys.readouterr()
assert "scheduled removal versions have been reached" in captured.out
def test_main_rejects_breakage_against_pr_base_ref(monkeypatch, capsys):
monkeypatch.setenv(_prod.BASE_REF_ENV, "main")
monkeypatch.setattr(_prod, "_read_version_from_pyproject", lambda _path: "1.16.1")
monkeypatch.setattr(
_prod, "_get_baseline_version", lambda _distribution, _current: "1.16.1"
)
monkeypatch.setattr(_prod, "_resolve_git_ref", lambda ref: f"origin/{ref}")
monkeypatch.setattr(_prod, "_find_sdk_deprecated_fastapi_routes", lambda _root: [])
monkeypatch.setattr(_prod, "_find_deprecation_policy_errors", lambda _schema: [])
monkeypatch.setattr(_prod, "_generate_current_openapi", lambda: {"paths": {}})
base_schema = _schema_with_operation("/foo", "get", {"responses": {}})
monkeypatch.setattr(
_prod,
"_generate_openapi_for_git_ref",
lambda ref: base_schema if ref == "origin/main" else {"paths": {}},
)
def _fake_run(prev_spec, _cur_spec):
prev_schema = json.loads(Path(prev_spec).read_text())
if "/foo" in prev_schema.get("paths", {}):
return (
[
{
"id": "removed-operation",
"details": {
"path": "/foo",
"method": "get",
"deprecated": False,
},
"text": "removed GET /foo",
}
],
1,
)
return [], 0
monkeypatch.setattr(_prod, "_run_oasdiff_breakage_check", _fake_run)
assert _prod.main() == 1
captured = capsys.readouterr()
assert "PR base ref origin/main" in captured.out
assert "Removed GET /foo without prior deprecation" in captured.out
def test_main_warns_when_pr_base_ref_cannot_be_resolved(monkeypatch, capsys):
monkeypatch.setenv(_prod.BASE_REF_ENV, "main")
monkeypatch.setattr(_prod, "_read_version_from_pyproject", lambda _path: "1.16.1")
monkeypatch.setattr(
_prod, "_get_baseline_version", lambda _distribution, _current: "1.16.1"
)
monkeypatch.setattr(_prod, "_resolve_git_ref", lambda _ref: None)
monkeypatch.setattr(_prod, "_find_sdk_deprecated_fastapi_routes", lambda _root: [])
monkeypatch.setattr(_prod, "_find_deprecation_policy_errors", lambda _schema: [])
monkeypatch.setattr(_prod, "_generate_current_openapi", lambda: {"paths": {}})
monkeypatch.setattr(
_prod, "_generate_openapi_for_git_ref", lambda _ref: {"paths": {}}
)
monkeypatch.setattr(
_prod, "_run_oasdiff_breakage_check", lambda _prev, _cur: ([], 0)
)
assert _prod.main() == 0
captured = capsys.readouterr()
assert "Unable to resolve base ref 'main'" in captured.out
assert (
"No breaking changes detected against baseline release v1.16.1." in captured.out
)
def test_main_rejects_non_removal_breakage_even_with_newer_version(monkeypatch, capsys):
monkeypatch.setattr(_prod, "_read_version_from_pyproject", lambda _path: "1.15.0")
monkeypatch.setattr(
_prod, "_get_baseline_version", lambda _distribution, _current: "1.14.0"
)
monkeypatch.setattr(_prod, "_find_sdk_deprecated_fastapi_routes", lambda _root: [])
monkeypatch.setattr(_prod, "_generate_current_openapi", lambda: {"paths": {}})
monkeypatch.setattr(_prod, "_find_deprecation_policy_errors", lambda _schema: [])
monkeypatch.setattr(
_prod, "_generate_openapi_for_git_ref", lambda _ref: {"paths": {}}
)
monkeypatch.setattr(_prod, "_normalize_openapi_for_oasdiff", lambda schema: schema)
monkeypatch.setattr(
_prod,
"_run_oasdiff_breakage_check",
lambda _prev, _cur: (
[
{
"id": "response-body-changed",
"details": {},
"text": "response body changed",
}
],
1,
),
)
assert _prod.main() == 1
captured = capsys.readouterr()
assert "MINOR version bump" not in captured.out
assert "other than removing previously-deprecated operations" in captured.out
def test_split_breaking_changes_separates_three_buckets():
changes = [
{
"id": "removed-operation",
"details": {"path": "/foo", "method": "get", "deprecated": True},
"text": "removed GET /foo",
},
{
"id": "response-property-one-of-added",
"details": {},
"text": "added '#/components/schemas/NewTool' to response oneOf",
},
{
"id": "response-body-one-of-added",
"details": {},
"text": "added body oneOf member",
},
{
"id": "response-body-any-of-added",
"details": {},
"text": "added body anyOf member",
},
{
"id": "response-body-changed",
"details": {},
"text": "response body changed",
},
]
removed, additive_oneof, other = _prod._split_breaking_changes(changes)
assert len(removed) == 1
assert removed[0]["path"] == "/foo"
assert {change["id"] for change in additive_oneof} == {
"response-property-one-of-added",
"response-body-one-of-added",
"response-body-any-of-added",
}
assert len(other) == 1
assert other[0]["id"] == "response-body-changed"
def test_main_passes_when_only_additive_oneof(monkeypatch, capsys):
monkeypatch.setattr(_prod, "_read_version_from_pyproject", lambda _path: "1.15.0")
monkeypatch.setattr(
_prod, "_get_baseline_version", lambda _distribution, _current: "1.14.0"
)
monkeypatch.setattr(_prod, "_find_sdk_deprecated_fastapi_routes", lambda _root: [])
monkeypatch.setattr(_prod, "_generate_current_openapi", lambda: {"paths": {}})
monkeypatch.setattr(_prod, "_find_deprecation_policy_errors", lambda _schema: [])
monkeypatch.setattr(
_prod, "_generate_openapi_for_git_ref", lambda _ref: {"paths": {}}
)
monkeypatch.setattr(_prod, "_normalize_openapi_for_oasdiff", lambda schema: schema)
monkeypatch.setattr(
_prod,
"_run_oasdiff_breakage_check",
lambda _prev, _cur: (
[
{
"id": "response-property-one-of-added",
"details": {},
"text": "added NewTool to response oneOf",
}
],
1,
),
)
assert _prod.main() == 0
captured = capsys.readouterr()
assert "Additive oneOf/anyOf expansion detected" in captured.out
assert "additive response oneOf expansions" in captured.out
def test_main_fails_when_additive_oneof_mixed_with_real_breakage(monkeypatch, capsys):
monkeypatch.setattr(_prod, "_read_version_from_pyproject", lambda _path: "1.15.0")
monkeypatch.setattr(
_prod, "_get_baseline_version", lambda _distribution, _current: "1.14.0"
)
monkeypatch.setattr(_prod, "_find_sdk_deprecated_fastapi_routes", lambda _root: [])
monkeypatch.setattr(_prod, "_generate_current_openapi", lambda: {"paths": {}})
monkeypatch.setattr(_prod, "_find_deprecation_policy_errors", lambda _schema: [])
monkeypatch.setattr(
_prod, "_generate_openapi_for_git_ref", lambda _ref: {"paths": {}}
)
monkeypatch.setattr(_prod, "_normalize_openapi_for_oasdiff", lambda schema: schema)
monkeypatch.setattr(
_prod,
"_run_oasdiff_breakage_check",
lambda _prev, _cur: (
[
{
"id": "response-property-one-of-added",
"details": {},
"text": "added NewTool to response oneOf",
},
{
"id": "response-body-changed",
"details": {},
"text": "response body changed",
},
],
1,
),
)
assert _prod.main() == 1
captured = capsys.readouterr()
assert "Additive oneOf/anyOf expansion detected" in captured.out
assert "other than removing previously-deprecated operations" in captured.out
def test_normalize_openapi_converts_numeric_exclusive_bounds():
schema = {
"components": {
"schemas": {
"Foo": {
"type": "number",
"exclusiveMinimum": 3,
"exclusiveMaximum": 8,
},
"Bar": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": 2,
},
}
},
"paths": [
{
"schema": {
"exclusiveMinimum": 1.5,
}
}
],
}
normalized = _normalize_openapi_for_oasdiff(schema)
foo = normalized["components"]["schemas"]["Foo"]
assert foo["minimum"] == 3
assert foo["exclusiveMinimum"] is True
assert foo["maximum"] == 8
assert foo["exclusiveMaximum"] is True
bar = normalized["components"]["schemas"]["Bar"]
assert bar["minimum"] == 0
assert bar["exclusiveMinimum"] is True
assert normalized["paths"][0]["schema"]["minimum"] == 1.5
assert normalized["paths"][0]["schema"]["exclusiveMinimum"] is True
def test_normalize_openapi_preserves_boolean_exclusive():
schema = {
"exclusiveMinimum": True,
"minimum": 4,
}
normalized = _normalize_openapi_for_oasdiff(schema)
assert normalized["exclusiveMinimum"] is True
assert normalized["minimum"] == 4