-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathtest_api.py
More file actions
1035 lines (888 loc) · 31.2 KB
/
Copy pathtest_api.py
File metadata and controls
1035 lines (888 loc) · 31.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from asu.config import settings
def test_api_build(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["zzz", "test1", "qqq", "test2", "aaa"],
),
)
assert response.status_code == 200
data = response.json()
assert data["manifest"]["test1"] == "1.0"
assert data["build_cmd"][3] == "PACKAGES=zzz test1 qqq test2 aaa"
def test_api_build_inputs(client):
"""Check both the required and optional default values for all of the
request values defined in the BuildRequest model."""
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
),
)
assert response.status_code == 200
data = response.json()
request = data["request"]
# Required
assert request["version"] == "1.2.3"
assert request["target"] == "testtarget/testsubtarget"
assert request["profile"] == "testprofile"
# Optional
assert request["distro"] == "openwrt"
assert request["version_code"] == ""
assert request["packages"] == []
assert request["packages_versions"] == {}
assert request["defaults"] is None
assert request["client"] is None
assert request["rootfs_size_mb"] is None
assert request["diff_packages"] is False
assert request["repositories_mode"] == "replace"
def test_api_build_version_code(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
version_code="r12647-cb44ab4f5d",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
),
)
assert response.status_code == 200
def test_api_build_rootfs_size(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
rootfs_size_mb=100,
),
)
assert response.status_code == 200
data = response.json()
assert data["build_cmd"][6] == "ROOTFS_PARTSIZE=100"
def test_api_build_rootfs_size_too_small(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
rootfs_size_mb=0,
),
)
assert response.status_code == 422
data = response.json()
assert data["detail"][0]["msg"] == "Input should be greater than or equal to 1"
def test_api_build_rootfs_size_too_big(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
rootfs_size_mb=settings.max_custom_rootfs_size_mb + 1,
),
)
assert response.status_code == 422
data = response.json()
assert (
data["detail"][0]["msg"]
== f"Input should be less than or equal to {settings.max_custom_rootfs_size_mb}"
)
def test_api_build_version_code_bad(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
version_code="some-bad-version-code",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
),
)
assert response.status_code == 500
data = response.json()
assert (
data["detail"]
== "Error: Received incorrect version r12647-cb44ab4f5d (requested some-bad-version-code)"
)
def test_build_missing_container():
from asu.build import build
from asu.build_request import BuildRequest
build_request = BuildRequest(
client="test/1.2.3",
target="lantiq/xrx200",
profile="bt_homehub-v5a",
version="24.10.1",
)
class fake_job:
meta = {}
def save_meta(self):
pass
try:
build(build_request, fake_job())
except Exception as exc:
chain = exc
while hasattr(chain, "__context__") and chain.__context__:
# We want the original exception, not anything that FakeRedis
# generated during processing of it.
chain = chain.__context__
if isinstance(chain, RuntimeError):
exc = chain
break
assert str(exc).startswith(
"Image not found: ghcr.io/openwrt/imagebuilder:lantiq-xrx200-v24.10.1"
)
else:
assert False, "No exception raised!"
base_packages_diff = (
"PACKAGES=-base-files -busybox -dnsmasq -dropbear -firewall -fstools"
" -ip6tables -iptables -kmod-ath9k -kmod-gpio-button-hotplug"
" -kmod-ipt-offload -kmod-usb-chipidea2 -kmod-usb-storage -kmod-usb2"
" -libc -libgcc -logd -mtd -netifd -odhcp6c -odhcpd-ipv6only -opkg"
" -ppp -ppp-mod-pppoe -swconfig -uboot-envtools -uci -uclient-fetch"
" -urandom-seed -urngd -wpad-basic"
)
def test_api_build_diff_packages(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "zzz", "test2", "aaa"], # Order must be maintained.
diff_packages=True,
),
)
assert response.status_code == 200
data = response.json()
assert data["build_cmd"][3] == base_packages_diff + " test1 zzz test2 aaa"
def test_api_build_request_hash(client):
"""Verify that request hash is unchanged by different package ordering."""
packages1 = ["test1", "zzz", "test2", "aaa"]
packages2 = sorted(packages1)
assert packages1 != packages2
json = dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
)
case12hash = "1c4a79c6b711a576996cf9a5e7046a4581008c4466574096266f0e6ea4208fbc"
case34hash = "c5a849e05b60611b465042594fc3489a44f7695c3d09e36433a577ee772ad7b7"
# Case 1 - diff_packages=True, first package ordering
json["diff_packages"] = True
json["packages"] = packages1
response = client.post("/api/v1/build", json=json)
assert response.status_code == 200
data = response.json()
assert data["build_cmd"][3] == base_packages_diff + " " + " ".join(packages1)
assert data["request_hash"] == case12hash
# Case 2 - diff_packages=True, second package ordering
json["diff_packages"] = True
json["packages"] = packages2
response = client.post("/api/v1/build", json=json)
assert response.status_code == 200
data = response.json()
assert data["request_hash"] == case12hash
# This fails, because the returned build command comes from the one hashed
# by the previous build...
# assert data["build_cmd"][3] == base_packages_diff + " " + " ".join(packages2)
# Case 3 - diff_packages=False, first package ordering
json["diff_packages"] = False
json["packages"] = packages1
response = client.post("/api/v1/build", json=json)
assert response.status_code == 200
data = response.json()
assert data["build_cmd"][3] == "PACKAGES=" + " ".join(packages1)
assert data["request_hash"] == case34hash
# Case 4 - diff_packages=False, second package ordering
json["diff_packages"] = False
json["packages"] = packages2
response = client.post("/api/v1/build", json=json)
assert response.status_code == 200
data = response.json()
assert data["request_hash"] == case34hash
# Same failure as case 2.
# assert data["build_cmd"][3] == "PACKAGES=" + " ".join(packages2)
def test_api_latest_default(client):
response = client.get("/api/v1/latest", follow_redirects=False)
assert response.status_code == 301
def test_api_overview(client):
response = client.get("/api/v1/overview", follow_redirects=False)
assert response.status_code == 301
def test_api_build_mapping(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
),
)
assert response.status_code == 200
def test_api_build_mapping_abi(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1-1", "test2"],
),
)
assert response.status_code == 200
def test_api_build_bad_target(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtargetbad",
profile="testvendor,testprofile",
packages=["test1", "test2"],
),
)
assert response.status_code == 400
data = response.json()
assert (
data.get("detail")
== "Unsupported target: testtarget/testsubtargetbad. The requested "
"target was either dropped, is still being built or is not supported "
"by the selected version. Please check the forums or try again later."
)
def test_api_build_head_get(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
),
)
data = response.json()
request_hash = data["request_hash"]
# verify HEAD response and that it has no payload
response = client.head(f"/api/v1/build/{request_hash}")
assert response.status_code == 200
headers = response.headers
assert headers["x-imagebuilder-status"] == "done"
assert headers["x-queue-position"] == "0"
assert response.num_bytes_downloaded == 0
data = response.text
assert data == ""
# verify GET response and its JSON payload
response = client.get(f"/api/v1/build/{request_hash}")
assert response.status_code == 200
headers = response.headers
assert headers["x-imagebuilder-status"] == "done"
assert headers["x-queue-position"] == "0"
assert response.num_bytes_downloaded > 0
data = response.json()
assert data["request_hash"] == request_hash
assert data["imagebuilder_status"] == "done"
request = data["request"]
assert request["version"] == "1.2.3"
assert request["target"] == "testtarget/testsubtarget"
assert request["profile"] == "testprofile"
def test_api_build_packages_versions(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages_versions={"test1": "1.0", "test2": "2.0"},
),
)
data = response.json()
request_hash = data["request_hash"]
response = client.get(f"/api/v1/build/{request_hash}")
assert response.status_code == 200
data = response.json()
assert data["request_hash"] == request_hash
def test_api_build_packages_versions_bad(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages_versions={"test1": "0.0", "test2": "2.0"},
),
)
data = response.json()
request_hash = data["request_hash"]
response = client.get(f"/api/v1/build/{request_hash}")
assert response.status_code == 500
assert (
data["detail"]
== "Error: Impossible package selection: test1 version not as requested: 0.0 vs. 1.0"
)
def test_api_build_packages_duplicate(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
packages_versions={"test1": "1.0", "test2": "2.0"},
),
)
assert response.status_code == 200
def test_api_build_get_not_found(client):
response = client.get("/api/v1/build/testtesttest")
assert response.status_code == 404
def test_api_build_get_no_post(client):
response = client.post("/api/v1/build/0222f0cd9290")
assert response.status_code == 405
def test_api_build_empty_packages_list(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=[],
),
)
assert response.status_code == 200
@pytest.mark.slow
def test_api_build_missing_package(app):
"""Use real build to get proper error for missing packages."""
settings.upstream_url = "https://downloads.openwrt.org"
client = TestClient(app)
response = client.post(
"/api/v1/build",
json=dict(
version="25.12.2",
target="ath79/generic",
profile="8dev_carambola2",
packages=["this-package-does-not-exist"],
),
)
assert response.status_code == 500
data = response.json()
assert "this-package-does-not-exist" in data["detail"]
def test_validate_packages_rejects_unknown(client, httpserver):
"""With validate_packages enabled, unknown packages are rejected at the
validation step rather than reaching the build worker."""
upstream_path = Path("./tests/upstream/")
for f in [
"snapshots/targets/testtarget/testsubtarget/packages/Packages",
"snapshots/packages/testarch/base/Packages",
]:
httpserver.expect_request(f"/{f}").respond_with_data(
(upstream_path / f).read_bytes()
)
settings.validate_packages = True
try:
response = client.post(
"/api/v1/build",
json=dict(
version="SNAPSHOT",
target="testtarget/testsubtarget",
profile="generic",
packages=["base-files", "this-package-does-not-exist"],
),
)
finally:
settings.validate_packages = False
assert response.status_code == 400
assert "this-package-does-not-exist" in response.json()["detail"]
assert "base-files" not in response.json()["detail"]
def test_validate_packages_custom_repo(client, httpserver):
"""Packages from a user-supplied repo (opkg or apk) are merged into the
available universe, so a name found there is accepted."""
import json as json_mod
upstream_path = Path("./tests/upstream/")
for f in [
"snapshots/targets/testtarget/testsubtarget/packages/Packages",
"snapshots/packages/testarch/base/Packages",
]:
httpserver.expect_request(f"/{f}").respond_with_data(
(upstream_path / f).read_bytes()
)
# opkg-style repo: serve a Packages file with an extra package.
httpserver.expect_request("/custom-repo/index.json").respond_with_data(
"", status=404
)
httpserver.expect_request("/custom-repo/Packages").respond_with_data(
"Package: from-custom-repo\n"
"Version: 1.0\n"
"Architecture: testarch\n"
"Filename: from-custom-repo_1.0_testarch.ipk\n"
"Size: 1\n"
"SHA256sum: 0000\n"
"Description: test\n"
)
# apk-style repo: client URL points at packages.adb but the v2 index.json
# sits in the same directory.
httpserver.expect_request("/apk-repo/index.json").respond_with_json(
json_mod.loads(
'{"version": 2, "architecture": "testarch", '
'"packages": {"from-apk-repo": "1.0"}}'
)
)
saved_allow_list = settings.repository_allow_list
settings.repository_allow_list = ["http://localhost:8123/"]
settings.validate_packages = True
try:
# Package only present in the opkg repo: must pass validation.
response = client.post(
"/api/v1/build",
json=dict(
version="SNAPSHOT",
target="testtarget/testsubtarget",
profile="generic",
packages=["from-custom-repo"],
repositories={"custom": "http://localhost:8123/custom-repo"},
repositories_mode="append",
),
)
assert response.status_code != 400, response.json()
# Package only present in the apk repo (URL ends with packages.adb).
response = client.post(
"/api/v1/build",
json=dict(
version="SNAPSHOT",
target="testtarget/testsubtarget",
profile="generic",
packages=["from-apk-repo"],
repositories={"custom": "http://localhost:8123/apk-repo/packages.adb"},
repositories_mode="append",
),
)
assert response.status_code != 400, response.json()
# Truly unknown package: still rejected even with the custom repo.
response = client.post(
"/api/v1/build",
json=dict(
version="SNAPSHOT",
target="testtarget/testsubtarget",
profile="generic",
packages=["this-package-does-not-exist"],
repositories={"custom": "http://localhost:8123/custom-repo"},
repositories_mode="append",
),
)
assert response.status_code == 400
assert "this-package-does-not-exist" in response.json()["detail"]
finally:
settings.validate_packages = False
settings.repository_allow_list = saved_allow_list
def test_validate_packages_skipped_when_disabled(client):
"""With validate_packages disabled (the default), unknown packages are
not rejected at validation — they would reach the build worker."""
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["this-package-does-not-exist"],
),
)
# No 400 from validation — request proceeds (will eventually fail in build).
assert response.status_code != 400
def test_api_build_without_packages_list(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
),
)
assert response.status_code == 200
def test_api_build_bad_packages_str(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
packages="testpackage",
),
)
assert response.status_code == 422
data = response.json()
assert data["detail"] == [
{
"input": "testpackage",
"loc": ["body", "packages"],
"msg": "Input should be a valid list",
"type": "list_type",
}
]
def test_api_build_empty_request(client):
response = client.post("/api/v1/build")
assert response.status_code == 422
data = response.json()
assert data["detail"] == [
{"input": None, "loc": ["body"], "msg": "Field required", "type": "missing"}
]
@pytest.mark.slow
def test_api_build_real_x86(app):
settings.upstream_url = "https://downloads.openwrt.org"
client = TestClient(app)
response = client.post(
"/api/v1/build",
json=dict(
target="x86/64",
version="25.12.2",
packages=["tmux", "vim"],
profile="some_random_cpu_which_doesnt_exists_as_profile",
),
)
assert response.status_code == 200
data = response.json()
assert data["id"] == "generic"
response = client.post(
"/api/v1/build",
json=dict(
target="x86/64",
version="25.12.2",
packages=["tmux", "vim"],
profile="some_random_cpu_which_doesnt_exists_as_profile",
filesystem="ext4",
),
)
assert response.status_code == 200
data = response.json()
assert data["id"] == "generic"
@pytest.mark.slow
def test_api_build_real_ath79(app):
settings.upstream_url = "https://downloads.openwrt.org"
client = TestClient(app)
response = client.post(
"/api/v1/build",
json=dict(
target="ath79/generic",
version="25.12.2",
packages=["tmux", "vim"],
profile="8dev,carambola2", # Test unsanitized profile.
),
)
assert response.status_code == 200
data = response.json()
assert data["id"] == "8dev_carambola2"
response = client.post(
"/api/v1/build",
json=dict(
target="ath79/generic",
version="25.12.2",
packages=["tmux", "vim"],
profile="8dev_carambola2",
filesystem="squashfs",
),
)
assert response.status_code == 200
data = response.json()
assert data["id"] == "8dev_carambola2"
def test_api_build_needed(client):
response = client.post(
"/api/v1/build",
json=dict(profile="testprofile", target="testtarget/testsubtarget"),
)
assert response.status_code == 422
data = response.json()
assert data["detail"] == [
{
"input": {"profile": "testprofile", "target": "testtarget/testsubtarget"},
"loc": ["body", "version"],
"msg": "Field required",
"type": "missing",
}
]
response = client.post(
"/api/v1/build",
json=dict(version="1.2.3", target="testtarget/testsubtarget"),
)
assert response.status_code == 422
data = response.json()
assert data["detail"] == [
{
"type": "missing",
"loc": ["body", "profile"],
"msg": "Field required",
"input": {"version": "1.2.3", "target": "testtarget/testsubtarget"},
}
]
response = client.post(
"/api/v1/build", json=dict(version="1.2.3", profile="testprofile")
)
assert response.status_code == 422
data = response.json()
assert data["detail"] == [
{
"type": "missing",
"loc": ["body", "target"],
"msg": "Field required",
"input": {"version": "1.2.3", "profile": "testprofile"},
}
]
def test_api_build_bad_distro(client):
response = client.post(
"/api/v1/build",
json=dict(
distro="Foobar",
target="testtarget/testsubtarget",
version="1.2.3",
profile="testprofile",
packages=["test1", "test2"],
),
)
assert response.status_code == 400
data = response.json()
assert data["detail"] == "Unsupported distro: Foobar"
def test_api_build_bad_branch(client):
response = client.post(
"/api/v1/build",
json=dict(
version="10.10.10",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
),
)
assert response.status_code == 400
data = response.json()
assert data["detail"] == "Unsupported branch: 10.10.10"
def test_api_build_bad_version(client):
response = client.post(
"/api/v1/build",
json=dict(
version="99.99.99",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
),
)
assert response.status_code == 400
data = response.json()
assert data["detail"] == "Unsupported branch: 99.99.99"
def test_api_build_bad_profile(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="Foobar",
packages=["test1", "test2"],
),
)
assert response.status_code == 400
data = response.json()
assert (
data["detail"] == "Unsupported profile: Foobar. The requested "
"profile was either dropped or never existed. Please check the forums "
"for more information."
)
def test_api_build_defaults_empty(client):
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
defaults="",
),
)
assert response.status_code == 200
def test_api_build_defaults_filled_not_allowed(client):
settings.allow_defaults = False
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
defaults="echo",
),
)
assert response.status_code == 400
data = response.json()
assert data["detail"] == "Handling `defaults` not enabled on server"
def test_api_build_defaults_filled_allowed(app):
settings.allow_defaults = True
client = TestClient(app)
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
defaults="echo",
),
)
assert response.status_code == 200
data = response.json()
assert (
data["request_hash"]
== "ba50558496f8fead41e8d5bc72afd1ad7d27bc053afb550a8bf6ee3bbcc64952"
)
def test_api_build_defaults_filled_too_big(app):
settings.allow_defaults = True
client = TestClient(app)
response = client.post(
"/api/v1/build",
json=dict(
version="1.2.3",
target="testtarget/testsubtarget",
profile="testprofile",
defaults="#" * (settings.max_defaults_length + 1),
),
)
assert response.status_code == 422
data = response.json()
assert (
data["detail"][0]["msg"]
== f"String should have at most {settings.max_defaults_length} characters"
)
def test_api_revision(client):
response = client.get(
"/api/v1/revision/23.05.5/ath79/generic", follow_redirects=False
)
assert response.status_code == 200
data = response.json()
assert data["revision"] == "r24106-10cc5fcd00"
def test_api_revision_bad_version(client):
response = client.get(
"/api/v1/revision/invalid-version/ath79/generic", follow_redirects=False
)
assert response.status_code == 400
data = response.json()
assert data["detail"] == "Unsupported version: invalid-version"
def test_api_stats(client):
response = client.get("/api/v1/stats", follow_redirects=False)
assert response.status_code == 200
data = response.json()
assert data["queue_length"] == 0
@pytest.mark.slow
def test_api_build_libremesh_apk(app):
"""Build with LibreMesh apk repository (25.12.2, x86/64)."""
settings.upstream_url = "https://downloads.openwrt.org"
settings.repository_allow_list = ["https://raw.githubusercontent.com/libremesh/"]
client = TestClient(app)
response = client.post(
"/api/v1/build",
json=dict(
target="x86/64",
version="25.12.2",
profile="generic",
packages=["lime-system"],
repositories={
"libremesh": "https://raw.githubusercontent.com/libremesh/lime-feed/gh-pages/master/openwrt-25.12/x86_64/packages.adb",
},
repository_keys=[
"-----BEGIN PUBLIC KEY-----\n"
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEdFJZ2qVti49Ol8LJZYuxgOCLowBS\n"
"8bI86a7zqhSbs5yon3JON7Yee7CQOgqwPOX5eMALGOu8iFGAqIRx5YjfYA==\n"
"-----END PUBLIC KEY-----\n"
],
repositories_mode="append",
),
)
data = response.json()
assert response.status_code == 200, data.get("stderr", data.get("detail", ""))[
:2000
]
assert "lime-system" in data["manifest"]
@pytest.mark.slow
def test_api_build_libremesh_opkg(app):
"""Build with LibreMesh opkg repository (23.05.5, ath79)."""
settings.upstream_url = "https://downloads.openwrt.org"
settings.repository_allow_list = ["https://raw.githubusercontent.com/libremesh/"]
client = TestClient(app)
response = client.post(
"/api/v1/build",
json=dict(
target="ath79/generic",
version="23.05.5",
profile="8dev_carambola2",
packages=["lime-system"],
repositories={
"libremesh": "https://raw.githubusercontent.com/libremesh/lime-feed/gh-pages/2024.1",
},
repository_keys=[
"RWSnGzyChavSiyQ+vLk3x7F0NqcLa4kKyXCdriThMhO78ldHgxGljM/8",
],
repositories_mode="append",
),
)
data = response.json()
assert response.status_code == 200, data.get("stderr", data.get("detail", ""))[
:2000
]
assert "lime-system" in data["manifest"]
@pytest.mark.slow
def test_api_build_freifunk_apk(app):
"""Build with Freifunk Weimarnetz apk repository (25.12.2, ath79)."""
settings.upstream_url = "https://downloads.openwrt.org"
settings.repository_allow_list = ["https://buildbot.weimarnetz.de/"]
client = TestClient(app)
response = client.post(
"/api/v1/build",
json=dict(
target="ath79/generic",
version="25.12.2",
profile="8dev_carambola2",
packages=["weimarnetz-feed-apk"],
repositories={
"weimarnetz": "https://buildbot.weimarnetz.de/builds/brauhaus/packages/stable/25.12/ath79/generic/weimarnetz_packages/packages.adb",
},
repository_keys=[
"-----BEGIN PUBLIC KEY-----\n"
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEzZWFJBl7JU/XlRXaU4duMoqnu/L1\n"
"aPZGMO349gtL2Wt3eo8fC2qcbnXV2FdcPXaySeY4RmbrlG1ehDonJfW7Jg==\n"
"-----END PUBLIC KEY-----\n"
],
repositories_mode="append",
),
)