-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: fix race condition between registry credentials and mirror tests
- Loading branch information
Showing
5 changed files
with
314 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
262 changes: 262 additions & 0 deletions
262
pkg/handlers/generic/mutation/imageregistries/credentials/tests/generate_mirror_patches.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apiserver/pkg/storage/names" | ||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1" | ||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation" | ||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest" | ||
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest/request" | ||
) | ||
|
||
const ( | ||
validMirrorCredentialsSecretName = "my-mirror-registry-credentials" | ||
validMirrorCASecretName = "myregistry-mirror-cacert" | ||
//nolint:gosec // Does not contain hard coded credentials. | ||
cpRegistryAsMirrorCreds = "kubeadmControlPlaneRegistryAsMirrorCreds" | ||
//nolint:gosec // Does not contain hard coded credentials. | ||
workerRegistryAsMirrorCreds = "kubeadmConfigTemplateRegistryAsMirrorCreds" | ||
registryStaticCredentialsSecretSuffix = "registry-config" | ||
) | ||
|
||
func TestGenerateMirrorPatches( | ||
t *testing.T, | ||
generatorFunc func() mutation.GeneratePatches, | ||
fakeClient client.Client, | ||
variableName string, | ||
variablePath ...string, | ||
) { | ||
t.Helper() | ||
|
||
require.NoError( | ||
t, | ||
fakeClient.Create( | ||
context.Background(), | ||
newRegistryCredentialsSecret(validMirrorCredentialsSecretName, request.Namespace), | ||
), | ||
) | ||
|
||
require.NoError( | ||
t, | ||
fakeClient.Create( | ||
context.Background(), | ||
newMirrorSecret(validMirrorCASecretName, request.Namespace), | ||
), | ||
) | ||
|
||
// Server side apply does not work with the fake client, hack around it by pre-creating empty Secrets | ||
// https://github.com/kubernetes-sigs/controller-runtime/issues/2341 | ||
require.NoError( | ||
t, | ||
fakeClient.Create( | ||
context.Background(), | ||
newEmptySecret( | ||
fmt.Sprintf( | ||
"%s-%s", | ||
cpRegistryAsMirrorCreds, | ||
registryStaticCredentialsSecretSuffix, | ||
), | ||
request.Namespace, | ||
), | ||
), | ||
) | ||
|
||
require.NoError( | ||
t, | ||
fakeClient.Create( | ||
context.Background(), | ||
newEmptySecret( | ||
fmt.Sprintf( | ||
"%s-%s", | ||
workerRegistryAsMirrorCreds, | ||
registryStaticCredentialsSecretSuffix, | ||
), | ||
request.Namespace, | ||
), | ||
), | ||
) | ||
|
||
capitest.ValidateGeneratePatches( | ||
t, | ||
generatorFunc, | ||
capitest.PatchTestDef{ | ||
Name: "files added in KubeadmControlPlaneTemplate for registry with mirror without CA Certificate", | ||
Vars: []runtimehooksv1.Variable{ | ||
capitest.VariableWithValue( | ||
variableName, | ||
v1alpha1.ImageRegistries{ | ||
v1alpha1.ImageRegistry{ | ||
URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com", | ||
Mirror: &v1alpha1.RegistryMirror{}, | ||
}, | ||
}, | ||
variablePath..., | ||
), | ||
}, | ||
RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem(""), | ||
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/kubeadmConfigSpec/files", | ||
ValueMatcher: gomega.ContainElements( | ||
gomega.HaveKeyWithValue( | ||
"path", "/etc/containerd/certs.d/_default/hosts.toml", | ||
), | ||
), | ||
}, | ||
}, | ||
}, | ||
capitest.PatchTestDef{ | ||
Name: "files added in KubeadmControlPlaneTemplate for registry with mirror with CA Certificate", | ||
Vars: []runtimehooksv1.Variable{ | ||
capitest.VariableWithValue( | ||
variableName, | ||
v1alpha1.ImageRegistries{ | ||
v1alpha1.ImageRegistry{ | ||
URL: "https://mirror-registry.com", | ||
Credentials: &v1alpha1.ImageCredentials{ | ||
SecretRef: &corev1.ObjectReference{ | ||
Name: validSecretName, | ||
}, | ||
}, | ||
Mirror: &v1alpha1.RegistryMirror{ | ||
SecretRef: &corev1.ObjectReference{ | ||
Name: validMirrorCASecretName, | ||
}, | ||
}, | ||
}, | ||
}, | ||
variablePath..., | ||
), | ||
}, | ||
RequestItem: request.NewKubeadmControlPlaneTemplateRequest("", cpRegistryAsMirrorCreds), | ||
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/kubeadmConfigSpec/files", | ||
ValueMatcher: gomega.ContainElements( | ||
gomega.HaveKeyWithValue( | ||
"path", "/etc/containerd/certs.d/_default/hosts.toml", | ||
), | ||
gomega.HaveKeyWithValue( | ||
"path", "/etc/certs/mirror.pem", | ||
), | ||
), | ||
}, | ||
}, | ||
}, | ||
capitest.PatchTestDef{ | ||
Name: "files added in KubeadmConfigTemplate for registry mirror wihthout CA certificate", | ||
Vars: []runtimehooksv1.Variable{ | ||
capitest.VariableWithValue( | ||
variableName, | ||
v1alpha1.ImageRegistries{ | ||
v1alpha1.ImageRegistry{ | ||
URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com", | ||
Mirror: &v1alpha1.RegistryMirror{}, | ||
}, | ||
}, | ||
variablePath..., | ||
), | ||
capitest.VariableWithValue( | ||
"builtin", | ||
map[string]any{ | ||
"machineDeployment": map[string]any{ | ||
"class": names.SimpleNameGenerator.GenerateName("worker-"), | ||
}, | ||
}, | ||
), | ||
}, | ||
RequestItem: request.NewKubeadmConfigTemplateRequestItem(""), | ||
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/files", | ||
ValueMatcher: gomega.ContainElements( | ||
gomega.HaveKeyWithValue( | ||
"path", "/etc/containerd/certs.d/_default/hosts.toml", | ||
), | ||
), | ||
}, | ||
}, | ||
}, | ||
capitest.PatchTestDef{ | ||
Name: "files added in KubeadmConfigTemplate for registry mirror with secret for CA certificate", | ||
Vars: []runtimehooksv1.Variable{ | ||
capitest.VariableWithValue( | ||
variableName, | ||
v1alpha1.ImageRegistries{ | ||
v1alpha1.ImageRegistry{ | ||
URL: "https://mirror-registry.io", | ||
Credentials: &v1alpha1.ImageCredentials{ | ||
SecretRef: &corev1.ObjectReference{ | ||
Name: validSecretName, | ||
}, | ||
}, | ||
Mirror: &v1alpha1.RegistryMirror{ | ||
SecretRef: &corev1.ObjectReference{ | ||
Name: validMirrorCASecretName, | ||
}, | ||
}, | ||
}, | ||
}, | ||
variablePath..., | ||
), | ||
capitest.VariableWithValue( | ||
"builtin", | ||
map[string]any{ | ||
"machineDeployment": map[string]any{ | ||
"class": names.SimpleNameGenerator.GenerateName("worker-"), | ||
}, | ||
}, | ||
), | ||
}, | ||
RequestItem: request.NewKubeadmConfigTemplateRequest("", workerRegistryAsMirrorCreds), | ||
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/files", | ||
ValueMatcher: gomega.ContainElements( | ||
gomega.HaveKeyWithValue( | ||
"path", "/etc/containerd/certs.d/_default/hosts.toml", | ||
), | ||
gomega.HaveKeyWithValue( | ||
"path", "/etc/certs/mirror.pem", | ||
), | ||
), | ||
}, | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
func newMirrorSecret(name, namespace string) *corev1.Secret { | ||
secretData := map[string][]byte{ | ||
"ca.crt": []byte("myCACert"), | ||
} | ||
return &corev1.Secret{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Secret", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Data: secretData, | ||
Type: corev1.SecretTypeOpaque, | ||
} | ||
} |
Oops, something went wrong.