diff --git a/conditions/conditions_test.go b/conditions/conditions_test.go index a9001f9..581b626 100644 --- a/conditions/conditions_test.go +++ b/conditions/conditions_test.go @@ -64,25 +64,21 @@ var _ = Describe("Condition", func() { } // Create Operator Condition - err := os.Setenv(operatorCondEnvVar, "operator-condition-test") - Expect(err).NotTo(HaveOccurred()) + Expect(os.Setenv(operatorCondEnvVar, "operator-condition-test")).To(Succeed()) readNamespace = func() (string, error) { return ns, nil } // create a new client sch := runtime.NewScheme() - err = apiv2.AddToScheme(sch) - Expect(err).NotTo(HaveOccurred()) + Expect(apiv2.AddToScheme(sch)).To(Succeed()) cl = fake.NewClientBuilder().WithScheme(sch).WithStatusSubresource(operatorCond).Build() // create an operator Condition resource - err = cl.Create(ctx, operatorCond) - Expect(err).NotTo(HaveOccurred()) + Expect(cl.Create(ctx, operatorCond)).To(Succeed()) // Update its status - err = cl.Status().Update(ctx, operatorCond) - Expect(err).NotTo(HaveOccurred()) + Expect(cl.Status().Update(ctx, operatorCond)).To(Succeed()) }) AfterEach(func() { @@ -107,14 +103,12 @@ var _ = Describe("Condition", func() { Expect(err).NotTo(HaveOccurred()) con, err := c.Get(ctx) - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("conditionType %v not found", conditionBar)))) Expect(con).To(BeNil()) - Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("conditionType %v not found", conditionBar))) }) It("should error when operator Condition is not present in cluster", func() { - err := os.Setenv(operatorCondEnvVar, "NON_EXISTING_COND") - Expect(err).NotTo(HaveOccurred()) + Expect(os.Setenv(operatorCondEnvVar, "NON_EXISTING_COND")).To(Succeed()) By("setting the status of a new condition") c, err := NewCondition(cl, conditionFoo) @@ -131,13 +125,11 @@ var _ = Describe("Condition", func() { By("setting the status of an existing condition") c, err := NewCondition(cl, conditionFoo) Expect(err).NotTo(HaveOccurred()) - err = c.Set(ctx, metav1.ConditionFalse, WithReason("not_in_foo_state"), WithMessage("test")) - Expect(err).NotTo(HaveOccurred()) + Expect(c.Set(ctx, metav1.ConditionFalse, WithReason("not_in_foo_state"), WithMessage("test"))).To(Succeed()) By("fetching the condition from cluster") op := &apiv2.OperatorCondition{} - err = cl.Get(ctx, objKey, op) - Expect(err).NotTo(HaveOccurred()) + Expect(cl.Get(ctx, objKey, op)).To(Succeed()) By("checking if the condition has been updated") res := op.Spec.Conditions[0] @@ -150,22 +142,19 @@ var _ = Describe("Condition", func() { By("setting the status of a new condition") c, err := NewCondition(cl, conditionBar) Expect(err).NotTo(HaveOccurred()) - err = c.Set(ctx, metav1.ConditionTrue, WithReason("in_bar_state"), WithMessage("test")) - Expect(err).NotTo(HaveOccurred()) + Expect(c.Set(ctx, metav1.ConditionTrue, WithReason("in_bar_state"), WithMessage("test"))).To(Succeed()) By("fetching the condition from cluster") op := &apiv2.OperatorCondition{} - err = cl.Get(ctx, objKey, op) - Expect(err).NotTo(HaveOccurred()) + Expect(cl.Get(ctx, objKey, op)).To(Succeed()) By("checking if the condition has been updated") res := op.Spec.Conditions - Expect(len(res)).To(BeEquivalentTo(2)) + Expect(res).To(HaveLen(2)) Expect(meta.IsStatusConditionTrue(res, string(conditionBar))).To(BeTrue()) }) It("should error when operator Condition is not present in cluster", func() { - err := os.Setenv(operatorCondEnvVar, "NON_EXISTING_COND") - Expect(err).NotTo(HaveOccurred()) + Expect(os.Setenv(operatorCondEnvVar, "NON_EXISTING_COND")).To(Succeed()) By("setting the status of a new condition") c, err := NewCondition(cl, conditionBar) diff --git a/conditions/factory_test.go b/conditions/factory_test.go index fdb339f..5973a3a 100644 --- a/conditions/factory_test.go +++ b/conditions/factory_test.go @@ -36,8 +36,7 @@ var _ = Describe("NewCondition", func() { var cl client.Client BeforeEach(func() { sch := runtime.NewScheme() - err := apiv2.AddToScheme(sch) - Expect(err).NotTo(HaveOccurred()) + Expect(apiv2.AddToScheme(sch)).To(Succeed()) cl = fake.NewClientBuilder().WithScheme(sch).Build() }) @@ -56,8 +55,7 @@ var _ = Describe("InClusterFactory", func() { BeforeEach(func() { sch := runtime.NewScheme() - err := apiv2.AddToScheme(sch) - Expect(err).NotTo(HaveOccurred()) + Expect(apiv2.AddToScheme(sch)).To(Succeed()) cl = fake.NewClientBuilder().WithScheme(sch).Build() f = InClusterFactory{cl} }) @@ -73,8 +71,7 @@ var _ = Describe("InClusterFactory", func() { func testNewCondition(fn func(apiv2.ConditionType) (Condition, error)) { It("should create a new condition", func() { - err := os.Setenv(operatorCondEnvVar, "test-operator-condition") - Expect(err).NotTo(HaveOccurred()) + Expect(os.Setenv(operatorCondEnvVar, "test-operator-condition")).To(Succeed()) readNamespace = func() (string, error) { return "default", nil } @@ -85,8 +82,7 @@ func testNewCondition(fn func(apiv2.ConditionType) (Condition, error)) { }) It("should error when namespacedName cannot be found", func() { - err := os.Unsetenv(operatorCondEnvVar) - Expect(err).NotTo(HaveOccurred()) + Expect(os.Unsetenv(operatorCondEnvVar)).To(Succeed()) c, err := fn(conditionFoo) Expect(err).To(HaveOccurred()) @@ -96,32 +92,27 @@ func testNewCondition(fn func(apiv2.ConditionType) (Condition, error)) { func testGetNamespacedName(fn func() (*types.NamespacedName, error)) { It("should error when name of the operator condition cannot be found", func() { - err := os.Unsetenv(operatorCondEnvVar) - Expect(err).NotTo(HaveOccurred()) + Expect(os.Unsetenv(operatorCondEnvVar)).To(Succeed()) objKey, err := fn() - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(ContainSubstring("could not determine operator condition name"))) Expect(objKey).To(BeNil()) - Expect(err.Error()).To(ContainSubstring("could not determine operator condition name")) }) It("should error when object namespace cannot be found", func() { - err := os.Setenv(operatorCondEnvVar, "test") - Expect(err).NotTo(HaveOccurred()) + Expect(os.Setenv(operatorCondEnvVar, "test")).To(Succeed()) readNamespace = func() (string, error) { return "", os.ErrNotExist } objKey, err := fn() - Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(ContainSubstring("get operator condition namespace: file does not exist"))) Expect(objKey).To(BeNil()) - Expect(err.Error()).To(ContainSubstring("get operator condition namespace: file does not exist")) }) It("should return the right namespaced name from SA namespace file", func() { - err := os.Setenv(operatorCondEnvVar, "test") - Expect(err).NotTo(HaveOccurred()) + Expect(os.Setenv(operatorCondEnvVar, "test")).To(Succeed()) readNamespace = func() (string, error) { return "testns", nil @@ -135,6 +126,5 @@ func testGetNamespacedName(fn func() (*types.NamespacedName, error)) { } func deleteCondition(ctx context.Context, client client.Client, obj client.Object) { - err := client.Delete(ctx, obj) - Expect(err).NotTo(HaveOccurred()) + Expect(client.Delete(ctx, obj)).To(Succeed()) } diff --git a/handler/enqueue_annotation_test.go b/handler/enqueue_annotation_test.go index f36658a..8835aa9 100644 --- a/handler/enqueue_annotation_test.go +++ b/handler/enqueue_annotation_test.go @@ -56,8 +56,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() { podOwner.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Kind: "Pod"}) - err := SetOwnerAnnotations(podOwner, pod) - Expect(err).ToNot(HaveOccurred()) + Expect(SetOwnerAnnotations(podOwner, pod)).To(Succeed()) instance = EnqueueRequestForAnnotation{ Type: schema.GroupKind{ Group: "", @@ -92,8 +91,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() { }, } - err := SetOwnerAnnotations(podOwner, repl) - Expect(err).ToNot(HaveOccurred()) + Expect(SetOwnerAnnotations(podOwner, repl)).To(Succeed()) evt := event.CreateEvent{ Object: repl, @@ -248,8 +246,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() { newPod.Name = pod.Name + "2" newPod.Namespace = pod.Namespace + "2" - err := SetOwnerAnnotations(podOwner, pod) - Expect(err).ToNot(HaveOccurred()) + Expect(SetOwnerAnnotations(podOwner, pod)).To(Succeed()) evt := event.UpdateEvent{ ObjectOld: pod, @@ -337,8 +334,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() { newPod.Name = pod.Name + "2" newPod.Namespace = pod.Namespace + "2" - err := SetOwnerAnnotations(podOwner, pod) - Expect(err).ToNot(HaveOccurred()) + Expect(SetOwnerAnnotations(podOwner, pod)).To(Succeed()) var podOwner2 = &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ @@ -348,8 +344,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() { } podOwner2.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Kind: "Pod"}) - err = SetOwnerAnnotations(podOwner2, newPod) - Expect(err).ToNot(HaveOccurred()) + Expect(SetOwnerAnnotations(podOwner2, newPod)).To(Succeed()) evt := event.UpdateEvent{ ObjectOld: pod, @@ -389,8 +384,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() { }, } - err := SetOwnerAnnotations(podOwner, nd) - Expect(err).ToNot(HaveOccurred()) + Expect(SetOwnerAnnotations(podOwner, nd)).To(Succeed()) expected := map[string]string{ "my-test-annotation": "should-keep", @@ -409,8 +403,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() { } podOwner.SetGroupVersionKind(schema.GroupVersionKind{Group: "Pod", Kind: ""}) - err := SetOwnerAnnotations(podOwner, nd) - Expect(err).To(HaveOccurred()) + Expect(SetOwnerAnnotations(podOwner, nd)).ToNot(Succeed()) }) It("should return an error when the owner Name is not set", func() { nd := &corev1.Node{ @@ -426,8 +419,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() { } ownerNew.SetGroupVersionKind(schema.GroupVersionKind{Group: "Pod", Kind: ""}) - err := SetOwnerAnnotations(ownerNew, nd) - Expect(err).To(HaveOccurred()) + Expect(SetOwnerAnnotations(ownerNew, nd)).ToNot(Succeed()) }) }) }) diff --git a/leader/leader_test.go b/leader/leader_test.go index 7d67a0a..4798795 100644 --- a/leader/leader_test.go +++ b/leader/leader_test.go @@ -16,7 +16,6 @@ package leader import ( "context" - "errors" "os" . "github.com/onsi/ginkgo/v2" @@ -65,8 +64,7 @@ var _ = Describe("Leader election", func() { }) It("should return an error when POD_NAME is not set", func() { os.Unsetenv("POD_NAME") - err := Become(context.TODO(), "leader-test") - Expect(err).Should(HaveOccurred()) + Expect(Become(context.TODO(), "leader-test")).ShouldNot(Succeed()) }) It("should return an ErrNoNamespace", func() { os.Setenv("POD_NAME", "leader-test") @@ -74,9 +72,7 @@ var _ = Describe("Leader election", func() { return "", ErrNoNamespace } err := Become(context.TODO(), "leader-test", WithClient(client)) - Expect(err).Should(HaveOccurred()) - Expect(err).To(Equal(ErrNoNamespace)) - Expect(errors.Is(err, ErrNoNamespace)).To(BeTrue()) + Expect(err).Should(MatchError(ErrNoNamespace)) }) It("should not return an error", func() { os.Setenv("POD_NAME", "leader-test") @@ -84,8 +80,7 @@ var _ = Describe("Leader election", func() { return "testns", nil } - err := Become(context.TODO(), "leader-test", WithClient(client)) - Expect(err).ShouldNot(HaveOccurred()) + Expect(Become(context.TODO(), "leader-test", WithClient(client))).To(Succeed()) }) }) Describe("isPodEvicted", func() { @@ -195,13 +190,11 @@ var _ = Describe("Leader election", func() { }) It("should return an error if no node is found", func() { node := corev1.Node{} - err := getNode(context.TODO(), client, "", &node) - Expect(err).Should(HaveOccurred()) + Expect(getNode(context.TODO(), client, "", &node)).ToNot(Succeed()) }) It("should return the node with the given name", func() { node := corev1.Node{} - err := getNode(context.TODO(), client, "mynode", &node) - Expect(err).ShouldNot(HaveOccurred()) + Expect(getNode(context.TODO(), client, "mynode", &node)).Should(Succeed()) Expect(node.TypeMeta.APIVersion).To(Equal("v1")) Expect(node.TypeMeta.Kind).To(Equal("Node")) }) @@ -293,28 +286,23 @@ var _ = Describe("Leader election", func() { }) It("should return an error if existing is not found", func() { client = fake.NewClientBuilder().WithObjects(pod).Build() - err := deleteLeader(context.TODO(), client, pod, configmap) - Expect(err).Should(HaveOccurred()) + Expect(deleteLeader(context.TODO(), client, pod, configmap)).ToNot(Succeed()) }) It("should return an error if pod is not found", func() { client = fake.NewClientBuilder().WithObjects(configmap).Build() - err := deleteLeader(context.TODO(), client, pod, configmap) - Expect(err).Should(HaveOccurred()) + Expect(deleteLeader(context.TODO(), client, pod, configmap)).ToNot(Succeed()) }) It("should return an error if pod is nil", func() { client = fake.NewClientBuilder().WithObjects(pod, configmap).Build() - err := deleteLeader(context.TODO(), client, nil, configmap) - Expect(err).Should(HaveOccurred()) + Expect(deleteLeader(context.TODO(), client, nil, configmap)).ToNot(Succeed()) }) It("should return an error if configmap is nil", func() { client = fake.NewClientBuilder().WithObjects(pod, configmap).Build() - err := deleteLeader(context.TODO(), client, pod, nil) - Expect(err).Should(HaveOccurred()) + Expect(deleteLeader(context.TODO(), client, pod, nil)).ToNot(Succeed()) }) It("should return nil if pod and configmap exists and configmap's owner is the pod", func() { client = fake.NewClientBuilder().WithObjects(pod, configmap).Build() - err := deleteLeader(context.TODO(), client, pod, configmap) - Expect(err).ShouldNot(HaveOccurred()) + Expect(deleteLeader(context.TODO(), client, pod, configmap)).To(Succeed()) }) }) diff --git a/prune/prune_test.go b/prune/prune_test.go index 30e9c38..9922eec 100644 --- a/prune/prune_test.go +++ b/prune/prune_test.go @@ -61,11 +61,11 @@ var _ = Describe("Prune", func() { Describe("Unprunable", func() { Describe("Error()", func() { It("Should Return a String Representation of Unprunable", func() { - unpruneable := Unprunable{ + unpruneable := &Unprunable{ Obj: &fakeObj, Reason: "TestReason", } - Expect(unpruneable.Error()).To(Equal(fmt.Sprintf("unable to prune %s: %s", client.ObjectKeyFromObject(fakeObj), unpruneable.Reason))) + Expect(unpruneable).To(MatchError(fmt.Sprintf("unable to prune %s: %s", client.ObjectKeyFromObject(fakeObj), unpruneable.Reason))) }) }) }) @@ -131,8 +131,7 @@ var _ = Describe("Prune", func() { It("Should Error if schema.GroupVersionKind Parameter is empty", func() { // empty GVK struct pruner, err := NewPruner(fakeClient, schema.GroupVersionKind{}, myStrategy) - Expect(err).Should(HaveOccurred()) - Expect(err.Error()).Should(Equal("error when creating a new Pruner: gvk parameter can not be empty")) + Expect(err).Should(MatchError("error when creating a new Pruner: gvk parameter can not be empty")) Expect(pruner).Should(BeNil()) }) }) @@ -150,54 +149,46 @@ var _ = Describe("Prune", func() { } It("Should Prune Pods with Default IsPrunableFunc", func() { // Create the test resources - in this case Pods - err := createTestPods(fakeClient) - Expect(err).ShouldNot(HaveOccurred()) + Expect(createTestPods(fakeClient)).To(Succeed()) // Make sure the pod resources are properly created pods := &unstructured.UnstructuredList{} pods.SetGroupVersionKind(podGVK) - err = fakeClient.List(context.Background(), pods) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), pods)).To(Succeed()) Expect(pods.Items).Should(HaveLen(3)) testPruneWithDefaultIsPrunableFunc(podGVK) // Get a list of the Pods to make sure we have pruned the ones we expected - err = fakeClient.List(context.Background(), pods) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), pods)).To(Succeed()) Expect(pods.Items).Should(HaveLen(1)) }) It("Should Prune Jobs with Default IsPrunableFunc", func() { // Create the test resources - in this case Jobs - err := createTestJobs(fakeClient) - Expect(err).ShouldNot(HaveOccurred()) + Expect(createTestJobs(fakeClient)).To(Succeed()) // Make sure the job resources are properly created jobs := &unstructured.UnstructuredList{} jobs.SetGroupVersionKind(jobGVK) - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(3)) testPruneWithDefaultIsPrunableFunc(jobGVK) // Get a list of the job to make sure we have pruned the ones we expected - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(1)) }) It("Should Remove Resource When Using a Custom IsPrunableFunc", func() { // Create the test resources - in this case Jobs - err := createTestJobs(fakeClient) - Expect(err).ShouldNot(HaveOccurred()) + Expect(createTestJobs(fakeClient)).To(Succeed()) // Make sure the job resources are properly created jobs := &unstructured.UnstructuredList{} jobs.SetGroupVersionKind(jobGVK) - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(3)) pruner, err := NewPruner(fakeClient, jobGVK, myStrategy, WithLabels(appLabels), WithNamespace(namespace)) @@ -212,21 +203,18 @@ var _ = Describe("Prune", func() { Expect(prunedObjects).Should(HaveLen(2)) // Get a list of the jobs to make sure we have pruned the ones we expected - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(1)) }) It("Should Not Prune Resources when using a DryRunClient", func() { // Create the test resources - in this case Pods - err := createTestPods(fakeClient) - Expect(err).ShouldNot(HaveOccurred()) + Expect(createTestPods(fakeClient)).To(Succeed()) // Make sure the pod resources are properly created pods := &unstructured.UnstructuredList{} pods.SetGroupVersionKind(podGVK) - err = fakeClient.List(context.Background(), pods) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), pods)).To(Succeed()) Expect(pods.Items).Should(HaveLen(3)) dryRunClient := client.NewDryRunClient(fakeClient) @@ -239,21 +227,18 @@ var _ = Describe("Prune", func() { Expect(prunedObjects).Should(HaveLen(2)) // Get a list of the Pods to make sure we haven't pruned any - err = fakeClient.List(context.Background(), pods) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), pods)).To(Succeed()) Expect(pods.Items).Should(HaveLen(3)) }) It("Should Skip Pruning a Resource If IsPrunable Returns an Error of Type Unprunable", func() { // Create the test resources - in this case Jobs - err := createTestJobs(fakeClient) - Expect(err).ShouldNot(HaveOccurred()) + Expect(createTestJobs(fakeClient)).To(Succeed()) // Make sure the job resources are properly created jobs := &unstructured.UnstructuredList{} jobs.SetGroupVersionKind(jobGVK) - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(3)) pruner, err := NewPruner(fakeClient, jobGVK, myStrategy, WithLabels(appLabels), WithNamespace(namespace)) @@ -276,8 +261,7 @@ var _ = Describe("Prune", func() { Expect(prunedObjects).Should(BeEmpty()) // Get a list of the jobs to make sure we have pruned the ones we expected - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(3)) }) @@ -285,14 +269,12 @@ var _ = Describe("Prune", func() { Context("Returns an Error", func() { It("Should Return an Error if IsPrunableFunc Returns an Error That is not of Type Unprunable", func() { // Create the test resources - in this case Jobs - err := createTestJobs(fakeClient) - Expect(err).ShouldNot(HaveOccurred()) + Expect(createTestJobs(fakeClient)).To(Succeed()) // Make sure the job resources are properly created jobs := &unstructured.UnstructuredList{} jobs.SetGroupVersionKind(jobGVK) - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(3)) pruner, err := NewPruner(fakeClient, jobGVK, myStrategy, WithLabels(appLabels), WithNamespace(namespace)) @@ -308,26 +290,22 @@ var _ = Describe("Prune", func() { RegisterIsPrunableFunc(jobGVK, errorPrunableFunc) prunedObjects, err := pruner.Prune(context.Background()) - Expect(err).Should(HaveOccurred()) - Expect(err.Error()).Should(Equal("TEST")) + Expect(err).Should(MatchError("TEST")) Expect(prunedObjects).Should(BeEmpty()) // Get a list of the jobs to make sure we have pruned the ones we expected - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(3)) }) It("Should Return An Error If Strategy Function Returns An Error", func() { // Create the test resources - in this case Jobs - err := createTestJobs(fakeClient) - Expect(err).ShouldNot(HaveOccurred()) + Expect(createTestJobs(fakeClient)).To(Succeed()) // Make sure the job resources are properly created jobs := &unstructured.UnstructuredList{} jobs.SetGroupVersionKind(jobGVK) - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(3)) // strategy that will return an error @@ -343,26 +321,22 @@ var _ = Describe("Prune", func() { RegisterIsPrunableFunc(jobGVK, myIsPrunable) prunedObjects, err := pruner.Prune(context.Background()) - Expect(err).Should(HaveOccurred()) - Expect(err.Error()).Should(Equal("error determining prunable objects: TESTERROR")) + Expect(err).Should(MatchError("error determining prunable objects: TESTERROR")) Expect(prunedObjects).Should(BeNil()) // Get a list of the jobs to make sure we have pruned the ones we expected - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(3)) }) It("Should Return an Error if it can not Prune a Resource", func() { // Create the test resources - in this case Jobs - err := createTestJobs(fakeClient) - Expect(err).ShouldNot(HaveOccurred()) + Expect(createTestJobs(fakeClient)).To(Succeed()) // Make sure the job resources are properly created jobs := &unstructured.UnstructuredList{} jobs.SetGroupVersionKind(jobGVK) - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(HaveLen(3)) pruner, err := NewPruner(fakeClient, jobGVK, myStrategy, WithLabels(appLabels), WithNamespace(namespace)) @@ -380,13 +354,11 @@ var _ = Describe("Prune", func() { RegisterIsPrunableFunc(jobGVK, prunableFunc) prunedObjects, err := pruner.Prune(context.Background()) - Expect(err).Should(HaveOccurred()) - Expect(err.Error()).Should(ContainSubstring("error pruning object: jobs.batch \"churro1\" not found")) + Expect(err).Should(MatchError(ContainSubstring("error pruning object: jobs.batch \"churro1\" not found"))) Expect(prunedObjects).Should(BeEmpty()) // Get a list of the jobs to make sure we have pruned the ones we expected - err = fakeClient.List(context.Background(), jobs) - Expect(err).ShouldNot(HaveOccurred()) + Expect(fakeClient.List(context.Background(), jobs)).To(Succeed()) Expect(jobs.Items).Should(BeEmpty()) }) @@ -437,8 +409,7 @@ var _ = Describe("Prune", func() { pod.SetGroupVersionKind(podGVK) // Run it through DefaultPodIsPrunable - err := DefaultPodIsPrunable(pod) - Expect(err).ShouldNot(HaveOccurred()) + Expect(DefaultPodIsPrunable(pod)).To(Succeed()) }) It("Should Panic When client.Object is not of type 'Pod'", func() { @@ -467,12 +438,11 @@ var _ = Describe("Prune", func() { // Run it through DefaultPodIsPrunable err := DefaultPodIsPrunable(pod) - Expect(err).Should(HaveOccurred()) + Expect(err).Should(MatchError(fmt.Sprintf("unable to prune %s: Pod has not succeeded", client.ObjectKeyFromObject(pod)))) var expectErr *Unprunable Expect(errors.As(err, &expectErr)).Should(BeTrue()) Expect(expectErr.Reason).Should(Equal("Pod has not succeeded")) Expect(expectErr.Obj).ShouldNot(BeNil()) - Expect(err.Error()).Should(Equal(fmt.Sprintf("unable to prune %s: Pod has not succeeded", client.ObjectKeyFromObject(pod)))) }) }) @@ -492,8 +462,7 @@ var _ = Describe("Prune", func() { job.SetGroupVersionKind(jobGVK) // Run it through DefaultJobIsPrunable - err := DefaultJobIsPrunable(job) - Expect(err).ShouldNot(HaveOccurred()) + Expect(DefaultJobIsPrunable(job)).To(Succeed()) }) It("Should Return An Error When Kind Is Not 'Job'", func() { @@ -522,12 +491,11 @@ var _ = Describe("Prune", func() { // Run it through DefaultJobIsPrunable err := DefaultJobIsPrunable(job) - Expect(err).Should(HaveOccurred()) + Expect(err).Should(MatchError(ContainSubstring(fmt.Sprintf("unable to prune %s: Job has not completed", client.ObjectKeyFromObject(job))))) var expectErr *Unprunable Expect(errors.As(err, &expectErr)).Should(BeTrue()) Expect(expectErr.Reason).Should(Equal("Job has not completed")) Expect(expectErr.Obj).ShouldNot(BeNil()) - Expect(err.Error()).Should(ContainSubstring(fmt.Sprintf("unable to prune %s: Job has not completed", client.ObjectKeyFromObject(job)))) }) })