diff --git a/test/e2e/helpers/broker_channel_flow_helper.go b/test/e2e/helpers/broker_channel_flow_helper.go index bbb16841525..aff82698e83 100644 --- a/test/e2e/helpers/broker_channel_flow_helper.go +++ b/test/e2e/helpers/broker_channel_flow_helper.go @@ -54,9 +54,6 @@ 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" @@ -64,20 +61,24 @@ func BrokerChannelFlowWithTransformation(t *testing.T, 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) diff --git a/test/e2e/helpers/broker_event_transformation_test_helper.go b/test/e2e/helpers/broker_event_transformation_test_helper.go index ac984eae021..83f7008a616 100644 --- a/test/e2e/helpers/broker_event_transformation_test_helper.go +++ b/test/e2e/helpers/broker_event_transformation_test_helper.go @@ -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" @@ -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) diff --git a/test/e2e/helpers/broker_test_helper.go b/test/e2e/helpers/broker_test_helper.go index c45c33fd342..0c0542b0e10 100644 --- a/test/e2e/helpers/broker_test_helper.go +++ b/test/e2e/helpers/broker_test_helper.go @@ -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 { +func (tc eventTestCase) Data() string { eventType := tc.Type eventSource := tc.Source extensions := tc.Extensions @@ -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 @@ -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) @@ -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: testlib.RandomSuffix()}, + {Type: eventType1, Source: any, NameSuffix: testlib.RandomSuffix()}, + {Type: any, Source: eventSource1, NameSuffix: testlib.RandomSuffix()}, + {Type: eventType1, Source: eventSource1, NameSuffix: testlib.RandomSuffix()}, }, deprecatedTriggerFilter: true, }, { @@ -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: testlib.RandomSuffix()}, + {Type: eventType1, Source: any, NameSuffix: testlib.RandomSuffix()}, + {Type: any, Source: eventSource1, NameSuffix: testlib.RandomSuffix()}, + {Type: eventType1, Source: eventSource1, NameSuffix: testlib.RandomSuffix()}, }, deprecatedTriggerFilter: false, }, { @@ -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: testlib.RandomSuffix()}, + {Type: eventType1, Source: any, NameSuffix: testlib.RandomSuffix()}, + {Type: any, Source: eventSource1, NameSuffix: testlib.RandomSuffix()}, + {Type: eventType1, Source: eventSource1, NameSuffix: testlib.RandomSuffix()}, }, deprecatedTriggerFilter: false, v1beta1: true, @@ -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: testlib.RandomSuffix()}, + {Type: any, Source: any, Extensions: map[string]interface{}{extensionName1: extensionValue1, extensionName2: extensionValue2}, NameSuffix: testlib.RandomSuffix()}, + {Type: any, Source: any, Extensions: map[string]interface{}{extensionName2: extensionValue2}, NameSuffix: testlib.RandomSuffix()}, + {Type: eventType1, Source: any, Extensions: map[string]interface{}{extensionName1: extensionValue1}, NameSuffix: testlib.RandomSuffix()}, + {Type: any, Source: any, Extensions: map[string]interface{}{extensionName1: any}, NameSuffix: testlib.RandomSuffix()}, + {Type: any, Source: eventSource1, Extensions: map[string]interface{}{extensionName1: extensionValue1}, NameSuffix: testlib.RandomSuffix()}, + {Type: any, Source: eventSource1, Extensions: map[string]interface{}{extensionName1: extensionValue1, extensionName2: extensionValue2}, NameSuffix: testlib.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) @@ -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() eventRecordPod := resources.EventRecordPod(subscriberName) client.CreatePodOrFail(eventRecordPod, testlib.WithService(subscriberName)) eventTracker, err := recordevents.NewEventInfoStore(client, subscriberName) @@ -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), @@ -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 @@ -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 diff --git a/test/lib/creation.go b/test/lib/creation.go index 9705f4b1e41..5bfc68b7eb5 100644 --- a/test/lib/creation.go +++ b/test/lib/creation.go @@ -20,6 +20,7 @@ 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" @@ -27,6 +28,7 @@ import ( 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" @@ -137,6 +139,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", @@ -456,3 +459,16 @@ func (c *Client) CreateRBACResourcesForBrokers() { c.Namespace, ) } + +// NameForTest generates an object name by appending a random suffix +// to the given prefix. +// 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("-") +} diff --git a/test/lib/test_runner.go b/test/lib/test_runner.go index 7fd86ed0f5d..21e013a5381 100644 --- a/test/lib/test_runner.go +++ b/test/lib/test_runner.go @@ -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, @@ -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) + } } }