From 1f23c48a58f9c282619f81c2aee133e289dfc252 Mon Sep 17 00:00:00 2001 From: Teresaliu Date: Thu, 11 Aug 2022 09:26:29 +0000 Subject: [PATCH 1/3] Port-SinkBindingV1Deployment-test-to-rekt --- test/rekt/features/sinkbinding/feature.go | 60 +++++++++++++++++++ test/rekt/resources/deployment/deployment.go | 6 +- .../rekt/resources/deployment/deployment.yaml | 5 ++ .../resources/deployment/deployment_test.go | 6 ++ .../rekt/resources/sinkbinding/sinkbinding.go | 2 +- test/rekt/sink_binding_test.go | 40 ++++--------- test/rekt/smoke_test.go | 43 +++++++++++-- 7 files changed, 125 insertions(+), 37 deletions(-) create mode 100644 test/rekt/features/sinkbinding/feature.go diff --git a/test/rekt/features/sinkbinding/feature.go b/test/rekt/features/sinkbinding/feature.go new file mode 100644 index 00000000000..08e7dba74a7 --- /dev/null +++ b/test/rekt/features/sinkbinding/feature.go @@ -0,0 +1,60 @@ +/* +Copyright 2021 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 sinkbinding + +import ( + "github.com/cloudevents/sdk-go/v2/test" + "k8s.io/apimachinery/pkg/util/uuid" + "knative.dev/eventing/test/rekt/resources/deployment" + "knative.dev/eventing/test/rekt/resources/sinkbinding" + "knative.dev/reconciler-test/pkg/eventshub" + eventasssert "knative.dev/reconciler-test/pkg/eventshub/assert" + "knative.dev/reconciler-test/pkg/feature" + "knative.dev/reconciler-test/pkg/manifest" + "knative.dev/reconciler-test/resources/svc" +) + +func SinkBindingV1Deployment() *feature.Feature { + sbinding := feature.MakeRandomK8sName("sinkbinding") + sink := feature.MakeRandomK8sName("sink") + subject := feature.MakeRandomK8sName("subject") + extensionSecret := string(uuid.NewUUID()) + + f := feature.NewFeatureNamed("SinkBinding V1 Deployment test") + + f.Setup("install sink", eventshub.Install(sink, eventshub.StartReceiver)) + f.Setup("install a deployment", deployment.Install(subject)) + + extensions := map[string]string{ + "sinkbinding": extensionSecret, + } + + cfg := []manifest.CfgFn{ + sinkbinding.WithExtensions(extensions), + } + + f.Setup("install SinkBinding", sinkbinding.Install(sbinding, svc.AsDestinationRef(sink), deployment.AsTrackerReference(subject), cfg...)) + f.Setup("SinkBinding goes ready", sinkbinding.IsReady(sbinding)) + + f.Stable("ApiServerSource as event source"). + Must("delivers events", + eventasssert.OnStore(sink).MatchEvent( + test.HasExtension("sinkbinding", extensionSecret), + ).AtLeast(1)) + + return f +} diff --git a/test/rekt/resources/deployment/deployment.go b/test/rekt/resources/deployment/deployment.go index 50ec6db08d2..f9460c8bd9d 100644 --- a/test/rekt/resources/deployment/deployment.go +++ b/test/rekt/resources/deployment/deployment.go @@ -34,9 +34,9 @@ var yaml embed.FS func Install(name string) feature.StepFn { cfg := map[string]interface{}{ "name": name, - "selectors": map[string]string{"app": name}, // default - "image": "gcr.io/knative-samples/helloworld-go", // default - "port": 8080, // default + "selectors": map[string]string{"app": name}, // default + "image": "gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats", // default + "port": 8080, // default } return func(ctx context.Context, t feature.T) { diff --git a/test/rekt/resources/deployment/deployment.yaml b/test/rekt/resources/deployment/deployment.yaml index 1e66876d1b4..75db2193f78 100644 --- a/test/rekt/resources/deployment/deployment.yaml +++ b/test/rekt/resources/deployment/deployment.yaml @@ -35,3 +35,8 @@ spec: image: {{ .image }} ports: - containerPort: {{ .port }} + env: + - name: POD_NAME + value: heartbeats + - name: POD_NAMESPACE + value: {{ .namespace }} \ No newline at end of file diff --git a/test/rekt/resources/deployment/deployment_test.go b/test/rekt/resources/deployment/deployment_test.go index 7bb0ebfd3e3..0e178b34d69 100644 --- a/test/rekt/resources/deployment/deployment_test.go +++ b/test/rekt/resources/deployment/deployment_test.go @@ -39,6 +39,7 @@ func Example() { } files, err := manifest.ExecuteYAML(ctx, yaml, images, cfg) + if err != nil { panic(err) } @@ -64,4 +65,9 @@ func Example() { // image: gcr.io/knative-samples/helloworld-go // ports: // - containerPort: 8080 + // env: + // - name: POD_NAME + // value: heartbeats + // - name: POD_NAMESPACE + // value: bar } diff --git a/test/rekt/resources/sinkbinding/sinkbinding.go b/test/rekt/resources/sinkbinding/sinkbinding.go index 37214c6f8b8..291c8ff0570 100644 --- a/test/rekt/resources/sinkbinding/sinkbinding.go +++ b/test/rekt/resources/sinkbinding/sinkbinding.go @@ -96,7 +96,7 @@ func WithExtensions(extensions map[string]string) manifest.CfgFn { if extensions != nil { if _, set := ceOverrides["extensions"]; !set { - ceOverrides["ref"] = map[string]interface{}{} + ceOverrides["extensions"] = map[string]interface{}{} } ceExt := ceOverrides["extensions"].(map[string]interface{}) for k, v := range extensions { diff --git a/test/rekt/sink_binding_test.go b/test/rekt/sink_binding_test.go index 81ce507fe4b..b7248e81f59 100644 --- a/test/rekt/sink_binding_test.go +++ b/test/rekt/sink_binding_test.go @@ -23,37 +23,23 @@ import ( "testing" "knative.dev/eventing/test/rekt/features/sinkbinding" - "knative.dev/eventing/test/rekt/resources/deployment" - sb "knative.dev/eventing/test/rekt/resources/sinkbinding" - "knative.dev/reconciler-test/pkg/feature" - "knative.dev/reconciler-test/resources/svc" + "knative.dev/pkg/system" + "knative.dev/reconciler-test/pkg/environment" + "knative.dev/reconciler-test/pkg/k8s" + "knative.dev/reconciler-test/pkg/knative" ) -// TestSmoke_SinkBinding -func TestSmoke_SinkBinding(t *testing.T) { +func TestSinkBindingV1Deployment(t *testing.T) { t.Parallel() - ctx, env := global.Environment() + ctx, env := global.Environment( + knative.WithKnativeNamespace(system.Namespace()), + knative.WithLoggingConfig, + knative.WithTracingConfig, + k8s.WithEventListener, + environment.Managed(t), + ) t.Cleanup(env.Finish) - names := []string{ - "customname", - "name-with-dash", - "name1with2numbers3", - "name63-01234567890123456789012345678901234567890123456789012345", - } - - for _, name := range names { - f := sinkbinding.GoesReady(name) - - sink := feature.MakeRandomK8sName("sink") - f.Setup("install a service", svc.Install(sink, "app", "rekt")) - - subject := feature.MakeRandomK8sName("subject") - f.Setup("install a deployment", deployment.Install(subject)) - - f.Setup("install a sinkbinding", sb.Install(name, svc.AsDestinationRef(sink), deployment.AsTrackerReference(subject))) - - env.Test(ctx, t, f) - } + env.Test(ctx, t, sinkbinding.SinkBindingV1Deployment()) } diff --git a/test/rekt/smoke_test.go b/test/rekt/smoke_test.go index ebff83a6368..4b16d8cd78e 100644 --- a/test/rekt/smoke_test.go +++ b/test/rekt/smoke_test.go @@ -24,10 +24,6 @@ import ( "testing" "k8s.io/utils/pointer" - _ "knative.dev/pkg/system/testing" - - "knative.dev/reconciler-test/pkg/manifest" - eventingduck "knative.dev/eventing/pkg/apis/duck/v1" "knative.dev/eventing/test/rekt/features/apiserversource" "knative.dev/eventing/test/rekt/features/broker" @@ -35,13 +31,19 @@ import ( "knative.dev/eventing/test/rekt/features/parallel" "knative.dev/eventing/test/rekt/features/pingsource" "knative.dev/eventing/test/rekt/features/sequence" + "knative.dev/eventing/test/rekt/features/sinkbinding" b "knative.dev/eventing/test/rekt/resources/broker" "knative.dev/eventing/test/rekt/resources/channel_impl" "knative.dev/eventing/test/rekt/resources/delivery" + "knative.dev/eventing/test/rekt/resources/deployment" + presources "knative.dev/eventing/test/rekt/resources/parallel" ps "knative.dev/eventing/test/rekt/resources/pingsource" sresources "knative.dev/eventing/test/rekt/resources/sequence" - - presources "knative.dev/eventing/test/rekt/resources/parallel" + sb "knative.dev/eventing/test/rekt/resources/sinkbinding" + _ "knative.dev/pkg/system/testing" + "knative.dev/reconciler-test/pkg/feature" + "knative.dev/reconciler-test/pkg/manifest" + "knative.dev/reconciler-test/resources/svc" ) // TestSmoke_Broker @@ -272,3 +274,32 @@ func SpecDelivery(spec map[string]interface{}) { linear := eventingduck.BackoffPolicyLinear delivery.WithRetry(10, &linear, pointer.StringPtr("PT1S"))(spec) } + +// TestSmoke_SinkBinding +func TestSmoke_SinkBinding(t *testing.T) { + t.Parallel() + + ctx, env := global.Environment() + t.Cleanup(env.Finish) + + names := []string{ + "customname", + "name-with-dash", + "name1with2numbers3", + "name63-01234567890123456789012345678901234567890123456789012345", + } + + for _, name := range names { + f := sinkbinding.GoesReady(name) + + sink := feature.MakeRandomK8sName("sink") + f.Setup("install a service", svc.Install(sink, "app", "rekt")) + + subject := feature.MakeRandomK8sName("subject") + f.Setup("install a deployment", deployment.Install(subject)) + + f.Setup("install a sinkbinding", sb.Install(name, svc.AsDestinationRef(sink), deployment.AsTrackerReference(subject))) + + env.Test(ctx, t, f) + } +} From 3eb6a2470c01263fa1190922a28deae7befaa942 Mon Sep 17 00:00:00 2001 From: Teresaliu Date: Thu, 11 Aug 2022 10:11:12 +0000 Subject: [PATCH 2/3] remove old e2e test --- test/e2e/source_sinkbinding_v1_test.go | 85 -------------------------- 1 file changed, 85 deletions(-) diff --git a/test/e2e/source_sinkbinding_v1_test.go b/test/e2e/source_sinkbinding_v1_test.go index cb6b11b79fa..7b3cbd311d4 100644 --- a/test/e2e/source_sinkbinding_v1_test.go +++ b/test/e2e/source_sinkbinding_v1_test.go @@ -29,7 +29,6 @@ import ( "k8s.io/apimachinery/pkg/util/uuid" . "github.com/cloudevents/sdk-go/v2/test" - appsv1 "k8s.io/api/apps/v1" batchv1 "k8s.io/api/batch/v1" batchv1beta1 "k8s.io/api/batch/v1beta1" duckv1 "knative.dev/pkg/apis/duck/v1" @@ -42,90 +41,6 @@ import ( rttestingv1 "knative.dev/eventing/pkg/reconciler/testing/v1" ) -func TestSinkBindingV1Deployment(t *testing.T) { - const ( - sinkBindingName = "e2e-sink-binding" - deploymentName = "e2e-sink-binding-deployment" - // the heartbeats image is built from test_images/heartbeats - imageName = "heartbeats" - - recordEventPodName = "e2e-sink-binding-recordevent-pod-v1beta1dep" - ) - - client := setup(t, true) - defer tearDown(client) - - ctx := context.Background() - - // create event logger pod and service - eventTracker, _ := recordevents.StartEventRecordOrFail(ctx, client, recordEventPodName) - extensionSecret := string(uuid.NewUUID()) - - // create sink binding - sinkBinding := rttestingv1.NewSinkBinding( - sinkBindingName, - client.Namespace, - rttestingv1.WithSink(duckv1.Destination{Ref: resources.KnativeRefForService(recordEventPodName, client.Namespace)}), - rttestingv1.WithSubject(tracker.Reference{ - APIVersion: "apps/v1", - Kind: "Deployment", - Namespace: client.Namespace, - Name: deploymentName, - }), - rttestingv1.WithCloudEventOverrides(duckv1.CloudEventOverrides{Extensions: map[string]string{ - "sinkbinding": extensionSecret, - }}), - ) - client.CreateSinkBindingV1OrFail(sinkBinding) - - message := fmt.Sprintf("msg %s for TestSinkBindingDeployment", uuid.NewUUID()) - client.CreateDeploymentOrFail(&appsv1.Deployment{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: client.Namespace, - Name: deploymentName, - }, - Spec: appsv1.DeploymentSpec{ - Selector: &metav1.LabelSelector{ - MatchLabels: map[string]string{ - "foo": "bar", - }, - }, - Template: corev1.PodTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{ - "foo": "bar", - }, - }, - Spec: corev1.PodSpec{ - Containers: []corev1.Container{{ - Name: imageName, - Image: pkgTest.ImagePath(imageName), - ImagePullPolicy: corev1.PullIfNotPresent, - Args: []string{"--msg=" + message}, - Env: []corev1.EnvVar{{ - Name: "POD_NAME", - Value: deploymentName, - }, { - Name: "POD_NAMESPACE", - Value: client.Namespace, - }}, - }}, - }, - }, - }, - }) - - // wait for all test resources to be ready - client.WaitForAllTestResourcesReadyOrFail(ctx) - - // Look for events with expected data, and sinkbinding extension - eventTracker.AssertAtLeast(2, recordevents.MatchEvent( - recordevents.MatchHeartBeatsImageMessage(message), - HasSource(fmt.Sprintf("https://knative.dev/eventing/test/heartbeats/#%s/%s", client.Namespace, deploymentName)), - HasExtension("sinkbinding", extensionSecret), - )) -} - func TestSinkBindingV1CronJob(t *testing.T) { const ( sinkBindingName = "e2e-sink-binding" From b8bb9be1670f24a3ae7273b1959fdff21f107cac Mon Sep 17 00:00:00 2001 From: Teresaliu Date: Thu, 11 Aug 2022 17:24:28 +0000 Subject: [PATCH 3/3] Update Copyright year and fix Code Style --- test/rekt/features/sinkbinding/feature.go | 2 +- test/rekt/resources/deployment/deployment.yaml | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/rekt/features/sinkbinding/feature.go b/test/rekt/features/sinkbinding/feature.go index 08e7dba74a7..396ff3c06dd 100644 --- a/test/rekt/features/sinkbinding/feature.go +++ b/test/rekt/features/sinkbinding/feature.go @@ -1,5 +1,5 @@ /* -Copyright 2021 The Knative Authors +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. diff --git a/test/rekt/resources/deployment/deployment.yaml b/test/rekt/resources/deployment/deployment.yaml index 75db2193f78..925cdd67761 100644 --- a/test/rekt/resources/deployment/deployment.yaml +++ b/test/rekt/resources/deployment/deployment.yaml @@ -39,4 +39,5 @@ spec: - name: POD_NAME value: heartbeats - name: POD_NAMESPACE - value: {{ .namespace }} \ No newline at end of file + value: {{ .namespace }} +