Skip to content

Commit

Permalink
Refactor filter receiver tests (#5991)
Browse files Browse the repository at this point in the history
* Refactor filter receiver tests

Signed-off-by: Ahmed Abdalla <aabdelre@redhat.com>

* missed cleanup

Signed-off-by: Ahmed Abdalla <aabdelre@redhat.com>

* set Subscriber URI with nil

Signed-off-by: Ahmed Abdalla <aabdelre@redhat.com>
  • Loading branch information
devguyio authored Dec 13, 2021
1 parent 5601111 commit b3bccaf
Showing 1 changed file with 70 additions and 64 deletions.
134 changes: 70 additions & 64 deletions pkg/broker/filter/filter_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ import (
"go.uber.org/zap/zaptest"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"knative.dev/pkg/apis"

eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
broker "knative.dev/eventing/pkg/broker"
"knative.dev/eventing/pkg/broker"
reconcilertesting "knative.dev/eventing/pkg/reconciler/testing/v1"
)

Expand All @@ -63,6 +64,8 @@ var (
validPath = fmt.Sprintf("/triggers/%s/%s/%s", testNS, triggerName, triggerUID)
)

type TriggerOption func(trigger *eventingv1.Trigger)

func TestReceiver(t *testing.T) {
testCases := map[string]struct {
triggers []*eventingv1.Trigger
Expand Down Expand Up @@ -103,46 +106,54 @@ func TestReceiver(t *testing.T) {
},
"Trigger doesn't have SubscriberURI": {
triggers: []*eventingv1.Trigger{
makeTriggerWithoutSubscriberURI(),
makeTrigger(withoutSubscriberURI()),
},
expectedStatus: http.StatusBadRequest,
expectedEventCount: true,
},
"Trigger without a Filter": {
triggers: []*eventingv1.Trigger{
makeTriggerWithoutFilter(),
makeTrigger(),
},
expectedDispatch: true,
expectedEventCount: true,
expectedEventDispatchTime: true,
},
"No TTL": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("some-other-type", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{
Attributes: map[string]string{"type": "some-other-type"},
})),
},
event: makeEventWithoutTTL(),
},
"Wrong type": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("some-other-type", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{
Attributes: map[string]string{"type": "some-other-type"},
})),
},
expectedEventCount: false,
},
"Wrong source": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "some-other-source")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{
Attributes: map[string]string{"source": "some-other-source"},
})),
},
expectedEventCount: false,
},
"Wrong extension": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributesAndExtension("", "", "some-other-extension")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{
Attributes: map[string]string{extensionName: "some-other-extension"},
})),
},
expectedEventCount: false,
},
"Dispatch failed": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
requestFails: true,
expectedStatus: http.StatusBadRequest,
Expand All @@ -152,31 +163,38 @@ func TestReceiver(t *testing.T) {
},
"GetTrigger fails": {
triggers: []*eventingv1.Trigger{
makeTriggerWithDifferentUID(makeTriggerFilterWithAttributes("", "")),
makeTrigger(
withAttributesFilter(&eventingv1.TriggerFilter{}),
withUID("wrongone"),
),
},
expectedDispatch: false,
expectedEventCount: false,
expectedEventDispatchTime: false,
},
"Dispatch succeeded - Any": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
expectedDispatch: true,
expectedEventCount: true,
expectedEventDispatchTime: true,
},
"Dispatch succeeded - Source with type": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes(eventType, eventSource)),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{
Attributes: map[string]string{"type": eventType, "source": eventSource},
})),
},
expectedDispatch: true,
expectedEventCount: true,
expectedEventDispatchTime: true,
},
"Dispatch succeeded - Source, type and extensions": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributesAndExtension(eventType, eventSource, extensionValue)),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{
Attributes: map[string]string{"type": eventType, "source": eventSource, extensionName: extensionValue},
})),
},
event: makeEventWithExtension(extensionName, extensionValue),
expectedDispatch: true,
Expand All @@ -185,7 +203,7 @@ func TestReceiver(t *testing.T) {
},
"Dispatch succeeded - Any - Arrival extension": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
event: makeEventWithExtension(broker.EventArrivalTime, "2019-08-26T23:38:17.834384404Z"),
expectedDispatch: true,
Expand All @@ -195,14 +213,19 @@ func TestReceiver(t *testing.T) {
},
"Wrong Extension with correct source and type": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributesAndExtension(eventType, eventSource, "some-other-extension-value")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{
Attributes: map[string]string{
"type": eventType,
"source": eventSource,
extensionName: "some-other-extension-value"},
})),
},
event: makeEventWithExtension(extensionName, extensionValue),
expectedEventCount: false,
},
"Returned Cloud Event": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
expectedDispatch: true,
expectedEventCount: true,
Expand All @@ -211,7 +234,7 @@ func TestReceiver(t *testing.T) {
},
"Error From Trigger": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
event: makeEvent(),
requestFails: true,
Expand All @@ -223,7 +246,7 @@ func TestReceiver(t *testing.T) {
},
"Returned Cloud Event with custom headers": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
request: func() *http.Request {
e := makeEvent()
Expand Down Expand Up @@ -262,7 +285,7 @@ func TestReceiver(t *testing.T) {
},
"Maintain `Prefer: reply` header when it is provided in the original request": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
request: func() *http.Request {
e := makeEvent()
Expand All @@ -287,7 +310,7 @@ func TestReceiver(t *testing.T) {
},
"Add `Prefer: reply` header when it isn't provided in the original request": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
request: func() *http.Request {
e := makeEvent()
Expand All @@ -308,7 +331,7 @@ func TestReceiver(t *testing.T) {
},
"Returned non empty non event response": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
expectedDispatch: true,
expectedEventCount: true,
Expand All @@ -318,7 +341,7 @@ func TestReceiver(t *testing.T) {
},
"Returned malformed Cloud Event": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
expectedDispatch: true,
expectedEventCount: true,
Expand All @@ -328,7 +351,7 @@ func TestReceiver(t *testing.T) {
},
"Returned malformed structured Cloud Event": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
expectedDispatch: true,
expectedEventCount: true,
Expand All @@ -338,7 +361,7 @@ func TestReceiver(t *testing.T) {
},
"Returned empty body 200": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
expectedDispatch: true,
expectedEventCount: true,
Expand All @@ -348,7 +371,7 @@ func TestReceiver(t *testing.T) {
},
"Returned empty body 202": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
expectedDispatch: true,
expectedEventCount: true,
Expand All @@ -358,7 +381,7 @@ func TestReceiver(t *testing.T) {
},
"Proxy CloudEvent response headers": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
expectedDispatch: true,
expectedEventCount: true,
Expand All @@ -368,7 +391,7 @@ func TestReceiver(t *testing.T) {
},
"Proxy empty non event response headers": {
triggers: []*eventingv1.Trigger{
makeTrigger(makeTriggerFilterWithAttributes("", "")),
makeTrigger(withAttributesFilter(&eventingv1.TriggerFilter{})),
},
expectedDispatch: true,
expectedEventCount: true,
Expand Down Expand Up @@ -611,33 +634,8 @@ func (h *fakeHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
}
}

func makeTriggerFilterWithAttributes(t, s string) *eventingv1.TriggerFilter {
return &eventingv1.TriggerFilter{
Attributes: eventingv1.TriggerFilterAttributes{
"type": t,
"source": s,
},
}
}

func makeTriggerFilterWithAttributesAndExtension(t, s, e string) *eventingv1.TriggerFilter {
return &eventingv1.TriggerFilter{
Attributes: eventingv1.TriggerFilterAttributes{
"type": t,
"source": s,
extensionName: e,
},
}
}

func makeTriggerWithDifferentUID(filter *eventingv1.TriggerFilter) *eventingv1.Trigger {
t := makeTrigger(filter)
t.ObjectMeta.UID = "wrongone"
return t
}

func makeTrigger(filter *eventingv1.TriggerFilter) *eventingv1.Trigger {
return &eventingv1.Trigger{
func makeTrigger(options ...TriggerOption) *eventingv1.Trigger {
t := &eventingv1.Trigger{
TypeMeta: metav1.TypeMeta{
APIVersion: "eventing.knative.dev/v1",
Kind: "Trigger",
Expand All @@ -647,25 +645,33 @@ func makeTrigger(filter *eventingv1.TriggerFilter) *eventingv1.Trigger {
Name: triggerName,
UID: triggerUID,
},
Spec: eventingv1.TriggerSpec{
Filter: filter,
},
Spec: eventingv1.TriggerSpec{},
Status: eventingv1.TriggerStatus{
SubscriberURI: &apis.URL{Host: "toBeReplaced"},
},
}
for _, opt := range options {
opt(t)
}
return t
}

func makeTriggerWithoutFilter() *eventingv1.Trigger {
t := makeTrigger(makeTriggerFilterWithAttributes("", ""))
t.Spec.Filter = nil
return t
func withUID(uid string) TriggerOption {
return func(t *eventingv1.Trigger) {
t.ObjectMeta.UID = types.UID(uid)
}
}

func makeTriggerWithoutSubscriberURI() *eventingv1.Trigger {
t := makeTrigger(makeTriggerFilterWithAttributes("", ""))
t.Status = eventingv1.TriggerStatus{}
return t
func withAttributesFilter(filter *eventingv1.TriggerFilter) TriggerOption {
return func(t *eventingv1.Trigger) {
t.Spec.Filter = filter
}
}

func withoutSubscriberURI() TriggerOption {
return func(t *eventingv1.Trigger) {
t.Status.SubscriberURI = nil
}
}

func makeEventWithoutTTL() *cloudevents.Event {
Expand Down

0 comments on commit b3bccaf

Please sign in to comment.