k8s.io/kubernetes@v1.29.3/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:  "mcr.microsoft.com/test:latest",
    55  			out: "test.io/microsoft/test:latest",
    56  		}, {
    57  			in:  "registry.k8s.io/e2e-test-images/test:latest",
    58  			out: "test.io/promoter/test:latest",
    59  		}, {
    60  			in:  "registry.k8s.io/build-image/test:latest",
    61  			out: "test.io/build/test:latest",
    62  		}, {
    63  			in:  "gcr.io/authenticated-image-pulling/test:latest",
    64  			out: "test.io/gcAuth/test:latest",
    65  		},
    66  	}
    67  	reg := RegistryList{
    68  		DockerLibraryRegistry:   "test.io/library",
    69  		GcRegistry:              "test.io",
    70  		PrivateRegistry:         "test.io/k8s-authenticated-test",
    71  		SigStorageRegistry:      "test.io/sig-storage",
    72  		InvalidRegistry:         "test.io/invalid",
    73  		MicrosoftRegistry:       "test.io/microsoft",
    74  		PromoterE2eRegistry:     "test.io/promoter",
    75  		BuildImageRegistry:      "test.io/build",
    76  		GcAuthenticatedRegistry: "test.io/gcAuth",
    77  	}
    78  	for i := 0; i < b.N; i++ {
    79  		tt := registryTests[i%len(registryTests)]
    80  		s, _ := replaceRegistryInImageURLWithList(tt.in, reg)
    81  		if s != tt.out {
    82  			b.Errorf("got %q, want %q", s, tt.out)
    83  		}
    84  	}
    85  }
    86  
    87  func TestReplaceRegistryInImageURL(t *testing.T) {
    88  	registryTests := []struct {
    89  		in        string
    90  		out       string
    91  		expectErr error
    92  	}{
    93  		{
    94  			in:  "docker.io/library/test:123",
    95  			out: "test.io/library/test:123",
    96  		}, {
    97  			in:  "docker.io/library/test",
    98  			out: "test.io/library/test",
    99  		}, {
   100  			in:  "test",
   101  			out: "test.io/library/test",
   102  		}, {
   103  			in:  "registry.k8s.io/test:123",
   104  			out: "test.io/test:123",
   105  		}, {
   106  			in:  "gcr.io/k8s-authenticated-test/test:123",
   107  			out: "test.io/k8s-authenticated-test/test:123",
   108  		}, {
   109  			in:  "registry.k8s.io/sig-storage/test:latest",
   110  			out: "test.io/sig-storage/test:latest",
   111  		}, {
   112  			in:  "invalid.registry.k8s.io/invalid/test:latest",
   113  			out: "test.io/invalid/test:latest",
   114  		}, {
   115  			in:  "mcr.microsoft.com/test:latest",
   116  			out: "test.io/microsoft/test:latest",
   117  		}, {
   118  			in:  "registry.k8s.io/e2e-test-images/test:latest",
   119  			out: "test.io/promoter/test:latest",
   120  		}, {
   121  			in:  "registry.k8s.io/build-image/test:latest",
   122  			out: "test.io/build/test:latest",
   123  		}, {
   124  			in:  "gcr.io/authenticated-image-pulling/test:latest",
   125  			out: "test.io/gcAuth/test:latest",
   126  		}, {
   127  			in:        "unknwon.io/google-samples/test:latest",
   128  			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"),
   129  		},
   130  	}
   131  
   132  	// Set custom registries
   133  	reg := RegistryList{
   134  		DockerLibraryRegistry:   "test.io/library",
   135  		GcRegistry:              "test.io",
   136  		PrivateRegistry:         "test.io/k8s-authenticated-test",
   137  		SigStorageRegistry:      "test.io/sig-storage",
   138  		InvalidRegistry:         "test.io/invalid",
   139  		MicrosoftRegistry:       "test.io/microsoft",
   140  		PromoterE2eRegistry:     "test.io/promoter",
   141  		BuildImageRegistry:      "test.io/build",
   142  		GcAuthenticatedRegistry: "test.io/gcAuth",
   143  	}
   144  
   145  	for _, tt := range registryTests {
   146  		t.Run(tt.in, func(t *testing.T) {
   147  			s, err := replaceRegistryInImageURLWithList(tt.in, reg)
   148  
   149  			if err != nil && err.Error() != tt.expectErr.Error() {
   150  				t.Errorf("got %q, want %q", err, tt.expectErr)
   151  			}
   152  			if s != tt.out {
   153  				t.Errorf("got %q, want %q", s, tt.out)
   154  			}
   155  		})
   156  	}
   157  }
   158  
   159  func TestGetOriginalImageConfigs(t *testing.T) {
   160  	if len(GetOriginalImageConfigs()) == 0 {
   161  		t.Fatalf("original map should not be empty")
   162  	}
   163  }
   164  
   165  func TestGetMappedImageConfigs(t *testing.T) {
   166  	originals := map[ImageID]Config{
   167  		10: {registry: "docker.io", name: "source/repo", version: "1.0"},
   168  	}
   169  	mapping := GetMappedImageConfigs(originals, "quay.io/repo/for-test")
   170  
   171  	actual := make(map[string]string)
   172  	for i, mapping := range mapping {
   173  		source := originals[i]
   174  		actual[source.GetE2EImage()] = mapping.GetE2EImage()
   175  	}
   176  	expected := map[string]string{
   177  		"docker.io/source/repo:1.0": "quay.io/repo/for-test:e2e-10-docker-io-source-repo-1-0-72R4aXm7YnxQ4_ek",
   178  	}
   179  	if !reflect.DeepEqual(expected, actual) {
   180  		t.Fatal(cmp.Diff(expected, actual))
   181  	}
   182  }