|
| 1 | +/* |
| 2 | + * Copyright 2024 The CNAI Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package backend |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/CloudNativeAI/modctl/test/mocks/storage" |
| 25 | + |
| 26 | + godigest "github.com/opencontainers/go-digest" |
| 27 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | +) |
| 30 | + |
| 31 | +func TestPrune(t *testing.T) { |
| 32 | + mockStore := &storage.Storage{} |
| 33 | + b := &backend{store: mockStore} |
| 34 | + ctx := context.Background() |
| 35 | + repos := []string{"example.com/repo1", "example.com/repo2"} |
| 36 | + |
| 37 | + mustToJson := func(v any) []byte { |
| 38 | + jsonByte, err := json.Marshal(v) |
| 39 | + assert.NoError(t, err) |
| 40 | + return jsonByte |
| 41 | + } |
| 42 | + |
| 43 | + indexs := []ocispec.Index{ |
| 44 | + { |
| 45 | + Manifests: []ocispec.Descriptor{ |
| 46 | + {Digest: godigest.Digest("sha256:1234567890abcdef-repo1")}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + Manifests: []ocispec.Descriptor{ |
| 51 | + {Digest: godigest.Digest("sha256:1234567890abcdef-repo2")}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + manifests := []ocispec.Manifest{ |
| 56 | + {Layers: []ocispec.Descriptor{{Digest: godigest.Digest("sha256:1234567890abcdef-repo1-blob1")}}}, |
| 57 | + {Layers: []ocispec.Descriptor{{Digest: godigest.Digest("sha256:1234567890abcdef-repo2-blob1")}}}, |
| 58 | + } |
| 59 | + blobs := []string{ |
| 60 | + "sha256:1234567890abcdef-repo1-blob1", |
| 61 | + "sha256:1234567890abcdef-repo1-blob2", |
| 62 | + "sha256:1234567890abcdef-repo2-blob1", |
| 63 | + "sha256:1234567890abcdef-repo2-blob2", |
| 64 | + } |
| 65 | + |
| 66 | + mockStore.On("ListRepositories", ctx).Return(repos, nil) |
| 67 | + mockStore.On("GetIndex", ctx, repos[0]).Return(mustToJson(indexs[0]), nil) |
| 68 | + mockStore.On("GetIndex", ctx, repos[1]).Return(mustToJson(indexs[1]), nil) |
| 69 | + mockStore.On("PullManifest", ctx, repos[0], indexs[0].Manifests[0].Digest.String()).Return(mustToJson(manifests[0]), indexs[0].Manifests[0].Digest.String(), nil) |
| 70 | + mockStore.On("PullManifest", ctx, repos[1], indexs[1].Manifests[0].Digest.String()).Return(mustToJson(manifests[1]), indexs[1].Manifests[0].Digest.String(), nil) |
| 71 | + mockStore.On("ListBlobs", ctx, repos[0]).Return([]string{blobs[0], blobs[1]}, nil) |
| 72 | + mockStore.On("ListBlobs", ctx, repos[1]).Return([]string{blobs[2], blobs[3]}, nil) |
| 73 | + mockStore.On("CleanupRepo", ctx, repos[0], []string{blobs[1]}, false).Return(1, nil) |
| 74 | + mockStore.On("CleanupRepo", ctx, repos[1], []string{blobs[3]}, false).Return(1, nil) |
| 75 | + |
| 76 | + prunedBlobs, err := b.Prune(ctx) |
| 77 | + assert.NoError(t, err) |
| 78 | + assert.Len(t, prunedBlobs, 2, "pruned blobs length is not equal to expected length") |
| 79 | + assert.Equal(t, repos[0]+"@"+blobs[1], prunedBlobs[0], "pruned blob is not equal to expected blob") |
| 80 | + assert.Equal(t, repos[1]+"@"+blobs[3], prunedBlobs[1], "pruned blob is not equal to expected blob") |
| 81 | +} |
0 commit comments