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] Introduce --namespace test flag and randomize test object names #3313

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions test/e2e/helpers/broker_channel_flow_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,31 @@ func BrokerChannelFlowWithTransformation(t *testing.T,
channelTestRunner testlib.ComponentsTestRunner,
options ...testlib.SetupClientOption) {
const (
senderName = "e2e-brokerchannel-sender"
brokerName = "e2e-brokerchannel-broker"

any = v1beta1.TriggerAnyFilter
eventType = "type1"
transformedEventType = "type2"
eventSource = "http://source1.com"
transformedEventSource = "http://source2.com"
eventBody = `{"msg":"e2e-brokerchannel-body"}`
transformedBody = `{"msg":"transformed body"}`

triggerName1 = "e2e-brokerchannel-trigger1"
triggerName2 = "e2e-brokerchannel-trigger2"
triggerName3 = "e2e-brokerchannel-trigger3"

transformationPodName = "e2e-brokerchannel-trans-pod"
allEventsRecorderPodName = "e2e-brokerchannel-logger-pod1"
transformedEventsRecorderPodName = "e2e-brokerchannel-logger-pod2"

channelName = "e2e-brokerchannel-channel"
subscriptionName = "e2e-brokerchannel-subscription"
)

channelTestRunner.RunTests(t, testlib.FeatureBasic, func(st *testing.T, channel metav1.TypeMeta) {
var (
senderName = testlib.NameForTest("e2e-brokerchannel-sender")
brokerName = testlib.NameForTest("e2e-brokerchannel-broker")
transformationPodName = testlib.NameForTest("e2e-brokerchannel-trans-pod")
allEventsRecorderPodName = testlib.NameForTest("e2e-brokerchannel-logger-pod1")
transformedEventsRecorderPodName = testlib.NameForTest("e2e-brokerchannel-logger-pod2")

triggerName1 = testlib.NameForTest("e2e-brokerchannel-trigger1")
triggerName2 = testlib.NameForTest("e2e-brokerchannel-trigger2")
triggerName3 = testlib.NameForTest("e2e-brokerchannel-trigger3")

channelName = testlib.NameForTest("e2e-brokerchannel-channel")
subscriptionName = testlib.NameForTest("e2e-brokerchannel-subscription")
)

client := testlib.Setup(st, true, options...)
defer testlib.TearDown(client)

Expand Down
20 changes: 9 additions & 11 deletions test/e2e/helpers/broker_event_transformation_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/google/uuid"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"knative.dev/eventing/pkg/apis/eventing/v1beta1"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/recordevents"
"knative.dev/eventing/test/lib/resources"
Expand All @@ -48,25 +47,24 @@ func EventTransformationForTriggerTestHelper(t *testing.T,
channelTestRunner testlib.ComponentsTestRunner,
options ...testlib.SetupClientOption) {
const (
senderName = "e2e-eventtransformation-sender"
brokerName = "e2e-eventtransformation-broker"

any = v1beta1.TriggerAnyFilter
eventType = "type1"
transformedEventType = "type2"
eventSource = "source1"
transformedEventSource = "source2"
eventBody = `{"msg":"e2e-eventtransformation-body"}`
transformedBody = `{"msg":"transformed body"}`

originalTriggerName = "trigger1"
transformedTriggerName = "trigger2"

transformationPodName = "trans-pod"
recordEventsPodName = "recordevents-pod"
)

channelTestRunner.RunTests(t, testlib.FeatureBasic, func(st *testing.T, channel metav1.TypeMeta) {
var (
senderName = testlib.NameForTest("e2e-eventtransformation-sender")
brokerName = testlib.NameForTest("e2e-eventtransformation-broker")
originalTriggerName = testlib.NameForTest("trigger1")
transformedTriggerName = testlib.NameForTest("trigger2")
transformationPodName = testlib.NameForTest("trans-pod")
recordEventsPodName = testlib.NameForTest("recordevents-pod")
)

client := testlib.Setup(st, true, options...)
defer testlib.TearDown(client)

Expand Down
64 changes: 35 additions & 29 deletions test/e2e/helpers/broker_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ type eventTestCase struct {
Type string
Source string
Extensions map[string]interface{}
NameSuffix string
}

// ToString converts the test case to a string to create names for different objects (e.g., triggers, services, etc.).
func (tc eventTestCase) String() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems the comment on line 46 was forgotten to be updated?

func (tc eventTestCase) Data() string {
eventType := tc.Type
eventSource := tc.Source
extensions := tc.Extensions
Expand All @@ -57,11 +58,15 @@ func (tc eventTestCase) String() string {
u, _ := url.Parse(eventSource)
eventSource = strings.Split(u.Host, ".")[0]
}
name := strings.ToLower(fmt.Sprintf("%s-%s", eventType, eventSource))
data := strings.ToLower(fmt.Sprintf("%s-%s", eventType, eventSource))
if len(extensions) > 0 {
name = strings.ToLower(fmt.Sprintf("%s-%s", name, extensionsToString(extensions)))
data = strings.ToLower(fmt.Sprintf("%s-%s", data, extensionsToString(extensions)))
}
return name
return data
}

func (tc eventTestCase) Name() string {
return fmt.Sprintf("%s%s", tc.Data(), tc.NameSuffix)
}

// ToEventMatcher converts the test case to the event matcher
Expand Down Expand Up @@ -97,7 +102,7 @@ type BrokerCreator func(client *testlib.Client) string
// ChannelBasedBrokerCreator creates a BrokerCreator that creates a broker based on the channel parameter.
func ChannelBasedBrokerCreator(channel metav1.TypeMeta, brokerClass string) BrokerCreator {
return func(client *testlib.Client) string {
brokerName := strings.ToLower(channel.Kind)
brokerName := testlib.NameForTest(channel.Kind)

// create a ConfigMap used by the broker.
config := client.CreateBrokerConfigMapOrFail("config-"+brokerName, &channel)
Expand Down Expand Up @@ -155,10 +160,10 @@ func TestBrokerWithManyTriggers(t *testing.T, brokerCreator BrokerCreator, shoul
{Type: eventType2, Source: eventSource2},
},
eventFilters: []eventTestCase{
{Type: any, Source: any},
{Type: eventType1, Source: any},
{Type: any, Source: eventSource1},
{Type: eventType1, Source: eventSource1},
{Type: any, Source: any, NameSuffix: lib.RandomSuffix()},
{Type: eventType1, Source: any, NameSuffix: lib.RandomSuffix()},
{Type: any, Source: eventSource1, NameSuffix: lib.RandomSuffix()},
{Type: eventType1, Source: eventSource1, NameSuffix: lib.RandomSuffix()},
},
deprecatedTriggerFilter: true,
}, {
Expand All @@ -170,10 +175,10 @@ func TestBrokerWithManyTriggers(t *testing.T, brokerCreator BrokerCreator, shoul
{Type: eventType2, Source: eventSource2},
},
eventFilters: []eventTestCase{
{Type: any, Source: any},
{Type: eventType1, Source: any},
{Type: any, Source: eventSource1},
{Type: eventType1, Source: eventSource1},
{Type: any, Source: any, NameSuffix: lib.RandomSuffix()},
{Type: eventType1, Source: any, NameSuffix: lib.RandomSuffix()},
{Type: any, Source: eventSource1, NameSuffix: lib.RandomSuffix()},
{Type: eventType1, Source: eventSource1, NameSuffix: lib.RandomSuffix()},
},
deprecatedTriggerFilter: false,
}, {
Expand All @@ -185,10 +190,10 @@ func TestBrokerWithManyTriggers(t *testing.T, brokerCreator BrokerCreator, shoul
{Type: eventType2, Source: eventSource2},
},
eventFilters: []eventTestCase{
{Type: any, Source: any},
{Type: eventType1, Source: any},
{Type: any, Source: eventSource1},
{Type: eventType1, Source: eventSource1},
{Type: any, Source: any, NameSuffix: lib.RandomSuffix()},
{Type: eventType1, Source: any, NameSuffix: lib.RandomSuffix()},
{Type: any, Source: eventSource1, NameSuffix: lib.RandomSuffix()},
{Type: eventType1, Source: eventSource1, NameSuffix: lib.RandomSuffix()},
},
deprecatedTriggerFilter: false,
v1beta1: true,
Expand All @@ -205,18 +210,19 @@ func TestBrokerWithManyTriggers(t *testing.T, brokerCreator BrokerCreator, shoul
{Type: eventType2, Source: eventSource2, Extensions: map[string]interface{}{extensionName1: extensionValue1, nonMatchingExtensionName: extensionValue2}},
},
eventFilters: []eventTestCase{
{Type: any, Source: any, Extensions: map[string]interface{}{extensionName1: extensionValue1}},
{Type: any, Source: any, Extensions: map[string]interface{}{extensionName1: extensionValue1, extensionName2: extensionValue2}},
{Type: any, Source: any, Extensions: map[string]interface{}{extensionName2: extensionValue2}},
{Type: eventType1, Source: any, Extensions: map[string]interface{}{extensionName1: extensionValue1}},
{Type: any, Source: any, Extensions: map[string]interface{}{extensionName1: any}},
{Type: any, Source: eventSource1, Extensions: map[string]interface{}{extensionName1: extensionValue1}},
{Type: any, Source: eventSource1, Extensions: map[string]interface{}{extensionName1: extensionValue1, extensionName2: extensionValue2}},
{Type: any, Source: any, Extensions: map[string]interface{}{extensionName1: extensionValue1}, NameSuffix: lib.RandomSuffix()},
{Type: any, Source: any, Extensions: map[string]interface{}{extensionName1: extensionValue1, extensionName2: extensionValue2}, NameSuffix: lib.RandomSuffix()},
{Type: any, Source: any, Extensions: map[string]interface{}{extensionName2: extensionValue2}, NameSuffix: lib.RandomSuffix()},
{Type: eventType1, Source: any, Extensions: map[string]interface{}{extensionName1: extensionValue1}, NameSuffix: lib.RandomSuffix()},
{Type: any, Source: any, Extensions: map[string]interface{}{extensionName1: any}, NameSuffix: lib.RandomSuffix()},
{Type: any, Source: eventSource1, Extensions: map[string]interface{}{extensionName1: extensionValue1}, NameSuffix: lib.RandomSuffix()},
{Type: any, Source: eventSource1, Extensions: map[string]interface{}{extensionName1: extensionValue1, extensionName2: extensionValue2}, NameSuffix: lib.RandomSuffix()},
},
deprecatedTriggerFilter: false,
},
}
for _, test := range tests {
test := test // Prevent race conditions when running in parallel.
t.Run(test.name, func(t *testing.T) {
client := testlib.Setup(t, true)
defer testlib.TearDown(client)
Expand Down Expand Up @@ -245,7 +251,7 @@ func TestBrokerWithManyTriggers(t *testing.T, brokerCreator BrokerCreator, shoul
eventTrackers := make(map[string]*recordevents.EventInfoStore, len(test.eventFilters))
for _, event := range test.eventFilters {
// Create event recorder pod and service
subscriberName := "dumper-" + event.String()
subscriberName := event.Name()
Copy link
Member

@lberk lberk Jul 14, 2020

Choose a reason for hiding this comment

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

Why did we remove the "dumper-" prefix (and "trigger-" one below)? Those common prefixes tend to help me establish the mental models when trying to debug a new (to me) test.

ninjaedit: maybe we can do something similar to your sender change? testlib.NameForTest("dumper-", even.Name()) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed it because the names need to be created in advance, and if I then prefix it with "dumper-" it may exceed the 63 limit, plus I'm getting segmentation faults because eventTrackers then don't include the right matcher and it calls a function on nil object. Anyway, I'll see if we can improve it a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now I'm facing a problem with running tests in parallel. Some of them label namespaces with knative-eventing-injection": "enabled" and some of them don't want this. These tests can't run in parallel. Pretty complicated...

eventRecordPod := resources.EventRecordPod(subscriberName)
client.CreatePodOrFail(eventRecordPod, testlib.WithService(subscriberName))
eventTracker, err := recordevents.NewEventInfoStore(client, subscriberName)
Expand All @@ -256,7 +262,7 @@ func TestBrokerWithManyTriggers(t *testing.T, brokerCreator BrokerCreator, shoul
defer eventTracker.Cleanup()

// Create trigger.
triggerName := "trigger-" + event.String()
triggerName := event.Name()
client.CreateTriggerOrFailV1Beta1(triggerName,
resources.WithSubscriberServiceRefForTriggerV1Beta1(subscriberName),
resources.WithAttributesTriggerFilterV1Beta1(event.Source, event.Type, event.Extensions),
Expand Down Expand Up @@ -284,13 +290,13 @@ func TestBrokerWithManyTriggers(t *testing.T, brokerCreator BrokerCreator, shoul
eventToSend.SetExtension(k, v)
}

data := fmt.Sprintf(`{"msg":"%s"}`, eventTestCase.String())
data := fmt.Sprintf(`{"msg":"%s"}`, eventTestCase.Data())
if err := eventToSend.SetData(cloudevents.ApplicationJSON, []byte(data)); err != nil {
t.Fatalf("Cannot set the payload of the event: %s", err.Error())
}

// Send event
senderPodName := "sender-" + eventTestCase.String()
senderPodName := testlib.NameForTest("sender-" + eventTestCase.Data())
client.SendEventToAddressable(senderPodName, brokerName, testlib.BrokerTypeMeta, eventToSend)

// Sent event matcher
Expand All @@ -301,7 +307,7 @@ func TestBrokerWithManyTriggers(t *testing.T, brokerCreator BrokerCreator, shoul

// Check on every dumper whether we should expect this event or not
for _, eventFilter := range test.eventFilters {
subscriberName := "dumper-" + eventFilter.String()
subscriberName := eventFilter.Name()

if eventFilter.ToEventMatcher()(eventToSend) == nil {
// This filter should match this event
Expand Down
17 changes: 16 additions & 1 deletion test/lib/creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ package lib

import (
"fmt"

sourcesv1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
"knative.dev/pkg/test/helpers"

appsv1 "k8s.io/api/apps/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/storage/names"

"knative.dev/eventing/pkg/apis/eventing/v1beta1"
flowsv1beta1 "knative.dev/eventing/pkg/apis/flows/v1beta1"
Expand Down Expand Up @@ -137,6 +138,7 @@ func (c *Client) CreateBrokerConfigMapOrFail(name string, channel *metav1.TypeMe
if err != nil {
c.T.Fatalf("Failed to create broker config %q: %v", name, err)
}
c.Tracker.Add(coreAPIGroup, coreAPIVersion, "configmaps", c.Namespace, name)
return &duckv1.KReference{
APIVersion: "v1",
Kind: "ConfigMap",
Expand Down Expand Up @@ -456,3 +458,16 @@ func (c *Client) CreateRBACResourcesForBrokers() {
c.Namespace,
)
}

// NameForTest generates an object name based on the desired object prefix
// A random suffix is appended so that each name is unique.
// If the resulting name is too long it will be trimmed to 63 characters (the random
// string is preserved in any case).
func NameForTest(prefix string) string {
return names.SimpleNameGenerator.GenerateName(helpers.MakeK8sNamePrefix(prefix) + "-")
}

// RandomSuffix generates a random string starting with "-".
func RandomSuffix() string {
return names.SimpleNameGenerator.GenerateName("-")
}
12 changes: 8 additions & 4 deletions test/lib/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ var SetupClientOptionNoop SetupClientOption = func(*Client) {
// Setup creates the client objects needed in the e2e tests,
// and does other setups, like creating namespaces, set the test case to run in parallel, etc.
func Setup(t *testing.T, runInParallel bool, options ...SetupClientOption) *Client {
// Create a new namespace to run this test case.
namespace := makeK8sNamespace(t.Name())
namespace := pkgTest.Flags.Namespace
if namespace == "" {
namespace = makeK8sNamespace(t.Name())
}
t.Logf("namespace is : %q", namespace)
client, err := NewClient(
pkgTest.Flags.Kubeconfig,
Expand Down Expand Up @@ -152,8 +154,10 @@ func TearDown(client *Client) {
}

client.Tracker.Clean(true)
if err := DeleteNameSpace(client); err != nil {
client.T.Logf("Could not delete the namespace %q: %v", client.Namespace, err)
if pkgTest.Flags.Namespace == "" { // Delete namespace only if it's not common for all tests.
if err := DeleteNameSpace(client); err != nil {
client.T.Logf("Could not delete the namespace %q: %v", client.Namespace, err)
}
}
}

Expand Down