Преглед изворни кода

Test: API not found edge case

Signed-off-by: Marcin Kubica <marcin.kubica@engineerbetter.com>
Co-authored-by: William Young <will.young@engineerbetter.com>
Co-authored-by: Tom Godkin <tom.godkin@engineerbetter.com>
Co-authored-by: James Cleveland <james.cleveland@engineerbetter.com>
Dominic Meddick пре 4 година
родитељ
комит
854525c244

+ 25 - 7
pkg/controllers/secretsink/secretsink_controller_test.go

@@ -56,14 +56,32 @@ var _ = Describe("#Reconcile", func() {
 		Expect(patch.Type()).To(Equal(types.MergePatchType))
 	})
 
-	It("notFound", func() {
-		client.GetReturnsOnCall(0, errors.New("UnknownError"))
+	When("an error returns in get", func() {
 
-		namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
-		_, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
+		BeforeEach(func() {
+			client.GetReturnsOnCall(0, errors.New("UnknownError"))
+		})
+		It("returns the error", func() {
 
-		Expect(err).To(HaveOccurred())
-		Expect(client.GetCallCount()).To(Equal(1))
-		Expect(client.StatusCallCount()).To(Equal(0))
+			namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
+			_, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
+
+			Expect(err).To(HaveOccurred())
+			Expect(client.GetCallCount()).To(Equal(1))
+			Expect(client.StatusCallCount()).To(Equal(0))
+		})
+	})
+
+	When("an object is not found", func() {
+		BeforeEach(func() {
+			err := statusErrorNotFound{}
+			client.GetReturns(err)
+		})
+		It("returns an empty result without error", func() {
+			namspacedName := types.NamespacedName{Namespace: "foo", Name: "Bar"}
+			_, err := reconciler.Reconcile(context.Background(), ctrl.Request{NamespacedName: namspacedName})
+
+			Expect(err).To(BeNil())
+		})
 	})
 })

+ 18 - 0
pkg/controllers/secretsink/suite_test.go

@@ -19,9 +19,27 @@ import (
 
 	. "github.com/onsi/ginkgo/v2"
 	. "github.com/onsi/gomega"
+	"k8s.io/apimachinery/pkg/api/errors"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
 
 func TestAPIs(t *testing.T) {
 	RegisterFailHandler(Fail)
 	RunSpecs(t, "Controller Suite")
 }
+
+type statusErrorNotFound struct {
+}
+
+func (statusErrorNotFound) Status() metav1.Status {
+	return v1.Status{
+		Reason: metav1.StatusReasonNotFound,
+	}
+}
+
+func (statusErrorNotFound) Error() string {
+	return "Blurb"
+}
+
+var _ errors.APIStatus = statusErrorNotFound{}