-
Notifications
You must be signed in to change notification settings - Fork 6
test: fix race condition between registry credentials and mirror tests #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
supershal
wants to merge
1
commit into
shalin/containerd-mirror
from
shalin/containerd-mirror-unit-tets
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -21,8 +21,8 @@ import ( | |
|
||
const ( | ||
ClusterName = "test-cluster" | ||
KubeadmConfigTemplateRequestObjectName = "test-kubeadmconfigtemplate" | ||
KubeadmControlPlaneTemplateRequestObjectName = "test-kubeadmcontrolplanetemplate" | ||
kubeadmConfigTemplateRequestObjectName = "test-kubeadmconfigtemplate" | ||
kubeadmControlPlaneTemplateRequestObjectName = "test-kubeadmcontrolplanetemplate" | ||
Namespace = corev1.NamespaceDefault | ||
) | ||
|
||
|
@@ -45,15 +45,24 @@ func NewRequestItem( | |
} | ||
} | ||
|
||
func NewKubeadmConfigTemplateRequestItem(uid types.UID) runtimehooksv1.GeneratePatchesRequestItem { | ||
func NewKubeadmConfigTemplateRequestItem( | ||
uid types.UID, | ||
) runtimehooksv1.GeneratePatchesRequestItem { | ||
return NewKubeadmConfigTemplateRequest(uid, kubeadmConfigTemplateRequestObjectName) | ||
} | ||
|
||
func NewKubeadmConfigTemplateRequest( | ||
uid types.UID, | ||
name string, | ||
) runtimehooksv1.GeneratePatchesRequestItem { | ||
return NewRequestItem( | ||
&bootstrapv1.KubeadmConfigTemplate{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: bootstrapv1.GroupVersion.String(), | ||
Kind: "KubeadmConfigTemplate", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: KubeadmConfigTemplateRequestObjectName, | ||
Name: name, | ||
Namespace: Namespace, | ||
}, | ||
Spec: bootstrapv1.KubeadmConfigTemplateSpec{ | ||
|
@@ -75,8 +84,9 @@ func NewKubeadmConfigTemplateRequestItem(uid types.UID) runtimehooksv1.GenerateP | |
) | ||
} | ||
|
||
func NewKubeadmControlPlaneTemplateRequestItem( | ||
func NewKubeadmControlPlaneTemplateRequest( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here |
||
uid types.UID, | ||
name string, | ||
) runtimehooksv1.GeneratePatchesRequestItem { | ||
return NewRequestItem( | ||
&controlplanev1.KubeadmControlPlaneTemplate{ | ||
|
@@ -85,7 +95,7 @@ func NewKubeadmControlPlaneTemplateRequestItem( | |
Kind: "KubeadmControlPlaneTemplate", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: KubeadmControlPlaneTemplateRequestObjectName, | ||
Name: name, | ||
Namespace: Namespace, | ||
}, | ||
Spec: controlplanev1.KubeadmControlPlaneTemplateSpec{ | ||
|
@@ -113,6 +123,12 @@ func NewKubeadmControlPlaneTemplateRequestItem( | |
) | ||
} | ||
|
||
func NewKubeadmControlPlaneTemplateRequestItem( | ||
uid types.UID, | ||
) runtimehooksv1.GeneratePatchesRequestItem { | ||
return NewKubeadmControlPlaneTemplateRequest(uid, kubeadmControlPlaneTemplateRequestObjectName) | ||
} | ||
|
||
func NewAWSClusterTemplateRequestItem( | ||
uid types.UID, | ||
existingSpec ...capav1.AWSClusterTemplateSpec, | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about something like this? I don't think dropping the "Item" is clear what the difference is