Skip to content

Commit

Permalink
Matchers
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Guardiani <[email protected]>
  • Loading branch information
slinkydeveloper committed Jun 24, 2020
1 parent a4e8873 commit ce9a058
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 69 deletions.
3 changes: 1 addition & 2 deletions test/conformance/helpers/broker_tracing_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"knative.dev/eventing/pkg/utils"
tracinghelper "knative.dev/eventing/test/conformance/helpers/tracing"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/recordevents"
"knative.dev/eventing/test/lib/resources"
"knative.dev/eventing/test/lib/resources/sender"
)
Expand Down Expand Up @@ -205,7 +204,7 @@ func setupBrokerTracing(brokerClass string) SetupTracingTestInfrastructureFunc {
return expected, cetest.AllOf(
cetest.HasSource(senderName),
cetest.HasId(eventID),
recordevents.DataContains(eventBody),
cetest.DataContains(eventBody),
)
}
}
Expand Down
3 changes: 1 addition & 2 deletions test/conformance/helpers/channel_tracing_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

tracinghelper "knative.dev/eventing/test/conformance/helpers/tracing"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/recordevents"
"knative.dev/eventing/test/lib/resources"
"knative.dev/eventing/test/lib/resources/sender"
)
Expand Down Expand Up @@ -228,6 +227,6 @@ func setupChannelTracingWithReply(
return expected, cetest.AllOf(
cetest.HasSource(senderName),
cetest.HasId(eventID),
recordevents.DataContains(body),
cetest.DataContains(body),
)
}
5 changes: 3 additions & 2 deletions test/e2e/helpers/parallel_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

cloudevents "github.com/cloudevents/sdk-go/v2"
duckv1 "knative.dev/pkg/apis/duck/v1"

"knative.dev/eventing/pkg/apis/flows/v1beta1"
messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1"
eventingtesting "knative.dev/eventing/pkg/reconciler/testing/v1beta1"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/recordevents"
"knative.dev/eventing/test/lib/resources"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

type branchConfig struct {
Expand Down Expand Up @@ -136,7 +137,7 @@ func ParallelTestHelper(t *testing.T,
// verify the logger service receives the correct transformed event
eventTracker.AssertExact(1, recordevents.MatchEvent(
HasSource(eventSource),
recordevents.DataContains(tc.expected),
DataContains(tc.expected),
))

eventTracker.Cleanup()
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/helpers/sequence_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import (
"github.com/google/uuid"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

duckv1 "knative.dev/pkg/apis/duck/v1"

"knative.dev/eventing/pkg/apis/flows/v1beta1"
messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1"
eventingtesting "knative.dev/eventing/pkg/reconciler/testing/v1beta1"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/recordevents"
"knative.dev/eventing/test/lib/resources"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

func SequenceTestHelper(t *testing.T,
Expand Down Expand Up @@ -142,7 +143,7 @@ func SequenceTestHelper(t *testing.T,
}
eventTracker.AssertAtLeast(1, recordevents.MatchEvent(
cetest.HasSource(eventSource),
recordevents.DataContains(expectedMsg),
cetest.DataContains(expectedMsg),
))
})
}
3 changes: 2 additions & 1 deletion test/e2e/source_api_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"
"time"

cetest "github.com/cloudevents/sdk-go/v2/test"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -172,7 +173,7 @@ func TestApiServerSource(t *testing.T) {
eventTracker.AssertNot(recordevents.Any())
} else {
eventTracker.AssertAtLeast(1, recordevents.MatchEvent(
recordevents.DataContains(tc.expected),
cetest.DataContains(tc.expected),
))
}
})
Expand Down
60 changes: 0 additions & 60 deletions test/lib/recordevents/event_info_matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package recordevents

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -102,62 +101,3 @@ func MatchHeartBeatsImageMessage(expectedMsg string) cetest.EventMatcher {
},
)
}

// DataContains matches that the data field of the event, converted to a string, contains the provided string
func DataContains(expectedContainedString string) cetest.EventMatcher {
return func(have cloudevents.Event) error {
dataAsString := string(have.Data())
if !strings.Contains(dataAsString, expectedContainedString) {
return fmt.Errorf("data '%s' doesn't contain '%s'", dataAsString, expectedContainedString)
}
return nil
}
}

// AnyOf returns a matcher which match if at least one of the provided matchers matches
func AnyOf(matchers ...cetest.EventMatcher) cetest.EventMatcher {
return func(have cloudevents.Event) error {
var errs []error
for _, m := range matchers {
if err := m(have); err == nil {
return nil
} else {
errs = append(errs, err)
}
}
var sb strings.Builder
sb.WriteString("Cannot match any of the provided matchers\n")
for i, err := range errs {
sb.WriteString(fmt.Sprintf("%d: %s\n", i+1, err))
}
return errors.New(sb.String())
}
}

// ContainsExactlyExtensions checks if the event contains only the provided extension names and no more
func ContainsExactlyExtensions(exts ...string) cetest.EventMatcher {
return func(have cloudevents.Event) error {
// Copy in a temporary set first
extsInEvent := map[string]struct{}{}
for k, _ := range have.Extensions() {
extsInEvent[k] = struct{}{}
}

for _, ext := range exts {
if _, ok := have.Extensions()[ext]; !ok {
return fmt.Errorf("expecting extension '%s'", ext)
} else {
delete(extsInEvent, ext)
}
}

if len(extsInEvent) != 0 {
var unexpectedKeys []string
for k, _ := range extsInEvent {
unexpectedKeys = append(unexpectedKeys, k)
}
return fmt.Errorf("not expecting extensions '%v'", unexpectedKeys)
}
return nil
}
}

0 comments on commit ce9a058

Please sign in to comment.