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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
|
# SPDX-FileCopyrightText: Copyright (C) 2025 Bruce Ashfield
#
# SPDX-License-Identifier: MIT
"""
Tests for container-registry.sh helper script.
These tests verify the container registry helper script commands:
- start/stop/status - Registry server lifecycle
- push - Push OCI images to registry (with tag/strategy options)
- import - Import 3rd party images
- delete - Delete tagged images
- gc - Garbage collection
- list/tags/catalog - Query registry contents
Prerequisites:
# Generate the script first:
bitbake container-registry-index -c generate_registry_script
# The script location is:
$TOPDIR/container-registry/container-registry.sh
Run with:
pytest tests/test_container_registry_script.py -v
Run with specific registry script:
pytest tests/test_container_registry_script.py -v \\
--registry-script /path/to/container-registry.sh
Environment variables:
CONTAINER_REGISTRY_SCRIPT: Path to the registry script
TOPDIR: Yocto build directory (script at $TOPDIR/container-registry/)
"""
import pytest
import subprocess
import os
import time
from pathlib import Path
# Note: Registry options (--registry-script, --skip-registry-network)
# are defined in conftest.py
@pytest.fixture(scope="module")
def registry_script(request):
"""Get path to the registry script.
Looks in order:
1. --registry-script command line option
2. CONTAINER_REGISTRY_SCRIPT environment variable
3. $TOPDIR/container-registry/container-registry.sh
4. Common locations based on cwd
"""
# Check command line option
script_path = request.config.getoption("--registry-script", default=None)
if script_path is None:
# Check environment variable
script_path = os.environ.get("CONTAINER_REGISTRY_SCRIPT")
if script_path is None:
# Try TOPDIR-based path
topdir = os.environ.get("TOPDIR")
if topdir:
script_path = os.path.join(topdir, "container-registry", "container-registry.sh")
if script_path is None:
# Try common locations relative to cwd
candidates = [
"container-registry/container-registry.sh",
"../container-registry/container-registry.sh",
"build/container-registry/container-registry.sh",
]
for candidate in candidates:
if os.path.exists(candidate):
script_path = candidate
break
if script_path is None or not os.path.exists(script_path):
pytest.skip(
"Registry script not found. Generate it with: "
"bitbake container-registry-index -c generate_registry_script\n"
"Or specify path with --registry-script or CONTAINER_REGISTRY_SCRIPT env var"
)
script_path = Path(script_path).resolve()
if not script_path.exists():
pytest.skip(f"Registry script not found at: {script_path}")
return script_path
@pytest.fixture(scope="module")
def skip_network(request):
"""Check if network tests should be skipped."""
return request.config.getoption("--skip-registry-network", default=False)
class RegistryScriptRunner:
"""Helper class for running registry script commands."""
def __init__(self, script_path: Path):
self.script_path = script_path
self._was_running = None
def run(self, *args, timeout=30, check=True, capture_output=True):
"""Run a registry script command."""
cmd = [str(self.script_path)] + list(args)
result = subprocess.run(
cmd,
timeout=timeout,
check=False,
capture_output=capture_output,
text=True,
)
if check and result.returncode != 0:
error_msg = f"Command failed: {' '.join(cmd)}\n"
error_msg += f"Exit code: {result.returncode}\n"
if result.stdout:
error_msg += f"stdout: {result.stdout}\n"
if result.stderr:
error_msg += f"stderr: {result.stderr}\n"
raise AssertionError(error_msg)
return result
def start(self, timeout=30):
"""Start the registry."""
return self.run("start", timeout=timeout)
def stop(self, timeout=10):
"""Stop the registry."""
return self.run("stop", timeout=timeout, check=False)
def status(self, timeout=10):
"""Check registry status."""
return self.run("status", timeout=timeout, check=False)
def is_running(self):
"""Check if registry is running."""
result = self.status()
return result.returncode == 0 and "running" in result.stdout.lower()
def ensure_running(self, timeout=30):
"""Ensure registry is running, starting if needed."""
if not self.is_running():
result = self.start(timeout=timeout)
if result.returncode != 0:
raise RuntimeError(f"Failed to start registry: {result.stderr}")
time.sleep(2)
def push(self, timeout=120):
"""Push OCI images to registry."""
return self.run("push", timeout=timeout)
def import_image(self, source, dest_name=None, timeout=300):
"""Import a 3rd party image."""
args = ["import", source]
if dest_name:
args.append(dest_name)
return self.run(*args, timeout=timeout)
def list_images(self, timeout=30):
"""List images in registry."""
return self.run("list", timeout=timeout)
def tags(self, image, timeout=30):
"""Get tags for an image."""
return self.run("tags", image, timeout=timeout, check=False)
def catalog(self, timeout=30):
"""Get raw catalog."""
return self.run("catalog", timeout=timeout)
def help(self):
"""Show help."""
return self.run("help", check=False)
def delete(self, image_tag, timeout=30):
"""Delete a tagged image."""
return self.run("delete", image_tag, timeout=timeout, check=False)
def gc(self, timeout=60):
"""Run garbage collection (non-interactive)."""
# gc prompts for confirmation, so we can't easily test interactive mode
# Just test that the command exists and shows dry-run
return self.run("gc", timeout=timeout, check=False)
def push_with_args(self, *args, timeout=120):
"""Push with custom arguments."""
return self.run("push", *args, timeout=timeout, check=False)
@pytest.fixture(scope="module")
def registry(registry_script):
"""Create a RegistryScriptRunner instance."""
return RegistryScriptRunner(registry_script)
@pytest.fixture(scope="module")
def registry_session(registry):
"""Module-scoped fixture that ensures registry is running.
Starts the registry if not running and stops it at the end
if we started it.
"""
was_running = registry.is_running()
if not was_running:
result = registry.start(timeout=30)
if result.returncode != 0:
pytest.skip(f"Failed to start registry: {result.stderr}")
# Wait a moment for registry to be ready
time.sleep(2)
yield registry
# Only stop if we started it
if not was_running:
registry.stop()
class TestRegistryScriptBasic:
"""Test basic registry script functionality."""
def test_script_exists_and_executable(self, registry_script):
"""Test that the script exists and is executable."""
assert registry_script.exists()
assert os.access(registry_script, os.X_OK)
def test_help_command(self, registry):
"""Test help command shows usage info."""
result = registry.help()
assert result.returncode == 0
assert "start" in result.stdout
assert "stop" in result.stdout
assert "push" in result.stdout
assert "import" in result.stdout
assert "list" in result.stdout
def test_unknown_command_shows_error(self, registry):
"""Test that unknown command shows error and help."""
result = registry.run("invalid-command", check=False)
assert result.returncode != 0
assert "unknown" in result.stdout.lower() or "usage" in result.stdout.lower()
class TestRegistryLifecycle:
"""Test registry start/stop/status commands."""
def test_start_registry(self, registry):
"""Test starting the registry."""
# Stop first if running
registry.stop()
time.sleep(1)
result = registry.start()
assert result.returncode == 0
assert "started" in result.stdout.lower() or "running" in result.stdout.lower()
# Verify it's running
assert registry.is_running()
def test_status_when_running(self, registry):
"""Test status command when registry is running."""
# Ensure running
if not registry.is_running():
registry.start()
time.sleep(2)
result = registry.status()
assert result.returncode == 0
assert "running" in result.stdout.lower()
assert "healthy" in result.stdout.lower() or "url" in result.stdout.lower()
def test_stop_registry(self, registry):
"""Test stopping the registry."""
# Ensure running first
if not registry.is_running():
registry.start()
time.sleep(2)
result = registry.stop()
assert result.returncode == 0
assert "stop" in result.stdout.lower()
# Verify it's stopped
assert not registry.is_running()
def test_status_when_stopped(self, registry):
"""Test status command when registry is stopped."""
# Ensure stopped
registry.stop()
time.sleep(1)
result = registry.status()
assert result.returncode != 0
assert "not running" in result.stdout.lower()
def test_start_when_already_running(self, registry):
"""Test that starting when already running is idempotent."""
# Start once
if not registry.is_running():
registry.start()
time.sleep(2)
# Start again
result = registry.start()
assert result.returncode == 0
assert "already running" in result.stdout.lower() or "running" in result.stdout.lower()
def test_stop_when_not_running(self, registry):
"""Test that stopping when not running is idempotent."""
# Ensure stopped
registry.stop()
time.sleep(1)
# Stop again
result = registry.stop()
assert result.returncode == 0
assert "not running" in result.stdout.lower()
class TestRegistryPush:
"""Test pushing OCI images to the registry.
Note: This requires OCI images in the deploy directory.
Tests will skip if no images are available.
"""
def test_push_requires_running_registry(self, registry):
"""Test that push fails when registry is not running."""
registry.stop()
time.sleep(1)
result = registry.run("push", check=False, timeout=10)
assert result.returncode != 0
assert "not responding" in result.stdout.lower() or "start" in result.stdout.lower()
def test_push_with_no_images(self, registry_session):
"""Test push when no OCI images are in deploy directory.
This may succeed (with "no images" message) or actually push
images if they exist. Either is acceptable.
"""
registry_session.ensure_running()
result = registry_session.push(timeout=120)
# Either succeeds (with images) or shows message (without)
# Both are valid outcomes
assert result.returncode == 0
class TestRegistryImport:
"""Test importing 3rd party images.
Note: Import tests require network access to docker.io.
Use --skip-registry-network to skip these tests.
"""
def test_import_requires_running_registry(self, registry):
"""Test that import fails when registry is not running."""
registry.stop()
time.sleep(1)
result = registry.run("import", "docker.io/library/alpine:latest",
check=False, timeout=10)
assert result.returncode != 0
assert "not responding" in result.stdout.lower() or "start" in result.stdout.lower()
def test_import_no_args_shows_usage(self, registry_session):
"""Test that import without args shows usage."""
registry_session.ensure_running()
result = registry_session.run("import", check=False)
assert result.returncode != 0
assert "usage" in result.stdout.lower()
assert "docker.io" in result.stdout.lower() or "example" in result.stdout.lower()
@pytest.mark.network
@pytest.mark.slow
def test_import_alpine(self, registry_session, skip_network):
"""Test importing alpine from docker.io."""
if skip_network:
pytest.skip("Skipping network test (--skip-registry-network)")
registry_session.ensure_running()
result = registry_session.import_image(
"docker.io/library/alpine:latest",
timeout=300
)
assert result.returncode == 0
assert "import complete" in result.stdout.lower() or "importing" in result.stdout.lower()
# Verify it appears in list
list_result = registry_session.list_images()
assert "alpine" in list_result.stdout
@pytest.mark.network
@pytest.mark.slow
def test_import_with_custom_name(self, registry_session, skip_network):
"""Test importing with a custom local name."""
if skip_network:
pytest.skip("Skipping network test (--skip-registry-network)")
registry_session.ensure_running()
result = registry_session.import_image(
"docker.io/library/busybox:latest",
"my-busybox",
timeout=300
)
assert result.returncode == 0
# Verify it appears with custom name
list_result = registry_session.list_images()
assert "my-busybox" in list_result.stdout
class TestRegistryQuery:
"""Test registry query commands (list, tags, catalog)."""
def test_catalog_requires_running_registry(self, registry):
"""Test that catalog fails when registry is not running."""
registry.stop()
time.sleep(1)
result = registry.run("catalog", check=False, timeout=10)
# May fail or return empty/error JSON
# Just verify it doesn't hang
def test_list_requires_running_registry(self, registry):
"""Test that list fails when registry is not running."""
registry.stop()
time.sleep(1)
result = registry.run("list", check=False, timeout=10)
assert result.returncode != 0
assert "not responding" in result.stdout.lower()
def test_catalog_returns_json(self, registry_session):
"""Test that catalog returns JSON format."""
registry_session.ensure_running()
result = registry_session.catalog()
assert result.returncode == 0
# Should be valid JSON with repositories key
import json
try:
data = json.loads(result.stdout)
assert "repositories" in data
except json.JSONDecodeError:
# May be pretty-printed, try parsing lines
assert "repositories" in result.stdout
def test_list_shows_images(self, registry_session):
"""Test that list shows images with their tags."""
registry_session.ensure_running()
result = registry_session.list_images()
assert result.returncode == 0
# Should show header or images
assert "images" in result.stdout.lower() or ":" in result.stdout or "(none)" in result.stdout
def test_tags_for_nonexistent_image(self, registry_session):
"""Test tags command for nonexistent image."""
registry_session.ensure_running()
result = registry_session.tags("nonexistent-image-xyz")
# Either returns non-zero with "not found", or returns empty/error JSON
# The important thing is it doesn't crash and indicates the image doesn't exist
if result.returncode == 0:
# If it returns 0, stdout should be empty or contain error info
assert "nonexistent" not in result.stdout.lower() or "error" in result.stdout.lower() or result.stdout.strip() == ""
else:
assert "not found" in result.stdout.lower() or "error" in result.stdout.lower()
def test_tags_usage_without_image(self, registry_session):
"""Test tags command without image argument shows usage."""
registry_session.ensure_running()
result = registry_session.run("tags", check=False)
assert result.returncode != 0
assert "usage" in result.stdout.lower()
class TestRegistryDelete:
"""Test delete command for removing tagged images."""
def test_delete_requires_running_registry(self, registry):
"""Test that delete fails when registry is not running."""
registry.stop()
time.sleep(1)
result = registry.delete("container-base:latest")
assert result.returncode != 0
assert "not responding" in result.stdout.lower()
def test_delete_no_args_shows_usage(self, registry_session):
"""Test that delete without args shows usage."""
registry_session.ensure_running()
result = registry_session.run("delete", check=False)
assert result.returncode != 0
assert "usage" in result.stdout.lower()
def test_delete_requires_tag(self, registry_session):
"""Test that delete requires image:tag format."""
registry_session.ensure_running()
result = registry_session.delete("container-base") # No tag
assert result.returncode != 0
assert "tag required" in result.stdout.lower()
def test_delete_nonexistent_tag(self, registry_session):
"""Test deleting a nonexistent tag."""
registry_session.ensure_running()
result = registry_session.delete("container-base:nonexistent-tag-xyz")
assert result.returncode != 0
assert "not found" in result.stdout.lower()
@pytest.mark.network
@pytest.mark.slow
def test_delete_workflow(self, registry_session, skip_network):
"""Test importing an image, then deleting it."""
if skip_network:
pytest.skip("Skipping network test (--skip-registry-network)")
registry_session.ensure_running()
# Import an image with unique name
result = registry_session.import_image(
"docker.io/library/alpine:latest",
"delete-test",
timeout=300
)
assert result.returncode == 0
# Verify it exists
result = registry_session.tags("delete-test")
assert result.returncode == 0
assert "latest" in result.stdout
# Delete it
result = registry_session.delete("delete-test:latest")
assert result.returncode == 0
assert "deleted successfully" in result.stdout.lower()
# Verify it's gone
result = registry_session.tags("delete-test")
assert result.returncode != 0 or "not found" in result.stdout.lower()
class TestRegistryGC:
"""Test garbage collection command."""
def test_gc_help_in_help_output(self, registry):
"""Test that gc command is listed in help."""
result = registry.help()
assert "gc" in result.stdout.lower()
def test_gc_requires_registry_binary(self, registry_session):
"""Test that gc checks for registry binary.
This test just verifies gc command runs and either:
- Works (shows dry-run output)
- Fails with useful error message
"""
# gc stops registry first, so just run it and check output
result = registry_session.gc(timeout=30)
# Should either work or show error about binary/not running
output = result.stdout.lower()
assert any([
"garbage" in output,
"collecting" in output,
"registry" in output,
"error" in output,
"not found" in output,
])
class TestRegistryPushOptions:
"""Test push command with various options."""
def test_push_tag_requires_image_name(self, registry_session):
"""Test that --tag without image name fails."""
registry_session.ensure_running()
result = registry_session.push_with_args("--tag", "v1.0.0")
assert result.returncode != 0
assert "--tag requires an image name" in result.stdout.lower()
def test_push_with_image_filter(self, registry_session):
"""Test pushing a specific image by name."""
registry_session.ensure_running()
result = registry_session.push_with_args("container-base")
# Should either succeed or report image not found
# (depending on whether container-base exists)
output = result.stdout.lower()
assert any([
"pushing" in output,
"not found" in output,
"done" in output,
])
def test_push_with_strategy(self, registry_session):
"""Test pushing with explicit strategy."""
registry_session.ensure_running()
result = registry_session.push_with_args("--strategy", "latest")
assert result.returncode == 0 or "pushing" in result.stdout.lower()
def test_push_help_shows_options(self, registry):
"""Test that help shows push options."""
result = registry.help()
assert "--tag" in result.stdout
assert "--strategy" in result.stdout
assert "image" in result.stdout.lower()
class TestRegistryIntegration:
"""Integration tests for full registry workflow.
These tests require:
- Registry script generated
- docker-distribution-native built
- skopeo-native built
- Network access (for import tests)
"""
@pytest.mark.network
@pytest.mark.slow
def test_full_workflow(self, registry, skip_network):
"""Test complete workflow: start -> import -> list -> stop."""
if skip_network:
pytest.skip("Skipping network test (--skip-registry-network)")
# Start fresh
registry.stop()
time.sleep(1)
try:
# Start
result = registry.start()
assert result.returncode == 0
time.sleep(2)
# Import an image
result = registry.import_image(
"docker.io/library/alpine:latest",
"workflow-test",
timeout=300
)
assert result.returncode == 0
# List should show it
result = registry.list_images()
assert result.returncode == 0
assert "workflow-test" in result.stdout
# Tags should work
result = registry.tags("workflow-test")
assert result.returncode == 0
assert "latest" in result.stdout
# Catalog should include it
result = registry.catalog()
assert result.returncode == 0
assert "workflow-test" in result.stdout
finally:
# Always stop
registry.stop()
class TestRegistryAuthentication:
"""Test registry authentication features.
Tests for the authentication modes:
- none (default)
- home (~/.docker/config.json)
- authfile (explicit Docker config path)
- credsfile (simple key=value credentials file)
- env (environment variables, script only)
"""
def test_help_shows_auth_options(self, registry):
"""Test that help shows authentication options."""
result = registry.help()
assert "--use-home-auth" in result.stdout or "--authfile" in result.stdout
assert "--credsfile" in result.stdout
assert "--auth-mode" in result.stdout
assert "authentication" in result.stdout.lower()
def test_help_shows_import_auth_options(self, registry):
"""Test that help shows import source authentication options."""
result = registry.help()
assert "--src-authfile" in result.stdout or "--src-creds" in result.stdout
def test_push_with_unknown_auth_mode_fails(self, registry_session):
"""Test that unknown auth mode fails."""
registry_session.ensure_running()
result = registry_session.push_with_args("--auth-mode", "invalid")
assert result.returncode != 0
assert "unknown" in result.stdout.lower() or "error" in result.stdout.lower()
def test_push_with_none_auth_mode(self, registry_session):
"""Test that none auth mode works (default)."""
registry_session.ensure_running()
result = registry_session.push_with_args("--auth-mode", "none")
# Should work (no auth required for local registry)
assert result.returncode == 0
def test_push_with_home_auth_no_config(self, registry_session, tmp_path, monkeypatch):
"""Test that home auth mode fails when config doesn't exist."""
registry_session.ensure_running()
# Point HOME to temp dir without .docker/config.json
monkeypatch.setenv("HOME", str(tmp_path))
result = registry_session.push_with_args("--use-home-auth")
assert result.returncode != 0
assert "not found" in result.stdout.lower() or "error" in result.stdout.lower()
def test_push_with_authfile_nonexistent(self, registry_session, tmp_path):
"""Test that authfile mode fails when file doesn't exist."""
registry_session.ensure_running()
nonexistent = tmp_path / "nonexistent-auth.json"
result = registry_session.push_with_args("--authfile", str(nonexistent))
assert result.returncode != 0
assert "not found" in result.stdout.lower() or "error" in result.stdout.lower()
def test_push_with_credsfile_nonexistent(self, registry_session, tmp_path):
"""Test that credsfile mode fails when file doesn't exist."""
registry_session.ensure_running()
nonexistent = tmp_path / "nonexistent-creds"
result = registry_session.push_with_args("--credsfile", str(nonexistent))
assert result.returncode != 0
assert "not found" in result.stdout.lower() or "error" in result.stdout.lower()
def test_push_with_credsfile_incomplete(self, registry_session, tmp_path):
"""Test that credsfile mode fails when file is incomplete."""
registry_session.ensure_running()
# Create credentials file with only username (missing password or token)
creds_file = tmp_path / "incomplete-creds"
creds_file.write_text("CONTAINER_REGISTRY_USER=testuser\n")
result = registry_session.push_with_args("--credsfile", str(creds_file))
assert result.returncode != 0
assert "must contain" in result.stdout.lower() or "error" in result.stdout.lower()
def test_push_with_valid_credsfile_user_password(self, registry_session, tmp_path):
"""Test push with valid credsfile (username/password).
Note: This test uses fake credentials but validates the parsing works.
The push may fail auth against the registry but shows credentials are parsed.
"""
registry_session.ensure_running()
creds_file = tmp_path / "test-creds"
creds_file.write_text(
"CONTAINER_REGISTRY_USER=testuser\n"
"CONTAINER_REGISTRY_PASSWORD=testpass\n"
)
result = registry_session.push_with_args("--credsfile", str(creds_file))
# Should parse the file successfully and attempt push
# May succeed or fail auth, but shouldn't fail on parsing
output = result.stdout.lower()
# Should not contain parsing errors
assert "must contain" not in output
assert "credentials file not found" not in output
def test_push_with_valid_credsfile_token(self, registry_session, tmp_path):
"""Test push with valid credsfile (token).
Token takes precedence over username/password.
"""
registry_session.ensure_running()
creds_file = tmp_path / "test-token-creds"
creds_file.write_text(
"# Comment line\n"
"CONTAINER_REGISTRY_TOKEN=test-token-123\n"
"\n"
"# Extra whitespace and comments are ignored\n"
)
result = registry_session.push_with_args("--credsfile", str(creds_file))
# Should parse the file successfully
output = result.stdout.lower()
assert "must contain" not in output
assert "credentials file not found" not in output
def test_push_with_credsfile_quoted_values(self, registry_session, tmp_path):
"""Test push with credsfile containing quoted values."""
registry_session.ensure_running()
creds_file = tmp_path / "quoted-creds"
creds_file.write_text(
'CONTAINER_REGISTRY_USER="quoted-user"\n'
"CONTAINER_REGISTRY_PASSWORD='quoted-pass'\n"
)
result = registry_session.push_with_args("--credsfile", str(creds_file))
# Should parse quoted values correctly
output = result.stdout.lower()
assert "must contain" not in output
def test_push_with_env_auth_mode_no_creds(self, registry_session, monkeypatch):
"""Test that env auth mode fails without env vars set."""
registry_session.ensure_running()
# Clear any existing auth env vars
monkeypatch.delenv("CONTAINER_REGISTRY_TOKEN", raising=False)
monkeypatch.delenv("CONTAINER_REGISTRY_USER", raising=False)
monkeypatch.delenv("CONTAINER_REGISTRY_PASSWORD", raising=False)
result = registry_session.push_with_args("--auth-mode", "env")
assert result.returncode != 0
assert "requires" in result.stdout.lower() or "error" in result.stdout.lower()
def test_push_with_env_auth_mode_token(self, registry_session, monkeypatch):
"""Test env auth mode with token environment variable."""
registry_session.ensure_running()
monkeypatch.setenv("CONTAINER_REGISTRY_TOKEN", "test-env-token")
result = registry_session.push_with_args("--auth-mode", "env")
# Should not fail on missing credentials
output = result.stdout.lower()
assert "requires" not in output or "requires" in output and "token or user" not in output
def test_push_with_direct_creds_option(self, registry_session):
"""Test push with --creds option."""
registry_session.ensure_running()
result = registry_session.push_with_args("--creds", "user:pass")
# Should attempt push with credentials
# May fail auth but shouldn't fail on parsing
output = result.stdout.lower()
assert "creds value missing" not in output
def test_push_with_direct_token_option(self, registry_session):
"""Test push with --token option."""
registry_session.ensure_running()
result = registry_session.push_with_args("--token", "test-direct-token")
# Should attempt push with token
output = result.stdout.lower()
assert "token value missing" not in output
def test_import_with_src_authfile_nonexistent(self, registry_session, tmp_path):
"""Test import with nonexistent source authfile."""
registry_session.ensure_running()
nonexistent = tmp_path / "nonexistent-src-auth.json"
result = registry_session.run(
"import", "docker.io/library/alpine:latest",
"--src-authfile", str(nonexistent),
check=False
)
# Skopeo should fail when auth file doesn't exist
# Just verify we pass the option through correctly
assert result.returncode != 0 or "error" in result.stdout.lower()
def test_import_with_src_credsfile(self, registry_session, tmp_path):
"""Test import with source credentials file."""
registry_session.ensure_running()
creds_file = tmp_path / "src-creds"
creds_file.write_text(
"CONTAINER_REGISTRY_USER=testuser\n"
"CONTAINER_REGISTRY_PASSWORD=testpass\n"
)
result = registry_session.run(
"import", "docker.io/library/alpine:latest",
"--src-credsfile", str(creds_file),
check=False, timeout=30
)
# Should parse credentials and attempt import
output = result.stdout.lower()
assert "must contain" not in output
assert "credentials file not found" not in output
def test_import_with_src_creds_direct(self, registry_session):
"""Test import with direct source credentials."""
registry_session.ensure_running()
result = registry_session.run(
"import", "docker.io/library/alpine:latest",
"--src-creds", "user:pass",
check=False, timeout=30
)
# Should attempt import with credentials
# Parsing should succeed even if auth fails
pass # Just verify no crash
class TestSecureRegistry:
"""Test secure registry mode with TLS and authentication.
These tests verify the secure registry infrastructure:
- PKI generation (CA cert, server cert with SAN)
- htpasswd authentication setup
- HTTPS endpoints
- Auto-credential usage for push
Prerequisites:
- openssl and htpasswd must be installed on the system
- CONTAINER_REGISTRY_SECURE=1 environment variable
"""
@pytest.fixture
def secure_env(self, tmp_path, monkeypatch):
"""Set up environment for secure registry testing."""
storage = tmp_path / "container-registry"
storage.mkdir()
monkeypatch.setenv("CONTAINER_REGISTRY_STORAGE", str(storage))
monkeypatch.setenv("CONTAINER_REGISTRY_SECURE", "1")
return storage
def test_help_shows_secure_mode(self, registry):
"""Test that help documents secure mode."""
result = registry.help()
output = result.stdout.lower()
# Help should mention secure mode or TLS
assert "secure" in output or "tls" in output or "https" in output
def test_start_generates_pki(self, registry, secure_env):
"""Test that start generates PKI in secure mode."""
# Note: Requires openssl and htpasswd installed
result = registry.start(timeout=60)
# Check for missing dependencies
output = result.stdout.lower()
if "openssl" in output and "not found" in output:
pytest.skip("openssl not available")
if "htpasswd" in output and "not found" in output:
pytest.skip("htpasswd not available")
# Skip if secure mode not enabled (baked script may not have it)
if "secure" not in output and "https" not in output:
pytest.skip("Secure mode not enabled in baked script")
pki_dir = secure_env / "pki"
auth_dir = secure_env / "auth"
# Check PKI files generated
assert (pki_dir / "ca.crt").exists(), "CA cert not generated"
assert (pki_dir / "ca.key").exists(), "CA key not generated"
assert (pki_dir / "server.crt").exists(), "Server cert not generated"
assert (pki_dir / "server.key").exists(), "Server key not generated"
# Check auth files generated
assert (auth_dir / "htpasswd").exists(), "htpasswd not generated"
assert (auth_dir / "password").exists(), "password file not generated"
# Verify permissions on sensitive files
ca_key_mode = oct((pki_dir / "ca.key").stat().st_mode)[-3:]
server_key_mode = oct((pki_dir / "server.key").stat().st_mode)[-3:]
password_mode = oct((auth_dir / "password").stat().st_mode)[-3:]
assert ca_key_mode == "600", f"CA key has wrong permissions: {ca_key_mode}"
assert server_key_mode == "600", f"Server key has wrong permissions: {server_key_mode}"
assert password_mode == "600", f"Password file has wrong permissions: {password_mode}"
def test_start_shows_https_url(self, registry, secure_env):
"""Test that start shows https:// URL in secure mode."""
result = registry.start(timeout=60)
output = result.stdout.lower()
# Skip if secure mode not enabled
if "secure" not in output and "https" not in output:
pytest.skip("Secure mode not enabled in baked script")
assert "https://" in result.stdout
def test_pki_not_regenerated(self, registry, secure_env):
"""Test that existing PKI is not overwritten."""
# First start generates PKI
result = registry.start(timeout=60)
output = result.stdout.lower()
# Skip if secure mode not enabled
if "secure" not in output and "https" not in output:
pytest.skip("Secure mode not enabled in baked script")
pki_dir = secure_env / "pki"
if not (pki_dir / "ca.crt").exists():
pytest.skip("PKI not generated (missing dependencies?)")
ca_crt_mtime = (pki_dir / "ca.crt").stat().st_mtime
# Stop and restart
registry.stop()
time.sleep(1)
registry.start(timeout=60)
# CA cert should not be regenerated
new_mtime = (pki_dir / "ca.crt").stat().st_mtime
assert new_mtime == ca_crt_mtime, "CA cert was regenerated"
def test_custom_username(self, registry, secure_env, monkeypatch):
"""Test custom username configuration."""
monkeypatch.setenv("CONTAINER_REGISTRY_USERNAME", "customuser")
result = registry.start(timeout=60)
output = result.stdout.lower()
# Skip if secure mode not enabled
if "secure" not in output and "https" not in output:
pytest.skip("Secure mode not enabled in baked script")
htpasswd = secure_env / "auth" / "htpasswd"
if not htpasswd.exists():
pytest.skip("htpasswd not generated (missing dependencies?)")
content = htpasswd.read_text()
assert content.startswith("customuser:"), f"htpasswd should start with customuser: but got {content[:20]}"
def test_custom_password(self, registry, secure_env, monkeypatch):
"""Test custom password configuration."""
monkeypatch.setenv("CONTAINER_REGISTRY_PASSWORD", "custompass123")
result = registry.start(timeout=60)
output = result.stdout.lower()
# Skip if secure mode not enabled
if "secure" not in output and "https" not in output:
pytest.skip("Secure mode not enabled in baked script")
password_file = secure_env / "auth" / "password"
if not password_file.exists():
pytest.skip("password file not generated (missing dependencies?)")
password = password_file.read_text().strip()
assert password == "custompass123", f"Password should be custompass123 but got {password}"
def test_server_cert_san(self, registry, secure_env):
"""Test that server cert includes expected SAN entries."""
result = registry.start(timeout=60)
output = result.stdout.lower()
# Skip if secure mode not enabled
if "secure" not in output and "https" not in output:
pytest.skip("Secure mode not enabled in baked script")
server_crt = secure_env / "pki" / "server.crt"
if not server_crt.exists():
pytest.skip("Server cert not generated (missing dependencies?)")
cert_result = subprocess.run(
["openssl", "x509", "-in", str(server_crt), "-noout", "-text"],
capture_output=True, text=True
)
if cert_result.returncode != 0:
pytest.skip("openssl not available")
cert_text = cert_result.stdout
# Check SAN entries
assert "DNS:localhost" in cert_text, "Server cert missing DNS:localhost SAN"
assert "IP Address:127.0.0.1" in cert_text, "Server cert missing IP:127.0.0.1 SAN"
assert "IP Address:10.0.2.2" in cert_text, "Server cert missing IP:10.0.2.2 SAN"
def test_push_uses_credentials(self, registry, secure_env):
"""Test that push auto-uses generated credentials in secure mode."""
result = registry.start(timeout=60)
output = result.stdout.lower()
# Skip if secure mode not enabled
if "secure" not in output and "https" not in output:
pytest.skip("Secure mode not enabled in baked script")
# Attempt push - it should use auto-generated credentials
push_result = registry.run("push", check=False, timeout=120)
push_output = push_result.stdout.lower()
# Should not show "unauthorized" errors (credentials should work)
# May show "no images" which is fine
assert "unauthorized" not in push_output, "Push failed with unauthorized - credentials not used"
@pytest.mark.network
@pytest.mark.slow
def test_secure_import(self, registry, secure_env, skip_network):
"""Test importing with TLS verification."""
if skip_network:
pytest.skip("Network tests disabled (--skip-registry-network)")
result = registry.start(timeout=60)
output = result.stdout.lower()
# Skip if secure mode not enabled
if "secure" not in output and "https" not in output:
pytest.skip("Secure mode not enabled in baked script")
# Import should work with our CA cert
import_result = registry.import_image(
"docker.io/library/alpine:latest",
timeout=300
)
assert import_result.returncode == 0, f"Import failed: {import_result.stdout}"
def test_tls_curl_verification(self, registry, secure_env):
"""Test that curl can verify the registry TLS."""
result = registry.start(timeout=60)
output = result.stdout.lower()
# Skip if secure mode not enabled
if "secure" not in output and "https" not in output:
pytest.skip("Secure mode not enabled in baked script")
ca_cert = secure_env / "pki" / "ca.crt"
password_file = secure_env / "auth" / "password"
if not ca_cert.exists() or not password_file.exists():
pytest.skip("PKI/auth not generated (missing dependencies?)")
password = password_file.read_text().strip()
curl_result = subprocess.run([
"curl", "-s", "--cacert", str(ca_cert),
"-u", f"yocto:{password}",
"https://localhost:5000/v2/_catalog"
], capture_output=True, text=True, timeout=30)
# Should get valid JSON response
assert curl_result.returncode == 0 or "repositories" in curl_result.stdout, \
f"Curl TLS verification failed: {curl_result.stderr}"
def test_status_shows_secure_mode(self, registry, secure_env):
"""Test that status indicates secure mode."""
result = registry.start(timeout=60)
start_output = result.stdout.lower()
# Skip if secure mode not enabled
if "secure" not in start_output and "https" not in start_output:
pytest.skip("Secure mode not enabled in baked script")
status_result = registry.status()
status_output = status_result.stdout.lower()
# Status should indicate secure/TLS mode
assert "https" in status_output or "secure" in status_output or "tls" in status_output, \
"Status should indicate secure mode"
class TestSecureRegistryTLSOnly:
"""Test TLS-only mode (SECURE=1, AUTH=0).
When AUTH is not enabled, the registry should:
- Use HTTPS (TLS) for connections
- NOT require authentication
- Allow anonymous pull/push
These tests work with an already-running secure registry.
"""
@pytest.fixture
def registry_storage(self, registry):
"""Get registry storage path from the script."""
result = registry.run("info", check=False)
# Parse storage path from info output
for line in result.stdout.splitlines():
if "Storage:" in line:
return Path(line.split("Storage:")[-1].strip())
# Fall back to common locations
for candidate in [
Path(os.environ.get("TOPDIR", "")) / "container-registry",
Path.cwd() / "container-registry",
Path.cwd().parent / "container-registry",
]:
if candidate.exists():
return candidate
pytest.skip("Cannot determine registry storage path")
def _ensure_secure_registry(self, registry):
"""Ensure a secure registry is running, starting one if needed."""
status = registry.status()
if status.returncode == 0:
# Already running - verify it's secure
if "secure" not in status.stdout.lower() and "tls" not in status.stdout.lower():
pytest.skip("Registry running but not in secure mode")
return
# Not running - try to start it
result = registry.start(timeout=60)
if result.returncode != 0:
pytest.skip(f"Could not start registry: {result.stderr}")
import time
time.sleep(2)
status = registry.status()
if status.returncode != 0:
pytest.skip("Registry failed to start")
if "secure" not in status.stdout.lower() and "tls" not in status.stdout.lower():
pytest.skip("Registry started but not in secure mode")
def test_status_shows_tls_only(self, registry):
"""Test that status shows TLS-only mode (not TLS+auth)."""
self._ensure_secure_registry(registry)
status = registry.status()
status_output = status.stdout.lower()
# Should show secure mode
assert "secure" in status_output or "tls" in status_output, \
"Status should indicate secure mode"
# In TLS-only mode, should say "tls only" not "tls + auth"
if "tls only" in status_output:
assert "tls + auth" not in status_output
def test_curl_without_auth(self, registry, registry_storage):
"""Test that curl can access registry without credentials."""
self._ensure_secure_registry(registry)
ca_cert = registry_storage / "pki" / "ca.crt"
if not ca_cert.exists():
pytest.skip("CA cert not found")
# Access WITHOUT credentials should work in TLS-only mode
curl_result = subprocess.run([
"curl", "-s", "--cacert", str(ca_cert),
"https://localhost:5000/v2/_catalog"
], capture_output=True, text=True, timeout=30)
assert "repositories" in curl_result.stdout, \
f"TLS-only registry should not require auth: {curl_result.stderr}"
def test_config_has_no_auth_section(self, registry, registry_storage):
"""Test that generated registry config has no active auth section."""
self._ensure_secure_registry(registry)
config_file = registry_storage / "registry-config.yml"
if not config_file.exists():
pytest.skip("Config file not found")
config_content = config_file.read_text()
# Check that the actual YAML auth: key is not present as a config directive
# (comments mentioning htpasswd are fine, only the active auth block matters)
non_comment_lines = [
line for line in config_content.splitlines()
if line.strip() and not line.strip().startswith('#')
]
active_config = '\n'.join(non_comment_lines)
assert "auth:" not in active_config, \
"TLS-only config should not have an active auth: section"
assert "tls:" in config_content, \
"TLS-only config should contain TLS section"
def test_pki_exists(self, registry, registry_storage):
"""Test that PKI infrastructure exists."""
self._ensure_secure_registry(registry)
pki_dir = registry_storage / "pki"
assert (pki_dir / "ca.crt").exists(), "CA cert should exist"
assert (pki_dir / "server.crt").exists(), "Server cert should exist"
assert (pki_dir / "server.key").exists(), "Server key should exist"
def test_no_htpasswd_generated(self, registry, registry_storage):
"""Test that TLS-only mode does not require htpasswd authentication.
Note: A stale auth/htpasswd may exist from previous runs when auth
was enabled. We verify the functional behavior: the registry config
has no auth section and anonymous access works (tested separately
in test_curl_without_auth). Here we check the config file.
"""
self._ensure_secure_registry(registry)
config_file = registry_storage / "registry-config.yml"
if not config_file.exists():
pytest.skip("Config file not found")
config_content = config_file.read_text()
non_comment_lines = [
line for line in config_content.splitlines()
if line.strip() and not line.strip().startswith('#')
]
active_config = '\n'.join(non_comment_lines)
assert "auth:" not in active_config, \
"TLS-only config should not reference htpasswd authentication"
def test_info_shows_no_auth(self, registry):
"""Test that info command reflects TLS-only (no auth section)."""
self._ensure_secure_registry(registry)
info_result = registry.run("info", check=False)
info_output = info_result.stdout
assert "PKI" in info_result.stdout or "pki" in info_result.stdout.lower(), \
"Info should show PKI directory"
assert "Password file" not in info_result.stdout, \
"Info should not show password file in TLS-only mode"
class TestSecureRegistryWithAuth:
"""Test TLS+auth mode (SECURE=1, AUTH=1).
Uses an isolated registry instance on port 5001 with its own
storage directory, so it never touches the user's running registry.
"""
TEST_PORT = "5001"
@pytest.fixture
def auth_registry(self, registry, tmp_path, monkeypatch):
"""Start an isolated auth-enabled registry on a different port."""
storage = tmp_path / "auth-registry"
storage.mkdir()
monkeypatch.setenv("CONTAINER_REGISTRY_STORAGE", str(storage))
monkeypatch.setenv("CONTAINER_REGISTRY_SECURE", "1")
monkeypatch.setenv("CONTAINER_REGISTRY_AUTH", "1")
monkeypatch.setenv("CONTAINER_REGISTRY_URL", f"localhost:{self.TEST_PORT}")
start_result = registry.start(timeout=60)
if start_result.returncode != 0:
pytest.skip(f"Could not start auth registry: {start_result.stderr}")
output = start_result.stdout.lower()
if "secure" not in output and "https" not in output:
registry.stop()
pytest.skip("Script does not support secure mode")
if "htpasswd" in output and "not found" in output:
registry.stop()
pytest.skip("htpasswd not available")
import time
time.sleep(2)
yield storage
registry.stop()
def test_curl_requires_auth(self, registry, auth_registry):
"""Test that curl without credentials is rejected."""
ca_cert = auth_registry / "pki" / "ca.crt"
if not ca_cert.exists():
pytest.skip("CA cert not found")
curl_result = subprocess.run([
"curl", "-s", "-o", "/dev/null", "-w", "%{http_code}",
"--cacert", str(ca_cert),
f"https://localhost:{self.TEST_PORT}/v2/_catalog"
], capture_output=True, text=True, timeout=30)
assert "401" in curl_result.stdout, \
f"Auth-enabled registry should reject unauthenticated requests, got: {curl_result.stdout}"
def test_curl_with_auth_succeeds(self, registry, auth_registry):
"""Test that curl with credentials succeeds."""
ca_cert = auth_registry / "pki" / "ca.crt"
password_file = auth_registry / "auth" / "password"
if not ca_cert.exists() or not password_file.exists():
pytest.skip("PKI/auth not generated")
password = password_file.read_text().strip()
curl_result = subprocess.run([
"curl", "-s", "--cacert", str(ca_cert),
"-u", f"yocto:{password}",
f"https://localhost:{self.TEST_PORT}/v2/_catalog"
], capture_output=True, text=True, timeout=30)
assert "repositories" in curl_result.stdout, \
f"Authenticated request should succeed: {curl_result.stderr}"
def test_config_has_auth_section(self, registry, auth_registry):
"""Test that generated registry config includes auth section."""
config_file = auth_registry / "registry-config.yml"
if not config_file.exists():
pytest.skip("Config file not found")
config_content = config_file.read_text()
assert "htpasswd" in config_content, \
"Auth-enabled config should contain htpasswd section"
assert "tls:" in config_content, \
"Auth-enabled config should also contain TLS section"
def test_htpasswd_generated(self, registry, auth_registry):
"""Test that htpasswd file is generated in auth mode."""
auth_dir = auth_registry / "auth"
assert (auth_dir / "htpasswd").exists(), "htpasswd not generated in auth mode"
assert (auth_dir / "password").exists(), "password file not generated"
class TestDockerRegistryConfig:
"""Test docker-registry-config.bb behavior.
Verifies the bitbake recipe logic for generating Docker daemon
configuration on target images, specifically:
- localhost→10.0.2.2 translation for QEMU targets
- CA cert installation path matches registry host
"""
def test_bbclass_has_auth_variable(self):
"""Test that container-registry.bbclass defines CONTAINER_REGISTRY_AUTH."""
bbclass = Path("/opt/bruce/poky/meta-virtualization/classes/container-registry.bbclass")
if not bbclass.exists():
pytest.skip("container-registry.bbclass not found")
content = bbclass.read_text()
assert 'CONTAINER_REGISTRY_AUTH' in content, \
"bbclass should define CONTAINER_REGISTRY_AUTH variable"
assert 'CONTAINER_REGISTRY_AUTH ?= "0"' in content, \
"CONTAINER_REGISTRY_AUTH should default to 0"
def test_bbclass_validates_auth_requires_secure(self):
"""Test that bbclass warns when AUTH=1 without SECURE=1."""
bbclass = Path("/opt/bruce/poky/meta-virtualization/classes/container-registry.bbclass")
if not bbclass.exists():
pytest.skip("container-registry.bbclass not found")
content = bbclass.read_text()
assert "auth and not secure" in content or "AUTH" in content, \
"bbclass should validate that AUTH requires SECURE"
def test_docker_registry_config_translates_localhost(self):
"""Test that docker-registry-config.bb translates localhost to 10.0.2.2."""
recipe = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"container-registry/docker-registry-config.bb")
if not recipe.exists():
pytest.skip("docker-registry-config.bb not found")
content = recipe.read_text()
assert "10.0.2.2" in content, \
"Recipe should translate localhost to 10.0.2.2 for QEMU"
assert "replace" in content.lower() and "localhost" in content, \
"Recipe should replace localhost with 10.0.2.2"
def test_docker_registry_config_translates_127(self):
"""Test that docker-registry-config.bb also translates 127.0.0.1."""
recipe = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"container-registry/docker-registry-config.bb")
if not recipe.exists():
pytest.skip("docker-registry-config.bb not found")
content = recipe.read_text()
assert "127.0.0.1" in content, \
"Recipe should also translate 127.0.0.1 to 10.0.2.2"
class TestContainerCrossInstallSecure:
"""Test container-cross-install.bbclass secure registry integration.
Verifies that the cross-install class automatically adds the
docker-registry-config package when CONTAINER_REGISTRY_SECURE=1.
"""
def test_cross_install_auto_adds_registry_config(self):
"""Test that cross-install adds docker-registry-config when SECURE=1."""
bbclass = Path("/opt/bruce/poky/meta-virtualization/classes/"
"container-cross-install.bbclass")
if not bbclass.exists():
pytest.skip("container-cross-install.bbclass not found")
content = bbclass.read_text()
assert "CONTAINER_REGISTRY_SECURE" in content, \
"Cross-install should check CONTAINER_REGISTRY_SECURE"
assert "docker-registry-config" in content, \
"Cross-install should add docker-registry-config in secure mode"
def test_cross_install_supports_podman_config(self):
"""Test that cross-install adds container-oci-registry-config for podman."""
bbclass = Path("/opt/bruce/poky/meta-virtualization/classes/"
"container-cross-install.bbclass")
if not bbclass.exists():
pytest.skip("container-cross-install.bbclass not found")
content = bbclass.read_text()
assert "container-oci-registry-config" in content, \
"Cross-install should support podman registry config"
def test_cross_install_checks_container_engine(self):
"""Test that cross-install selects config package based on engine."""
bbclass = Path("/opt/bruce/poky/meta-virtualization/classes/"
"container-cross-install.bbclass")
if not bbclass.exists():
pytest.skip("container-cross-install.bbclass not found")
content = bbclass.read_text()
assert "VIRTUAL-RUNTIME_container_engine" in content, \
"Cross-install should check container engine to select config package"
class TestVcontainerSecureRegistry:
"""Test vcontainer shell script secure registry support.
Verifies the host-side scripts handle CA certificates correctly:
- Auto-detection of bundled CA cert
- CA cert transport via virtio-9p (not kernel cmdline)
- Daemon mode sets _9p=1 for share mounting
"""
def test_vcontainer_common_auto_detects_ca_cert(self):
"""Test that vcontainer-common.sh auto-detects bundled CA cert."""
script = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"vcontainer/files/vcontainer-common.sh")
if not script.exists():
pytest.skip("vcontainer-common.sh not found")
content = script.read_text()
assert "registry/ca.crt" in content, \
"Should check for bundled CA cert at registry/ca.crt"
assert "BUNDLED_CA_CERT" in content, \
"Should define BUNDLED_CA_CERT variable"
assert 'SECURE_REGISTRY="true"' in content, \
"Should auto-enable SECURE_REGISTRY when CA cert found"
def test_vrunner_passes_ca_via_virtio9p(self):
"""Test that vrunner.sh passes CA cert via virtio-9p, not cmdline."""
script = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"vcontainer/files/vrunner.sh")
if not script.exists():
pytest.skip("vrunner.sh not found")
content = script.read_text()
# Should copy CA cert to shared folder in daemon mode
assert "DAEMON_SHARE_DIR" in content and "ca.crt" in content, \
"Should copy CA cert to DAEMON_SHARE_DIR in daemon mode"
# Should NOT base64 encode CA cert for cmdline
assert "base64" not in content.split("CA_CERT")[0].split("CA_CERT")[-1] \
or "registry_pass" in content, \
"Should not base64 encode CA cert for kernel cmdline"
def test_vrunner_daemon_sets_9p(self):
"""Test that daemon mode sets _9p=1 in kernel cmdline."""
script = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"vcontainer/files/vrunner.sh")
if not script.exists():
pytest.skip("vrunner.sh not found")
content = script.read_text()
# Find the daemon mode block and check for _9p=1
# The daemon block should contain both virtfs and _9p=1
lines = content.split('\n')
in_daemon_block = False
daemon_has_virtfs = False
daemon_has_9p = False
for line in lines:
if 'DAEMON_MODE" = "start"' in line or "DAEMON_MODE\" = \"start\"" in line:
in_daemon_block = True
if in_daemon_block:
if "virtfs" in line and "DAEMON_SHARE_DIR" in line:
daemon_has_virtfs = True
if "_9p=1" in line:
daemon_has_9p = True
# Detect end of the if block (next top-level statement)
if line.startswith("fi") and in_daemon_block and daemon_has_virtfs:
break
assert daemon_has_virtfs, "Daemon mode should set up virtio-9p share"
assert daemon_has_9p, "Daemon mode should set _9p=1 in kernel cmdline"
def test_vrunner_nondaemon_ca_cert_virtio9p(self):
"""Test that non-daemon mode creates virtio-9p share for CA cert."""
script = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"vcontainer/files/vrunner.sh")
if not script.exists():
pytest.skip("vrunner.sh not found")
content = script.read_text()
assert "CA_SHARE_DIR" in content, \
"Non-daemon mode should create CA_SHARE_DIR for virtio-9p"
assert "cashare" in content, \
"Should add virtio-9p device for CA cert sharing"
def test_vdkr_init_reads_ca_from_share(self):
"""Test that vdkr-init.sh reads CA cert from /mnt/share/ca.crt."""
script = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"vcontainer/files/vdkr-init.sh")
if not script.exists():
pytest.skip("vdkr-init.sh not found")
content = script.read_text()
assert "/mnt/share/ca.crt" in content, \
"Should check for CA cert at /mnt/share/ca.crt"
def test_vdkr_init_no_base64_ca_decode(self):
"""Test that vdkr-init.sh no longer decodes base64 CA from cmdline."""
script = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"vcontainer/files/vdkr-init.sh")
if not script.exists():
pytest.skip("vdkr-init.sh not found")
content = script.read_text()
# Should NOT have docker_registry_ca=<base64> pattern in cmdline parsing
assert "docker_registry_ca=" not in content or "docker_registry_ca=1" in content, \
"Should not parse base64 CA cert from kernel cmdline"
def test_vdkr_init_copies_ca_from_share(self):
"""Test that vdkr-init.sh copies CA cert from shared folder."""
script = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"vcontainer/files/vdkr-init.sh")
if not script.exists():
pytest.skip("vdkr-init.sh not found")
content = script.read_text()
# Should copy from DOCKER_REGISTRY_CA (which is /mnt/share/ca.crt)
assert "cp" in content and "DOCKER_REGISTRY_CA" in content, \
"Should copy CA cert from shared folder path"
assert "/etc/docker/certs.d/" in content, \
"Should install CA cert to Docker certs.d directory"
def test_vcontainer_tarball_tracks_scripts(self):
"""Test that vcontainer-tarball.bb tracks script files via SRC_URI."""
recipe = Path("/opt/bruce/poky/meta-virtualization/recipes-containers/"
"vcontainer/vcontainer-tarball.bb")
if not recipe.exists():
pytest.skip("vcontainer-tarball.bb not found")
content = recipe.read_text()
assert "SRC_URI" in content, "Should have SRC_URI for file tracking"
assert "vrunner.sh" in content, "Should track vrunner.sh"
assert "vcontainer-common.sh" in content, "Should track vcontainer-common.sh"
|