diff --git a/test/rekt/features/sinkbinding/feature.go b/test/rekt/features/sinkbinding/feature.go index bc6b83e43fc..29bde0261b7 100644 --- a/test/rekt/features/sinkbinding/feature.go +++ b/test/rekt/features/sinkbinding/feature.go @@ -120,6 +120,45 @@ func SinkBindingV1Job(ctx context.Context) *feature.Feature { return f } +func SinkBindingV1DeploymentTLS(ctx context.Context) *feature.Feature { + sbinding := feature.MakeRandomK8sName("sinkbinding") + sink := feature.MakeRandomK8sName("sink") + subject := feature.MakeRandomK8sName("subject") + + f := feature.NewFeatureNamed("SinkBinding V1 Deployment test with TLS") + + f.Setup("install sink", eventshub.Install(sink, eventshub.StartReceiverTLS)) + + f.Setup("install a deployment", deployment.Install(subject, heartbeatsImage, + deployment.WithEnvs(map[string]string{ + "POD_NAME": "heartbeats", + "POD_NAMESPACE": env.Namespace(), + }))) + extensions := map[string]string{ + "sinkbinding": extensionSecret, + } + cfg := []manifest.CfgFn{ + sinkbinding.WithExtensions(extensions), + deployment.AsTrackerReference(subject), + } + + f.Requirement("install SinkBinding", func(ctx context.Context, t feature.T) { + d := service.AsDestinationRef(sink) + d.CACerts = eventshub.GetCaCerts(ctx) + + cfg = append(cfg, sinkbinding.WithSink(d)) + sinkbinding.Install(sbinding, cfg...)(ctx, t) + }) + f.Requirement("SinkBinding goes ready", sinkbinding.IsReady(sbinding)) + f.Stable("SinkBinding as event source"). + Must("delivers events on sink with ref", + eventasssert.OnStore(sink). + Match(eventasssert.MatchKind(eventshub.EventReceived)). + AtLeast(1), + ) + return f +} + // AsTrackerReference returns a tracker.Reference for a Job without namespace. func AsTrackerReference(name string) *tracker.Reference { return &tracker.Reference{ diff --git a/test/rekt/resources/sinkbinding/sinkbinding.go b/test/rekt/resources/sinkbinding/sinkbinding.go index 291c8ff0570..a1c68c1303f 100644 --- a/test/rekt/resources/sinkbinding/sinkbinding.go +++ b/test/rekt/resources/sinkbinding/sinkbinding.go @@ -19,6 +19,7 @@ package sinkbinding import ( "context" "embed" + "strings" "time" "k8s.io/apimachinery/pkg/runtime/schema" @@ -106,6 +107,38 @@ func WithExtensions(extensions map[string]string) manifest.CfgFn { } } +func WithSink(d *duckv1.Destination) manifest.CfgFn { + return func(cfg map[string]interface{}) { + if _, set := cfg["sink"]; !set { + cfg["sink"] = map[string]interface{}{} + } + sink := cfg["sink"].(map[string]interface{}) + + ref := d.Ref + uri := d.URI + + if d.CACerts != nil { + // This is a multi-line string and should be indented accordingly. + // Replace "new line" with "new line + spaces". + sink["CACerts"] = strings.ReplaceAll(*d.CACerts, "\n", "\n ") + } + + if uri != nil { + sink["uri"] = uri.String() + } + if ref != nil { + if _, set := sink["ref"]; !set { + sink["ref"] = map[string]interface{}{} + } + sref := sink["ref"].(map[string]interface{}) + sref["apiVersion"] = ref.APIVersion + sref["kind"] = ref.Kind + // skip namespace + sref["name"] = ref.Name + } + } +} + // IsReady tests to see if a PingSource becomes ready within the time given. func IsReady(name string, timing ...time.Duration) feature.StepFn { return k8s.IsReady(Gvr(), name, timing...) diff --git a/test/rekt/resources/sinkbinding/sinkbinding.yaml b/test/rekt/resources/sinkbinding/sinkbinding.yaml index c38f9dd5f3c..441b688cfad 100644 --- a/test/rekt/resources/sinkbinding/sinkbinding.yaml +++ b/test/rekt/resources/sinkbinding/sinkbinding.yaml @@ -38,6 +38,10 @@ spec: namespace: {{ .namespace }} name: {{ .sink.ref.name }} {{ end }} + {{ if .sink.CACerts }} + CACerts: |- + {{ .sink.CACerts }} + {{ end }} {{ if .sink.uri }} uri: {{ .sink.uri }} {{ end }} diff --git a/test/rekt/resources/sinkbinding/sinkbinding_test.go b/test/rekt/resources/sinkbinding/sinkbinding_test.go index aa0684b761b..fb2d4e47c5f 100644 --- a/test/rekt/resources/sinkbinding/sinkbinding_test.go +++ b/test/rekt/resources/sinkbinding/sinkbinding_test.go @@ -141,3 +141,42 @@ func Example_full() { // match1: this // match2: that } + +func Example_withSink() { + ctx := testlog.NewContext() + images := map[string]string{} + cfg := map[string]interface{}{ + "name": "foo", + "namespace": "bar", + } + + sinkRef := &duckv1.Destination{ + Ref: &duckv1.KReference{ + Kind: "sinkkind", + Namespace: "sinknamespace", + Name: "sinkname", + APIVersion: "sinkversion", + }, + URI: &apis.URL{Path: "uri/parts"}, + } + sinkbinding.WithSink(sinkRef)(cfg) + files, err := manifest.ExecuteYAML(ctx, yaml, images, cfg) + if err != nil { + panic(err) + } + + manifest.OutputYAML(os.Stdout, files) + // Output: + // apiVersion: sources.knative.dev/v1 + // kind: SinkBinding + // metadata: + // name: foo + // namespace: bar + // spec: + // sink: + // ref: + // kind: sinkkind + // namespace: bar + // name: sinkname + // apiVersion: sinkversion + // uri: uri/parts \ No newline at end of file diff --git a/test/rekt/sink_binding_test.go b/test/rekt/sink_binding_test.go index 47635bd45d3..721778cf675 100644 --- a/test/rekt/sink_binding_test.go +++ b/test/rekt/sink_binding_test.go @@ -24,6 +24,7 @@ import ( "knative.dev/pkg/system" "knative.dev/reconciler-test/pkg/environment" + "knative.dev/reconciler-test/pkg/eventshub" "knative.dev/reconciler-test/pkg/k8s" "knative.dev/reconciler-test/pkg/knative" @@ -45,6 +46,22 @@ func TestSinkBindingV1Deployment(t *testing.T) { env.Test(ctx, t, sinkbinding.SinkBindingV1Deployment(ctx)) } +func TestSinkBindingV1DeploymentTLS(t *testing.T) { + t.Parallel() + + ctx, env := global.Environment( + knative.WithKnativeNamespace(system.Namespace()), + knative.WithLoggingConfig, + knative.WithTracingConfig, + k8s.WithEventListener, + //environment.Managed(t), + eventshub.WithTLS(t), + ) + t.Cleanup(env.Finish) + + env.Test(ctx, t, sinkbinding.SinkBindingV1DeploymentTLS(ctx)) +} + func TestSinkBindingV1Job(t *testing.T) { t.Parallel()