From 60b0cfa4c474adc38ecca97335c96d98e1f71c9e Mon Sep 17 00:00:00 2001 From: Grigoriy Mikhalkin Date: Sun, 18 Jul 2021 00:44:38 +0200 Subject: [PATCH] util tests namespace creation refactoring --- util/patch/patch_test.go | 32 +++++++++++++++++++------------- util/util_test.go | 26 +++++++++++++------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/util/patch/patch_test.go b/util/patch/patch_test.go index 5c5693b602d6..80abf2529ab9 100644 --- a/util/patch/patch_test.go +++ b/util/patch/patch_test.go @@ -36,6 +36,16 @@ import ( ) func TestPatchHelper(t *testing.T) { + ns, err := env.CreateNamespace(ctx, "test-patch-helper") + if err != nil { + t.Fatal(err) + } + defer func() { + if err := env.Delete(ctx, ns); err != nil { + t.Fatal(err) + } + }() + t.Run("should patch an unstructured object", func(t *testing.T) { obj := &unstructured.Unstructured{ Object: map[string]interface{}{ @@ -43,7 +53,7 @@ func TestPatchHelper(t *testing.T) { "apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4", "metadata": map[string]interface{}{ "generateName": "test-bootstrap-", - "namespace": "default", + "namespace": ns.Name, }, }, } @@ -51,8 +61,6 @@ func TestPatchHelper(t *testing.T) { t.Run("adding an owner reference, preserving its status", func(t *testing.T) { g := NewWithT(t) - obj := obj.DeepCopy() - t.Log("Creating the unstructured object") g.Expect(env.Create(ctx, obj)).To(Succeed()) defer func() { @@ -110,10 +118,10 @@ func TestPatchHelper(t *testing.T) { g := NewWithT(t) conditionTime := metav1.Date(2015, 1, 1, 12, 0, 0, 0, metav1.Now().Location()) - obj := &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "node-patch-test-", + Namespace: ns.Name, Annotations: map[string]string{ "test": "1", }, @@ -165,7 +173,7 @@ func TestPatchHelper(t *testing.T) { obj := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", - Namespace: "default", + Namespace: ns.Name, }, } @@ -478,7 +486,7 @@ func TestPatchHelper(t *testing.T) { obj := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", - Namespace: "default", + Namespace: ns.Name, }, } @@ -565,7 +573,6 @@ func TestPatchHelper(t *testing.T) { g := NewWithT(t) obj := obj.DeepCopy() - obj.ObjectMeta.Namespace = "default" t.Log("Creating the object") g.Expect(env.Create(ctx, obj)).To(Succeed()) @@ -589,7 +596,7 @@ func TestPatchHelper(t *testing.T) { obj.Spec.InfrastructureRef = &corev1.ObjectReference{ Kind: "test-kind", Name: "test-ref", - Namespace: "default", + Namespace: ns.Name, } t.Log("Patching the object") @@ -649,7 +656,6 @@ func TestPatchHelper(t *testing.T) { g := NewWithT(t) obj := obj.DeepCopy() - obj.ObjectMeta.Namespace = "default" t.Log("Creating the object") g.Expect(env.Create(ctx, obj)).To(Succeed()) @@ -673,7 +679,7 @@ func TestPatchHelper(t *testing.T) { obj.Spec.InfrastructureRef = &corev1.ObjectReference{ Kind: "test-kind", Name: "test-ref", - Namespace: "default", + Namespace: ns.Name, } t.Log("Updating the object status") @@ -703,7 +709,7 @@ func TestPatchHelper(t *testing.T) { obj := &clusterv1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-ms", - Namespace: "default", + Namespace: ns.Name, }, Spec: clusterv1.MachineSetSpec{ ClusterName: "test1", @@ -851,14 +857,14 @@ func TestPatchHelper(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", - Namespace: "default", + Namespace: ns.Name, }, } machineSet := &clusterv1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-ms", - Namespace: "default", + Namespace: ns.Name, }, Spec: clusterv1.MachineSetSpec{ ClusterName: "test1", diff --git a/util/util_test.go b/util/util_test.go index d76265a5ac5c..6d2a39f90df5 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -53,7 +53,7 @@ func TestMachineToInfrastructureMapFunc(t *testing.T) { }, request: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "default", + Namespace: metav1.NamespaceDefault, Name: "test-1", }, Spec: clusterv1.MachineSpec{ @@ -67,7 +67,7 @@ func TestMachineToInfrastructureMapFunc(t *testing.T) { output: []reconcile.Request{ { NamespacedName: client.ObjectKey{ - Namespace: "default", + Namespace: metav1.NamespaceDefault, Name: "infra-1", }, }, @@ -82,7 +82,7 @@ func TestMachineToInfrastructureMapFunc(t *testing.T) { }, request: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "default", + Namespace: metav1.NamespaceDefault, Name: "test-1", }, Spec: clusterv1.MachineSpec{ @@ -124,7 +124,7 @@ func TestClusterToInfrastructureMapFunc(t *testing.T) { }, request: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "default", + Namespace: metav1.NamespaceDefault, Name: "test-1", }, Spec: clusterv1.ClusterSpec{ @@ -138,7 +138,7 @@ func TestClusterToInfrastructureMapFunc(t *testing.T) { output: []reconcile.Request{ { NamespacedName: client.ObjectKey{ - Namespace: "default", + Namespace: metav1.NamespaceDefault, Name: "infra-1", }, }, @@ -153,7 +153,7 @@ func TestClusterToInfrastructureMapFunc(t *testing.T) { }, request: &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "default", + Namespace: metav1.NamespaceDefault, Name: "test-1", }, Spec: clusterv1.ClusterSpec{ @@ -395,7 +395,7 @@ func TestGetOwnerClusterSuccessByName(t *testing.T) { myCluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "my-cluster", - Namespace: "my-ns", + Namespace: metav1.NamespaceDefault, }, } @@ -411,7 +411,7 @@ func TestGetOwnerClusterSuccessByName(t *testing.T) { Name: "my-cluster", }, }, - Namespace: "my-ns", + Namespace: metav1.NamespaceDefault, Name: "my-resource-owned-by-cluster", } cluster, err := GetOwnerCluster(ctx, c, objm) @@ -431,7 +431,7 @@ func TestGetOwnerMachineSuccessByName(t *testing.T) { myMachine := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "my-machine", - Namespace: "my-ns", + Namespace: metav1.NamespaceDefault, }, } @@ -447,7 +447,7 @@ func TestGetOwnerMachineSuccessByName(t *testing.T) { Name: "my-machine", }, }, - Namespace: "my-ns", + Namespace: metav1.NamespaceDefault, Name: "my-resource-owned-by-machine", } machine, err := GetOwnerMachine(ctx, c, objm) @@ -461,7 +461,7 @@ func TestGetOwnerMachineSuccessByNameFromDifferentVersion(t *testing.T) { myMachine := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: "my-machine", - Namespace: "my-ns", + Namespace: metav1.NamespaceDefault, }, } @@ -477,7 +477,7 @@ func TestGetOwnerMachineSuccessByNameFromDifferentVersion(t *testing.T) { Name: "my-machine", }, }, - Namespace: "my-ns", + Namespace: metav1.NamespaceDefault, Name: "my-resource-owned-by-machine", } machine, err := GetOwnerMachine(ctx, c, objm) @@ -491,7 +491,7 @@ func TestGetMachinesForCluster(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "my-cluster", - Namespace: "my-ns", + Namespace: metav1.NamespaceDefault, }, }