kubernetes/test/utils/image/manifest_test.go
Clayton Coleman 386f94ff03
test/utils/image: Support a single repository
In downstream contexts, it's extremely useful to be able to combine
all the "testable" images in Kubernetes into a single repo so that
a user could mirror these offline in one chunk, and audit the set of
images for changes. For instance, within OpenShift we would like to
have a single place we can place all the images used by all the tests
with a single authentication scheme. While some images are not "real"
and can't be mirrored (for instance, the images that point to an
auth protected registry), that is not the majority.

This code makes it possible to specify an environment variable
KUBE_TEST_REPO that maps the static strings of the registry to a
single repository by placing the uniqueness in a tag. For instance:

KUBE_TEST_REPO=quay.io/openshift/community-e2e-images

would translate `k8s.gcr.io/prometheus-to-sd:v0.5.0` to `quay.io/openshift/community-e2e-images:e2e-30-k8s-gcr-io-prometheus-to-sd-v0-5-0-6JI59Yih4oaj3oQOjRfhyQ`.

The tag is a safe form of the name, plus the index (the constant within
manifest.go), plus a hash of the full input. The length of the tag is
constrained to the minimum of hash + index + the safe name.

The public method is changed to return two maps - index to original
name and index to test repo name. These maps would be the same if
the env var is not set.
2021-01-12 15:22:30 -05:00

166 lines
3.7 KiB
Go

/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package image
import (
"fmt"
"reflect"
"testing"
"k8s.io/apimachinery/pkg/util/diff"
)
type result struct {
result string
err error
}
var registryTests = []struct {
in string
out result
}{
{
"docker.io/library/test:123",
result{
result: "test.io/library/test:123",
err: nil,
},
},
{
"docker.io/library/test",
result{
result: "test.io/library/test",
err: nil,
},
},
{
"test",
result{
result: "test.io/library/test",
err: nil,
},
},
{
"gcr.io/kubernetes-e2e-test-images/test:123",
result{
result: "test.io/kubernetes-e2e-test-images/test:123",
err: nil,
},
},
{
"gcr.io/kubernetes-e2e-test-images/volume/test:123",
result{
result: "test.io/kubernetes-e2e-test-images/volume/test:123",
err: nil,
},
},
{
"k8s.gcr.io/test:123",
result{
result: "test.io/test:123",
err: nil,
},
},
{
"gcr.io/k8s-authenticated-test/test:123",
result{
result: "test.io/k8s-authenticated-test/test:123",
err: nil,
},
},
{
"gcr.io/gke-release/test:latest",
result{
result: "test.io/gke-release/test:latest",
err: nil,
},
},
{
"gcr.io/google-samples/test:latest",
result{
result: "test.io/google-samples/test:latest",
err: nil,
},
},
{
"k8s.gcr.io/sig-storage/test:latest",
result{
result: "test.io/sig-storage/test:latest",
err: nil,
},
},
{
"unknwon.io/google-samples/test:latest",
result{
result: "",
err: 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"),
},
},
}
// ToDo Add Benchmark
func TestReplaceRegistryInImageURL(t *testing.T) {
// Set custom registries
dockerLibraryRegistry = "test.io/library"
e2eRegistry = "test.io/kubernetes-e2e-test-images"
e2eVolumeRegistry = "test.io/kubernetes-e2e-test-images/volume"
gcRegistry = "test.io"
gcrReleaseRegistry = "test.io/gke-release"
PrivateRegistry = "test.io/k8s-authenticated-test"
sampleRegistry = "test.io/google-samples"
sigStorageRegistry = "test.io/sig-storage"
for _, tt := range registryTests {
t.Run(tt.in, func(t *testing.T) {
s, err := ReplaceRegistryInImageURL(tt.in)
if err != nil && err.Error() != tt.out.err.Error() {
t.Errorf("got %q, want %q", err, tt.out.err)
}
if s != tt.out.result {
t.Errorf("got %q, want %q", s, tt.out.result)
}
})
}
}
func TestGetOriginalImageConfigs(t *testing.T) {
if len(GetOriginalImageConfigs()) == 0 {
t.Fatalf("original map should not be empty")
}
}
func TestGetMappedImageConfigs(t *testing.T) {
originals := map[int]Config{
0: {registry: "docker.io", name: "source/repo", version: "1.0"},
}
mapping := GetMappedImageConfigs(originals, "quay.io/repo/for-test")
actual := make(map[string]string)
for i, mapping := range mapping {
source := originals[i]
actual[source.GetE2EImage()] = mapping.GetE2EImage()
}
expected := map[string]string{
"docker.io/source/repo:1.0": "quay.io/repo/for-test:e2e-0-docker-io-source-repo-1-0-72R4aXm7YnxQ4_ekf1DrFA",
}
if !reflect.DeepEqual(expected, actual) {
t.Fatal(diff.ObjectReflectDiff(expected, actual))
}
}