Skip to content

Commit

Permalink
remove debug line, return error instead of bool
Browse files Browse the repository at this point in the history
Signed-off-by: everettraven <everettraven@gmail.com>
  • Loading branch information
everettraven committed May 20, 2024
1 parent 56f26bb commit daf9fb4
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions pkg/reconciler/internal/hook/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ var _ = Describe("Hook", func() {
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(2))
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeNil())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeNil())
})

Context("when the owner is cluster-scoped", func() {
Expand All @@ -153,8 +153,8 @@ var _ = Describe("Hook", func() {
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(2))
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeNil())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeNil())
})
It("should watch cluster-scoped resources with ownerRef handler", func() {
rel = &release.Release{
Expand All @@ -163,8 +163,8 @@ var _ = Describe("Hook", func() {
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(2))
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeNil())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeNil())
})
It("should watch resource policy keep resources with annotation handler", func() {
rel = &release.Release{
Expand All @@ -173,10 +173,10 @@ var _ = Describe("Hook", func() {
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(4))
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[2].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[3].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeNil())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeNil())
Expect(validateSourceHandlerType(c.WatchCalls[2].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeNil())
Expect(validateSourceHandlerType(c.WatchCalls[3].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeNil())
})
})

Expand All @@ -200,7 +200,7 @@ var _ = Describe("Hook", func() {
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(1))
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeNil())
})
It("should watch cluster-scoped resources with annotation handler", func() {
rel = &release.Release{
Expand All @@ -209,6 +209,7 @@ var _ = Describe("Hook", func() {
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(1))
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeNil())
})
It("should watch namespace-scoped resources in a different namespace with annotation handler", func() {
rel = &release.Release{
Expand All @@ -217,7 +218,7 @@ var _ = Describe("Hook", func() {
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(1))
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeNil())
})
It("should watch resource policy keep resources with annotation handler", func() {
rel = &release.Release{
Expand All @@ -226,9 +227,9 @@ var _ = Describe("Hook", func() {
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(3))
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[2].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeNil())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeNil())
Expect(validateSourceHandlerType(c.WatchCalls[2].Source, &sdkhandler.EnqueueRequestForAnnotation[*unstructured.Unstructured]{})).To(BeNil())
})
It("should iterate the kind list and be able to set watches on each item", func() {
rel = &release.Release{
Expand All @@ -237,8 +238,8 @@ var _ = Describe("Hook", func() {
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(2))
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeTrue())
Expect(validateSourceHandlerType(c.WatchCalls[0].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeNil())
Expect(validateSourceHandlerType(c.WatchCalls[1].Source, handler.TypedEnqueueRequestForOwner[*unstructured.Unstructured](sch, rm, owner, handler.OnlyControllerOwner()))).To(BeNil())
})
It("should error when unable to list objects", func() {
rel = &release.Release{
Expand All @@ -256,7 +257,7 @@ var _ = Describe("Hook", func() {
// validateSourceHandlerType takes in a source.Source and uses reflection to determine
// if the handler used by the source matches the expected type.
// It is assumed that the source.Source was created via the source.Kind() function.
func validateSourceHandlerType(s source.Source, expected interface{}) bool {
func validateSourceHandlerType(s source.Source, expected interface{}) error {
sourceVal := reflect.Indirect(reflect.ValueOf(s))
handlerFieldVal := sourceVal.FieldByName("Handler")

Expand All @@ -266,8 +267,10 @@ func validateSourceHandlerType(s source.Source, expected interface{}) bool {
expectedValue := reflect.Indirect(reflect.ValueOf(expected))

expectedType := expectedValue.Type()
fmt.Println("XXX DEBUG", handlerType, expectedType, handlerType == expectedType)
return handlerType == expectedType
if handlerType != expectedType {
return fmt.Errorf("detected source.Source handler type %q does not match expected type %q", handlerType, expectedType)
}
return nil
}

var (
Expand Down

0 comments on commit daf9fb4

Please sign in to comment.