@@ -19,6 +19,7 @@ package image
19
19
import (
20
20
"fmt"
21
21
"reflect"
22
+ "strings"
22
23
"testing"
23
24
24
25
"github.com/google/go-cmp/cmp"
@@ -172,3 +173,105 @@ func TestGetMappedImageConfigs(t *testing.T) {
172
173
t .Fatal (cmp .Diff (expected , actual ))
173
174
}
174
175
}
176
+
177
+ func TestGetImageConfigsWithMappedImage (t * testing.T ) {
178
+ tests := []struct {
179
+ name string
180
+ originalImageConfigs map [ImageID ]Config
181
+ repo string
182
+ specialImages []ImageID // Images that should not be mapped
183
+ }{
184
+ {
185
+ name : "maps normal images to new repository" ,
186
+ originalImageConfigs : map [ImageID ]Config {
187
+ Agnhost : {registry : "registry.k8s.io/e2e-test-images" , name : "agnhost" , version : "2.47" },
188
+ BusyBox : {registry : "registry.k8s.io/e2e-test-images" , name : "busybox" , version : "1.36.1-1" },
189
+ },
190
+ repo : "quay.io/test/repo" ,
191
+ specialImages : []ImageID {},
192
+ },
193
+ {
194
+ name : "does not map special images" ,
195
+ originalImageConfigs : map [ImageID ]Config {
196
+ InvalidRegistryImage : {registry : "invalid.registry.k8s.io/invalid" , name : "alpine" , version : "3.1" },
197
+ AuthenticatedAlpine : {registry : "gcr.io/authenticated-image-pulling" , name : "alpine" , version : "3.7" },
198
+ AuthenticatedWindowsNanoServer : {registry : "gcr.io/authenticated-image-pulling" , name : "windows-nanoserver" , version : "v1" },
199
+ AgnhostPrivate : {registry : "gcr.io/k8s-authenticated-test" , name : "agnhost" , version : "2.6" },
200
+ },
201
+ repo : "quay.io/test/repo" ,
202
+ specialImages : []ImageID {InvalidRegistryImage , AuthenticatedAlpine , AuthenticatedWindowsNanoServer , AgnhostPrivate },
203
+ },
204
+ {
205
+ name : "handles mixed normal and special images" ,
206
+ originalImageConfigs : map [ImageID ]Config {
207
+ Nginx : {registry : "registry.k8s.io/e2e-test-images" , name : "nginx" , version : "1.14-4" },
208
+ AuthenticatedAlpine : {registry : "gcr.io/authenticated-image-pulling" , name : "alpine" , version : "3.7" },
209
+ Pause : {registry : "registry.k8s.io" , name : "pause" , version : "3.9" },
210
+ InvalidRegistryImage : {registry : "invalid.registry.k8s.io/invalid" , name : "alpine" , version : "3.1" },
211
+ },
212
+ repo : "my-registry.io/my-repo" ,
213
+ specialImages : []ImageID {AuthenticatedAlpine , InvalidRegistryImage },
214
+ },
215
+ {
216
+ name : "handles empty input" ,
217
+ originalImageConfigs : map [ImageID ]Config {},
218
+ repo : "quay.io/test/repo" ,
219
+ specialImages : []ImageID {},
220
+ },
221
+ }
222
+
223
+ for _ , tt := range tests {
224
+ t .Run (tt .name , func (t * testing.T ) {
225
+ result := GetImageConfigsWithMappedImage (tt .originalImageConfigs , tt .repo )
226
+
227
+ if len (result ) != len (tt .originalImageConfigs ) {
228
+ t .Errorf ("expected %d mapped configs, got %d" , len (tt .originalImageConfigs ), len (result ))
229
+ }
230
+
231
+ for imageID , originalConfig := range tt .originalImageConfigs {
232
+ actualConfigWithMapped , exists := result [imageID ]
233
+ if ! exists {
234
+ t .Errorf ("expected imageID %v to be present in result" , imageID )
235
+ continue
236
+ }
237
+
238
+ // Check that original config is preserved
239
+ actualOriginalImage := actualConfigWithMapped .Config .GetE2EImage ()
240
+ expectedOriginalImage := originalConfig .GetE2EImage ()
241
+ if actualOriginalImage != expectedOriginalImage {
242
+ t .Errorf ("original config mismatch for imageID %v: expected %q, got %q" , imageID , expectedOriginalImage , actualOriginalImage )
243
+ }
244
+
245
+ // Check if this is a special image that should not be mapped
246
+ isSpecial := false
247
+ for _ , specialID := range tt .specialImages {
248
+ if imageID == specialID {
249
+ isSpecial = true
250
+ break
251
+ }
252
+ }
253
+
254
+ actualMappedImage := actualConfigWithMapped .mapped .GetE2EImage ()
255
+ if isSpecial {
256
+ // Special images should have empty mapped config (which results in "/:")
257
+ if actualMappedImage != "/:" {
258
+ t .Errorf ("special image %v should have empty mapped config (resulting in '/:'), got %q" , imageID , actualMappedImage )
259
+ }
260
+ } else {
261
+ // Normal images should have a mapped config that's different from original
262
+ if actualMappedImage == "" || actualMappedImage == "/:" {
263
+ t .Errorf ("normal image %v should have non-empty mapped config, got %q" , imageID , actualMappedImage )
264
+ }
265
+ if actualMappedImage == actualOriginalImage {
266
+ t .Errorf ("mapped image should be different from original for imageID %v" , imageID )
267
+ }
268
+ // Verify the mapped image uses the new repository
269
+ expectedRepoPrefix := strings .Split (tt .repo , "/" )[0 ]
270
+ if ! strings .HasPrefix (actualMappedImage , expectedRepoPrefix ) {
271
+ t .Errorf ("mapped image %q should start with repository %q" , actualMappedImage , expectedRepoPrefix )
272
+ }
273
+ }
274
+ }
275
+ })
276
+ }
277
+ }
0 commit comments