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
|
# 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()
|