test(registry): assert SpaceReclaimed in blob GC test
fakeBlobStorage.DeleteBlob previously always returned 0, so the service accumulation logic was never exercised. Add blobSizes map to the fake and verify the GC test aggregates the correct total.
- commit
- 16606581f4dba44b75ea14434784c61255a3482e
- author
- brice · · 2026-03-27 19:03 UTC
- parent
- 52cf0df
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +11 -2
M internal/usecase/images/service_test.go +11 -2
| @@ -318,13 +318,17 @@ func TestService_PruneRegistry_GarbageCollectsUnreferencedBlobs(t *testing.T) { | |||
| 318 | 318 | manifestStorage.modTimes[manifestRefKey("gordon/api", "latest")] = time.Date(2026, 2, 8, 12, 0, 0, 0, time.UTC) | |
| 319 | 319 | manifestStorage.manifests[manifestRefKey("gordon/api", "latest")] = mustManifestJSON(t, "sha256:cfg-live", "sha256:layer-live") | |
| 320 | 320 | ||
| 321 | - | blobStorage := &fakeBlobStorage{blobs: []string{"sha256:cfg-live", "sha256:layer-live", "sha256:orphan"}} | |
| 321 | + | blobStorage := &fakeBlobStorage{ | |
| 322 | + | blobs: []string{"sha256:cfg-live", "sha256:layer-live", "sha256:orphan"}, | |
| 323 | + | blobSizes: map[string]int64{"sha256:orphan": 4096}, | |
| 324 | + | } | |
| 322 | 325 | svc := NewService(&fakeRuntime{}, manifestStorage, blobStorage, zerowrap.Default()) | |
| 323 | 326 | ||
| 324 | 327 | report, err := svc.PruneRegistry(context.Background(), 1) | |
| 325 | 328 | ||
| 326 | 329 | require.NoError(t, err) | |
| 327 | 330 | assert.Equal(t, 1, report.Registry.BlobsRemoved) | |
| 331 | + | assert.Equal(t, int64(4096), report.Registry.SpaceReclaimed) | |
| 328 | 332 | assert.Equal(t, []string{"sha256:orphan"}, blobStorage.deletedBlobs) | |
| 329 | 333 | } | |
| 330 | 334 | ||
| @@ -663,6 +667,7 @@ type fakeBlobStorage struct { | |||
| 663 | 667 | out.BlobStorage | |
| 664 | 668 | ||
| 665 | 669 | blobs []string | |
| 670 | + | blobSizes map[string]int64 | |
| 666 | 671 | deletedBlobs []string | |
| 667 | 672 | } | |
| 668 | 673 | ||
| @@ -791,6 +796,10 @@ func (f *fakeBlobStorage) ListBlobs() ([]string, error) { | |||
| 791 | 796 | ||
| 792 | 797 | func (f *fakeBlobStorage) DeleteBlob(digest string) (int64, error) { | |
| 793 | 798 | f.deletedBlobs = append(f.deletedBlobs, digest) | |
| 799 | + | var size int64 | |
| 800 | + | if f.blobSizes != nil { | |
| 801 | + | size = f.blobSizes[digest] | |
| 802 | + | } | |
| 794 | 803 | for i, existing := range f.blobs { | |
| 795 | 804 | if existing != digest { | |
| 796 | 805 | continue | |
| @@ -798,7 +807,7 @@ func (f *fakeBlobStorage) DeleteBlob(digest string) (int64, error) { | |||
| 798 | 807 | f.blobs = append(f.blobs[:i], f.blobs[i+1:]...) | |
| 799 | 808 | break | |
| 800 | 809 | } | |
| 801 | - | return 0, nil | |
| 810 | + | return size, nil | |
| 802 | 811 | } | |
| 803 | 812 | ||
| 804 | 813 | func (f *fakeBlobStorage) CleanupStaleUploads(_ time.Duration) (int, int64, error) { | |
| @@ -318,13 +318,17 @@ func TestService_PruneRegistry_GarbageCollectsUnreferencedBlobs(t *testing.T) { | |||||
| 318 | manifestStorage.modTimes[manifestRefKey("gordon/api", "latest")] = time.Date(2026, 2, 8, 12, 0, 0, 0, time.UTC) | 318 | manifestStorage.modTimes[manifestRefKey("gordon/api", "latest")] = time.Date(2026, 2, 8, 12, 0, 0, 0, time.UTC) | ||
| 319 | manifestStorage.manifests[manifestRefKey("gordon/api", "latest")] = mustManifestJSON(t, "sha256:cfg-live", "sha256:layer-live") | 319 | manifestStorage.manifests[manifestRefKey("gordon/api", "latest")] = mustManifestJSON(t, "sha256:cfg-live", "sha256:layer-live") | ||
| 320 | 320 | ||||
| 321 | - | blobStorage := &fakeBlobStorage{blobs: []string{"sha256:cfg-live", "sha256:layer-live", "sha256:orphan"}} | 321 | + | blobStorage := &fakeBlobStorage{ |
| 322 | + | blobs: []string{"sha256:cfg-live", "sha256:layer-live", "sha256:orphan"}, | |||
| 323 | + | blobSizes: map[string]int64{"sha256:orphan": 4096}, | |||
| 324 | + | } | |||
| 322 | svc := NewService(&fakeRuntime{}, manifestStorage, blobStorage, zerowrap.Default()) | 325 | svc := NewService(&fakeRuntime{}, manifestStorage, blobStorage, zerowrap.Default()) | ||
| 323 | 326 | ||||
| 324 | report, err := svc.PruneRegistry(context.Background(), 1) | 327 | report, err := svc.PruneRegistry(context.Background(), 1) | ||
| 325 | 328 | ||||
| 326 | require.NoError(t, err) | 329 | require.NoError(t, err) | ||
| 327 | assert.Equal(t, 1, report.Registry.BlobsRemoved) | 330 | assert.Equal(t, 1, report.Registry.BlobsRemoved) | ||
| 331 | + | assert.Equal(t, int64(4096), report.Registry.SpaceReclaimed) | |||
| 328 | assert.Equal(t, []string{"sha256:orphan"}, blobStorage.deletedBlobs) | 332 | assert.Equal(t, []string{"sha256:orphan"}, blobStorage.deletedBlobs) | ||
| 329 | } | 333 | } | ||
| 330 | 334 | ||||
| @@ -663,6 +667,7 @@ type fakeBlobStorage struct { | |||||
| 663 | out.BlobStorage | 667 | out.BlobStorage | ||
| 664 | 668 | ||||
| 665 | blobs []string | 669 | blobs []string | ||
| 670 | + | blobSizes map[string]int64 | |||
| 666 | deletedBlobs []string | 671 | deletedBlobs []string | ||
| 667 | } | 672 | } | ||
| 668 | 673 | ||||
| @@ -791,6 +796,10 @@ func (f *fakeBlobStorage) ListBlobs() ([]string, error) { | |||||
| 791 | 796 | ||||
| 792 | func (f *fakeBlobStorage) DeleteBlob(digest string) (int64, error) { | 797 | func (f *fakeBlobStorage) DeleteBlob(digest string) (int64, error) { | ||
| 793 | f.deletedBlobs = append(f.deletedBlobs, digest) | 798 | f.deletedBlobs = append(f.deletedBlobs, digest) | ||
| 799 | + | var size int64 | |||
| 800 | + | if f.blobSizes != nil { | |||
| 801 | + | size = f.blobSizes[digest] | |||
| 802 | + | } | |||
| 794 | for i, existing := range f.blobs { | 803 | for i, existing := range f.blobs { | ||
| 795 | if existing != digest { | 804 | if existing != digest { | ||
| 796 | continue | 805 | continue | ||
| @@ -798,7 +807,7 @@ func (f *fakeBlobStorage) DeleteBlob(digest string) (int64, error) { | |||||
| 798 | f.blobs = append(f.blobs[:i], f.blobs[i+1:]...) | 807 | f.blobs = append(f.blobs[:i], f.blobs[i+1:]...) | ||
| 799 | break | 808 | break | ||
| 800 | } | 809 | } | ||
| 801 | - | return 0, nil | 810 | + | return size, nil |
| 802 | } | 811 | } | ||
| 803 | 812 | ||||
| 804 | func (f *fakeBlobStorage) CleanupStaleUploads(_ time.Duration) (int, int64, error) { | 813 | func (f *fakeBlobStorage) CleanupStaleUploads(_ time.Duration) (int, int64, error) { | ||