Skip to content
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

🌱 Randomly generated namespace in controllers pkg tests #4959

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions controllers/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,40 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha4"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

const (
clusterReconcileNamespace = "test-cluster-reconcile"
)

func TestClusterReconciler(t *testing.T) {
ns, err := env.CreateNamespace(ctx, clusterReconcileNamespace)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := env.Delete(ctx, ns); err != nil {
t.Fatal(err)
}
}()

t.Run("Should create a Cluster", func(t *testing.T) {
g := NewWithT(t)

instance := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test1-",
Namespace: "default",
Namespace: ns.Name,
},
Spec: clusterv1.ClusterSpec{},
}
Expand Down Expand Up @@ -70,7 +85,7 @@ func TestClusterReconciler(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test2-",
Namespace: "default",
Namespace: ns.Name,
},
}
g.Expect(env.Create(ctx, cluster)).To(Succeed())
Expand Down Expand Up @@ -116,7 +131,7 @@ func TestClusterReconciler(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test3-",
Namespace: "default",
Namespace: ns.Name,
},
}
g.Expect(env.Create(ctx, cluster)).To(Succeed())
Expand Down Expand Up @@ -160,7 +175,7 @@ func TestClusterReconciler(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test4-",
Namespace: "default",
Namespace: ns.Name,
},
}

Expand Down Expand Up @@ -208,7 +223,7 @@ func TestClusterReconciler(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test5-",
Namespace: "default",
Namespace: ns.Name,
},
}
g.Expect(env.Create(ctx, cluster)).To(Succeed())
Expand Down Expand Up @@ -253,7 +268,7 @@ func TestClusterReconciler(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test6-",
Namespace: corev1.NamespaceDefault,
Namespace: ns.Name,
},
}

Expand Down Expand Up @@ -289,7 +304,7 @@ func TestClusterReconciler(t *testing.T) {
machine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test6-",
Namespace: corev1.NamespaceDefault,
Namespace: ns.Name,
Labels: map[string]string{
clusterv1.MachineControlPlaneLabelName: "",
},
Expand Down
41 changes: 19 additions & 22 deletions controllers/external/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ var (
)

const (
testNamespace = "test"
testClusterName = "test-cluster"
)

Expand All @@ -53,36 +52,34 @@ func TestGetResourceFound(t *testing.T) {
testResource.SetKind(testResourceKind)
testResource.SetAPIVersion(testResourceAPIVersion)
testResource.SetName(testResourceName)
testResource.SetNamespace(testNamespace)
testResource.SetNamespace(metav1.NamespaceDefault)
fabriziopandini marked this conversation as resolved.
Show resolved Hide resolved
testResource.SetResourceVersion(testResourceVersion)

testResourceReference := &corev1.ObjectReference{
Kind: testResourceKind,
APIVersion: testResourceAPIVersion,
Name: testResourceName,
Namespace: testNamespace,
Namespace: metav1.NamespaceDefault,
}

fakeClient := fake.NewClientBuilder().WithObjects(testResource.DeepCopy()).Build()
got, err := Get(ctx, fakeClient, testResourceReference, testNamespace)
got, err := Get(ctx, fakeClient, testResourceReference, metav1.NamespaceDefault)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(got).To(Equal(testResource))
}

func TestGetResourceNotFound(t *testing.T) {
g := NewWithT(t)

namespace := "test"

testResourceReference := &corev1.ObjectReference{
Kind: "BlueTemplate",
APIVersion: "blue.io/v1",
Name: "blueTemplate",
Namespace: namespace,
Namespace: metav1.NamespaceDefault,
}

fakeClient := fake.NewClientBuilder().Build()
_, err := Get(ctx, fakeClient, testResourceReference, namespace)
_, err := Get(ctx, fakeClient, testResourceReference, metav1.NamespaceDefault)
g.Expect(err).To(HaveOccurred())
g.Expect(apierrors.IsNotFound(errors.Cause(err))).To(BeTrue())
}
Expand All @@ -96,14 +93,14 @@ func TestCloneTemplateResourceNotFound(t *testing.T) {
Kind: "OrangeTemplate",
APIVersion: "orange.io/v1",
Name: "orangeTemplate",
Namespace: testNamespace,
Namespace: metav1.NamespaceDefault,
}

fakeClient := fake.NewClientBuilder().Build()
_, err := CloneTemplate(ctx, &CloneTemplateInput{
Client: fakeClient,
TemplateRef: testResourceReference,
Namespace: testNamespace,
Namespace: metav1.NamespaceDefault,
ClusterName: testClusterName,
})
g.Expect(err).To(HaveOccurred())
Expand All @@ -123,7 +120,7 @@ func TestCloneTemplateResourceFound(t *testing.T) {
"apiVersion": templateAPIVersion,
"metadata": map[string]interface{}{
"name": templateName,
"namespace": testNamespace,
"namespace": metav1.NamespaceDefault,
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
Expand All @@ -149,13 +146,13 @@ func TestCloneTemplateResourceFound(t *testing.T) {
Kind: templateKind,
APIVersion: templateAPIVersion,
Name: templateName,
Namespace: testNamespace,
Namespace: metav1.NamespaceDefault,
}

owner := metav1.OwnerReference{
Kind: "Cluster",
APIVersion: clusterv1.GroupVersion.String(),
Name: "test-cluster",
Name: testClusterName,
}

expectedKind := "Purple"
Expand All @@ -175,7 +172,7 @@ func TestCloneTemplateResourceFound(t *testing.T) {
ref, err := CloneTemplate(ctx, &CloneTemplateInput{
Client: fakeClient,
TemplateRef: templateRef.DeepCopy(),
Namespace: testNamespace,
Namespace: metav1.NamespaceDefault,
ClusterName: testClusterName,
OwnerRef: owner.DeepCopy(),
Labels: map[string]string{
Expand All @@ -191,7 +188,7 @@ func TestCloneTemplateResourceFound(t *testing.T) {
g.Expect(ref).NotTo(BeNil())
g.Expect(ref.Kind).To(Equal(expectedKind))
g.Expect(ref.APIVersion).To(Equal(expectedAPIVersion))
g.Expect(ref.Namespace).To(Equal(testNamespace))
g.Expect(ref.Namespace).To(Equal(metav1.NamespaceDefault))
g.Expect(ref.Name).To(HavePrefix(templateRef.Name))

clone := &unstructured.Unstructured{}
Expand Down Expand Up @@ -234,7 +231,7 @@ func TestCloneTemplateResourceFoundNoOwner(t *testing.T) {
"apiVersion": templateAPIVersion,
"metadata": map[string]interface{}{
"name": templateName,
"namespace": testNamespace,
"namespace": metav1.NamespaceDefault,
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
Expand All @@ -250,7 +247,7 @@ func TestCloneTemplateResourceFoundNoOwner(t *testing.T) {
Kind: templateKind,
APIVersion: templateAPIVersion,
Name: templateName,
Namespace: testNamespace,
Namespace: metav1.NamespaceDefault,
}

expectedKind := "Yellow"
Expand All @@ -267,14 +264,14 @@ func TestCloneTemplateResourceFoundNoOwner(t *testing.T) {
ref, err := CloneTemplate(ctx, &CloneTemplateInput{
Client: fakeClient,
TemplateRef: templateRef,
Namespace: testNamespace,
Namespace: metav1.NamespaceDefault,
ClusterName: testClusterName,
})
g.Expect(err).NotTo(HaveOccurred())
g.Expect(ref).NotTo(BeNil())
g.Expect(ref.Kind).To(Equal(expectedKind))
g.Expect(ref.APIVersion).To(Equal(expectedAPIVersion))
g.Expect(ref.Namespace).To(Equal(testNamespace))
g.Expect(ref.Namespace).To(Equal(metav1.NamespaceDefault))
g.Expect(ref.Name).To(HavePrefix(templateRef.Name))

clone := &unstructured.Unstructured{}
Expand Down Expand Up @@ -303,7 +300,7 @@ func TestCloneTemplateMissingSpecTemplate(t *testing.T) {
"apiVersion": templateAPIVersion,
"metadata": map[string]interface{}{
"name": templateName,
"namespace": testNamespace,
"namespace": metav1.NamespaceDefault,
},
"spec": map[string]interface{}{},
},
Expand All @@ -313,15 +310,15 @@ func TestCloneTemplateMissingSpecTemplate(t *testing.T) {
Kind: templateKind,
APIVersion: templateAPIVersion,
Name: templateName,
Namespace: testNamespace,
Namespace: metav1.NamespaceDefault,
}

fakeClient := fake.NewClientBuilder().WithObjects(template.DeepCopy()).Build()

_, err := CloneTemplate(ctx, &CloneTemplateInput{
Client: fakeClient,
TemplateRef: templateRef,
Namespace: testNamespace,
Namespace: metav1.NamespaceDefault,
ClusterName: testClusterName,
})
g.Expect(err).To(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion controllers/machine_controller_node_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestReconcileInterruptibleNodeLabel(t *testing.T) {

defer func(do ...client.Object) {
g.Expect(env.Cleanup(ctx, do...)).To(Succeed())
}(cluster, node, infraMachine, machine)
}(cluster, ns, node, infraMachine, machine)

r := &MachineReconciler{
Client: env.Client,
Expand Down
Loading