github.com/openshift/installer@v1.4.17/pkg/asset/agent/mirror/registriesconf_test.go (about) 1 package mirror 2 3 import ( 4 "context" 5 "errors" 6 "os" 7 "testing" 8 9 "github.com/golang/mock/gomock" 10 "github.com/stretchr/testify/assert" 11 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 13 "github.com/openshift/installer/pkg/asset" 14 "github.com/openshift/installer/pkg/asset/agent" 15 "github.com/openshift/installer/pkg/asset/agent/joiner" 16 "github.com/openshift/installer/pkg/asset/agent/workflow" 17 "github.com/openshift/installer/pkg/asset/installconfig" 18 "github.com/openshift/installer/pkg/asset/mock" 19 "github.com/openshift/installer/pkg/asset/releaseimage" 20 "github.com/openshift/installer/pkg/types" 21 ) 22 23 func TestRegistriesConf_Generate(t *testing.T) { 24 25 cases := []struct { 26 name string 27 dependencies []asset.Asset 28 expectedConfig string 29 }{ 30 { 31 name: "missing-config", 32 dependencies: []asset.Asset{ 33 &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall}, 34 &joiner.ClusterInfo{}, 35 &agent.OptionalInstallConfig{}, 36 &releaseimage.Image{}, 37 }, 38 expectedConfig: defaultRegistriesConf, 39 }, 40 { 41 name: "default", 42 dependencies: []asset.Asset{ 43 &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall}, 44 &joiner.ClusterInfo{}, 45 &agent.OptionalInstallConfig{ 46 Supplied: true, 47 AssetBase: installconfig.AssetBase{ 48 Config: &types.InstallConfig{ 49 ObjectMeta: v1.ObjectMeta{ 50 Namespace: "cluster-0", 51 }, 52 }, 53 }, 54 }, 55 &releaseimage.Image{ 56 PullSpec: "registry.ci.openshift.org/ocp/release:4.11.0-0.ci-2022-05-16-202609", 57 }, 58 }, 59 expectedConfig: defaultRegistriesConf, 60 }, 61 { 62 name: "invalid-config-image-content-source-does-not-match-releaseImage", 63 dependencies: []asset.Asset{ 64 &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall}, 65 &joiner.ClusterInfo{}, 66 &agent.OptionalInstallConfig{ 67 Supplied: true, 68 AssetBase: installconfig.AssetBase{ 69 Config: &types.InstallConfig{ 70 ObjectMeta: v1.ObjectMeta{ 71 Namespace: "cluster-0", 72 }, 73 DeprecatedImageContentSources: []types.ImageContentSource{ 74 { 75 Source: "registry.ci.openshift.org/origin/release", 76 Mirrors: []string{ 77 "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image", 78 }, 79 }, 80 { 81 Source: "quay.io/openshift-release-dev/ocp-v4.0-art-dev", 82 Mirrors: []string{ 83 "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image", 84 }, 85 }, 86 }, 87 }, 88 }, 89 }, 90 &releaseimage.Image{ 91 PullSpec: "registry.ci.openshift.org/ocp/release:4.11.0-0.ci-2022-05-16-202609", 92 }, 93 }, 94 expectedConfig: "credential-helpers = []\nshort-name-mode = \"\"\nunqualified-search-registries = []\n\n[[registry]]\n location = \"registry.ci.openshift.org/origin/release\"\n mirror-by-digest-only = true\n prefix = \"\"\n\n [[registry.mirror]]\n location = \"virthost.ostest.test.metalkube.org:5000/localimages/local-release-image\"\n\n[[registry]]\n location = \"quay.io/openshift-release-dev/ocp-v4.0-art-dev\"\n mirror-by-digest-only = true\n prefix = \"\"\n\n [[registry.mirror]]\n location = \"virthost.ostest.test.metalkube.org:5000/localimages/local-release-image\"\n", 95 }, 96 { 97 name: "valid-image-content-sources", 98 dependencies: []asset.Asset{ 99 &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall}, 100 &joiner.ClusterInfo{}, 101 &agent.OptionalInstallConfig{ 102 Supplied: true, 103 AssetBase: installconfig.AssetBase{ 104 Config: &types.InstallConfig{ 105 ObjectMeta: v1.ObjectMeta{ 106 Namespace: "cluster-0", 107 }, 108 DeprecatedImageContentSources: []types.ImageContentSource{ 109 { 110 Source: "registry.ci.openshift.org/ocp/release", 111 Mirrors: []string{ 112 "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image", 113 }, 114 }, 115 { 116 Source: "quay.io/openshift-release-dev/ocp-v4.0-art-dev", 117 Mirrors: []string{ 118 "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image", 119 }, 120 }, 121 }, 122 }, 123 }, 124 }, 125 &releaseimage.Image{ 126 PullSpec: "registry.ci.openshift.org/ocp/release:4.11.0-0.ci-2022-05-16-202609", 127 }, 128 }, 129 expectedConfig: `credential-helpers = [] 130 short-name-mode = "" 131 unqualified-search-registries = [] 132 133 [[registry]] 134 location = "registry.ci.openshift.org/ocp/release" 135 mirror-by-digest-only = true 136 prefix = "" 137 138 [[registry.mirror]] 139 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image" 140 141 [[registry]] 142 location = "quay.io/openshift-release-dev/ocp-v4.0-art-dev" 143 mirror-by-digest-only = true 144 prefix = "" 145 146 [[registry.mirror]] 147 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image" 148 `, 149 }, 150 { 151 name: "image-digest-sources", 152 dependencies: []asset.Asset{ 153 &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall}, 154 &joiner.ClusterInfo{}, 155 &agent.OptionalInstallConfig{ 156 Supplied: true, 157 AssetBase: installconfig.AssetBase{ 158 Config: &types.InstallConfig{ 159 ObjectMeta: v1.ObjectMeta{ 160 Namespace: "cluster-0", 161 }, 162 ImageDigestSources: []types.ImageDigestSource{ 163 { 164 Source: "registry.ci.openshift.org/ocp/release", 165 Mirrors: []string{ 166 "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image", 167 }, 168 }, 169 { 170 Source: "quay.io/openshift-release-dev/ocp-v4.0-art-dev", 171 Mirrors: []string{ 172 "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image", 173 }, 174 }, 175 }, 176 }, 177 }, 178 }, 179 &releaseimage.Image{ 180 PullSpec: "registry.ci.openshift.org/ocp/release:4.11.0-0.ci-2022-05-16-202609", 181 }, 182 }, 183 expectedConfig: `credential-helpers = [] 184 short-name-mode = "" 185 unqualified-search-registries = [] 186 187 [[registry]] 188 location = "registry.ci.openshift.org/ocp/release" 189 mirror-by-digest-only = true 190 prefix = "" 191 192 [[registry.mirror]] 193 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image" 194 195 [[registry]] 196 location = "quay.io/openshift-release-dev/ocp-v4.0-art-dev" 197 mirror-by-digest-only = true 198 prefix = "" 199 200 [[registry.mirror]] 201 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image" 202 `, 203 }, 204 { 205 name: "add-nodes command - missing-config", 206 dependencies: []asset.Asset{ 207 &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeAddNodes}, 208 &joiner.ClusterInfo{}, 209 &agent.OptionalInstallConfig{}, 210 &releaseimage.Image{}, 211 }, 212 expectedConfig: defaultRegistriesConf, 213 }, 214 { 215 name: "add-nodes command - valid image sources", 216 dependencies: []asset.Asset{ 217 &workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeAddNodes}, 218 &joiner.ClusterInfo{ 219 ReleaseImage: "registry.ci.openshift.org/ocp/release:4.11.0-0.ci-2022-05-16-202609", 220 ImageDigestSources: []types.ImageDigestSource{ 221 { 222 Source: "registry.ci.openshift.org/ocp/release", 223 Mirrors: []string{ 224 "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image", 225 }, 226 }, 227 { 228 Source: "quay.io/openshift-release-dev/ocp-v4.0-art-dev", 229 Mirrors: []string{ 230 "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image", 231 }, 232 }, 233 }, 234 }, 235 &agent.OptionalInstallConfig{}, 236 &releaseimage.Image{}, 237 }, 238 expectedConfig: `credential-helpers = [] 239 short-name-mode = "" 240 unqualified-search-registries = [] 241 242 [[registry]] 243 location = "registry.ci.openshift.org/ocp/release" 244 mirror-by-digest-only = true 245 prefix = "" 246 247 [[registry.mirror]] 248 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image" 249 250 [[registry]] 251 location = "quay.io/openshift-release-dev/ocp-v4.0-art-dev" 252 mirror-by-digest-only = true 253 prefix = "" 254 255 [[registry.mirror]] 256 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image" 257 `, 258 }, 259 } 260 for _, tc := range cases { 261 t.Run(tc.name, func(t *testing.T) { 262 263 parents := asset.Parents{} 264 parents.Add(tc.dependencies...) 265 266 asset := &RegistriesConf{} 267 err := asset.Generate(context.Background(), parents) 268 269 assert.NoError(t, err) 270 271 files := asset.Files() 272 assert.Len(t, files, 1) 273 assert.Equal(t, tc.expectedConfig, string(files[0].Data)) 274 }) 275 } 276 } 277 278 func TestRegistries_LoadedFromDisk(t *testing.T) { 279 280 cases := []struct { 281 name string 282 data string 283 fetchError error 284 expectedFound bool 285 expectedError string 286 }{ 287 { 288 name: "location-key-not-found-in-config-file", 289 data: ` 290 [[registry]] 291 wrongLocationkey = "registry.ci.openshift.org/origin/release" 292 mirror-by-digest-only = false 293 294 [[registry.mirror]] 295 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image" 296 297 [[registry]] 298 locations = "quay.io/openshift-release-dev/ocp-v4.0-art-dev" 299 mirror-by-digest-only = false 300 301 [[registry.mirror]] 302 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image"`, 303 expectedFound: true, 304 }, 305 { 306 name: "valid-config-file", 307 data: ` 308 [[registry]] 309 location = "registry.ci.openshift.org/origin/release" 310 mirror-by-digest-only = false 311 312 [[registry.mirror]] 313 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image" 314 315 [[registry]] 316 location = "quay.io/openshift-release-dev/ocp-v4.0-art-dev" 317 mirror-by-digest-only = false 318 319 [[registry.mirror]] 320 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image"`, 321 expectedFound: true, 322 expectedError: "", 323 }, 324 { 325 name: "location-does-not-match-with-releaseImage", 326 data: ` 327 [[registry]] 328 location = "registry.ci.openshift.org/ocp/release" 329 mirror-by-digest-only = false 330 331 [[registry.mirror]] 332 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image" 333 334 [[registry]] 335 location = "quay.io/openshift-release-dev/ocp-v4.0-art-dev" 336 mirror-by-digest-only = false 337 338 [[registry.mirror]] 339 location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image"`, 340 expectedFound: true, 341 expectedError: "", 342 }, 343 { 344 name: "file-not-found", 345 fetchError: &os.PathError{Err: os.ErrNotExist}, 346 }, 347 { 348 name: "error-fetching-file", 349 fetchError: errors.New("fetch failed"), 350 expectedError: "failed to load mirror/registries.conf file: fetch failed", 351 }, 352 } 353 for _, tc := range cases { 354 t.Run(tc.name, func(t *testing.T) { 355 356 mockCtrl := gomock.NewController(t) 357 defer mockCtrl.Finish() 358 359 fileFetcher := mock.NewMockFileFetcher(mockCtrl) 360 fileFetcher.EXPECT().FetchByName(RegistriesConfFilename). 361 Return( 362 &asset.File{ 363 Filename: RegistriesConfFilename, 364 Data: []byte(tc.data)}, 365 tc.fetchError, 366 ) 367 368 asset := &RegistriesConf{} 369 found, err := asset.Load(fileFetcher) 370 assert.Equal(t, tc.expectedFound, found, "unexpected found value returned from Load") 371 if tc.expectedError != "" { 372 assert.Equal(t, tc.expectedError, err.Error()) 373 } else { 374 assert.NoError(t, err) 375 } 376 }) 377 } 378 379 }