diff --git a/util/patch/patch_test.go b/util/patch/patch_test.go index a719f63abc8d..b1868ea4a251 100644 --- a/util/patch/patch_test.go +++ b/util/patch/patch_test.go @@ -21,7 +21,6 @@ import ( "testing" "github.com/google/go-cmp/cmp" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" appsv1 "k8s.io/api/apps/v1" @@ -36,9 +35,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -var _ = Describe("Patch Helper", func() { - - It("Should patch an unstructured object", func() { +func TestPatchHelper(t *testing.T) { + t.Run("should patch an unstructured object", func(t *testing.T) { obj := &unstructured.Unstructured{ Object: map[string]interface{}{ "kind": "BootstrapMachine", @@ -50,18 +48,20 @@ var _ = Describe("Patch Helper", func() { }, } - Context("adding an owner reference, preserving its status", func() { + t.Run("adding an owner reference, preserving its status", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the unstructured object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.GetName(), Namespace: obj.GetNamespace()} + t.Log("Creating the unstructured object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.GetName(), Namespace: obj.GetNamespace()} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -72,13 +72,13 @@ var _ = Describe("Patch Helper", func() { obj.Object["status"] = map[string]interface{}{ "ready": true, } - Expect(testEnv.Status().Update(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Status().Update(ctx, obj)).To(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Modifying the OwnerReferences") + t.Log("Modifying the OwnerReferences") refs := []metav1.OwnerReference{ { APIVersion: "cluster.x-k8s.io/v1alpha4", @@ -89,28 +89,29 @@ var _ = Describe("Patch Helper", func() { } obj.SetOwnerReferences(refs) - By("Patching the unstructured object") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the unstructured object") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating that the status has been preserved") + t.Log("Validating that the status has been preserved") ready, err := external.IsReady(obj) - Expect(err).ToNot(HaveOccurred()) - Expect(ready).To(BeTrue()) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(ready).To(BeTrue()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false } - return reflect.DeepEqual(obj.GetOwnerReferences(), objAfter.GetOwnerReferences()) }, timeout).Should(BeTrue()) }) }) - Describe("Should patch conditions", func() { - Specify("on a corev1.Node object", func() { + t.Run("Should patch conditions", func(t *testing.T) { + t.Run("on a corev1.Node object", func(t *testing.T) { + g := NewWithT(t) + conditionTime := metav1.Date(2015, 1, 1, 12, 0, 0, 0, metav1.Now().Location()) obj := &corev1.Node{ @@ -122,15 +123,15 @@ var _ = Describe("Patch Helper", func() { }, } - By("Creating a Node object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.GetName()} + t.Log("Creating a Node object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.GetName()} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -138,11 +139,11 @@ var _ = Describe("Patch Helper", func() { return nil }).Should(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Appending a new condition") + t.Log("Appending a new condition") condition := corev1.NodeCondition{ Type: "CustomCondition", Status: corev1.ConditionTrue, @@ -153,20 +154,20 @@ var _ = Describe("Patch Helper", func() { } obj.Status.Conditions = append(obj.Status.Conditions, condition) - By("Patching the Node") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the Node") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() - Expect(testEnv.Get(ctx, key, objAfter)).To(Succeed()) + g.Expect(testEnv.Get(ctx, key, objAfter)).To(Succeed()) ok, _ := ContainElement(condition).Match(objAfter.Status.Conditions) return ok }, timeout).Should(BeTrue()) }) - Describe("on a clusterv1.Cluster object", func() { + t.Run("on a clusterv1.Cluster object", func(t *testing.T) { obj := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", @@ -174,18 +175,20 @@ var _ = Describe("Patch Helper", func() { }, } - Specify("should mark it ready", func() { + t.Run("should mark it ready", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -193,18 +196,18 @@ var _ = Describe("Patch Helper", func() { return nil }).Should(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Marking Ready=True") + t.Log("Marking Ready=True") conditions.MarkTrue(obj, clusterv1.ReadyCondition) - By("Patching the object") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -213,18 +216,20 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Specify("should recover if there is a resolvable conflict", func() { + t.Run("should recover if there is a resolvable conflict", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -234,25 +239,25 @@ var _ = Describe("Patch Helper", func() { objCopy := obj.DeepCopy() - By("Marking a custom condition to be false") + t.Log("Marking a custom condition to be false") conditions.MarkFalse(objCopy, clusterv1.ConditionType("TestCondition"), "reason", clusterv1.ConditionSeverityInfo, "message") - Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) + g.Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) - By("Validating that the local object's resource version is behind") - Expect(obj.ResourceVersion).ToNot(Equal(objCopy.ResourceVersion)) + t.Log("Validating that the local object's resource version is behind") + g.Expect(obj.ResourceVersion).NotTo(Equal(objCopy.ResourceVersion)) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Marking Ready=True") + t.Log("Marking Ready=True") conditions.MarkTrue(obj, clusterv1.ReadyCondition) - By("Patching the object") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -268,18 +273,20 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Specify("should recover if there is a resolvable conflict, incl. patch spec and status", func() { + t.Run("should recover if there is a resolvable conflict, incl. patch spec and status", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -289,30 +296,30 @@ var _ = Describe("Patch Helper", func() { objCopy := obj.DeepCopy() - By("Marking a custom condition to be false") + t.Log("Marking a custom condition to be false") conditions.MarkFalse(objCopy, clusterv1.ConditionType("TestCondition"), "reason", clusterv1.ConditionSeverityInfo, "message") - Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) + g.Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) - By("Validating that the local object's resource version is behind") - Expect(obj.ResourceVersion).ToNot(Equal(objCopy.ResourceVersion)) + t.Log("Validating that the local object's resource version is behind") + g.Expect(obj.ResourceVersion).NotTo(Equal(objCopy.ResourceVersion)) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Changing the object spec, status, and adding Ready=True condition") + t.Log("Changing the object spec, status, and adding Ready=True condition") obj.Spec.Paused = true obj.Spec.ControlPlaneEndpoint.Host = "test://endpoint" obj.Spec.ControlPlaneEndpoint.Port = 8443 obj.Status.Phase = "custom-phase" conditions.MarkTrue(obj, clusterv1.ReadyCondition) - By("Patching the object") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating the object has been updated") + t.Log("Validating the object has been updated") objAfter := obj.DeepCopy() - Eventually(func() bool { + g.Eventually(func() bool { if err := testEnv.Get(ctx, key, objAfter); err != nil { return false } @@ -330,18 +337,20 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue(), cmp.Diff(obj, objAfter)) }) - Specify("should return an error if there is an unresolvable conflict", func() { + t.Run("should return an error if there is an unresolvable conflict", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -351,25 +360,25 @@ var _ = Describe("Patch Helper", func() { objCopy := obj.DeepCopy() - By("Marking a custom condition to be false") + t.Log("Marking a custom condition to be false") conditions.MarkFalse(objCopy, clusterv1.ReadyCondition, "reason", clusterv1.ConditionSeverityInfo, "message") - Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) + g.Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) - By("Validating that the local object's resource version is behind") - Expect(obj.ResourceVersion).ToNot(Equal(objCopy.ResourceVersion)) + t.Log("Validating that the local object's resource version is behind") + g.Expect(obj.ResourceVersion).NotTo(Equal(objCopy.ResourceVersion)) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Marking Ready=True") + t.Log("Marking Ready=True") conditions.MarkTrue(obj, clusterv1.ReadyCondition) - By("Patching the object") - Expect(patcher.Patch(ctx, obj)).ToNot(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj)).NotTo(Succeed()) - By("Validating the object has not been updated") - Eventually(func() bool { + t.Log("Validating the object has not been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -379,18 +388,20 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Specify("should not return an error if there is an unresolvable conflict but the conditions is owned by the controller", func() { + t.Run("should not return an error if there is an unresolvable conflict but the conditions is owned by the controller", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -400,25 +411,25 @@ var _ = Describe("Patch Helper", func() { objCopy := obj.DeepCopy() - By("Marking a custom condition to be false") + t.Log("Marking a custom condition to be false") conditions.MarkFalse(objCopy, clusterv1.ReadyCondition, "reason", clusterv1.ConditionSeverityInfo, "message") - Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) + g.Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) - By("Validating that the local object's resource version is behind") - Expect(obj.ResourceVersion).ToNot(Equal(objCopy.ResourceVersion)) + t.Log("Validating that the local object's resource version is behind") + g.Expect(obj.ResourceVersion).NotTo(Equal(objCopy.ResourceVersion)) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Marking Ready=True") + t.Log("Marking Ready=True") conditions.MarkTrue(obj, clusterv1.ReadyCondition) - By("Patching the object") - Expect(patcher.Patch(ctx, obj, WithOwnedConditions{Conditions: []clusterv1.ConditionType{clusterv1.ReadyCondition}})).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj, WithOwnedConditions{Conditions: []clusterv1.ConditionType{clusterv1.ReadyCondition}})).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -431,18 +442,20 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Specify("should not return an error if there is an unresolvable conflict when force overwrite is enabled", func() { + t.Run("should not return an error if there is an unresolvable conflict when force overwrite is enabled", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -452,25 +465,25 @@ var _ = Describe("Patch Helper", func() { objCopy := obj.DeepCopy() - By("Marking a custom condition to be false") + t.Log("Marking a custom condition to be false") conditions.MarkFalse(objCopy, clusterv1.ReadyCondition, "reason", clusterv1.ConditionSeverityInfo, "message") - Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) + g.Expect(testEnv.Status().Update(ctx, objCopy)).To(Succeed()) - By("Validating that the local object's resource version is behind") - Expect(obj.ResourceVersion).ToNot(Equal(objCopy.ResourceVersion)) + t.Log("Validating that the local object's resource version is behind") + g.Expect(obj.ResourceVersion).NotTo(Equal(objCopy.ResourceVersion)) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Marking Ready=True") + t.Log("Marking Ready=True") conditions.MarkTrue(obj, clusterv1.ReadyCondition) - By("Patching the object") - Expect(patcher.Patch(ctx, obj, WithForceOverwriteConditions{})).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj, WithForceOverwriteConditions{})).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -482,11 +495,10 @@ var _ = Describe("Patch Helper", func() { return cmp.Equal(readyBefore, readyAfter) }, timeout).Should(BeTrue()) }) - }) }) - Describe("Should patch a clusterv1.Cluster", func() { + t.Run("Should patch a clusterv1.Cluster", func(t *testing.T) { obj := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", @@ -494,18 +506,20 @@ var _ = Describe("Patch Helper", func() { }, } - Specify("add a finalizers", func() { + t.Run("add a finalizer", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -513,18 +527,18 @@ var _ = Describe("Patch Helper", func() { return nil }).Should(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Adding a finalizer") + t.Log("Adding a finalizer") obj.Finalizers = append(obj.Finalizers, clusterv1.ClusterFinalizer) - By("Patching the object") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -534,19 +548,21 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Specify("removing finalizers", func() { + t.Run("removing finalizers", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() obj.Finalizers = append(obj.Finalizers, clusterv1.ClusterFinalizer) - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -554,18 +570,18 @@ var _ = Describe("Patch Helper", func() { return nil }).Should(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Removing the finalizers") + t.Log("Removing the finalizers") obj.SetFinalizers(nil) - By("Patching the object") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -575,19 +591,21 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Specify("updating spec", func() { + t.Run("updating spec", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() obj.ObjectMeta.Namespace = "default" - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -595,11 +613,11 @@ var _ = Describe("Patch Helper", func() { return nil }).Should(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Updating the object spec") + t.Log("Updating the object spec") obj.Spec.Paused = true obj.Spec.InfrastructureRef = &corev1.ObjectReference{ Kind: "test-kind", @@ -607,11 +625,11 @@ var _ = Describe("Patch Helper", func() { Namespace: "default", } - By("Patching the object") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -622,18 +640,20 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Specify("updating status", func() { + t.Run("updating status", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -641,18 +661,18 @@ var _ = Describe("Patch Helper", func() { return nil }).Should(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Updating the object status") + t.Log("Updating the object status") obj.Status.InfrastructureReady = true - By("Patching the object") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -661,19 +681,21 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Specify("updating both spec, status, and adding a condition", func() { + t.Run("updating both spec, status, and adding a condition", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() obj.ObjectMeta.Namespace = "default" - By("Creating the object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -681,11 +703,11 @@ var _ = Describe("Patch Helper", func() { return nil }).Should(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Updating the object spec") + t.Log("Updating the object spec") obj.Spec.Paused = true obj.Spec.InfrastructureRef = &corev1.ObjectReference{ Kind: "test-kind", @@ -693,17 +715,17 @@ var _ = Describe("Patch Helper", func() { Namespace: "default", } - By("Updating the object status") + t.Log("Updating the object status") obj.Status.InfrastructureReady = true - By("Setting Ready condition") + t.Log("Setting Ready condition") conditions.MarkTrue(obj, clusterv1.ReadyCondition) - By("Patching the object") - Expect(patcher.Patch(ctx, obj)).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj)).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -716,7 +738,7 @@ var _ = Describe("Patch Helper", func() { }) }) - It("Should update Status.ObservedGeneration when using WithStatusObservedGeneration option", func() { + t.Run("Should update Status.ObservedGeneration when using WithStatusObservedGeneration option", func(t *testing.T) { obj := &clusterv1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-ms", @@ -732,18 +754,20 @@ var _ = Describe("Patch Helper", func() { }, } - Context("when updating spec", func() { + t.Run("when updating spec", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the MachineSet object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the MachineSet object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -751,18 +775,18 @@ var _ = Describe("Patch Helper", func() { return nil }).Should(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Updating the object spec") + t.Log("Updating the object spec") obj.Spec.Replicas = pointer.Int32Ptr(10) - By("Patching the object") - Expect(patcher.Patch(ctx, obj, WithStatusObservedGeneration{})).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj, WithStatusObservedGeneration{})).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -773,18 +797,20 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Context("when updating spec, status, and metadata", func() { + t.Run("when updating spec, status, and metadata", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the MachineSet object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the MachineSet object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -792,27 +818,27 @@ var _ = Describe("Patch Helper", func() { return nil }).Should(Succeed()) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Updating the object spec") + t.Log("Updating the object spec") obj.Spec.Replicas = pointer.Int32Ptr(10) - By("Updating the object status") + t.Log("Updating the object status") obj.Status.AvailableReplicas = 6 obj.Status.ReadyReplicas = 6 - By("Updating the object metadata") + t.Log("Updating the object metadata") obj.ObjectMeta.Annotations = map[string]string{ "test1": "annotation", } - By("Patching the object") - Expect(patcher.Patch(ctx, obj, WithStatusObservedGeneration{})).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj, WithStatusObservedGeneration{})).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -824,18 +850,20 @@ var _ = Describe("Patch Helper", func() { }, timeout).Should(BeTrue()) }) - Context("without any changes", func() { + t.Run("without any changes", func(t *testing.T) { + g := NewWithT(t) + obj := obj.DeepCopy() - By("Creating the MachineSet object") - Expect(testEnv.Create(ctx, obj)).ToNot(HaveOccurred()) - key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} + t.Log("Creating the MachineSet object") + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) defer func() { - Expect(testEnv.Delete(ctx, obj)).To(Succeed()) + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) }() + key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace} - By("Checking that the object has been created") - Eventually(func() error { + t.Log("Checking that the object has been created") + g.Eventually(func() error { obj := obj.DeepCopy() if err := testEnv.Get(ctx, key, obj); err != nil { return err @@ -845,17 +873,17 @@ var _ = Describe("Patch Helper", func() { obj.Status.ObservedGeneration = obj.GetGeneration() lastGeneration := obj.GetGeneration() - Expect(testEnv.Status().Update(ctx, obj)) + g.Expect(testEnv.Status().Update(ctx, obj)) - By("Creating a new patch helper") + t.Log("Creating a new patch helper") patcher, err := NewHelper(obj, testEnv) - Expect(err).NotTo(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - By("Patching the object") - Expect(patcher.Patch(ctx, obj, WithStatusObservedGeneration{})).To(Succeed()) + t.Log("Patching the object") + g.Expect(patcher.Patch(ctx, obj, WithStatusObservedGeneration{})).To(Succeed()) - By("Validating the object has been updated") - Eventually(func() bool { + t.Log("Validating the object has been updated") + g.Eventually(func() bool { objAfter := obj.DeepCopy() if err := testEnv.Get(ctx, key, objAfter); err != nil { return false @@ -866,7 +894,9 @@ var _ = Describe("Patch Helper", func() { }) }) - It("Should error if the object isn't the same", func() { + t.Run("Should error if the object isn't the same", func(t *testing.T) { + g := NewWithT(t) + cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "test-", @@ -889,21 +919,27 @@ var _ = Describe("Patch Helper", func() { }, } - Expect(testEnv.Create(ctx, cluster)).To(Succeed()) - Expect(testEnv.Create(ctx, machineSet)).To(Succeed()) + g.Expect(testEnv.Create(ctx, cluster)).To(Succeed()) + defer func() { + g.Expect(testEnv.Delete(ctx, cluster)).To(Succeed()) + }() + g.Expect(testEnv.Create(ctx, machineSet)).To(Succeed()) + defer func() { + g.Expect(testEnv.Delete(ctx, machineSet)).To(Succeed()) + }() patcher, err := NewHelper(cluster, testEnv) - Expect(err).ToNot(HaveOccurred()) + g.Expect(err).NotTo(HaveOccurred()) - Expect(patcher.Patch(ctx, machineSet)).ToNot(Succeed()) + g.Expect(patcher.Patch(ctx, machineSet)).NotTo(Succeed()) }) -}) +} func TestNewHelperNil(t *testing.T) { var x *appsv1.Deployment g := NewWithT(t) _, err := NewHelper(x, nil) - g.Expect(err).ToNot(BeNil()) + g.Expect(err).NotTo(BeNil()) _, err = NewHelper(nil, nil) - g.Expect(err).ToNot(BeNil()) + g.Expect(err).NotTo(BeNil()) } diff --git a/util/patch/suite_test.go b/util/patch/suite_test.go index bb7e261c3552..605f17d34794 100644 --- a/util/patch/suite_test.go +++ b/util/patch/suite_test.go @@ -17,21 +17,16 @@ limitations under the License. package patch import ( + "fmt" + "os" "testing" "time" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "sigs.k8s.io/cluster-api/test/helpers" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/envtest/printer" // +kubebuilder:scaffold:imports ) -// These tests use Ginkgo (BDD-style Go testing framework). Refer to -// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. - const ( timeout = time.Second * 10 ) @@ -41,30 +36,25 @@ var ( ctx = ctrl.SetupSignalHandler() ) -func TestPatch(t *testing.T) { - RegisterFailHandler(Fail) - - RunSpecsWithDefaultAndCustomReporters(t, - "Controller Suite", - []Reporter{printer.NewlineReporter{}}) -} - -var _ = BeforeSuite(func() { - By("bootstrapping test environment") +func TestMain(m *testing.M) { + fmt.Println("Creating new test environment") testEnv = helpers.NewTestEnvironment() - By("starting the manager") go func() { - defer GinkgoRecover() - Expect(testEnv.StartManager(ctx)).To(Succeed()) + fmt.Println("Starting the manager") + if err := testEnv.StartManager(ctx); err != nil { + panic(fmt.Sprintf("Failed to start the envtest manager: %v", err)) + } }() <-testEnv.Manager.Elected() testEnv.WaitForWebhooks() -}, 60) -var _ = AfterSuite(func() { - if testEnv != nil { - By("tearing down the test environment") - Expect(testEnv.Stop()).To(Succeed()) + code := m.Run() + + fmt.Println("Tearing down test suite") + if err := testEnv.Stop(); err != nil { + panic(fmt.Sprintf("Failed to stop envtest: %v", err)) } -}) + + os.Exit(code) +}