|
| 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 | + "errors" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/CloudNativeAI/modctl/test/mocks/storage" |
| 26 | + |
| 27 | + v1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "github.com/stretchr/testify/mock" |
| 30 | +) |
| 31 | + |
| 32 | +func TestTag(t *testing.T) { |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + source string |
| 36 | + target string |
| 37 | + setupMocks func(*storage.Storage) |
| 38 | + expectedErr string |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "successful tag", |
| 42 | + source: "localhost:5000/repo:tag1", |
| 43 | + target: "localhost:5000/repo:tag2", |
| 44 | + setupMocks: func(s *storage.Storage) { |
| 45 | + manifest := v1.Manifest{ |
| 46 | + Config: v1.Descriptor{ |
| 47 | + MediaType: "application/vnd.oci.image.config.v1+json", |
| 48 | + Digest: "sha256:config", |
| 49 | + Size: 100, |
| 50 | + }, |
| 51 | + Layers: []v1.Descriptor{ |
| 52 | + { |
| 53 | + MediaType: "application/vnd.oci.image.layer.v1.tar+gzip", |
| 54 | + Digest: "sha256:layer1", |
| 55 | + Size: 200, |
| 56 | + }, |
| 57 | + { |
| 58 | + MediaType: "application/vnd.oci.image.layer.v1.tar+gzip", |
| 59 | + Digest: "sha256:layer2", |
| 60 | + Size: 300, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | + manifestBytes, _ := json.Marshal(manifest) |
| 65 | + s.On("PullManifest", mock.Anything, "localhost:5000/repo", "tag1"). |
| 66 | + Return(manifestBytes, "sha256:manifest", nil) |
| 67 | + |
| 68 | + s.On("MountBlob", mock.Anything, "localhost:5000/repo", "localhost:5000/repo", manifest.Config). |
| 69 | + Return(nil) |
| 70 | + |
| 71 | + for _, layer := range manifest.Layers { |
| 72 | + s.On("MountBlob", mock.Anything, "localhost:5000/repo", "localhost:5000/repo", layer). |
| 73 | + Return(nil) |
| 74 | + } |
| 75 | + |
| 76 | + s.On("PushManifest", mock.Anything, "localhost:5000/repo", "tag2", manifestBytes). |
| 77 | + Return("sha256:manifest", nil) |
| 78 | + }, |
| 79 | + expectedErr: "", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "invalid source reference", |
| 83 | + source: "invalid-reference", |
| 84 | + target: "localhost:5000/repo:tag2", |
| 85 | + setupMocks: func(s *storage.Storage) { |
| 86 | + // No mocks needed as we expect to fail before hitting the storage |
| 87 | + }, |
| 88 | + expectedErr: "failed to parse source", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "invalid target reference", |
| 92 | + source: "localhost:5000/repo:tag1", |
| 93 | + target: "invalid-reference", |
| 94 | + setupMocks: func(s *storage.Storage) { |
| 95 | + // No mocks needed as we expect to fail before hitting the storage |
| 96 | + }, |
| 97 | + expectedErr: "failed to parse target", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "pull manifest error", |
| 101 | + source: "localhost:5000/repo:tag1", |
| 102 | + target: "localhost:5000/repo:tag2", |
| 103 | + setupMocks: func(s *storage.Storage) { |
| 104 | + s.On("PullManifest", mock.Anything, "localhost:5000/repo", "tag1"). |
| 105 | + Return([]byte{}, "", errors.New("manifest not found")) |
| 106 | + }, |
| 107 | + expectedErr: "failed to pull manifest", |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "mount blob error", |
| 111 | + source: "localhost:5000/repo:tag1", |
| 112 | + target: "localhost:5000/repo:tag2", |
| 113 | + setupMocks: func(s *storage.Storage) { |
| 114 | + manifest := v1.Manifest{ |
| 115 | + Config: v1.Descriptor{ |
| 116 | + MediaType: "application/vnd.oci.image.config.v1+json", |
| 117 | + Digest: "sha256:config", |
| 118 | + Size: 100, |
| 119 | + }, |
| 120 | + Layers: []v1.Descriptor{ |
| 121 | + { |
| 122 | + MediaType: "application/vnd.oci.image.layer.v1.tar+gzip", |
| 123 | + Digest: "sha256:layer1", |
| 124 | + Size: 200, |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | + manifestBytes, _ := json.Marshal(manifest) |
| 129 | + |
| 130 | + s.On("PullManifest", mock.Anything, "localhost:5000/repo", "tag1"). |
| 131 | + Return(manifestBytes, "sha256:manifest", nil) |
| 132 | + |
| 133 | + s.On("MountBlob", mock.Anything, "localhost:5000/repo", "localhost:5000/repo", manifest.Config). |
| 134 | + Return(errors.New("mount blob failed")) |
| 135 | + }, |
| 136 | + expectedErr: "failed to mount blob", |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "push manifest error", |
| 140 | + source: "localhost:5000/repo:tag1", |
| 141 | + target: "localhost:5000/repo:tag2", |
| 142 | + setupMocks: func(s *storage.Storage) { |
| 143 | + manifest := v1.Manifest{ |
| 144 | + Config: v1.Descriptor{ |
| 145 | + MediaType: "application/vnd.oci.image.config.v1+json", |
| 146 | + Digest: "sha256:config", |
| 147 | + Size: 100, |
| 148 | + }, |
| 149 | + Layers: []v1.Descriptor{}, |
| 150 | + } |
| 151 | + manifestBytes, _ := json.Marshal(manifest) |
| 152 | + s.On("PullManifest", mock.Anything, "localhost:5000/repo", "tag1"). |
| 153 | + Return(manifestBytes, "sha256:manifest", nil) |
| 154 | + |
| 155 | + s.On("MountBlob", mock.Anything, "localhost:5000/repo", "localhost:5000/repo", manifest.Config). |
| 156 | + Return(nil) |
| 157 | + |
| 158 | + s.On("PushManifest", mock.Anything, "localhost:5000/repo", "tag2", manifestBytes). |
| 159 | + Return("", errors.New("push manifest failed")) |
| 160 | + }, |
| 161 | + expectedErr: "failed to push manifest", |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "invalid manifest json", |
| 165 | + source: "localhost:5000/repo:tag1", |
| 166 | + target: "localhost:5000/repo:tag2", |
| 167 | + setupMocks: func(s *storage.Storage) { |
| 168 | + // Return invalid JSON as manifest |
| 169 | + s.On("PullManifest", mock.Anything, "localhost:5000/repo", "tag1"). |
| 170 | + Return([]byte{123}, "sha256:invalid", nil) |
| 171 | + }, |
| 172 | + expectedErr: "failed to unmarshal manifest", |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + for _, tt := range tests { |
| 177 | + t.Run(tt.name, func(t *testing.T) { |
| 178 | + mockStorage := storage.NewStorage(t) |
| 179 | + tt.setupMocks(mockStorage) |
| 180 | + |
| 181 | + b := &backend{ |
| 182 | + store: mockStorage, |
| 183 | + } |
| 184 | + |
| 185 | + err := b.Tag(context.Background(), tt.source, tt.target) |
| 186 | + if tt.expectedErr != "" { |
| 187 | + assert.Error(t, err) |
| 188 | + assert.Contains(t, err.Error(), tt.expectedErr) |
| 189 | + } else { |
| 190 | + assert.NoError(t, err) |
| 191 | + } |
| 192 | + }) |
| 193 | + } |
| 194 | +} |
0 commit comments