Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add tests in controller that use source.Channel #1373

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions pkg/internal/controller/controller_test.go
Expand Up @@ -28,16 +28,19 @@ import (
dto "github.com/prometheus/client_model/go"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
"sigs.k8s.io/controller-runtime/pkg/controller/controllertest"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
"sigs.k8s.io/controller-runtime/pkg/internal/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/source"
)

Expand Down Expand Up @@ -169,6 +172,83 @@ var _ = Describe("controller", func() {
close(done)
}, 10.0)

It("should process events from source.Channel", func(done Done) {
// channel to be closed when event is processed
processed := make(chan struct{})
// source channel to be injected
ch := make(chan event.GenericEvent, 1)

ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

// event to be sent to the channel
p := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "bar"},
}
evt := event.GenericEvent{
Object: p,
}

ins := &source.Channel{Source: ch}
ins.DestBufferSize = 1
Expect(inject.StopChannelInto(ctx.Done(), ins)).To(BeTrue())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just deprecated the injection, lets not add more code that uses it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While starting the source (with source.channel) we check if the stop channel has been injected and return an error if not (here). I think we may have to modify the implementation while starting source.channel to remove this or is there any other way to go about this?


// send the event to the channel
ch <- evt

ctrl.startWatches = []watchDescription{{
src: ins,
handler: handler.Funcs{
GenericFunc: func(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
defer GinkgoRecover()
close(processed)
},
},
}}

go func() {
defer GinkgoRecover()
Expect(ctrl.Start(ctx)).To(Succeed())
}()
<-processed
close(done)
})

It("should error when channel is passed as a source but stop channel is not injected", func(done Done) {
ch := make(chan event.GenericEvent)
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

ins := &source.Channel{Source: ch}
ctrl.startWatches = []watchDescription{{
src: ins,
}}

e := ctrl.Start(ctx)

Expect(e).NotTo(BeNil())
Expect(e.Error()).To(ContainSubstring("must call InjectStop on Channel before calling Start"))
close(done)
})

It("should error when channel source is not specified", func(done Done) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

ins := &source.Channel{}
Expect(inject.StopChannelInto(make(<-chan struct{}), ins)).To(BeTrue())

ctrl.startWatches = []watchDescription{{
src: &source.Channel{},
}}

e := ctrl.Start(ctx)
Expect(e).NotTo(BeNil())
Expect(e.Error()).To(ContainSubstring("must specify Channel.Source"))

close(done)
})

It("should call Start on sources with the appropriate EventHandler, Queue, and Predicates", func() {
pr1 := &predicate.Funcs{}
pr2 := &predicate.Funcs{}
Expand Down