k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/utils/image/manifest_test.go (about) 1 /* 2 Copyright 2019 The Kubernetes 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 image 18 19 import ( 20 "fmt" 21 "reflect" 22 "testing" 23 24 "github.com/google/go-cmp/cmp" 25 ) 26 27 func BenchmarkReplaceRegistryInImageURL(b *testing.B) { 28 registryTests := []struct { 29 in string 30 out string 31 }{ 32 { 33 in: "docker.io/library/test:123", 34 out: "test.io/library/test:123", 35 }, { 36 in: "docker.io/library/test", 37 out: "test.io/library/test", 38 }, { 39 in: "test", 40 out: "test.io/library/test", 41 }, { 42 in: "registry.k8s.io/test:123", 43 out: "test.io/test:123", 44 }, { 45 in: "gcr.io/k8s-authenticated-test/test:123", 46 out: "test.io/k8s-authenticated-test/test:123", 47 }, { 48 in: "registry.k8s.io/sig-storage/test:latest", 49 out: "test.io/sig-storage/test:latest", 50 }, { 51 in: "invalid.registry.k8s.io/invalid/test:latest", 52 out: "test.io/invalid/test:latest", 53 }, { 54 in: "registry.k8s.io/e2e-test-images/test:latest", 55 out: "test.io/promoter/test:latest", 56 }, { 57 in: "registry.k8s.io/build-image/test:latest", 58 out: "test.io/build/test:latest", 59 }, { 60 in: "gcr.io/authenticated-image-pulling/test:latest", 61 out: "test.io/gcAuth/test:latest", 62 }, 63 } 64 reg := RegistryList{ 65 DockerLibraryRegistry: "test.io/library", 66 GcRegistry: "test.io", 67 PrivateRegistry: "test.io/k8s-authenticated-test", 68 SigStorageRegistry: "test.io/sig-storage", 69 InvalidRegistry: "test.io/invalid", 70 PromoterE2eRegistry: "test.io/promoter", 71 BuildImageRegistry: "test.io/build", 72 GcAuthenticatedRegistry: "test.io/gcAuth", 73 } 74 for i := 0; i < b.N; i++ { 75 tt := registryTests[i%len(registryTests)] 76 s, _ := replaceRegistryInImageURLWithList(tt.in, reg) 77 if s != tt.out { 78 b.Errorf("got %q, want %q", s, tt.out) 79 } 80 } 81 } 82 83 func TestReplaceRegistryInImageURL(t *testing.T) { 84 registryTests := []struct { 85 in string 86 out string 87 expectErr error 88 }{ 89 { 90 in: "docker.io/library/test:123", 91 out: "test.io/library/test:123", 92 }, { 93 in: "docker.io/library/test", 94 out: "test.io/library/test", 95 }, { 96 in: "test", 97 out: "test.io/library/test", 98 }, { 99 in: "registry.k8s.io/test:123", 100 out: "test.io/test:123", 101 }, { 102 in: "gcr.io/k8s-authenticated-test/test:123", 103 out: "test.io/k8s-authenticated-test/test:123", 104 }, { 105 in: "registry.k8s.io/sig-storage/test:latest", 106 out: "test.io/sig-storage/test:latest", 107 }, { 108 in: "invalid.registry.k8s.io/invalid/test:latest", 109 out: "test.io/invalid/test:latest", 110 }, { 111 in: "registry.k8s.io/e2e-test-images/test:latest", 112 out: "test.io/promoter/test:latest", 113 }, { 114 in: "registry.k8s.io/build-image/test:latest", 115 out: "test.io/build/test:latest", 116 }, { 117 in: "gcr.io/authenticated-image-pulling/test:latest", 118 out: "test.io/gcAuth/test:latest", 119 }, { 120 in: "unknwon.io/google-samples/test:latest", 121 expectErr: fmt.Errorf("Registry: unknwon.io/google-samples is missing in test/utils/image/manifest.go, please add the registry, otherwise the test will fail on air-gapped clusters"), 122 }, 123 } 124 125 // Set custom registries 126 reg := RegistryList{ 127 DockerLibraryRegistry: "test.io/library", 128 GcRegistry: "test.io", 129 PrivateRegistry: "test.io/k8s-authenticated-test", 130 SigStorageRegistry: "test.io/sig-storage", 131 InvalidRegistry: "test.io/invalid", 132 PromoterE2eRegistry: "test.io/promoter", 133 BuildImageRegistry: "test.io/build", 134 GcAuthenticatedRegistry: "test.io/gcAuth", 135 } 136 137 for _, tt := range registryTests { 138 t.Run(tt.in, func(t *testing.T) { 139 s, err := replaceRegistryInImageURLWithList(tt.in, reg) 140 141 if err != nil && err.Error() != tt.expectErr.Error() { 142 t.Errorf("got %q, want %q", err, tt.expectErr) 143 } 144 if s != tt.out { 145 t.Errorf("got %q, want %q", s, tt.out) 146 } 147 }) 148 } 149 } 150 151 func TestGetOriginalImageConfigs(t *testing.T) { 152 if len(GetOriginalImageConfigs()) == 0 { 153 t.Fatalf("original map should not be empty") 154 } 155 } 156 157 func TestGetMappedImageConfigs(t *testing.T) { 158 originals := map[ImageID]Config{ 159 10: {registry: "docker.io", name: "source/repo", version: "1.0"}, 160 } 161 mapping := GetMappedImageConfigs(originals, "quay.io/repo/for-test") 162 163 actual := make(map[string]string) 164 for i, mapping := range mapping { 165 source := originals[i] 166 actual[source.GetE2EImage()] = mapping.GetE2EImage() 167 } 168 expected := map[string]string{ 169 "docker.io/source/repo:1.0": "quay.io/repo/for-test:e2e-10-docker-io-source-repo-1-0-72R4aXm7YnxQ4_ek", 170 } 171 if !reflect.DeepEqual(expected, actual) { 172 t.Fatal(cmp.Diff(expected, actual)) 173 } 174 }