Skip to content

Commit

Permalink
Add tests for additional ingress error cases (#5281)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Moss authored Apr 19, 2021
1 parent facbd02 commit 56631a5
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions pkg/broker/ingress/ingress_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
nethttp "net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand All @@ -37,6 +38,7 @@ import (
broker "knative.dev/eventing/pkg/broker"
"knative.dev/eventing/pkg/kncloudevents"
reconcilertestingv1 "knative.dev/eventing/pkg/reconciler/testing/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

const (
Expand Down Expand Up @@ -148,6 +150,31 @@ func TestHandler_ServeHTTP(t *testing.T) {
makeBroker("name", "ns"),
},
},
{
name: "malformed event",
method: nethttp.MethodPost,
uri: "/ns/name",
body: strings.NewReader("not an event"),
statusCode: nethttp.StatusBadRequest,
handler: handler(),
reporter: &mockReporter{},
brokers: []*eventingv1.Broker{
makeBroker("name", "ns"),
},
},
{
name: "no broker annotations",
method: nethttp.MethodPost,
uri: "/ns/name",
body: getValidEvent(),
statusCode: nethttp.StatusInternalServerError,
handler: handler(),
reporter: &mockReporter{StatusCode: nethttp.StatusInternalServerError, EventDispatchTimeReported: true},
defaulter: broker.TTLDefaulter(logger, 100),
brokers: []*eventingv1.Broker{
withUninitializedAnnotations(makeBroker("name", "ns")),
},
},
{
name: "root request URI",
method: nethttp.MethodPost,
Expand Down Expand Up @@ -203,16 +230,19 @@ func TestHandler_ServeHTTP(t *testing.T) {
request.Header.Add(cehttp.ContentType, event.ApplicationCloudEventsJSON)
}

annotatedBrokers := make([]runtime.Object, 0, len(tc.brokers))
brokers := make([]runtime.Object, 0, len(tc.brokers))
for _, b := range tc.brokers {
// Write the channel address in the broker status annotation
if b.Status.Annotations == nil {
b.Status.Annotations = make(map[string]string, 1)
// Write the channel address in the broker status annotation unless explicitly set to nil
if b.Status.Annotations != nil {
if _, set := b.Status.Annotations[eventing.BrokerChannelAddressStatusAnnotationKey]; !set {
b.Status.Annotations = map[string]string{
eventing.BrokerChannelAddressStatusAnnotationKey: s.URL,
}
}
}
b.Status.Annotations[eventing.BrokerChannelAddressStatusAnnotationKey] = s.URL
annotatedBrokers = append(annotatedBrokers, b)
brokers = append(brokers, b)
}
listers := reconcilertestingv1.NewListers(annotatedBrokers)
listers := reconcilertestingv1.NewListers(brokers)
sender, _ := kncloudevents.NewHTTPMessageSenderWithTarget("")
h := &Handler{
Sender: sender,
Expand Down Expand Up @@ -296,5 +326,15 @@ func makeBroker(name, namespace string) *eventingv1.Broker {
Name: name,
},
Spec: eventingv1.BrokerSpec{},
Status: eventingv1.BrokerStatus{
Status: duckv1.Status{
Annotations: map[string]string{},
},
},
}
}

func withUninitializedAnnotations(b *eventingv1.Broker) *eventingv1.Broker {
b.Status.Annotations = nil
return b
}

0 comments on commit 56631a5

Please sign in to comment.