diff --git a/controllers/cluster_controller_test.go b/controllers/cluster_controller_test.go index 0b1cf8e54917..143aa9f15835 100644 --- a/controllers/cluster_controller_test.go +++ b/controllers/cluster_controller_test.go @@ -23,25 +23,33 @@ 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) { t.Run("Should create a Cluster", func(t *testing.T) { g := NewWithT(t) + ns, err := env.CreateNamespace(ctx, clusterReconcileNamespace) + g.Expect(err).ToNot(HaveOccurred()) + instance := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test1-", - Namespace: "default", + Namespace: ns.Name, }, Spec: clusterv1.ClusterSpec{}, } @@ -67,10 +75,13 @@ func TestClusterReconciler(t *testing.T) { g := NewWithT(t) // Setup + ns, err := env.CreateNamespace(ctx, clusterReconcileNamespace) + g.Expect(err).ToNot(HaveOccurred()) + cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test2-", - Namespace: "default", + Namespace: ns.Name, }, } g.Expect(env.Create(ctx, cluster)).To(Succeed()) @@ -113,10 +124,13 @@ func TestClusterReconciler(t *testing.T) { g := NewWithT(t) // Setup + ns, err := env.CreateNamespace(ctx, clusterReconcileNamespace) + g.Expect(err).ToNot(HaveOccurred()) + cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test3-", - Namespace: "default", + Namespace: ns.Name, }, } g.Expect(env.Create(ctx, cluster)).To(Succeed()) @@ -157,10 +171,13 @@ func TestClusterReconciler(t *testing.T) { g := NewWithT(t) // Setup + ns, err := env.CreateNamespace(ctx, clusterReconcileNamespace) + g.Expect(err).ToNot(HaveOccurred()) + cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test4-", - Namespace: "default", + Namespace: ns.Name, }, } @@ -205,10 +222,13 @@ func TestClusterReconciler(t *testing.T) { g := NewWithT(t) // Setup + ns, err := env.CreateNamespace(ctx, clusterReconcileNamespace) + g.Expect(err).ToNot(HaveOccurred()) + cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test5-", - Namespace: "default", + Namespace: ns.Name, }, } g.Expect(env.Create(ctx, cluster)).To(Succeed()) @@ -250,10 +270,13 @@ func TestClusterReconciler(t *testing.T) { t.Run("Should successfully set ControlPlaneInitialized on the cluster object if controlplane is ready", func(t *testing.T) { g := NewWithT(t) + ns, err := env.CreateNamespace(ctx, clusterReconcileNamespace) + g.Expect(err).ToNot(HaveOccurred()) + cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test6-", - Namespace: corev1.NamespaceDefault, + Namespace: ns.Name, }, } @@ -289,7 +312,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: "", }, diff --git a/controllers/external/util_test.go b/controllers/external/util_test.go index 1860bf762020..8e85596c0b23 100644 --- a/controllers/external/util_test.go +++ b/controllers/external/util_test.go @@ -37,7 +37,6 @@ var ( ) const ( - testNamespace = "test" testClusterName = "test-cluster" ) @@ -53,18 +52,18 @@ func TestGetResourceFound(t *testing.T) { testResource.SetKind(testResourceKind) testResource.SetAPIVersion(testResourceAPIVersion) testResource.SetName(testResourceName) - testResource.SetNamespace(testNamespace) + testResource.SetNamespace(metav1.NamespaceDefault) 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)) } @@ -72,17 +71,15 @@ func TestGetResourceFound(t *testing.T) { 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()) } @@ -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()) @@ -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{}{ @@ -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" @@ -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{ @@ -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{} @@ -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{}{ @@ -250,7 +247,7 @@ func TestCloneTemplateResourceFoundNoOwner(t *testing.T) { Kind: templateKind, APIVersion: templateAPIVersion, Name: templateName, - Namespace: testNamespace, + Namespace: metav1.NamespaceDefault, } expectedKind := "Yellow" @@ -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{} @@ -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{}{}, }, @@ -313,7 +310,7 @@ func TestCloneTemplateMissingSpecTemplate(t *testing.T) { Kind: templateKind, APIVersion: templateAPIVersion, Name: templateName, - Namespace: testNamespace, + Namespace: metav1.NamespaceDefault, } fakeClient := fake.NewClientBuilder().WithObjects(template.DeepCopy()).Build() @@ -321,7 +318,7 @@ func TestCloneTemplateMissingSpecTemplate(t *testing.T) { _, err := CloneTemplate(ctx, &CloneTemplateInput{ Client: fakeClient, TemplateRef: templateRef, - Namespace: testNamespace, + Namespace: metav1.NamespaceDefault, ClusterName: testClusterName, }) g.Expect(err).To(HaveOccurred()) diff --git a/controllers/machine_controller_phases_test.go b/controllers/machine_controller_phases_test.go index aa40221726ea..e6b8152c5165 100644 --- a/controllers/machine_controller_phases_test.go +++ b/controllers/machine_controller_phases_test.go @@ -30,15 +30,16 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" "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" + "sigs.k8s.io/controller-runtime/pkg/log" + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" "sigs.k8s.io/cluster-api/controllers/external" "sigs.k8s.io/cluster-api/controllers/remote" "sigs.k8s.io/cluster-api/util/conditions" "sigs.k8s.io/cluster-api/util/kubeconfig" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "sigs.k8s.io/controller-runtime/pkg/log" ) func init() { @@ -59,7 +60,7 @@ func TestReconcileMachinePhases(t *testing.T) { defaultMachine := clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine-test", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.MachineControlPlaneLabelName: "", }, @@ -87,7 +88,7 @@ func TestReconcileMachinePhases(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "spec": map[string]interface{}{}, "status": map[string]interface{}{}, @@ -100,7 +101,7 @@ func TestReconcileMachinePhases(t *testing.T) { "apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "infra-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "spec": map[string]interface{}{}, "status": map[string]interface{}{}, @@ -593,7 +594,7 @@ func TestReconcileBootstrap(t *testing.T) { defaultMachine := clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine-test", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -612,7 +613,7 @@ func TestReconcileBootstrap(t *testing.T) { defaultCluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, } @@ -631,7 +632,7 @@ func TestReconcileBootstrap(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "spec": map[string]interface{}{}, "status": map[string]interface{}{ @@ -654,7 +655,7 @@ func TestReconcileBootstrap(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "spec": map[string]interface{}{}, "status": map[string]interface{}{ @@ -675,7 +676,7 @@ func TestReconcileBootstrap(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "spec": map[string]interface{}{}, "status": map[string]interface{}{}, @@ -726,7 +727,7 @@ func TestReconcileBootstrap(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "spec": map[string]interface{}{}, "status": map[string]interface{}{ @@ -737,7 +738,7 @@ func TestReconcileBootstrap(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "bootstrap-test-existing", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineSpec{ Bootstrap: clusterv1.Bootstrap{ @@ -767,7 +768,7 @@ func TestReconcileBootstrap(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, "ownerReferences": []interface{}{ map[string]interface{}{ "apiVersion": clusterv1.GroupVersion.String(), @@ -786,7 +787,7 @@ func TestReconcileBootstrap(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "bootstrap-test-existing", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineSpec{ Bootstrap: clusterv1.Bootstrap{ @@ -814,7 +815,7 @@ func TestReconcileBootstrap(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, "ownerReferences": []interface{}{ map[string]interface{}{ "apiVersion": "cluster.x-k8s.io/v1alpha2", @@ -833,7 +834,7 @@ func TestReconcileBootstrap(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "bootstrap-test-existing", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineSpec{ Bootstrap: clusterv1.Bootstrap{ @@ -893,7 +894,7 @@ func TestReconcileInfrastructure(t *testing.T) { defaultMachine := clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine-test", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -917,7 +918,7 @@ func TestReconcileInfrastructure(t *testing.T) { defaultCluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, } @@ -938,7 +939,7 @@ func TestReconcileInfrastructure(t *testing.T) { "apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "infra-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, "ownerReferences": []interface{}{ map[string]interface{}{ "apiVersion": clusterv1.GroupVersion.String(), @@ -979,7 +980,7 @@ func TestReconcileInfrastructure(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine-test", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineSpec{ Bootstrap: clusterv1.Bootstrap{ @@ -1006,7 +1007,7 @@ func TestReconcileInfrastructure(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "spec": map[string]interface{}{}, "status": map[string]interface{}{ @@ -1035,7 +1036,7 @@ func TestReconcileInfrastructure(t *testing.T) { "apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "infra-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, "annotations": map[string]interface{}{ "cluster.x-k8s.io/paused": "true", }, diff --git a/controllers/machine_controller_test.go b/controllers/machine_controller_test.go index d2f9c1087130..4e14f50cc774 100644 --- a/controllers/machine_controller_test.go +++ b/controllers/machine_controller_test.go @@ -28,17 +28,18 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/client-go/kubernetes/scheme" "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" + "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" "sigs.k8s.io/cluster-api/controllers/external" "sigs.k8s.io/cluster-api/controllers/remote" "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" - "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/reconcile" ) func TestWatches(t *testing.T) { @@ -178,13 +179,17 @@ func TestWatches(t *testing.T) { func TestMachine_Reconcile(t *testing.T) { g := NewWithT(t) + + ns, err := env.CreateNamespace(ctx, "test-machine-reconcile") + g.Expect(err).ToNot(HaveOccurred()) + infraMachine := &unstructured.Unstructured{ Object: map[string]interface{}{ "kind": "InfrastructureMachine", "apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "infra-config1", - "namespace": "default", + "namespace": ns.Name, }, "spec": map[string]interface{}{ "providerID": "test://id-1", @@ -198,7 +203,7 @@ func TestMachine_Reconcile(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config-machinereconcile", - "namespace": "default", + "namespace": ns.Name, }, "spec": map[string]interface{}{}, "status": map[string]interface{}{}, @@ -208,7 +213,7 @@ func TestMachine_Reconcile(t *testing.T) { testCluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "machine-reconcile-", - Namespace: "default", + Namespace: ns.Name, }, } @@ -223,7 +228,7 @@ func TestMachine_Reconcile(t *testing.T) { machine := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "machine-created-", - Namespace: "default", + Namespace: ns.Name, Finalizers: []string{clusterv1.MachineFinalizer}, }, Spec: clusterv1.MachineSpec{ @@ -318,15 +323,15 @@ func TestMachineFinalizer(t *testing.T) { bootstrapData := "some valid data" clusterCorrectMeta := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "default", Name: "valid-cluster", + Namespace: metav1.NamespaceDefault, }, } machineValidCluster := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine1", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineSpec{ Bootstrap: clusterv1.Bootstrap{ @@ -339,7 +344,7 @@ func TestMachineFinalizer(t *testing.T) { machineWithFinalizer := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine2", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Finalizers: []string{"some-other-finalizer"}, }, Spec: clusterv1.MachineSpec{ @@ -405,13 +410,13 @@ func TestMachineOwnerReference(t *testing.T) { bootstrapData := "some valid data" testCluster := &clusterv1.Cluster{ TypeMeta: metav1.TypeMeta{Kind: "Cluster", APIVersion: clusterv1.GroupVersion.String()}, - ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "test-cluster"}, + ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "test-cluster"}, } machineInvalidCluster := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine1", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineSpec{ ClusterName: "invalid", @@ -421,7 +426,7 @@ func TestMachineOwnerReference(t *testing.T) { machineValidCluster := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine2", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineSpec{ Bootstrap: clusterv1.Bootstrap{ @@ -434,7 +439,7 @@ func TestMachineOwnerReference(t *testing.T) { machineValidMachine := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine3", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "valid-cluster", }, @@ -458,7 +463,7 @@ func TestMachineOwnerReference(t *testing.T) { machineValidControlled := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "machine4", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "valid-cluster", clusterv1.MachineControlPlaneLabelName: "", @@ -576,7 +581,7 @@ func TestReconcileRequest(t *testing.T) { "apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "infra-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "spec": map[string]interface{}{ "providerID": "test://id-1", @@ -598,14 +603,14 @@ func TestReconcileRequest(t *testing.T) { testCluster := clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, } node := &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ Name: "test", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: corev1.NodeSpec{ProviderID: "test://id-1"}, } @@ -622,7 +627,7 @@ func TestReconcileRequest(t *testing.T) { machine: clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "created", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Finalizers: []string{clusterv1.MachineFinalizer}, }, Spec: clusterv1.MachineSpec{ @@ -650,7 +655,7 @@ func TestReconcileRequest(t *testing.T) { machine: clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "updated", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Finalizers: []string{clusterv1.MachineFinalizer}, }, Spec: clusterv1.MachineSpec{ @@ -678,7 +683,7 @@ func TestReconcileRequest(t *testing.T) { machine: clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "deleted", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.MachineControlPlaneLabelName: "", }, @@ -739,7 +744,7 @@ func TestMachineConditions(t *testing.T) { "apiVersion": "infrastructure.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "infra-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "spec": map[string]interface{}{ "providerID": "test://id-1", @@ -770,7 +775,7 @@ func TestMachineConditions(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "bootstrap-config1", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, "status": status, }, @@ -780,14 +785,14 @@ func TestMachineConditions(t *testing.T) { testCluster := clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, } machine := clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "blah", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.MachineControlPlaneLabelName: "", }, @@ -977,7 +982,7 @@ func TestMachineConditions(t *testing.T) { func TestReconcileDeleteExternal(t *testing.T) { testCluster := &clusterv1.Cluster{ - ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "test-cluster"}, + ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "test-cluster"}, } bootstrapConfig := &unstructured.Unstructured{ @@ -986,7 +991,7 @@ func TestReconcileDeleteExternal(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "name": "delete-bootstrap", - "namespace": "default", + "namespace": metav1.NamespaceDefault, }, }, } @@ -994,7 +999,7 @@ func TestReconcileDeleteExternal(t *testing.T) { machine := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "delete", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineSpec{ ClusterName: "test-cluster", @@ -1023,7 +1028,7 @@ func TestReconcileDeleteExternal(t *testing.T) { "kind": "BootstrapConfig", "metadata": map[string]interface{}{ "name": "delete-bootstrap", - "namespace": "default", + "namespace": metav1.NamespaceDefault, "resourceVersion": "999", }, }, @@ -1069,13 +1074,13 @@ func TestRemoveMachineFinalizerAfterDeleteReconcile(t *testing.T) { dt := metav1.Now() testCluster := &clusterv1.Cluster{ - ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "test-cluster"}, + ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "test-cluster"}, } m := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "delete123", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Finalizers: []string{clusterv1.MachineFinalizer, "test"}, DeletionTimestamp: &dt, }, @@ -1104,7 +1109,7 @@ func TestRemoveMachineFinalizerAfterDeleteReconcile(t *testing.T) { func TestIsNodeDrainedAllowed(t *testing.T) { testCluster := &clusterv1.Cluster{ TypeMeta: metav1.TypeMeta{Kind: "Cluster", APIVersion: clusterv1.GroupVersion.String()}, - ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "test-cluster"}, + ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "test-cluster"}, } tests := []struct { @@ -1117,7 +1122,7 @@ func TestIsNodeDrainedAllowed(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "test-machine", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Finalizers: []string{clusterv1.MachineFinalizer}, Annotations: map[string]string{clusterv1.ExcludeNodeDrainingAnnotation: "existed!!"}, }, @@ -1135,7 +1140,7 @@ func TestIsNodeDrainedAllowed(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "test-machine", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Finalizers: []string{clusterv1.MachineFinalizer}, }, Spec: clusterv1.MachineSpec{ @@ -1162,7 +1167,7 @@ func TestIsNodeDrainedAllowed(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "test-machine", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Finalizers: []string{clusterv1.MachineFinalizer}, }, Spec: clusterv1.MachineSpec{ @@ -1188,7 +1193,7 @@ func TestIsNodeDrainedAllowed(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "test-machine", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Finalizers: []string{clusterv1.MachineFinalizer}, }, Spec: clusterv1.MachineSpec{ @@ -1240,13 +1245,13 @@ func TestIsDeleteNodeAllowed(t *testing.T) { cluster: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, }, machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "created", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -1266,13 +1271,13 @@ func TestIsDeleteNodeAllowed(t *testing.T) { cluster: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, }, machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "created", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -1296,13 +1301,13 @@ func TestIsDeleteNodeAllowed(t *testing.T) { cluster: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, }, machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "created", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", clusterv1.MachineControlPlaneLabelName: "", @@ -1328,13 +1333,13 @@ func TestIsDeleteNodeAllowed(t *testing.T) { cluster: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, }, machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "created", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -1358,7 +1363,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) { cluster: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, DeletionTimestamp: &deletionts, }, }, @@ -1370,7 +1375,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) { cluster: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.ClusterSpec{ ControlPlaneRef: &corev1.ObjectReference{ @@ -1384,7 +1389,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "created", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -1408,7 +1413,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) { cluster: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.ClusterSpec{ ControlPlaneRef: &corev1.ObjectReference{ @@ -1422,7 +1427,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "created", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -1446,7 +1451,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) { cluster: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.ClusterSpec{ ControlPlaneRef: &corev1.ObjectReference{ @@ -1460,7 +1465,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) { machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "created", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -1522,7 +1527,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) { m1 := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "cp1", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -1542,7 +1547,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) { m2 := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "cp2", - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, diff --git a/controllers/machinedeployment_controller_test.go b/controllers/machinedeployment_controller_test.go index 13970495dc62..0da78dea4788 100644 --- a/controllers/machinedeployment_controller_test.go +++ b/controllers/machinedeployment_controller_test.go @@ -36,32 +36,39 @@ import ( "sigs.k8s.io/cluster-api/util/conditions" ) +const ( + machineDeploymentNamespace = "md-test" +) + var _ reconcile.Reconciler = &MachineDeploymentReconciler{} func TestMachineDeploymentReconciler(t *testing.T) { - namespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "md-test"}} - testCluster := &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Namespace: namespace.Name, Name: "test-cluster"}} - - setup := func(t *testing.T, g *WithT) { + setup := func(t *testing.T, g *WithT) (*corev1.Namespace, *clusterv1.Cluster) { t.Log("Creating the namespace") - g.Expect(env.Create(ctx, namespace)).To(Succeed()) + ns, err := env.CreateNamespace(ctx, machineDeploymentNamespace) + g.Expect(err).To(BeNil()) + t.Log("Creating the Cluster") - g.Expect(env.Create(ctx, testCluster)).To(Succeed()) + cluster := &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Namespace: ns.Name, Name: "test-cluster"}} + g.Expect(env.Create(ctx, cluster)).To(Succeed()) + t.Log("Creating the Cluster Kubeconfig Secret") - g.Expect(env.CreateKubeconfigSecret(ctx, testCluster)).To(Succeed()) + g.Expect(env.CreateKubeconfigSecret(ctx, cluster)).To(Succeed()) + + return ns, cluster } - teardown := func(t *testing.T, g *WithT) { + teardown := func(t *testing.T, g *WithT, ns *corev1.Namespace, cluster *clusterv1.Cluster) { t.Log("Deleting the Cluster") - g.Expect(env.Delete(ctx, testCluster)).To(Succeed()) + g.Expect(env.Delete(ctx, cluster)).To(Succeed()) t.Log("Deleting the namespace") - g.Expect(env.Delete(ctx, namespace)).To(Succeed()) + g.Expect(env.Delete(ctx, ns)).To(Succeed()) } t.Run("Should reconcile a MachineDeployment", func(t *testing.T) { g := NewWithT(t) - setup(t, g) - defer teardown(t, g) + namespace, testCluster := setup(t, g) + defer teardown(t, g, namespace, testCluster) labels := map[string]string{ "foo": "bar", @@ -408,7 +415,7 @@ func TestMachineSetToDeployments(t *testing.T) { machineDeployment := &clusterv1.MachineDeployment{ ObjectMeta: metav1.ObjectMeta{ Name: "withMatchingLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineDeploymentSpec{ Selector: metav1.LabelSelector{ @@ -428,7 +435,7 @@ func TestMachineSetToDeployments(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "withOwnerRef", - Namespace: "test", + Namespace: metav1.NamespaceDefault, OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(machineDeployment, machineDeploymentKind), }, @@ -443,7 +450,7 @@ func TestMachineSetToDeployments(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "noOwnerRefNoLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "test-cluster", }, @@ -455,7 +462,7 @@ func TestMachineSetToDeployments(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "withMatchingLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ "foo": "bar", clusterv1.ClusterLabelName: "test-cluster", @@ -482,7 +489,7 @@ func TestMachineSetToDeployments(t *testing.T) { machineSet: ms3, mapObject: &ms3, expected: []reconcile.Request{ - {NamespacedName: client.ObjectKey{Namespace: "test", Name: "withMatchingLabels"}}, + {NamespacedName: client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "withMatchingLabels"}}, }, }, } @@ -504,7 +511,7 @@ func TestGetMachineDeploymentsForMachineSet(t *testing.T) { machineDeployment := &clusterv1.MachineDeployment{ ObjectMeta: metav1.ObjectMeta{ Name: "withLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineDeploymentSpec{ Selector: metav1.LabelSelector{ @@ -522,7 +529,7 @@ func TestGetMachineDeploymentsForMachineSet(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "NoMatchingLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, } ms2 := clusterv1.MachineSet{ @@ -531,7 +538,7 @@ func TestGetMachineDeploymentsForMachineSet(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "withMatchingLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ "foo": "bar", }, @@ -570,7 +577,7 @@ func TestGetMachineSetsForDeployment(t *testing.T) { machineDeployment1 := clusterv1.MachineDeployment{ ObjectMeta: metav1.ObjectMeta{ Name: "withMatchingOwnerRefAndLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, UID: "UID", }, Spec: clusterv1.MachineDeploymentSpec{ @@ -584,7 +591,7 @@ func TestGetMachineSetsForDeployment(t *testing.T) { machineDeployment2 := clusterv1.MachineDeployment{ ObjectMeta: metav1.ObjectMeta{ Name: "withNoMatchingOwnerRef", - Namespace: "test", + Namespace: metav1.NamespaceDefault, UID: "unMatchingUID", }, Spec: clusterv1.MachineDeploymentSpec{ @@ -598,7 +605,7 @@ func TestGetMachineSetsForDeployment(t *testing.T) { machineDeployment3 := clusterv1.MachineDeployment{ ObjectMeta: metav1.ObjectMeta{ Name: "withMatchingOwnerRefAndNoMatchingLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, UID: "UID3", }, Spec: clusterv1.MachineDeploymentSpec{ @@ -616,7 +623,7 @@ func TestGetMachineSetsForDeployment(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "withNoOwnerRefShouldBeAdopted2", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ "foo": "bar2", }, @@ -628,7 +635,7 @@ func TestGetMachineSetsForDeployment(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "withOwnerRefAndLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(&machineDeployment1, machineDeploymentKind), }, @@ -643,7 +650,7 @@ func TestGetMachineSetsForDeployment(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "withNoOwnerRefShouldBeAdopted1", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ "foo": "bar", }, @@ -655,7 +662,7 @@ func TestGetMachineSetsForDeployment(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "withNoOwnerRefNoMatch", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ "foo": "nomatch", }, @@ -667,7 +674,7 @@ func TestGetMachineSetsForDeployment(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "withOwnerRefAndNoMatchLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(&machineDeployment3, machineDeploymentKind), }, diff --git a/controllers/machinehealthcheck_controller_test.go b/controllers/machinehealthcheck_controller_test.go index 97e4b57f86a1..64dbd551b3b9 100644 --- a/controllers/machinehealthcheck_controller_test.go +++ b/controllers/machinehealthcheck_controller_test.go @@ -36,21 +36,17 @@ import ( "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/record" "k8s.io/utils/pointer" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" "sigs.k8s.io/cluster-api/controllers/remote" capierrors "sigs.k8s.io/cluster-api/errors" "sigs.k8s.io/cluster-api/util" "sigs.k8s.io/cluster-api/util/conditions" "sigs.k8s.io/cluster-api/util/patch" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -const ( - defaultNamespaceName = "default" - testClusterName = "test-cluster" ) func TestMachineHealthCheck_Reconcile(t *testing.T) { @@ -1802,7 +1798,7 @@ func TestClusterToMachineHealthCheck(t *testing.T) { Client: fakeClient, } - namespace := defaultNamespaceName + namespace := metav1.NamespaceDefault clusterName := testClusterName labels := make(map[string]string) @@ -1881,7 +1877,7 @@ func TestMachineToMachineHealthCheck(t *testing.T) { Client: fakeClient, } - namespace := defaultNamespaceName + namespace := metav1.NamespaceDefault clusterName := testClusterName nodeName := "node1" labels := map[string]string{"cluster": "foo", "nodepool": "bar"} @@ -1956,7 +1952,7 @@ func TestNodeToMachineHealthCheck(t *testing.T) { Client: fakeClient, } - namespace := defaultNamespaceName + namespace := metav1.NamespaceDefault clusterName := testClusterName nodeName := "node1" labels := map[string]string{"cluster": "foo", "nodepool": "bar"} @@ -2566,7 +2562,7 @@ func newMachineHealthCheck(namespace, clusterName string) *clusterv1.MachineHeal func TestPatchTargets(t *testing.T) { g := NewWithT(t) - namespace := defaultNamespaceName + namespace := metav1.NamespaceDefault clusterName := testClusterName defaultCluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ diff --git a/controllers/machineset_controller_test.go b/controllers/machineset_controller_test.go index b4695004dd5a..e9ab43a8eca0 100644 --- a/controllers/machineset_controller_test.go +++ b/controllers/machineset_controller_test.go @@ -27,40 +27,44 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/client-go/tools/record" - clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" - "sigs.k8s.io/cluster-api/controllers/external" - "sigs.k8s.io/cluster-api/util" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" "sigs.k8s.io/controller-runtime/pkg/reconcile" + + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" + "sigs.k8s.io/cluster-api/controllers/external" + "sigs.k8s.io/cluster-api/util" ) var _ reconcile.Reconciler = &MachineSetReconciler{} func TestMachineSetReconciler(t *testing.T) { - namespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "ms-test"}} - testCluster := &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Namespace: namespace.Name, Name: "test-cluster"}} - - setup := func(t *testing.T, g *WithT) { + setup := func(t *testing.T, g *WithT) (*corev1.Namespace, *clusterv1.Cluster) { t.Log("Creating the namespace") - g.Expect(env.Create(ctx, namespace)).To(Succeed()) + ns, err := env.CreateNamespace(ctx, "test-machine-set-reconciler") + g.Expect(err).To(BeNil()) + t.Log("Creating the Cluster") - g.Expect(env.Create(ctx, testCluster)).To(Succeed()) + cluster := &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Namespace: ns.Name, Name: testClusterName}} + g.Expect(env.Create(ctx, cluster)).To(Succeed()) + t.Log("Creating the Cluster Kubeconfig Secret") - g.Expect(env.CreateKubeconfigSecret(ctx, testCluster)).To(Succeed()) + g.Expect(env.CreateKubeconfigSecret(ctx, cluster)).To(Succeed()) + + return ns, cluster } - teardown := func(t *testing.T, g *WithT) { + teardown := func(t *testing.T, g *WithT, ns *corev1.Namespace, cluster *clusterv1.Cluster) { t.Log("Deleting the Cluster") - g.Expect(env.Delete(ctx, testCluster)).To(Succeed()) + g.Expect(env.Delete(ctx, cluster)).To(Succeed()) t.Log("Deleting the namespace") - g.Expect(env.Delete(ctx, namespace)).To(Succeed()) + g.Expect(env.Delete(ctx, ns)).To(Succeed()) } t.Run("Should reconcile a MachineSet", func(t *testing.T) { g := NewWithT(t) - setup(t, g) - defer teardown(t, g) + namespace, testCluster := setup(t, g) + defer teardown(t, g, namespace, testCluster) replicas := int32(2) version := "v1.14.2" @@ -283,7 +287,7 @@ func TestMachineSetReconciler(t *testing.T) { func TestMachineSetOwnerReference(t *testing.T) { testCluster := &clusterv1.Cluster{ TypeMeta: metav1.TypeMeta{Kind: "Cluster", APIVersion: clusterv1.GroupVersion.String()}, - ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "test-cluster"}, + ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: testClusterName}, } ms1 := newMachineSet("machineset1", "valid-cluster") @@ -369,7 +373,9 @@ func TestMachineSetOwnerReference(t *testing.T) { } func TestMachineSetReconcile(t *testing.T) { - testCluster := &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "test-cluster"}} + testCluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: testClusterName}, + } t.Run("ignore machine sets marked for deletion", func(t *testing.T) { g := NewWithT(t) @@ -378,11 +384,11 @@ func TestMachineSetReconcile(t *testing.T) { ms := &clusterv1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ Name: "machineset1", - Namespace: "default", + Namespace: metav1.NamespaceDefault, DeletionTimestamp: &dt, }, Spec: clusterv1.MachineSetSpec{ - ClusterName: "test-cluster", + ClusterName: testClusterName, }, } request := reconcile.Request{ @@ -401,7 +407,7 @@ func TestMachineSetReconcile(t *testing.T) { t.Run("records event if reconcile fails", func(t *testing.T) { g := NewWithT(t) - ms := newMachineSet("machineset1", "test-cluster") + ms := newMachineSet("machineset1", testClusterName) ms.Spec.Selector.MatchLabels = map[string]string{ "--$-invalid": "true", } @@ -422,7 +428,7 @@ func TestMachineSetReconcile(t *testing.T) { t.Run("reconcile successfully when labels are missing", func(t *testing.T) { g := NewWithT(t) - ms := newMachineSet("machineset1", "test-cluster") + ms := newMachineSet("machineset1", testClusterName) ms.Labels = nil ms.Spec.Selector.MatchLabels = nil ms.Spec.Template.Labels = nil @@ -446,13 +452,13 @@ func TestMachineSetToMachines(t *testing.T) { &clusterv1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ Name: "withMatchingLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.MachineSetSpec{ Selector: metav1.LabelSelector{ MatchLabels: map[string]string{ "foo": "bar", - clusterv1.ClusterLabelName: "test-cluster", + clusterv1.ClusterLabelName: testClusterName, }, }, }, @@ -462,9 +468,9 @@ func TestMachineSetToMachines(t *testing.T) { m := clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "withOwnerRef", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ - clusterv1.ClusterLabelName: "test-cluster", + clusterv1.ClusterLabelName: testClusterName, }, OwnerReferences: []metav1.OwnerReference{ { @@ -478,19 +484,19 @@ func TestMachineSetToMachines(t *testing.T) { m2 := clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "noOwnerRefNoLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ - clusterv1.ClusterLabelName: "test-cluster", + clusterv1.ClusterLabelName: testClusterName, }, }, } m3 := clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "withMatchingLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ "foo": "bar", - clusterv1.ClusterLabelName: "test-cluster", + clusterv1.ClusterLabelName: testClusterName, }, }, } @@ -513,7 +519,7 @@ func TestMachineSetToMachines(t *testing.T) { name: "should return request if machine set's labels matches machine's labels", mapObject: &m3, expected: []reconcile.Request{ - {NamespacedName: client.ObjectKey{Namespace: "test", Name: "withMatchingLabels"}}, + {NamespacedName: client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "withMatchingLabels"}}, }, }, } @@ -546,7 +552,7 @@ func TestShouldExcludeMachine(t *testing.T) { machine: clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "withNoMatchingOwnerRef", - Namespace: "test", + Namespace: metav1.NamespaceDefault, OwnerReferences: []metav1.OwnerReference{ { Name: "Owner", @@ -566,7 +572,7 @@ func TestShouldExcludeMachine(t *testing.T) { machine: clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "withMatchingOwnerRef", - Namespace: "test", + Namespace: metav1.NamespaceDefault, OwnerReferences: []metav1.OwnerReference{ { Name: "Owner", @@ -592,7 +598,7 @@ func TestShouldExcludeMachine(t *testing.T) { machine: clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "withMatchingLabels", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ "foo": "bar", }, @@ -605,7 +611,7 @@ func TestShouldExcludeMachine(t *testing.T) { machine: clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "withDeletionTimestamp", - Namespace: "test", + Namespace: metav1.NamespaceDefault, DeletionTimestamp: &metav1.Time{Time: time.Now()}, Labels: map[string]string{ "foo": "bar", @@ -680,13 +686,13 @@ func newMachineSet(name, cluster string) *clusterv1.MachineSet { return &clusterv1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ Name: name, - Namespace: "default", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: cluster, }, }, Spec: clusterv1.MachineSetSpec{ - ClusterName: "test-cluster", + ClusterName: testClusterName, Replicas: &replicas, Template: clusterv1.MachineTemplateSpec{ ObjectMeta: clusterv1.ObjectMeta{ diff --git a/controllers/mdutil/util_test.go b/controllers/mdutil/util_test.go index d847afa87f70..f6b038fbfebb 100644 --- a/controllers/mdutil/util_test.go +++ b/controllers/mdutil/util_test.go @@ -767,7 +767,7 @@ func TestReplicasAnnotationsNeedUpdate(t *testing.T) { { name: "test Annotations nil", machineSet: &clusterv1.MachineSet{ - ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"}, + ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: metav1.NamespaceDefault}, Spec: clusterv1.MachineSetSpec{ Selector: metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}, }, @@ -779,7 +779,7 @@ func TestReplicasAnnotationsNeedUpdate(t *testing.T) { machineSet: &clusterv1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ Name: "hello", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Annotations: map[string]string{clusterv1.DesiredReplicasAnnotation: "8", clusterv1.MaxReplicasAnnotation: maxReplicas}, }, Spec: clusterv1.MachineSetSpec{ @@ -793,7 +793,7 @@ func TestReplicasAnnotationsNeedUpdate(t *testing.T) { machineSet: &clusterv1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ Name: "hello", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Annotations: map[string]string{clusterv1.DesiredReplicasAnnotation: desiredReplicas, clusterv1.MaxReplicasAnnotation: "16"}, }, Spec: clusterv1.MachineSetSpec{ @@ -807,7 +807,7 @@ func TestReplicasAnnotationsNeedUpdate(t *testing.T) { machineSet: &clusterv1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ Name: "hello", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Annotations: map[string]string{clusterv1.DesiredReplicasAnnotation: desiredReplicas, clusterv1.MaxReplicasAnnotation: maxReplicas}, }, Spec: clusterv1.MachineSetSpec{ diff --git a/controllers/remote/cluster_cache_healthcheck_test.go b/controllers/remote/cluster_cache_healthcheck_test.go index b4915b75e09a..3d5b785ff511 100644 --- a/controllers/remote/cluster_cache_healthcheck_test.go +++ b/controllers/remote/cluster_cache_healthcheck_test.go @@ -24,7 +24,6 @@ import ( "time" . "github.com/onsi/gomega" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" @@ -43,7 +42,6 @@ func TestClusterCacheHealthCheck(t *testing.T) { var mgrCancel context.CancelFunc var k8sClient client.Client - var testNamespace *corev1.Namespace var testClusterKey client.ObjectKey var cct *ClusterCacheTracker var cc *stoppableCache @@ -75,14 +73,14 @@ func TestClusterCacheHealthCheck(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) t.Log("Creating a namespace for the test") - testNamespace = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{GenerateName: "cluster-cache-test-"}} - g.Expect(k8sClient.Create(ctx, testNamespace)).To(Succeed()) + ns, err := env.CreateNamespace(ctx, "cluster-cache-health-test") + g.Expect(err).To(BeNil()) t.Log("Creating a test cluster") testCluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", - Namespace: testNamespace.GetName(), + Namespace: ns.GetName(), }, } g.Expect(k8sClient.Create(ctx, testCluster)).To(Succeed()) diff --git a/controllers/remote/cluster_cache_reconciler_test.go b/controllers/remote/cluster_cache_reconciler_test.go index 055e945997b9..d2ee34b373cc 100644 --- a/controllers/remote/cluster_cache_reconciler_test.go +++ b/controllers/remote/cluster_cache_reconciler_test.go @@ -36,16 +36,15 @@ import ( func TestClusterCacheReconciler(t *testing.T) { t.Run("When running the ClusterCacheReconciler", func(t *testing.T) { var ( - mgr manager.Manager - mgrContext context.Context - mgrCancel context.CancelFunc - cct *ClusterCacheTracker - k8sClient client.Client - testNamespace *corev1.Namespace + mgr manager.Manager + mgrContext context.Context + mgrCancel context.CancelFunc + cct *ClusterCacheTracker + k8sClient client.Client ) // createAndWatchCluster creates a new cluster and ensures the clusterCacheTracker has a clusterAccessor for it - createAndWatchCluster := func(clusterName string, g *WithT) { + createAndWatchCluster := func(clusterName string, testNamespace *corev1.Namespace, g *WithT) { t.Log(fmt.Sprintf("Creating a cluster %q", clusterName)) testCluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ @@ -75,7 +74,7 @@ func TestClusterCacheReconciler(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) } - setup := func(t *testing.T, g *WithT) { + setup := func(t *testing.T, g *WithT) *corev1.Namespace { t.Log("Setting up a new manager") var err error mgr, err = manager.New(env.Config, manager.Options{ @@ -106,13 +105,15 @@ func TestClusterCacheReconciler(t *testing.T) { k8sClient = mgr.GetClient() t.Log("Creating a namespace for the test") - testNamespace = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{GenerateName: "cluster-cache-test-"}} - g.Expect(k8sClient.Create(ctx, testNamespace)).To(Succeed()) + ns, err := env.CreateNamespace(ctx, "cluster-cache-test") + g.Expect(err).To(BeNil()) t.Log("Creating clusters to test with") - createAndWatchCluster("cluster-1", g) - createAndWatchCluster("cluster-2", g) - createAndWatchCluster("cluster-3", g) + createAndWatchCluster("cluster-1", ns, g) + createAndWatchCluster("cluster-2", ns, g) + createAndWatchCluster("cluster-3", ns, g) + + return ns } teardown := func(t *testing.T, g *WithT) { @@ -126,7 +127,7 @@ func TestClusterCacheReconciler(t *testing.T) { t.Run("should remove clusterAccessors when clusters are deleted", func(t *testing.T) { g := NewWithT(t) - setup(t, g) + testNamespace := setup(t, g) defer teardown(t, g) for _, clusterName := range []string{"cluster-1", "cluster-2", "cluster-3"} { diff --git a/controllers/remote/cluster_cache_tracker_test.go b/controllers/remote/cluster_cache_tracker_test.go index aa343a25fd81..3cca80c924ef 100644 --- a/controllers/remote/cluster_cache_tracker_test.go +++ b/controllers/remote/cluster_cache_tracker_test.go @@ -49,15 +49,14 @@ func mapper(i client.Object) []reconcile.Request { func TestClusterCacheTracker(t *testing.T) { t.Run("watching", func(t *testing.T) { var ( - mgr manager.Manager - mgrContext context.Context - mgrCancel context.CancelFunc - cct *ClusterCacheTracker - k8sClient client.Client - testNamespace *corev1.Namespace - c *testController - w Watcher - clusterA *clusterv1.Cluster + mgr manager.Manager + mgrContext context.Context + mgrCancel context.CancelFunc + cct *ClusterCacheTracker + k8sClient client.Client + c *testController + w Watcher + clusterA *clusterv1.Cluster ) setup := func(t *testing.T, g *WithT) { @@ -89,13 +88,13 @@ func TestClusterCacheTracker(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) t.Log("Creating a namespace for the test") - testNamespace = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{GenerateName: "cluster-cache-test-"}} - g.Expect(k8sClient.Create(ctx, testNamespace)).To(Succeed()) + ns, err := env.CreateNamespace(ctx, "cluster-cache-tracker-test") + g.Expect(err).To(BeNil()) t.Log("Creating a test cluster") clusterA = &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Namespace: testNamespace.GetName(), + Namespace: ns.GetName(), Name: "test-cluster", Annotations: make(map[string]string), }, diff --git a/controllers/remote/cluster_test.go b/controllers/remote/cluster_test.go index 90756147490d..7e57878cfae6 100644 --- a/controllers/remote/cluster_test.go +++ b/controllers/remote/cluster_test.go @@ -34,17 +34,17 @@ import ( var ( clusterWithValidKubeConfig = client.ObjectKey{ Name: "test1", - Namespace: "test", + Namespace: metav1.NamespaceDefault, } clusterWithInvalidKubeConfig = client.ObjectKey{ Name: "test2", - Namespace: "test", + Namespace: metav1.NamespaceDefault, } clusterWithNoKubeConfig = client.ObjectKey{ Name: "test3", - Namespace: "test", + Namespace: metav1.NamespaceDefault, } validKubeConfig = ` @@ -67,7 +67,7 @@ users: validSecret = &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: "test1-kubeconfig", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Data: map[string][]byte{ secret.KubeconfigDataName: []byte(validKubeConfig), @@ -77,7 +77,7 @@ users: invalidSecret = &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: "test2-kubeconfig", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Data: map[string][]byte{ secret.KubeconfigDataName: []byte("Not valid!!1"), diff --git a/controllers/suite_test.go b/controllers/suite_test.go index d55dd363906c..2ca0c49e7b58 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -27,18 +27,20 @@ import ( "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/log" + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" "sigs.k8s.io/cluster-api/controllers/noderefutil" "sigs.k8s.io/cluster-api/controllers/remote" "sigs.k8s.io/cluster-api/internal/envtest" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/controller" - "sigs.k8s.io/controller-runtime/pkg/log" // +kubebuilder:scaffold:imports ) const ( - timeout = time.Second * 30 + timeout = time.Second * 30 + testClusterName = "test-cluster" ) var (