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

[WIP] Add reconciler test for EventType references #7114

Closed
wants to merge 9 commits into from
49 changes: 49 additions & 0 deletions test/rekt/eventtype_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//go:build e2e
// +build e2e

/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rekt

import (
"testing"
"time"

"eventing/test/rekt/features/eventtype"
Copy link
Member Author

Choose a reason for hiding this comment

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

Question: I added the new module eventtype in rekt. How should I import the module here?

Please lmk if this is not the right way to approach this ticket.

/cc @matzew @pierDipi @creydr

Copy link
Member

Choose a reason for hiding this comment

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

Seems you're using an incorrect import path. The knative modules are all under knative.dev/<repo>/... (no matter if it is a knative-sandbox project or not). So in this case it would be "knative.dev/eventing/test/rekt/features/eventtype"

Copy link
Member

Choose a reason for hiding this comment

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

Regarding the approach: In the test files (under test/rekt//_test.go), we usually setup only the environment and call the feature test (as you have done). In the feature test (test/rekt/features/) we have the according test implementation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried that before. If I use knative.dev/eventing/test/rekt/features/eventtype, it will throw this error

test/rekt/eventtype_test.go:47:32: undefined: eventtype.eventTypeWithBrokerAsReference

Does importing the packages that starting with knative.dev/*** will actually download the latest knative package from the source? Because this eventTypeWithBrokerAsReference is a new function that I added in this PR, it hasn't been merged into the main branch. @creydr

Copy link
Member

Choose a reason for hiding this comment

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

It's probably, because you have to make eventTypeWithBrokerAsReference public

"knative.dev/pkg/system"
_ "knative.dev/pkg/system/testing"
"knative.dev/reconciler-test/pkg/environment"
"knative.dev/reconciler-test/pkg/k8s"
"knative.dev/reconciler-test/pkg/knative"
"knative.dev/reconciler-test/pkg/tracing"
)

func TestEventTypeWithBrokerAsReference(t *testing.T) {
t.Parallel()

ctx, env := global.Environment(
knative.WithKnativeNamespace(system.Namespace()),
knative.WithLoggingConfig,
knative.WithTracingConfig,
k8s.WithEventListener,
environment.Managed(t),
tracing.WithGatherer(t),
environment.WithPollTimings(5*time.Second, 4*time.Minute),
)

env.TestSet(ctx, t, eventtype.eventTypeWithBrokerAsReference())
}
80 changes: 80 additions & 0 deletions test/rekt/features/eventtype/feature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2022 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package eventtype

import (
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/google/uuid"
duckv1 "knative.dev/eventing/pkg/apis/duck/v1"
"knative.dev/eventing/test/rekt/resources/broker"
"knative.dev/eventing/test/rekt/resources/eventtype"
"knative.dev/eventing/test/rekt/resources/trigger"
"knative.dev/pkg/ptr"
"knative.dev/reconciler-test/pkg/eventshub"
"knative.dev/reconciler-test/pkg/feature"
"knative.dev/reconciler-test/pkg/manifest"
"knative.dev/reconciler-test/pkg/resources/service"
)

func eventTypeWithBrokerAsReference(retryNum int32, dropNum uint) *feature.Feature {
f := feature.NewFeatureNamed("Broker reply with a bad status code to the first n events")

source := feature.MakeRandomK8sName("source")
sink := feature.MakeRandomK8sName("sink")
via := feature.MakeRandomK8sName("via")

eventSource := "source2"
eventType := "type2"
eventBody := `{"msg":"DropN"}`
event := cloudevents.NewEvent()
event.SetID(uuid.New().String())
event.SetType(eventType)
event.SetSource(eventSource)
event.SetData(cloudevents.ApplicationJSON, []byte(eventBody))

//Install the broker
brokerName := feature.MakeRandomK8sName("broker")
exp := duckv1.BackoffPolicyLinear
brokerConfig := append(broker.WithEnvConfig(), broker.WithRetry(retryNum, &exp, ptr.String("PT1S")))

f.Setup("install broker", broker.Install(brokerName, brokerConfig...))
f.Requirement("broker is ready", broker.IsReady(brokerName))
f.Requirement("broker is addressable", broker.IsAddressable(brokerName))

f.Setup("install sink", eventshub.Install(sink, eventshub.StartReceiver))

// Point the Trigger subscriber to the sink svc.
cfg := []manifest.CfgFn{trigger.WithSubscriber(service.AsKReference(sink), "")}

// Install the trigger
f.Setup("install trigger", trigger.Install(via, brokerName, cfg...))
f.Setup("trigger goes ready", trigger.IsReady(via))

f.Requirement("install source", eventshub.Install(
source,
eventshub.StartSenderToResource(broker.GVR(), brokerName),
eventshub.InputEvent(event),
))

// The eventType should be already auto-created. We then need to pop up the eventType in the event registry.
// We need to validate the reference of the eventType is pointing to the broker.
f.Stable("ApiServerSource as event source").
Must("ApiServerSource test eventtypes match",
eventtype.WaitForEventType(eventtype.AssertReferenceMatch("InMemoryChannel")))

return f
}
16 changes: 16 additions & 0 deletions test/rekt/resources/eventtype/eventtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,19 @@ func AssertPresent(expectedCeTypes sets.String) EventType {
}

}

func AssertReferenceMatch(expectedCeType string) EventType {
return EventType{
Name: "test eventtypes's reference match or not",
EventTypes: func(etl eventingv1beta2.EventTypeList) (bool, error) {
eventtypesCount := 0
for _, et := range etl.Items {
if expectedCeType == et.Spec.Reference.Kind {
eventtypesCount++
}
}
return (eventtypesCount == len(etl.Items)), nil
},
}

}