From 9816419295643f07fe29c961a3e954af678ebba6 Mon Sep 17 00:00:00 2001 From: Dibyo Mukherjee Date: Fri, 7 May 2021 12:41:26 -0400 Subject: [PATCH] Migrate from test builders to structs This is helpful as we migrate to the v1beta1 types since we won't have to maintain separate test builders for v1beta1 types. Ref #1067, #735 Signed-off-by: Dibyo Mukherjee --- ...cluster_trigger_binding_validation_test.go | 77 +- .../event_listener_validation_test.go | 1006 ++++++++++++----- .../trigger_binding_validation_test.go | 152 ++- .../trigger_template_validation_test.go | 427 ++++--- .../v1alpha1/trigger_validation_test.go | 335 +++--- .../eventlistener/eventlistener_test.go | 182 +-- pkg/template/event_test.go | 325 ++++-- pkg/template/resource_test.go | 170 ++- 8 files changed, 1750 insertions(+), 924 deletions(-) diff --git a/pkg/apis/triggers/v1alpha1/cluster_trigger_binding_validation_test.go b/pkg/apis/triggers/v1alpha1/cluster_trigger_binding_validation_test.go index 48e531b18..40182c98f 100644 --- a/pkg/apis/triggers/v1alpha1/cluster_trigger_binding_validation_test.go +++ b/pkg/apis/triggers/v1alpha1/cluster_trigger_binding_validation_test.go @@ -21,7 +21,7 @@ import ( "testing" "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" - bldr "github.com/tektoncd/triggers/test/builder" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func Test_ClusterTriggerBindingValidate(t *testing.T) { @@ -30,23 +30,49 @@ func Test_ClusterTriggerBindingValidate(t *testing.T) { tb *v1alpha1.ClusterTriggerBinding }{{ name: "empty", - tb: bldr.ClusterTriggerBinding("name"), + tb: &v1alpha1.ClusterTriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + }, + }, }, { name: "multiple params", - tb: bldr.ClusterTriggerBinding("name", - bldr.ClusterTriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$(body.input1)"), - bldr.TriggerBindingParam("param2", "$(body.input2)"), - bldr.TriggerBindingParam("param3", "$(body.input3)"), - )), + tb: &v1alpha1.ClusterTriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$(body.input1)", + }, { + Name: "param2", + Value: "$(body.input2)", + }, { + Name: "param3", + Value: "$(body.input3)", + }}, + }, + }, }, { name: "multiple params case sensitive", - tb: bldr.ClusterTriggerBinding("name", - bldr.ClusterTriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$(body.input1)"), - bldr.TriggerBindingParam("PARAM1", "$(body.input2)"), - bldr.TriggerBindingParam("Param1", "$(body.input3)"), - )), + tb: &v1alpha1.ClusterTriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$(body.input1)", + }, { + Name: "PARAM1", + Value: "$(body.input2)", + }, { + Name: "param3", + Value: "$(body.input3)", + }}, + }, + }, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -63,12 +89,23 @@ func Test_ClusterTriggerBindingValidate_error(t *testing.T) { tb *v1alpha1.ClusterTriggerBinding }{{ name: "duplicate params", - tb: bldr.ClusterTriggerBinding("name", - bldr.ClusterTriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$(body.param1)"), - bldr.TriggerBindingParam("param1", "$(body.param1)"), - bldr.TriggerBindingParam("param3", "$(body.param1)"), - )), + tb: &v1alpha1.ClusterTriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$(body.input1)", + }, { + Name: "param1", + Value: "$(body.input2)", + }, { + Name: "param3", + Value: "$(body.input3)", + }}, + }, + }, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/apis/triggers/v1alpha1/event_listener_validation_test.go b/pkg/apis/triggers/v1alpha1/event_listener_validation_test.go index ca484d9b4..cd36c7ef1 100644 --- a/pkg/apis/triggers/v1alpha1/event_listener_validation_test.go +++ b/pkg/apis/triggers/v1alpha1/event_listener_validation_test.go @@ -20,10 +20,10 @@ import ( "context" "testing" - pipelinev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" "github.com/tektoncd/triggers/test" - bldr "github.com/tektoncd/triggers/test/builder" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -38,113 +38,276 @@ func Test_EventListenerValidate(t *testing.T) { el *v1alpha1.EventListener }{{ name: "TriggerTemplate Does Not Exist", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("dne", "v1alpha1"))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("dne"), + APIVersion: "v1alpha1", + }, + }}, + }, + }, }, { name: "Valid EventListener No TriggerBinding", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1"))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + APIVersion: "v1alpha1", + }, + }}, + }, + }, }, { name: "Valid EventListener with TriggerRef", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTriggerRef("tt"))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + TriggerRef: "tt", + }}, + }, + }, }, { name: "Valid EventListener with TriggerBinding", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "TriggerBinding", "v1alpha1"), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{ + Ref: "tb", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }}, + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt2"), + }, + }}, + }, + }, }, { name: "Valid EventListener with ClusterTriggerBinding", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "ClusterTriggerBinding", "v1alpha1"), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{ + Ref: "tb", + Kind: v1alpha1.ClusterTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }}, + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt2"), + }, + }}, + }, + }, }, { name: "Valid EventListener with multiple TriggerBindings", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb1", "ClusterTriggerBinding", "v1alpha1"), - bldr.EventListenerTriggerBinding("tb2", "TriggerBinding", "v1alpha1"), - bldr.EventListenerTriggerBinding("tb3", "", "v1alpha1"), - ))), - }, { - name: "Valid EventListener No Interceptor", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - ))), - }, { - name: "Valid EventListener Interceptor", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("svc", "v1", "Service", "namespace"), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{ + Ref: "tb1", + Kind: v1alpha1.ClusterTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }, { + Ref: "tb2", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }, { + Ref: "tb3", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }}, + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt2"), + }, + }}, + }, + }, + }, { + name: "Valid EventListener Webhook Interceptor", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Interceptors: []*v1alpha1.EventInterceptor{{ + Webhook: &v1alpha1.WebhookInterceptor{ + ObjectRef: &corev1.ObjectReference{ + Name: "svc", + Kind: "Service", + APIVersion: "v1", + Namespace: "namespace", + }, + }, + }}, + Bindings: []*v1alpha1.EventListenerBinding{{ + Ref: "tb", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }}, + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt2"), + }, + }}, + }, + }, }, { name: "Valid EventListener Interceptor With Header", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("svc", "v1", "Service", "namespace", - bldr.EventInterceptorParam("Valid-Header-Key", "valid value"), - ), - ))), - }, { - name: "Valid EventListener Interceptor With Headers", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("svc", "v1", "Service", "namespace", - bldr.EventInterceptorParam("Valid-Header-Key1", "valid value1"), - bldr.EventInterceptorParam("Valid-Header-Key1", "valid value2"), - bldr.EventInterceptorParam("Valid-Header-Key2", "valid value"), - ), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Interceptors: []*v1alpha1.EventInterceptor{{ + Webhook: &v1alpha1.WebhookInterceptor{ + Header: []pipelinev1.Param{{ + Name: "Valid-Header-Key", + Value: pipelinev1.ArrayOrString{ + Type: pipelinev1.ParamTypeString, + StringVal: "valid-value", + }, + }, { + Name: "Valid-Header-Key2", + Value: pipelinev1.ArrayOrString{ + Type: pipelinev1.ParamTypeString, + StringVal: "valid value 2", + }, + }}, + ObjectRef: &corev1.ObjectReference{ + Name: "svc", + Kind: "Service", + APIVersion: "v1", + Namespace: "namespace", + }, + }, + }}, + Bindings: []*v1alpha1.EventListenerBinding{{ + Ref: "tb", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }}, + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt2"), + }, + }}, + }, + }, }, { name: "Valid EventListener Two Triggers", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("svc", "v1", "Service", "namespace"), - ), - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{ + Ref: "tb", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }}, + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }, { + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt2"), + }, + Bindings: []*v1alpha1.EventListenerBinding{{ + Ref: "tb", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }}, + }}, + }, + }, }, { name: "Valid EventListener with CEL interceptor", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerCELInterceptor("body.value == 'test'"), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Interceptors: []*v1alpha1.EventInterceptor{{ + Ref: v1alpha1.InterceptorRef{ + Name: "cel", + Kind: v1alpha1.ClusterInterceptorKind, + APIVersion: "triggers.tekton.dev/v1alpha1", + }, + Params: []v1alpha1.InterceptorParams{{ + Name: "filter", + Value: test.ToV1JSON(t, "body.value == test"), + }, { + Name: "overlays", + Value: test.ToV1JSON(t, []v1alpha1.CELOverlay{{ + Key: "value", + Expression: "testing", + }}), + }}, + }}, + Bindings: []*v1alpha1.EventListenerBinding{{ + Ref: "tb", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }}, + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + }, + }, }, { name: "Valid EventListener with no trigger name", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - ))), - }, { - name: "Valid EventListener with CEL overlays", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerCELInterceptor("", bldr.EventListenerCELOverlay("body.value", "'testing'")), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{ + Ref: "tb", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", // TODO: APIVersions seem wrong? + }}, + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + }, + }, }, { name: "Namespace selector with label selector", el: &v1alpha1.EventListener{ @@ -169,12 +332,20 @@ func Test_EventListenerValidate(t *testing.T) { }, }, { name: "Valid EventListener with kubernetes env for podspec", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1"), - bldr.EventListenerResources( - bldr.EventListenerKubernetesResources( - bldr.EventListenerPodSpec(duckv1.WithPodSpec{ + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + Resources: v1alpha1.Resources{ + KubernetesResource: &v1alpha1.KubernetesResource{ + WithPodSpec: duckv1.WithPodSpec{ Template: duckv1.PodSpecable{ Spec: corev1.PodSpec{ ServiceAccountName: "k8sresource", @@ -199,9 +370,11 @@ func Test_EventListenerValidate(t *testing.T) { }}, }, }, - }), - )), - )), + }, + }, + }, + }, + }, }, { name: "Valid Replicas for EventListener", el: &v1alpha1.EventListener{ @@ -219,12 +392,20 @@ func Test_EventListenerValidate(t *testing.T) { }, }, { name: "Valid EventListener with env for TLS connection", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1"), - bldr.EventListenerResources( - bldr.EventListenerKubernetesResources( - bldr.EventListenerPodSpec(duckv1.WithPodSpec{ + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + Resources: v1alpha1.Resources{ + KubernetesResource: &v1alpha1.KubernetesResource{ + WithPodSpec: duckv1.WithPodSpec{ Template: duckv1.PodSpecable{ Spec: corev1.PodSpec{ ServiceAccountName: "k8sresource", @@ -249,9 +430,11 @@ func Test_EventListenerValidate(t *testing.T) { }}, }, }, - }), - )), - )), + }, + }, + }, + }, + }, }, { name: "Valid EventListener with custom resources", el: &v1alpha1.EventListener{ @@ -292,7 +475,7 @@ func Test_EventListenerValidate(t *testing.T) { Default: ptr.String("val"), }}, ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ - RawExtension: test.RawExtension(t, pipelinev1alpha1.PipelineRun{ + RawExtension: test.RawExtension(t, pipelinev1.PipelineRun{ TypeMeta: metav1.TypeMeta{ APIVersion: "tekton.dev/v1alpha1", Kind: "TaskRun", @@ -307,41 +490,162 @@ func Test_EventListenerValidate(t *testing.T) { }}, }, }, - }} - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - err := test.el.Validate(context.Background()) - if err != nil { - t.Errorf("EventListener.Validate() expected no error, but got one, EventListener: %v, error: %v", test.el, err) - } - }) - } -} - -func TestEventListenerValidate_error(t *testing.T) { - tests := []struct { - name string - el *v1alpha1.EventListener - }{{ - name: "Invalid EventListener name", - el: bldr.EventListener("longlonglonglonglonglonglonglonglonglonglonglonglonglonglname", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1"))), - }, { - name: "Valid EventListener with empty TriggerTemplate name", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("", "v1alpha1"))), - }, { - name: "TriggerBinding with no ref or spec", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("", "", "v1alpha1"), - ))), + }} + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := tc.el.Validate(context.Background()) + if err != nil { + t.Errorf("EventListener.Validate() expected no error, but got one, EventListener: %v, error: %v", tc.el, err) + } + }) + } +} + +func TestEventListenerValidate_error(t *testing.T) { + tests := []struct { + name string + el *v1alpha1.EventListener + }{{ + name: "Invalid EventListener name", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "longlonglonglonglonglonglonglonglonglonglonglonglonglonglname", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + }, + }, + }, { + name: "Valid EventListener with empty TriggerTemplate name", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String(""), + }, + }}, + }, + }, + }, { + name: "TriggerBinding with no ref or spec", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{ + APIVersion: "triggers.tekton.dev/v1alpha1", + }}, + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + }, + }, + }, { + name: "Bindings invalid ref", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{Ref: "", Kind: v1alpha1.NamespacedTriggerBindingKind}}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + }}, + }, + }, + }, { + name: "Bindings missing kind", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{Ref: "tb", Kind: ""}}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + }}, + }, + }, + }, { + name: "Template with wrong apiVersion", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt"), APIVersion: "invalid"}, + }}, + }, + }, + }, { + name: "Template with missing name", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String(""), APIVersion: "v1alpha1"}, + }}, + }, + }, + }, { + name: "Interceptor Name only", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + Interceptors: []*v1alpha1.EventInterceptor{{ + Webhook: &v1alpha1.WebhookInterceptor{ + ObjectRef: &corev1.ObjectReference{Name: "svc"}, + }, + }}, + }}, + }, + }, + }, { + name: "Interceptor Missing ObjectRef", + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + Interceptors: []*v1alpha1.EventInterceptor{{}}, + }}, + }, + }, }, { - name: "Bindings invalid ref", + name: "Interceptor Empty ObjectRef", el: &v1alpha1.EventListener{ ObjectMeta: metav1.ObjectMeta{ Name: "name", @@ -349,13 +653,20 @@ func TestEventListenerValidate_error(t *testing.T) { }, Spec: v1alpha1.EventListenerSpec{ Triggers: []v1alpha1.EventListenerTrigger{{ - Bindings: []*v1alpha1.EventListenerBinding{{Ref: "", Kind: v1alpha1.NamespacedTriggerBindingKind}}, + Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + Interceptors: []*v1alpha1.EventInterceptor{{ + Webhook: &v1alpha1.WebhookInterceptor{ + ObjectRef: &corev1.ObjectReference{ + Name: "", + }, + }, + }}, }}, }, }, }, { - name: "Bindings missing kind", + name: "Valid EventListener with invalid TriggerBinding", el: &v1alpha1.EventListener{ ObjectMeta: metav1.ObjectMeta{ Name: "name", @@ -363,13 +674,13 @@ func TestEventListenerValidate_error(t *testing.T) { }, Spec: v1alpha1.EventListenerSpec{ Triggers: []v1alpha1.EventListenerTrigger{{ - Bindings: []*v1alpha1.EventListenerBinding{{Ref: "tb", Kind: ""}}, - Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + Bindings: []*v1alpha1.EventListenerBinding{{Kind: "badBindingKind", Ref: "tb"}}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String(""), APIVersion: "v1alpha1"}, }}, }, }, }, { - name: "Template with wrong apiVersion", + name: "Interceptor Wrong APIVersion", el: &v1alpha1.EventListener{ ObjectMeta: metav1.ObjectMeta{ Name: "name", @@ -378,12 +689,22 @@ func TestEventListenerValidate_error(t *testing.T) { Spec: v1alpha1.EventListenerSpec{ Triggers: []v1alpha1.EventListenerTrigger{{ Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, - Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt"), APIVersion: "invalid"}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + Interceptors: []*v1alpha1.EventInterceptor{{ + Webhook: &v1alpha1.WebhookInterceptor{ + ObjectRef: &corev1.ObjectReference{ + Name: "foo", + APIVersion: "v3", + Kind: "Service", + Namespace: "default", + }, + }, + }}, }}, }, }, }, { - name: "Template with missing name", + name: "Interceptor Wrong Kind", el: &v1alpha1.EventListener{ ObjectMeta: metav1.ObjectMeta{ Name: "name", @@ -392,20 +713,22 @@ func TestEventListenerValidate_error(t *testing.T) { Spec: v1alpha1.EventListenerSpec{ Triggers: []v1alpha1.EventListenerTrigger{{ Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, - Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String(""), APIVersion: "v1alpha1"}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + Interceptors: []*v1alpha1.EventInterceptor{{ + Webhook: &v1alpha1.WebhookInterceptor{ + ObjectRef: &corev1.ObjectReference{ + Name: "foo", + APIVersion: "v1", + Kind: "Deployment", + Namespace: "default", + }, + }, + }}, }}, }, }, }, { - name: "Interceptor Name only", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("svc", "", "", ""), - ))), - }, { - name: "Interceptor Missing ObjectRef", + name: "Interceptor Non-Canonical Header", el: &v1alpha1.EventListener{ ObjectMeta: metav1.ObjectMeta{ Name: "name", @@ -413,14 +736,30 @@ func TestEventListenerValidate_error(t *testing.T) { }, Spec: v1alpha1.EventListenerSpec{ Triggers: []v1alpha1.EventListenerTrigger{{ - Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, - Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, - Interceptors: []*v1alpha1.EventInterceptor{{}}, + Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + Interceptors: []*v1alpha1.EventInterceptor{{ + Webhook: &v1alpha1.WebhookInterceptor{ + Header: []v1beta1.Param{{ + Name: "non-canonical-header-key", + Value: v1beta1.ArrayOrString{ + Type: v1beta1.ParamTypeString, + StringVal: "valid value", + }, + }}, + ObjectRef: &corev1.ObjectReference{ + Name: "foo", + APIVersion: "v1", + Kind: "Service", + Namespace: "default", + }, + }, + }}, }}, }, }, }, { - name: "Interceptor Empty ObjectRef", + name: "Interceptor Empty Header Name", el: &v1alpha1.EventListener{ ObjectMeta: metav1.ObjectMeta{ Name: "name", @@ -432,67 +771,55 @@ func TestEventListenerValidate_error(t *testing.T) { Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, Interceptors: []*v1alpha1.EventInterceptor{{ Webhook: &v1alpha1.WebhookInterceptor{ - ObjectRef: &corev1.ObjectReference{ + Header: []v1beta1.Param{{ Name: "", + Value: v1beta1.ArrayOrString{ + Type: v1beta1.ParamTypeString, + StringVal: "valid value", + }, + }}, + ObjectRef: &corev1.ObjectReference{ + Name: "foo", + APIVersion: "v1", + Kind: "Service", + Namespace: "default", }, }, }}, }}, }, }, - }, { - name: "Valid EventListener with invalid TriggerBinding", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "NamespaceTriggerBinding", "v1alpha1"), - ))), - }, { - name: "Interceptor Wrong APIVersion", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("foo", "v3", "Service", ""), - ))), - }, { - name: "Interceptor Wrong Kind", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("foo", "v1", "Deployment", ""), - ))), - }, { - name: "Interceptor Non-Canonical Header", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("foo", "v1", "Deployment", "", - bldr.EventInterceptorParam("non-canonical-header-key", "valid value"), - ), - ))), - }, { - name: "Interceptor Empty Header Name", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("foo", "v1", "Deployment", "", - bldr.EventInterceptorParam("", "valid value"), - ), - ))), }, { name: "Interceptor Empty Header Value", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerInterceptor("foo", "v1", "Deployment", "", - bldr.EventInterceptorParam("Valid-Header-Key", ""), - ), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String("tt")}, + Interceptors: []*v1alpha1.EventInterceptor{{ + Webhook: &v1alpha1.WebhookInterceptor{ + Header: []v1beta1.Param{{ + Name: "Valid-Header-Key", + Value: v1beta1.ArrayOrString{ + Type: v1beta1.ParamTypeString, + StringVal: "", + }, + }}, + ObjectRef: &corev1.ObjectReference{ + Name: "foo", + APIVersion: "v1", + Kind: "Service", + Namespace: "default", + }, + }, + }}, + }}, + }, + }, }, { name: "Multiple interceptors set", el: &v1alpha1.EventListener{ @@ -531,44 +858,94 @@ func TestEventListenerValidate_error(t *testing.T) { }, }, { name: "CEL interceptor with bad filter expression", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerCELInterceptor("body.value == 'test')"), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Interceptors: []*v1alpha1.EventInterceptor{{ + DeprecatedCEL: &v1alpha1.CELInterceptor{ + Filter: "body.value == 'test'", + }, + }}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String(""), APIVersion: "v1alpha1"}, + }}, + }, + }, }, { name: "CEL interceptor with bad overlay expression", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerCELInterceptor("", bldr.EventListenerCELOverlay("body.value", "'testing')")), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Interceptors: []*v1alpha1.EventInterceptor{{ + DeprecatedCEL: &v1alpha1.CELInterceptor{ + Overlays: []v1alpha1.CELOverlay{{ + Key: "value", + Expression: "'testing')", + }}, + }, + }}, + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String(""), APIVersion: "v1alpha1"}, + }}, + }, + }, }, { name: "Triggers name has invalid label characters", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerName("github.com/tektoncd/triggers"), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, + Name: "github.com/tektoncd/triggers", + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String(""), APIVersion: "v1alpha1"}, + }}, + }, + }, }, { name: "Triggers name is longer than the allowable label value (63 characters)", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "", "v1alpha1"), - bldr.EventListenerTriggerName("1234567890123456789012345678901234567890123456789012345678901234"), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Bindings: []*v1alpha1.EventListenerBinding{{Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, + Name: "1234567890123456789012345678901234567890123456789012345678901234", + Template: &v1alpha1.EventListenerTemplate{Ref: ptr.String(""), APIVersion: "v1alpha1"}, + }}, + }, + }, }, { name: "user specify invalid deprecated replicas", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerReplicas(-1), - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "TriggerBinding", "v1alpha1"), - ))), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + DeprecatedReplicas: ptr.Int32(-1), + Resources: v1alpha1.Resources{ + KubernetesResource: &v1alpha1.KubernetesResource{ + Replicas: ptr.Int32(-1), + }, + }, + }, + }, }, { name: "user specify invalid replicas", el: &v1alpha1.EventListener{ @@ -577,6 +954,11 @@ func TestEventListenerValidate_error(t *testing.T) { Namespace: "namespace", }, Spec: v1alpha1.EventListenerSpec{ + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, Resources: v1alpha1.Resources{ KubernetesResource: &v1alpha1.KubernetesResource{ Replicas: ptr.Int32(-1), @@ -586,14 +968,15 @@ func TestEventListenerValidate_error(t *testing.T) { }, }, { name: "user specify multiple containers", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "TriggerBinding", "v1alpha1"), - ), - bldr.EventListenerResources( - bldr.EventListenerKubernetesResources( - bldr.EventListenerPodSpec(duckv1.WithPodSpec{ + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Resources: v1alpha1.Resources{ + KubernetesResource: &v1alpha1.KubernetesResource{ + WithPodSpec: duckv1.WithPodSpec{ Template: duckv1.PodSpecable{ Spec: corev1.PodSpec{ Containers: []corev1.Container{{ @@ -603,37 +986,53 @@ func TestEventListenerValidate_error(t *testing.T) { }}, }, }, - }), - )), - )), + }, + }, + }, + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + }, + }, }, { name: "user specifies an unsupported podspec field", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "TriggerBinding", "v1alpha1"), - ), - bldr.EventListenerResources( - bldr.EventListenerKubernetesResources( - bldr.EventListenerPodSpec(duckv1.WithPodSpec{ + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Resources: v1alpha1.Resources{ + KubernetesResource: &v1alpha1.KubernetesResource{ + WithPodSpec: duckv1.WithPodSpec{ Template: duckv1.PodSpecable{ Spec: corev1.PodSpec{ NodeName: "minikube", }, }, - }), - )), - )), + }, + }, + }, + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + }, + }, }, { name: "user specifies an unsupported container fields", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "TriggerBinding", "v1alpha1"), - ), - bldr.EventListenerResources( - bldr.EventListenerKubernetesResources( - bldr.EventListenerPodSpec(duckv1.WithPodSpec{ + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Resources: v1alpha1.Resources{ + KubernetesResource: &v1alpha1.KubernetesResource{ + WithPodSpec: duckv1.WithPodSpec{ Template: duckv1.PodSpecable{ Spec: corev1.PodSpec{ Containers: []corev1.Container{{ @@ -652,19 +1051,27 @@ func TestEventListenerValidate_error(t *testing.T) { }}, }, }, - }), - )), - )), + }, + }, + }, + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + }, + }, }, { name: "user specifies an invalid env for TLS connection", - el: bldr.EventListener("name", "namespace", - bldr.EventListenerSpec( - bldr.EventListenerTrigger("tt", "v1alpha1", - bldr.EventListenerTriggerBinding("tb", "TriggerBinding", "v1alpha1"), - ), - bldr.EventListenerResources( - bldr.EventListenerKubernetesResources( - bldr.EventListenerPodSpec(duckv1.WithPodSpec{ + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.EventListenerSpec{ + Resources: v1alpha1.Resources{ + KubernetesResource: &v1alpha1.KubernetesResource{ + WithPodSpec: duckv1.WithPodSpec{ Template: duckv1.PodSpecable{ Spec: corev1.PodSpec{ Containers: []corev1.Container{{ @@ -682,9 +1089,16 @@ func TestEventListenerValidate_error(t *testing.T) { }}, }, }, - }), - )), - )), + }, + }, + }, + Triggers: []v1alpha1.EventListenerTrigger{{ + Template: &v1alpha1.EventListenerTemplate{ + Ref: ptr.String("tt"), + }, + }}, + }, + }, }, { name: "user specify both kubernetes and custom resources", el: &v1alpha1.EventListener{ @@ -733,7 +1147,7 @@ func TestEventListenerValidate_error(t *testing.T) { }, }, }, { - name: "Specify TriggerTemplate along with TriggerRef", + name: "specify TriggerTemplate along with TriggerRef", el: &v1alpha1.EventListener{ ObjectMeta: metav1.ObjectMeta{ Name: "name", @@ -755,10 +1169,10 @@ func TestEventListenerValidate_error(t *testing.T) { }, }} - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - if err := test.el.Validate(context.Background()); err == nil { - t.Errorf("EventListener.Validate() expected error, but get none, EventListener: %v", test.el) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if err := tc.el.Validate(context.Background()); err == nil { + t.Errorf("EventListener.Validate() expected error, but get none, EventListener: %v", tc.el) } }) } diff --git a/pkg/apis/triggers/v1alpha1/trigger_binding_validation_test.go b/pkg/apis/triggers/v1alpha1/trigger_binding_validation_test.go index 97528b21d..63bd78b1e 100644 --- a/pkg/apis/triggers/v1alpha1/trigger_binding_validation_test.go +++ b/pkg/apis/triggers/v1alpha1/trigger_binding_validation_test.go @@ -22,7 +22,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" - bldr "github.com/tektoncd/triggers/test/builder" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func Test_TriggerBindingValidate(t *testing.T) { @@ -31,30 +31,69 @@ func Test_TriggerBindingValidate(t *testing.T) { tb *v1alpha1.TriggerBinding }{{ name: "empty", - tb: bldr.TriggerBinding("name", "namespace"), + tb: &v1alpha1.TriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + }, }, { name: "multiple params", - tb: bldr.TriggerBinding("name", "namespace", - bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$(body.input1)"), - bldr.TriggerBindingParam("param2", "$(body.input2)"), - bldr.TriggerBindingParam("param3", "$(body.(input3))"), - bldr.TriggerBindingParam("param4", "static-input"), - )), + tb: &v1alpha1.TriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$(body.input1)", + }, { + Name: "param2", + Value: "$(body.input2)", + }, { + Name: "param3", + Value: "$(body.(input3))", + }, { + Name: "param4", + Value: "static-input", + }}, + }, + }, }, { name: "multiple params case sensitive", - tb: bldr.TriggerBinding("name", "namespace", - bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$(body.input1)"), - bldr.TriggerBindingParam("PARAM1", "$(body.input2)"), - bldr.TriggerBindingParam("Param1", "$(body.input3)"), - )), + tb: &v1alpha1.TriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$(body.input1)", + }, { + Name: "PARAM1", + Value: "$(body.input2)", + }, { + Name: "param3", + Value: "$(body.input3)", + }}, + }, + }, }, { name: "multiple expressions in one body", - tb: bldr.TriggerBinding("name", "namespace", - bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$(body.input1)-$(body.input2)"), - )), + tb: &v1alpha1.TriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$(body.input1)-$(body.input2)", + }}, + }, + }, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -72,33 +111,72 @@ func Test_TriggerBindingValidate_error(t *testing.T) { errMsg string }{{ name: "duplicate params", - tb: bldr.TriggerBinding("name", "namespace", - bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$(body.param1)"), - bldr.TriggerBindingParam("param1", "$(body.param1)"), - bldr.TriggerBindingParam("param3", "$(body.param1)"), - )), + tb: &v1alpha1.TriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$(body.input1)", + }, { + Name: "param1", + Value: "$(body.input2)", + }, { + Name: "param3", + Value: "$(body.input3)", + }}, + }, + }, errMsg: "expected exactly one, got both: spec.params[1].name", }, { name: "invalid parameter", - tb: bldr.TriggerBinding("name", "namespace", - bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$($(body.param1))"), - )), + tb: &v1alpha1.TriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$($(body.param1))", + }, + }, + }, + }, errMsg: "invalid value: $($(body.param1)): spec.params[0].value", }, { name: "invalid parameter further nested", - tb: bldr.TriggerBinding("name", "namespace", - bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$(body.test-$(body.param1))"), - )), + tb: &v1alpha1.TriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$(body.test-$(body.param1))", + }, + }, + }, + }, errMsg: "invalid value: $(body.test-$(body.param1)): spec.params[0].value", }, { name: "invalid parameter triple nested", - tb: bldr.TriggerBinding("name", "namespace", - bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "$($($(body.param1)))"), - )), + tb: &v1alpha1.TriggerBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerBindingSpec{ + Params: []v1alpha1.Param{{ + Name: "param1", + Value: "$($($(body.param1)))", + }, + }, + }, + }, errMsg: "invalid value: $($($(body.param1))): spec.params[0].value", }} for _, tt := range tests { diff --git a/pkg/apis/triggers/v1alpha1/trigger_template_validation_test.go b/pkg/apis/triggers/v1alpha1/trigger_template_validation_test.go index fc349ea3b..37833c6a9 100644 --- a/pkg/apis/triggers/v1alpha1/trigger_template_validation_test.go +++ b/pkg/apis/triggers/v1alpha1/trigger_template_validation_test.go @@ -21,14 +21,12 @@ import ( "testing" pipelinev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - pipelinev1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" "github.com/tektoncd/triggers/test" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "knative.dev/pkg/ptr" "github.com/google/go-cmp/cmp" - "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" - b "github.com/tektoncd/triggers/test/builder" - "k8s.io/apimachinery/pkg/runtime" "knative.dev/pkg/apis" ) @@ -42,10 +40,10 @@ func simpleResourceTemplate(t *testing.T) runtime.RawExtension { }) } -func v1beta1ResourceTemplate(t *testing.T) runtime.RawExtension { - return test.RawExtension(t, pipelinev1beta1.PipelineRun{ +func v1alpha1ResourceTemplate(t *testing.T) runtime.RawExtension { + return test.RawExtension(t, pipelinev1alpha1.PipelineRun{ TypeMeta: metav1.TypeMeta{ - APIVersion: "tekton.dev/v1beta1", + APIVersion: "tekton.dev/v1alpha1", Kind: "PipelineRun", }, }) @@ -96,134 +94,293 @@ func TestTriggerTemplate_Validate(t *testing.T) { name string template *v1alpha1.TriggerTemplate want *apis.FieldError - }{ - { - name: "invalid objectmetadata, name with dot", - template: b.TriggerTemplate("t.t", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(simpleResourceTemplate(t)))), - want: &apis.FieldError{ - Message: "Invalid resource name: special character . must not be present", - Paths: []string{"metadata.name"}, - }, - }, - { - name: "invalid objectmetadata, name too long", - template: b.TriggerTemplate( - "ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt", - "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(simpleResourceTemplate(t)))), - want: &apis.FieldError{ - Message: "Invalid resource name: length must be no more than 63 characters", - Paths: []string{"metadata.name"}, - }, - }, - { - name: "valid template", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(simpleResourceTemplate(t)))), - want: nil, - }, { - name: "valid v1beta1 template", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(v1beta1ResourceTemplate(t)))), - want: nil, - }, { - name: "missing resource template", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"))), - want: &apis.FieldError{ - Message: "missing field(s)", - Paths: []string{"spec.resourcetemplates"}, - }, - }, { - name: "resource template missing kind", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(test.RawExtension(t, pipelinev1alpha1.PipelineRun{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "tekton.dev/v1alpha1", - }, - })))), - want: &apis.FieldError{ - Message: "missing field(s)", - Paths: []string{"spec.resourcetemplates[0].kind"}, - }, - }, { - name: "resource template missing apiVersion", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(test.RawExtension(t, pipelinev1alpha1.PipelineRun{ - TypeMeta: metav1.TypeMeta{ - Kind: "PipelineRun", - }, - })))), - want: &apis.FieldError{ - Message: "missing field(s)", - Paths: []string{"spec.resourcetemplates[0].apiVersion"}, - }, - }, { - name: "resource template invalid apiVersion", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(test.RawExtension(t, pipelinev1alpha1.PipelineRun{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "foobar", - Kind: "pipelinerun", - }, - })))), - want: &apis.FieldError{ - Message: `invalid value: no kind "pipelinerun" is registered for version "foobar"`, - Paths: []string{"spec.resourcetemplates[0]"}, - }, - }, { - name: "resource template invalid kind", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(test.RawExtension(t, pipelinev1alpha1.PipelineRun{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "foo", - Kind: "tekton.dev/v1alpha1", - }, - })))), - want: &apis.FieldError{ - Message: `invalid value: no kind "tekton.dev/v1alpha1" is registered for version "foo"`, - Paths: []string{"spec.resourcetemplates[0]"}, - }, - }, { - name: "tt.params used in resource template are declared", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(paramResourceTemplate(t)))), - want: nil, - }, { - name: "tt.params used in resource template are not declared", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerResourceTemplate(paramResourceTemplate(t)))), - want: &apis.FieldError{ - Message: "invalid value: undeclared param '$(tt.params.foo)'", - Paths: []string{"spec.resourcetemplates[0]"}, - Details: "'$(tt.params.foo)' must be declared in spec.params", - }, - }, { - name: "invalid params used in resource template are not declared", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerResourceTemplate(invalidParamResourceTemplate(t)))), - want: nil, - }, { - name: "invalid params used in resource template are declared", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec( - b.TriggerTemplateParam("foo", "desc", "val"), - b.TriggerResourceTemplate(invalidParamResourceTemplate(t)))), - want: nil, - }, { - name: "no spec to triggertemplate", - template: b.TriggerTemplate("tt", "foo", b.TriggerTemplateSpec()), - want: apis.ErrMissingField("spec", "spec.resourcetemplates"), - }} + }{{ + name: "invalid objectmetadata, name with dot", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "t.t", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: simpleResourceTemplate(t), + }}, + }, + }, + want: &apis.FieldError{ + Message: "Invalid resource name: special character . must not be present", + Paths: []string{"metadata.name"}, + }, + }, { + name: "invalid objectmetadata, name too long", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: simpleResourceTemplate(t), + }}, + }, + }, + want: &apis.FieldError{ + Message: "Invalid resource name: length must be no more than 63 characters", + Paths: []string{"metadata.name"}, + }, + }, { + name: "valid template", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: simpleResourceTemplate(t), + }}, + }, + }, + want: nil, + }, { + name: "valid v1alpha1 template", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: v1alpha1ResourceTemplate(t), + }}, + }, + }, + want: nil, + }, { + name: "missing resource template", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + }, + }, + want: &apis.FieldError{ + Message: "missing field(s)", + Paths: []string{"spec.resourcetemplates"}, + }, + }, { + name: "resource template missing kind", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: test.RawExtension(t, pipelinev1alpha1.PipelineRun{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "tekton.dev/v1alpha1", + }, + }), + }}, + }, + }, + want: &apis.FieldError{ + Message: "missing field(s)", + Paths: []string{"spec.resourcetemplates[0].kind"}, + }, + }, { + name: "resource template missing apiVersion", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: test.RawExtension(t, pipelinev1alpha1.PipelineRun{ + TypeMeta: metav1.TypeMeta{ + Kind: "PipelineRun", + }, + }), + }}, + }, + }, + want: &apis.FieldError{ + Message: "missing field(s)", + Paths: []string{"spec.resourcetemplates[0].apiVersion"}, + }, + }, { + name: "resource template invalid apiVersion", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: test.RawExtension(t, pipelinev1alpha1.PipelineRun{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "foobar", + Kind: "pipelinerun", + }, + }), + }}, + }, + }, + want: &apis.FieldError{ + Message: `invalid value: no kind "pipelinerun" is registered for version "foobar"`, + Paths: []string{"spec.resourcetemplates[0]"}, + }, + }, { + name: "resource template invalid kind", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: test.RawExtension(t, pipelinev1alpha1.PipelineRun{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "foo", + Kind: "tekton.dev/v1alpha1", + }, + }), + }}, + }, + }, + want: &apis.FieldError{ + Message: `invalid value: no kind "tekton.dev/v1alpha1" is registered for version "foo"`, + Paths: []string{"spec.resourcetemplates[0]"}, + }, + }, { + name: "tt.params used in resource template are declared", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: paramResourceTemplate(t), + }}, + }, + }, + want: nil, + }, { + name: "tt.params used in resource template are not declared", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: paramResourceTemplate(t), + }}, + }, + }, + want: &apis.FieldError{ + Message: "invalid value: undeclared param '$(tt.params.foo)'", + Paths: []string{"spec.resourcetemplates[0]"}, + Details: "'$(tt.params.foo)' must be declared in spec.params", + }, + }, { + name: "invalid params used in resource template are not declared", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: invalidParamResourceTemplate(t), + }}, + }, + }, + want: nil, + }, { + name: "invalid params used in resource template are declared", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + Spec: v1alpha1.TriggerTemplateSpec{ + Params: []v1alpha1.ParamSpec{{ + Name: "foo", + Description: "desc", + Default: ptr.String("val"), + }}, + ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ + RawExtension: invalidParamResourceTemplate(t), + }}, + }, + }, + want: nil, + }, { + name: "no spec to triggertemplate", + template: &v1alpha1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: "foo", + }, + }, + want: apis.ErrMissingField("spec", "spec.resourcetemplates"), + }} for _, tc := range tcs { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/apis/triggers/v1alpha1/trigger_validation_test.go b/pkg/apis/triggers/v1alpha1/trigger_validation_test.go index fc8699067..3460e8022 100644 --- a/pkg/apis/triggers/v1alpha1/trigger_validation_test.go +++ b/pkg/apis/triggers/v1alpha1/trigger_validation_test.go @@ -23,10 +23,7 @@ import ( pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" "github.com/tektoncd/triggers/test" - bldr "github.com/tektoncd/triggers/test/builder" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "knative.dev/pkg/ptr" ) @@ -36,32 +33,83 @@ func Test_TriggerValidate(t *testing.T) { tr *v1alpha1.Trigger }{{ name: "Valid Trigger No TriggerBinding", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"))), + tr: &v1alpha1.Trigger{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerSpec{ + Template: v1alpha1.TriggerSpecTemplate{ + Ref: ptr.String("tt"), + APIVersion: "v1alpha1", + }, + }, + }, }, { name: "Valid Trigger with TriggerBinding", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "TriggerBinding", "", "v1alpha1"), - )), + tr: &v1alpha1.Trigger{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerSpec{ + Bindings: []*v1alpha1.TriggerSpecBinding{{ + Ref: "tb", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", + }}, + Template: v1alpha1.TriggerSpecTemplate{ + Ref: ptr.String("tt"), + APIVersion: "v1alpha1", + }, + }, + }, }, { name: "Valid Trigger with ClusterTriggerBinding", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "ClusterTriggerBinding", "", "v1alpha1"), - )), + tr: &v1alpha1.Trigger{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerSpec{ + Bindings: []*v1alpha1.TriggerSpecBinding{{ + Ref: "tb", + Kind: v1alpha1.ClusterTriggerBindingKind, + APIVersion: "v1alpha1", + }}, + Template: v1alpha1.TriggerSpecTemplate{ + Ref: ptr.String("tt"), + APIVersion: "v1alpha1", + }, + }, + }, }, { name: "Valid Trigger with multiple TriggerBindings", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "ClusterTriggerBinding", "", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "TriggerBinding", "", "v1alpha1"), - bldr.TriggerSpecBinding("tb3", "", "", "v1alpha1"), - )), + tr: &v1alpha1.Trigger{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerSpec{ + Bindings: []*v1alpha1.TriggerSpecBinding{{ + Ref: "tb", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", + }, { + Ref: "tb", + Kind: v1alpha1.ClusterTriggerBindingKind, + APIVersion: "v1alpha1", + }, { + Ref: "tb2", + Kind: v1alpha1.NamespacedTriggerBindingKind, + APIVersion: "v1alpha1", + }}, + Template: v1alpha1.TriggerSpecTemplate{ + Ref: ptr.String("tt"), + APIVersion: "v1alpha1", + }, + }, + }, }, { name: "Trigger with new embedded TriggerBindings", tr: &v1alpha1.Trigger{ @@ -85,57 +133,53 @@ func Test_TriggerValidate(t *testing.T) { }, }, { name: "Valid Trigger Interceptor", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "", "v1alpha1"), - bldr.TriggerSpecInterceptor("svc", "v1", "Service", "namespace"), - )), - }, { - name: "Valid Trigger Interceptor With Header", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "", "v1alpha1"), - bldr.TriggerSpecInterceptor("svc", "v1", "Service", "namespace", - bldr.TriggerSpecInterceptorParam("Valid-Header-Key", "valid value"), - ), - )), - }, { - name: "Valid Trigger Interceptor With Headers", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "", "v1alpha1"), - bldr.TriggerSpecInterceptor("svc", "v1", "Service", "namespace", - bldr.TriggerSpecInterceptorParam("Valid-Header-Key1", "valid value1"), - bldr.TriggerSpecInterceptorParam("Valid-Header-Key1", "valid value2"), - bldr.TriggerSpecInterceptorParam("Valid-Header-Key2", "valid value"), - ), - )), - }, { - name: "Valid Trigger with CEL interceptor", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "", "v1alpha1"), - bldr.TriggerSpecCELInterceptor("body.value == 'test'"), - )), + tr: &v1alpha1.Trigger{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerSpec{ + Interceptors: []*v1alpha1.TriggerInterceptor{{ + Ref: v1alpha1.InterceptorRef{ + Name: "cel", + Kind: v1alpha1.ClusterInterceptorKind, + APIVersion: "v1alpha1", + }, + Params: []v1alpha1.InterceptorParams{{ + Name: "filter", + Value: test.ToV1JSON(t, "body.value == test"), + }, { + Name: "overlays", + Value: test.ToV1JSON(t, []v1alpha1.CELOverlay{{ + Key: "value", + Expression: "testing", + }}), + }}, + }}, + Bindings: []*v1alpha1.TriggerSpecBinding{{ + Ref: "tb", + Kind: v1alpha1.ClusterTriggerBindingKind, + APIVersion: "v1alpha1", + }}, + Template: v1alpha1.TriggerSpecTemplate{ + Ref: ptr.String("tt"), + APIVersion: "v1alpha1", + }, + }, + }, }, { name: "Valid Trigger with no trigger name", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "", "v1alpha1"), - )), - }, { - name: "Valid Trigger with CEL overlays", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "", "v1alpha1"), - bldr.TriggerSpecCELInterceptor("", bldr.TriggerSpecCELOverlay("body.value", "'testing'")), - )), + tr: &v1alpha1.Trigger{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "namespace", + }, + Spec: v1alpha1.TriggerSpec{ + Template: v1alpha1.TriggerSpecTemplate{ + Ref: ptr.String("tt"), + APIVersion: "v1alpha1", + }, + }, + }, }, { name: "Trigger with embedded Template", tr: &v1alpha1.Trigger{ @@ -190,7 +234,12 @@ func TestTriggerValidate_error(t *testing.T) { tr *v1alpha1.Trigger }{{ name: "TriggerBinding with no spec", - tr: bldr.Trigger("name", "namespace"), + tr: &v1alpha1.Trigger{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + }, }, { name: "Bindings missing ref", tr: &v1alpha1.Trigger{ @@ -275,147 +324,25 @@ func TestTriggerValidate_error(t *testing.T) { Template: v1alpha1.TriggerSpecTemplate{Ref: ptr.String(""), APIVersion: "v1alpha1"}, }, }, - }, { - name: "Interceptor Name only", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "tb", "v1alpha1"), - bldr.TriggerSpecInterceptor("svc", "", "", ""), - )), - }, { - name: "Interceptor Missing ObjectRef", - tr: &v1alpha1.Trigger{ - ObjectMeta: metav1.ObjectMeta{ - Name: "name", - Namespace: "namespace", - }, - Spec: v1alpha1.TriggerSpec{ - Bindings: []*v1alpha1.TriggerSpecBinding{{Name: "tb", Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb", APIVersion: "v1alpha1"}}, - Template: v1alpha1.TriggerSpecTemplate{Ref: ptr.String("tt"), APIVersion: "v1alpha1"}, - Interceptors: []*v1alpha1.TriggerInterceptor{{}}, - }, - }, - }, { - name: "Interceptor Empty ObjectRef", - tr: &v1alpha1.Trigger{ - ObjectMeta: metav1.ObjectMeta{ - Name: "name", - Namespace: "namespace", - }, - Spec: v1alpha1.TriggerSpec{ - Bindings: []*v1alpha1.TriggerSpecBinding{{Name: "tb", Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb", APIVersion: "v1alpha1"}}, - Template: v1alpha1.TriggerSpecTemplate{Ref: ptr.String("tt"), APIVersion: "v1alpha1"}, - Interceptors: []*v1alpha1.TriggerInterceptor{{ - Webhook: &v1alpha1.WebhookInterceptor{ - ObjectRef: &corev1.ObjectReference{ - Name: "", - }, - }, - }}, - }, - }, }, { name: "Valid Trigger with invalid TriggerBinding", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "NamespaceTriggerBinding", "tb", "v1alpha1"), - )), - }, { - name: "Interceptor Wrong APIVersion", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "tb", "v1alpha1"), - bldr.TriggerSpecInterceptor("foo", "v3", "Service", ""), - )), - }, { - name: "Interceptor Wrong Kind", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "tb", "v1alpha1"), - bldr.TriggerSpecInterceptor("foo", "v1", "Deployment", ""), - )), - }, { - name: "Interceptor Non-Canonical Header", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "tb", "v1alpha1"), - bldr.TriggerSpecInterceptor("foo", "v1", "Deployment", "", - bldr.TriggerSpecInterceptorParam("non-canonical-header-key", "valid value"), - ), - )), - }, { - name: "Interceptor Empty Header Name", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "tb", "v1alpha1"), - bldr.TriggerSpecInterceptor("foo", "v1", "Deployment", "", - bldr.TriggerSpecInterceptorParam("", "valid value"), - ), - )), - }, { - name: "Interceptor Empty Header Value", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "tb", "v1alpha1"), - bldr.TriggerSpecInterceptor("foo", "v1", "Deployment", "", - bldr.TriggerSpecInterceptorParam("Valid-Header-Key", ""), - ), - )), - }, { - name: "Multiple interceptors set", - tr: &v1alpha1.Trigger{ - ObjectMeta: metav1.ObjectMeta{ - Name: "name", - Namespace: "namespace", - }, - Spec: v1alpha1.TriggerSpec{ - Bindings: []*v1alpha1.TriggerSpecBinding{{Name: "tb", Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, - Template: v1alpha1.TriggerSpecTemplate{Ref: ptr.String("tt")}, - Interceptors: []*v1alpha1.TriggerInterceptor{{ - DeprecatedGitHub: &v1alpha1.GitHubInterceptor{}, - DeprecatedGitLab: &v1alpha1.GitLabInterceptor{}, - DeprecatedBitbucket: &v1alpha1.BitbucketInterceptor{}, - }}, - }, - }, - }, { - name: "CEL interceptor with no filter or overlays", tr: &v1alpha1.Trigger{ ObjectMeta: metav1.ObjectMeta{ Name: "name", Namespace: "namespace", }, Spec: v1alpha1.TriggerSpec{ - Bindings: []*v1alpha1.TriggerSpecBinding{{Name: "tb", Kind: v1alpha1.NamespacedTriggerBindingKind, Ref: "tb"}}, - Template: v1alpha1.TriggerSpecTemplate{Ref: ptr.String("tt")}, - Interceptors: []*v1alpha1.TriggerInterceptor{{ - DeprecatedCEL: &v1alpha1.CELInterceptor{}, + Bindings: []*v1alpha1.TriggerSpecBinding{{ + Ref: "tb", + Kind: "BADBINDINGKIND", + APIVersion: "v1alpha1", }}, + Template: v1alpha1.TriggerSpecTemplate{ + Ref: ptr.String("tt"), + APIVersion: "v1alpha1", + }, }, }, - }, { - name: "CEL interceptor with bad filter expression", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "tb", "v1alpha1"), - bldr.TriggerSpecCELInterceptor("body.value == 'test')"), - )), - }, { - name: "CEL interceptor with bad overlay expression", - tr: bldr.Trigger("name", "namespace", - bldr.TriggerSpec( - bldr.TriggerSpecTemplate("tt", "v1alpha1"), - bldr.TriggerSpecBinding("tb", "", "tb", "v1alpha1"), - bldr.TriggerSpecCELInterceptor("", bldr.TriggerSpecCELOverlay("body.value", "'testing')")), - )), }, { name: "Trigger template with both ref and spec", tr: &v1alpha1.Trigger{ @@ -432,7 +359,7 @@ func TestTriggerValidate_error(t *testing.T) { }, }, }, { - name: "Trigger template with both name and spec", // TODO(#FIXME): Remove when name field is removed + name: "Trigger template with both name and spec", tr: &v1alpha1.Trigger{ ObjectMeta: metav1.ObjectMeta{Name: "name"}, Spec: v1alpha1.TriggerSpec{ diff --git a/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go b/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go index 95ddac56f..72c763603 100644 --- a/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go +++ b/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go @@ -29,7 +29,6 @@ import ( "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" "github.com/tektoncd/triggers/pkg/system" "github.com/tektoncd/triggers/test" - bldr "github.com/tektoncd/triggers/test/builder" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -40,6 +39,8 @@ import ( k8stest "k8s.io/client-go/testing" "knative.dev/pkg/apis" duckv1 "knative.dev/pkg/apis/duck/v1" + duckv1alpha1 "knative.dev/pkg/apis/duck/v1alpha1" + duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1" fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake" cminformer "knative.dev/pkg/configmap/informer" "knative.dev/pkg/ptr" @@ -163,11 +164,15 @@ func makeConfig(ops ...func(d *Config)) *Config { // It generates a base EventListener that can then be modified by the passed in op function // If no ops are specified, it generates a base EventListener with no triggers and no Status func makeEL(ops ...func(el *v1alpha1.EventListener)) *v1alpha1.EventListener { - e := bldr.EventListener(eventListenerName, namespace, - bldr.EventListenerSpec( - bldr.EventListenerServiceAccount("sa"), - ), - ) + e := &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: eventListenerName, + Namespace: namespace, + }, + Spec: v1alpha1.EventListenerSpec{ + ServiceAccountName: "sa", + }, + } for _, op := range ops { op(e) } @@ -508,50 +513,68 @@ func logConfig(ns string) *corev1.ConfigMap { return lc } -var withTLSPort = bldr.EventListenerStatus( - bldr.EventListenerAddress(listenerHostname(generatedResourceName, namespace, 8443)), -) +func withTLSPort(el *v1alpha1.EventListener) { + el.Status.Address = &duckv1alpha1.Addressable{ + Addressable: duckv1beta1.Addressable{ + URL: &apis.URL{ + Scheme: "http", + Host: listenerHostname(generatedResourceName, namespace, 8443), + }, + }, + } +} -var withKnativeStatus = bldr.EventListenerStatus( - bldr.EventListenerCondition( - v1alpha1.ServiceExists, - corev1.ConditionFalse, - "", "", - ), - bldr.EventListenerCondition( - v1alpha1.DeploymentExists, - corev1.ConditionFalse, - "", "", - ), -) +func withKnativeStatus(el *v1alpha1.EventListener) { + el.Status.Status = duckv1.Status{ + Conditions: []apis.Condition{{ + Type: v1alpha1.ServiceExists, + Status: corev1.ConditionFalse, + }, { + Type: v1alpha1.DeploymentExists, + Status: corev1.ConditionFalse, + }}, + } +} -var withStatus = bldr.EventListenerStatus( - bldr.EventListenerConfig(generatedResourceName), - bldr.EventListenerAddress(listenerHostname(generatedResourceName, namespace, DefaultPort)), - bldr.EventListenerCondition( - v1alpha1.ServiceExists, - corev1.ConditionTrue, - "Service exists", "", - ), - bldr.EventListenerCondition( - v1alpha1.DeploymentExists, - corev1.ConditionTrue, - "Deployment exists", "", - ), - bldr.EventListenerCondition( - apis.ConditionType(appsv1.DeploymentAvailable), - corev1.ConditionTrue, - "Deployment has minimum availability", - "MinimumReplicasAvailable", - ), - bldr.EventListenerCondition( - apis.ConditionType(appsv1.DeploymentProgressing), - corev1.ConditionTrue, - fmt.Sprintf("ReplicaSet \"%s\" has successfully progressed.", eventListenerName), - "NewReplicaSetAvailable", - ), -) +func withStatus(el *v1alpha1.EventListener) { + el.Status = v1alpha1.EventListenerStatus{ + Status: duckv1.Status{ + Conditions: []apis.Condition{{ + Type: apis.ConditionType(appsv1.DeploymentAvailable), + Status: corev1.ConditionTrue, + Message: "Deployment has minimum availability", + Reason: "MinimumReplicasAvailable", + }, { + Type: v1alpha1.DeploymentExists, + Status: corev1.ConditionTrue, + Message: "Deployment exists", + }, { + Type: apis.ConditionType(appsv1.DeploymentProgressing), + Status: corev1.ConditionTrue, + Message: fmt.Sprintf("ReplicaSet \"%s\" has successfully progressed.", eventListenerName), + Reason: "NewReplicaSetAvailable", + }, { + Type: v1alpha1.ServiceExists, + Status: corev1.ConditionTrue, + Message: "Service exists", + }}, + }, + AddressStatus: duckv1alpha1.AddressStatus{ + Address: &duckv1alpha1.Addressable{ + Addressable: duckv1beta1.Addressable{ + URL: &apis.URL{ + Scheme: "http", + Host: listenerHostname(generatedResourceName, namespace, DefaultPort), + }, + }, + }, + }, + Configuration: v1alpha1.EventListenerConfig{ + GeneratedResourceName: generatedResourceName, + }, + } +} func withAddedLabels(el *v1alpha1.EventListener) { el.Labels = updateLabel } @@ -824,9 +847,16 @@ func TestReconcile(t *testing.T) { } }) - elWithPortSet := makeEL(withStatus, bldr.EventListenerStatus( - bldr.EventListenerAddress(listenerHostname(generatedResourceName, namespace, customPort)), - )) + elWithPortSet := makeEL(withStatus, func(el *v1alpha1.EventListener) { + el.Status.Address = &duckv1alpha1.Addressable{ + Addressable: duckv1beta1.Addressable{ + URL: &apis.URL{ + Scheme: "http", + Host: listenerHostname(generatedResourceName, namespace, customPort), + }, + }, + } + }) elDeployment := makeDeployment() elDeploymentWithLabels := makeDeployment(func(d *appsv1.Deployment) { @@ -1847,7 +1877,12 @@ func Test_generateObjectMeta(t *testing.T) { expectedObjectMeta metav1.ObjectMeta }{{ name: "Empty EventListener", - el: bldr.EventListener(eventListenerName, ""), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: eventListenerName, + Namespace: "", + }, + }, expectedObjectMeta: metav1.ObjectMeta{ Namespace: "", Name: "", @@ -1863,11 +1898,18 @@ func Test_generateObjectMeta(t *testing.T) { }, }, { name: "EventListener with Configuration", - el: bldr.EventListener(eventListenerName, "", - bldr.EventListenerStatus( - bldr.EventListenerConfig("generatedName"), - ), - ), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: eventListenerName, + Namespace: "", + }, + Status: v1alpha1.EventListenerStatus{ + Configuration: v1alpha1.EventListenerConfig{ + GeneratedResourceName: "generatedName", + }, + }, + }, + expectedObjectMeta: metav1.ObjectMeta{ Namespace: "", Name: "generatedName", @@ -1883,11 +1925,15 @@ func Test_generateObjectMeta(t *testing.T) { }, }, { name: "EventListener with Labels", - el: bldr.EventListener(eventListenerName, "", - bldr.EventListenerMeta( - bldr.Label("k", "v"), - ), - ), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: eventListenerName, + Namespace: "", + Labels: map[string]string{ + "k": "v", + }, + }, + }, expectedObjectMeta: metav1.ObjectMeta{ Namespace: "", Name: "", @@ -1903,11 +1949,15 @@ func Test_generateObjectMeta(t *testing.T) { }, }, { name: "EventListener with Annotation", - el: bldr.EventListener(eventListenerName, "", - bldr.EventListenerMeta( - bldr.Annotation("k", "v"), - ), - ), + el: &v1alpha1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: eventListenerName, + Namespace: "", + Annotations: map[string]string{ + "k": "v", + }, + }, + }, expectedObjectMeta: metav1.ObjectMeta{ Namespace: "", Name: "", diff --git a/pkg/template/event_test.go b/pkg/template/event_test.go index 0dad0d19d..6c578e64c 100644 --- a/pkg/template/event_test.go +++ b/pkg/template/event_test.go @@ -28,8 +28,9 @@ import ( "github.com/google/uuid" triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" "github.com/tektoncd/triggers/test" - bldr "github.com/tektoncd/triggers/test/builder" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "knative.dev/pkg/ptr" ) const ( @@ -150,126 +151,126 @@ func TestApplyEventValuesToParams(t *testing.T) { extensions map[string]interface{} }{{ name: "header with single values", - params: []triggersv1.Param{bldr.Param("foo", "$(header)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(header)"}}, header: map[string][]string{ "Header-One": {"val1", "val2"}, }, - want: []triggersv1.Param{bldr.Param("foo", `{"Header-One":"val1,val2"}`)}, + want: []triggersv1.Param{{Name: "foo", Value: `{"Header-One":"val1,val2"}`}}, }, { name: "header keys miss-match case", - params: []triggersv1.Param{bldr.Param("foo", "$(header.header-one)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(header.header-one)"}}, header: map[string][]string{ "Header-One": {"val1"}, }, - want: []triggersv1.Param{bldr.Param("foo", "val1")}, + want: []triggersv1.Param{{Name: "foo", Value: "val1"}}, }, { name: "header keys match case", - params: []triggersv1.Param{bldr.Param("foo", "$(header.Header-One)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(header.Header-One)"}}, header: map[string][]string{ "Header-One": {"val1"}, }, - want: []triggersv1.Param{bldr.Param("foo", "val1")}, + want: []triggersv1.Param{{Name: "foo", Value: "val1"}}, }, { name: "headers - multiple values joined by comma", - params: []triggersv1.Param{bldr.Param("foo", "$(header.header-one)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(header.header-one)"}}, header: map[string][]string{ "Header-One": {"val1", "val2"}, }, - want: []triggersv1.Param{bldr.Param("foo", "val1,val2")}, + want: []triggersv1.Param{{Name: "foo", Value: "val1,val2"}}, }, { name: "header values", - params: []triggersv1.Param{bldr.Param("foo", "$(header)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(header)"}}, header: map[string][]string{ "Header-One": {"val1", "val2"}, }, - want: []triggersv1.Param{bldr.Param("foo", `{"Header-One":"val1,val2"}`)}, + want: []triggersv1.Param{{Name: "foo", Value: `{"Header-One":"val1,val2"}`}}, }, { name: "no body", - params: []triggersv1.Param{bldr.Param("foo", "$(body)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body)"}}, body: []byte{}, - want: []triggersv1.Param{bldr.Param("foo", "null")}, + want: []triggersv1.Param{{Name: "foo", Value: "null"}}, }, { name: "empty body", - params: []triggersv1.Param{bldr.Param("foo", "$(body)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body)"}}, body: json.RawMessage(`{}`), - want: []triggersv1.Param{bldr.Param("foo", "{}")}, + want: []triggersv1.Param{{Name: "foo", Value: "{}"}}, }, { name: "entire body", - params: []triggersv1.Param{bldr.Param("foo", "$(body)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body)"}}, body: json.RawMessage(objects), - want: []triggersv1.Param{bldr.Param("foo", strings.ReplaceAll(objects, " ", ""))}, + want: []triggersv1.Param{{Name: "foo", Value: strings.ReplaceAll(objects, " ", "")}}, }, { name: "entire array body", - params: []triggersv1.Param{bldr.Param("foo", "$(body)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body)"}}, body: json.RawMessage(arrays), - want: []triggersv1.Param{bldr.Param("foo", strings.ReplaceAll(arrays, " ", ""))}, + want: []triggersv1.Param{{Name: "foo", Value: strings.ReplaceAll(arrays, " ", "")}}, }, { name: "array key", - params: []triggersv1.Param{bldr.Param("foo", "$(body.a[1])")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body.a[1])"}}, body: json.RawMessage(`{"a": [{"k": 1}, {"k": 2}, {"k": 3}]}`), - want: []triggersv1.Param{bldr.Param("foo", `{"k":2}`)}, + want: []triggersv1.Param{{Name: "foo", Value: `{"k":2}`}}, }, { name: "array last key", - params: []triggersv1.Param{bldr.Param("foo", "$(body.a[-1:])")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body.a[-1:])"}}, body: json.RawMessage(`{"a": [{"k": 1}, {"k": 2}, {"k": 3}]}`), - want: []triggersv1.Param{bldr.Param("foo", `{"k":3}`)}, + want: []triggersv1.Param{{Name: "foo", Value: `{"k":3}`}}, }, { name: "body - key with string val", - params: []triggersv1.Param{bldr.Param("foo", "$(body.a)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body.a)"}}, body: json.RawMessage(objects), - want: []triggersv1.Param{bldr.Param("foo", "v")}, + want: []triggersv1.Param{{Name: "foo", Value: "v"}}, }, { name: "body - key with object val", - params: []triggersv1.Param{bldr.Param("foo", "$(body.c)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body.c)"}}, body: json.RawMessage(objects), - want: []triggersv1.Param{bldr.Param("foo", `{"d":"e"}`)}, + want: []triggersv1.Param{{Name: "foo", Value: `{"d":"e"}`}}, }, { name: "body with special chars", - params: []triggersv1.Param{bldr.Param("foo", "$(body)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body)"}}, body: json.RawMessage(`{"a": "v\r\n烈"}`), - want: []triggersv1.Param{bldr.Param("foo", `{"a":"v\r\n烈"}`)}, + want: []triggersv1.Param{{Name: "foo", Value: `{"a":"v\r\n烈"}`}}, }, { name: "param contains multiple JSONPath expressions", - params: []triggersv1.Param{bldr.Param("foo", "$(body.a): $(body.b)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body.a): $(body.b)"}}, body: json.RawMessage(`{"a": "val1", "b": "val2"}`), - want: []triggersv1.Param{bldr.Param("foo", `val1: val2`)}, + want: []triggersv1.Param{{Name: "foo", Value: `val1: val2`}}, }, { name: "param contains both static values and JSONPath expressions", - params: []triggersv1.Param{bldr.Param("foo", "body.a is: $(body.a)")}, + params: []triggersv1.Param{{Name: "foo", Value: "body.a is: $(body.a)"}}, body: json.RawMessage(`{"a": "val1"}`), - want: []triggersv1.Param{bldr.Param("foo", `body.a is: val1`)}, + want: []triggersv1.Param{{Name: "foo", Value: `body.a is: val1`}}, }, { name: "multiple params", params: []triggersv1.Param{ - bldr.Param("foo", "$(body.a)"), - bldr.Param("bar", "$(header.header-1)"), + {Name: "foo", Value: "$(body.a)"}, + {Name: "bar", Value: "$(header.header-1)"}, }, body: json.RawMessage(`{"a": "val1"}`), header: map[string][]string{ "Header-1": {"val2"}, }, want: []triggersv1.Param{ - bldr.Param("foo", `val1`), - bldr.Param("bar", `val2`), + {Name: "foo", Value: `val1`}, + {Name: "bar", Value: `val2`}, }, }, { name: "Array filters", body: json.RawMessage(`{"child":[{"a": "b", "w": "1"}, {"a": "c", "w": "2"}, {"a": "d", "w": "3"}]}`), - params: []triggersv1.Param{bldr.Param("a", "$(body.child[?(@.a == 'd')].w)")}, - want: []triggersv1.Param{bldr.Param("a", "3")}, + params: []triggersv1.Param{{Name: "a", Value: "$(body.child[?(@.a == 'd')].w)"}}, + want: []triggersv1.Param{{Name: "a", Value: "3"}}, }, { name: "filters + multiple JSONPath expressions", body: json.RawMessage(`{"child":[{"a": "b", "w": "1"}, {"a": "c", "w": "2"}, {"a": "d", "w": "3"}]}`), - params: []triggersv1.Param{bldr.Param("a", "$(body.child[?(@.a == 'd')].w) : $(body.child[0].a)")}, - want: []triggersv1.Param{bldr.Param("a", "3 : b")}, + params: []triggersv1.Param{{Name: "a", Value: "$(body.child[?(@.a == 'd')].w) : $(body.child[0].a)"}}, + want: []triggersv1.Param{{Name: "a", Value: "3 : b"}}, }, { name: "extensions", body: []byte{}, extensions: map[string]interface{}{ "foo": "bar", }, - params: []triggersv1.Param{bldr.Param("a", "$(extensions.foo)")}, - want: []triggersv1.Param{bldr.Param("a", "bar")}, + params: []triggersv1.Param{{Name: "a", Value: "$(extensions.foo)"}}, + want: []triggersv1.Param{{Name: "a", Value: "bar"}}, }, { name: "extensions - extract single value from JSON body", body: []byte{}, @@ -278,8 +279,8 @@ func TestApplyEventValuesToParams(t *testing.T) { "bar": []interface{}{"a", "b", "c"}, }, }, - params: []triggersv1.Param{bldr.Param("a", "$(extensions.foo.bar[1])")}, - want: []triggersv1.Param{bldr.Param("a", "b")}, + params: []triggersv1.Param{{Name: "a", Value: "$(extensions.foo.bar[1])"}}, + want: []triggersv1.Param{{Name: "a", Value: "b"}}, }, { name: "extensions - extract JSON values", body: []byte{}, @@ -293,8 +294,8 @@ func TestApplyEventValuesToParams(t *testing.T) { }, }, }, - params: []triggersv1.Param{bldr.Param("a", "$(extensions.foo)")}, - want: []triggersv1.Param{bldr.Param("a", `[{"a":"1"},{"b":"2"}]`)}, + params: []triggersv1.Param{{Name: "a", Value: "$(extensions.foo)"}}, + want: []triggersv1.Param{{Name: "a", Value: `[{"a":"1"},{"b":"2"}]`}}, }} for _, tt := range tests { @@ -319,19 +320,19 @@ func TestApplyEventValuesToParams_Error(t *testing.T) { extensions map[string]interface{} }{{ name: "missing key", - params: []triggersv1.Param{bldr.Param("foo", "$(body.missing)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body.missing)"}}, body: json.RawMessage(`{}`), }, { name: "non JSON body", - params: []triggersv1.Param{bldr.Param("foo", "$(body)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body)"}}, body: json.RawMessage(`{blahblah}`), }, { name: "invalid expression(s)", - params: []triggersv1.Param{bldr.Param("foo", "$(body.[0])")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(body.[0])"}}, body: json.RawMessage(`["a", "b"]`), }, { name: "invalid extension", - params: []triggersv1.Param{bldr.Param("foo", "$(extensions.missing)")}, + params: []triggersv1.Param{{Name: "foo", Value: "$(extensions.missing)"}}, body: json.RawMessage(`{}`), extensions: map[string]interface{}{ "foo": "bar", @@ -357,73 +358,102 @@ func TestResolveParams(t *testing.T) { template *triggersv1.TriggerTemplate want []triggersv1.Param }{{ - name: "add default values for params with missing values", - bindingParams: []triggersv1.Param{ - bldr.Param("p1", "val1"), + name: "add default values for params with missing values", + bindingParams: []triggersv1.Param{{Name: "p1", Value: "val1"}}, + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt-name", + }, + Spec: triggersv1.TriggerTemplateSpec{ + Params: []triggersv1.ParamSpec{{ + Name: "p2", + Default: ptr.String("defaultVal"), + }}, + }, }, - template: bldr.TriggerTemplate("tt-name", ns, - bldr.TriggerTemplateSpec( - bldr.TriggerTemplateParam("p2", "", "defaultVal"), - ), - ), want: []triggersv1.Param{ - bldr.Param("p1", "val1"), - bldr.Param("p2", "defaultVal"), + {Name: "p2", Value: "defaultVal"}, + {Name: "p1", Value: "val1"}, }, }, { name: "add default values if param missing from body", bindingParams: []triggersv1.Param{ - bldr.Param("p1", "val1"), - bldr.Param("p2", "$(body.p2)"), - }, - template: bldr.TriggerTemplate("tt-name", ns, - bldr.TriggerTemplateSpec( - bldr.TriggerTemplateParam("p2", "", "defaultVal"), - ), - ), - want: []triggersv1.Param{ - bldr.Param("p1", "val1"), - bldr.Param("p2", "defaultVal"), + {Name: "p1", Value: "val1"}, + {Name: "p2", Value: "$(body.p2)"}, }, - }, { - name: "default values do not override event values", - bindingParams: []triggersv1.Param{ - bldr.Param("p1", "val1"), + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt-name", + Namespace: ns, + }, + Spec: triggersv1.TriggerTemplateSpec{ + Params: []triggersv1.ParamSpec{{ + Name: "p2", + Default: ptr.String("defaultVal"), + }}, + }, }, - template: bldr.TriggerTemplate("tt-name", ns, - bldr.TriggerTemplateSpec( - bldr.TriggerTemplateParam("p1", "", "defaultVal"), - ), - ), want: []triggersv1.Param{ - bldr.Param("p1", "val1"), + {Name: "p2", Value: "defaultVal"}, + {Name: "p1", Value: "val1"}, }, + }, { + name: "default values do not override event values", + bindingParams: []triggersv1.Param{{Name: "p1", Value: "val1"}}, + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt-name", + Namespace: ns, + }, + Spec: triggersv1.TriggerTemplateSpec{ + Params: []triggersv1.ParamSpec{{ + Name: "p1", + Default: ptr.String("defaultVal"), + }}, + }, + }, + want: []triggersv1.Param{{Name: "p1", Value: "val1"}}, }, { name: "combination of static values and JSONPath expressions", body: json.RawMessage(`{"foo": "fooValue", "bar": "barValue"}`), bindingParams: []triggersv1.Param{ - bldr.Param("p1", "Event values are - foo: $(body.foo); bar: $(body.bar)"), + {Name: "p1", Value: "Event values are - foo: $(body.foo); bar: $(body.bar)"}, + }, + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt-name", + Namespace: ns, + }, + Spec: triggersv1.TriggerTemplateSpec{}, }, - template: bldr.TriggerTemplate("tt", ns), want: []triggersv1.Param{ - bldr.Param("p1", "Event values are - foo: fooValue; bar: barValue"), + {Name: "p1", Value: "Event values are - foo: fooValue; bar: barValue"}, }, }, { name: "values with newlines", body: json.RawMessage(`{"foo": "bar\r\nbaz"}`), - template: bldr.TriggerTemplate("tt-name", "", - bldr.TriggerTemplateSpec( - bldr.TriggerTemplateParam("param1", "", ""), - bldr.TriggerTemplateParam("param2", "", ""), - ), - ), + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt-name", + Namespace: ns, + }, + Spec: triggersv1.TriggerTemplateSpec{ + Params: []triggersv1.ParamSpec{{ + Name: "param1", + Default: ptr.String(""), + }, { + Name: "param2", + Default: ptr.String(""), + }}, + }, + }, bindingParams: []triggersv1.Param{ - bldr.Param("param1", "qux"), - bldr.Param("param2", "$(body.foo)"), + {Name: "param1", Value: "qux"}, + {Name: "param2", Value: "$(body.foo)"}, }, want: []triggersv1.Param{ - bldr.Param("param1", "qux"), - bldr.Param("param2", "bar\\r\\nbaz"), + {Name: "param1", Value: "qux"}, + {Name: "param2", Value: "bar\\r\\nbaz"}, }, }} @@ -453,13 +483,13 @@ func TestResolveParams_Error(t *testing.T) { }{{ name: "invalid body", bindingParams: []triggersv1.Param{ - bldr.Param("p1", "val1"), + {Name: "p1", Value: "val1"}, }, body: json.RawMessage(`{`), }, { name: "invalid expression", bindingParams: []triggersv1.Param{ - bldr.Param("p1", "$(header.[)"), + {Name: "p1", Value: "$(header.[)"}, }, }} @@ -488,15 +518,27 @@ func TestResolveResources(t *testing.T) { want []json.RawMessage }{{ name: "replace single values in templates", - template: bldr.TriggerTemplate("tt", ns, bldr.TriggerTemplateSpec( - bldr.TriggerTemplateParam("p1", "desc", ""), - bldr.TriggerTemplateParam("p2", "desc", ""), - bldr.TriggerResourceTemplate(runtime.RawExtension{Raw: []byte(`{"rt1": "$(tt.params.p1)-$(tt.params.p2)"}`)}), - bldr.TriggerResourceTemplate(runtime.RawExtension{Raw: []byte(`{"rt2": "$(tt.params.p1)-$(tt.params.p2)"}`)}), - )), + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: ns, + }, + Spec: triggersv1.TriggerTemplateSpec{ + Params: []triggersv1.ParamSpec{{ + Name: "p1", + }, { + Name: "p2", + }}, + ResourceTemplates: []triggersv1.TriggerResourceTemplate{{ + RawExtension: runtime.RawExtension{Raw: []byte(`{"rt1": "$(tt.params.p1)-$(tt.params.p2)"}`)}, + }, { + RawExtension: runtime.RawExtension{Raw: []byte(`{"rt2": "$(tt.params.p1)-$(tt.params.p2)"}`)}, + }}, + }, + }, params: []triggersv1.Param{ - bldr.Param("p1", "val1"), - bldr.Param("p2", "42"), + {Name: "p1", Value: "val1"}, + {Name: "p2", Value: "42"}, }, want: []json.RawMessage{ json.RawMessage(`{"rt1": "val1-42"}`), @@ -504,12 +546,24 @@ func TestResolveResources(t *testing.T) { }, }, { name: "replace JSON string in templates", - template: bldr.TriggerTemplate("tt", ns, bldr.TriggerTemplateSpec( - bldr.TriggerTemplateParam("p1", "desc", ""), - bldr.TriggerResourceTemplate(runtime.RawExtension{Raw: []byte(`{"rt1": "$(tt.params.p1)"}`)}), - )), + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: ns, + }, + Spec: triggersv1.TriggerTemplateSpec{ + Params: []triggersv1.ParamSpec{{ + Name: "p1", + }, { + Name: "p2", + }}, + ResourceTemplates: []triggersv1.TriggerResourceTemplate{{ + RawExtension: runtime.RawExtension{Raw: []byte(`{"rt1": "$(tt.params.p1)"}`)}, + }}, + }, + }, params: []triggersv1.Param{ - bldr.Param("p1", `{"a": "b"}`), + {Name: "p1", Value: `{"a": "b"}`}, }, want: []json.RawMessage{ // json objects get inserted as a valid JSON string @@ -517,30 +571,57 @@ func TestResolveResources(t *testing.T) { }, }, { name: "replace JSON string with special chars in templates", - template: bldr.TriggerTemplate("tt", ns, bldr.TriggerTemplateSpec( - bldr.TriggerTemplateParam("p1", "desc", ""), - bldr.TriggerResourceTemplate(runtime.RawExtension{Raw: []byte(`{"rt1": "$(tt.params.p1)"}`)}), - )), + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: ns, + }, + Spec: triggersv1.TriggerTemplateSpec{ + Params: []triggersv1.ParamSpec{{ + Name: "p1", + }}, + ResourceTemplates: []triggersv1.TriggerResourceTemplate{{ + RawExtension: runtime.RawExtension{Raw: []byte(`{"rt1": "$(tt.params.p1)"}`)}, + }}, + }, + }, params: []triggersv1.Param{ - bldr.Param("p1", `{"a": "v\\r\\n烈"}`), + {Name: "p1", Value: `{"a": "v\\r\\n烈"}`}, }, want: []json.RawMessage{ json.RawMessage(`{"rt1": "{\"a\": \"v\\r\\n烈\"}"}`), }, }, { name: "$(uid) gets replaced with a string", - template: bldr.TriggerTemplate("tt", ns, bldr.TriggerTemplateSpec( - bldr.TriggerResourceTemplate(runtime.RawExtension{Raw: []byte(`{"rt1": "$(uid)"}`)}), - )), + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: ns, + }, + Spec: triggersv1.TriggerTemplateSpec{ + ResourceTemplates: []triggersv1.TriggerResourceTemplate{{ + RawExtension: runtime.RawExtension{Raw: []byte(`{"rt1": "$(uid)"}`)}, + }}, + }, + }, want: []json.RawMessage{ json.RawMessage(`{"rt1": "31313131-3131-4131-b131-313131313131"}`), }, }, { name: "uid replacement is consistent across multiple templates", - template: bldr.TriggerTemplate("tt", ns, bldr.TriggerTemplateSpec( - bldr.TriggerResourceTemplate(runtime.RawExtension{Raw: []byte(`{"rt1": "$(uid)"}`)}), - bldr.TriggerResourceTemplate(runtime.RawExtension{Raw: []byte(`{"rt2": "$(uid)"}`)}), - )), + template: &triggersv1.TriggerTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tt", + Namespace: ns, + }, + Spec: triggersv1.TriggerTemplateSpec{ + ResourceTemplates: []triggersv1.TriggerResourceTemplate{{ + RawExtension: runtime.RawExtension{Raw: []byte(`{"rt1": "$(uid)"}`)}, + }, { + RawExtension: runtime.RawExtension{Raw: []byte(`{"rt2": "$(uid)"}`)}, + }}, + }, + }, want: []json.RawMessage{ json.RawMessage(`{"rt1": "31313131-3131-4131-b131-313131313131"}`), json.RawMessage(`{"rt2": "31313131-3131-4131-b131-313131313131"}`), diff --git a/pkg/template/resource_test.go b/pkg/template/resource_test.go index c0c1c029e..077f8106a 100644 --- a/pkg/template/resource_test.go +++ b/pkg/template/resource_test.go @@ -26,7 +26,6 @@ import ( pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" "github.com/tektoncd/triggers/test" - bldr "github.com/tektoncd/triggers/test/builder" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/ptr" ) @@ -692,19 +691,39 @@ func TestMergeBindingParams(t *testing.T) { }{{ name: "empty bindings", clusterBindings: []*triggersv1.ClusterTriggerBinding{}, - bindings: []*triggersv1.TriggerBinding{ - bldr.TriggerBinding("", "", bldr.TriggerBindingSpec()), - bldr.TriggerBinding("", "", bldr.TriggerBindingSpec()), + bindings: []*triggersv1.TriggerBinding{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "", + Namespace: "", + }, + Spec: triggersv1.TriggerBindingSpec{}, + }, { + ObjectMeta: metav1.ObjectMeta{ + Name: "", + Namespace: "", + }, + Spec: triggersv1.TriggerBindingSpec{}, + }, }, want: []triggersv1.Param{}, }, { name: "single binding with multiple params", clusterBindings: []*triggersv1.ClusterTriggerBinding{}, - bindings: []*triggersv1.TriggerBinding{ - bldr.TriggerBinding("", "", bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "value1"), - bldr.TriggerBindingParam("param2", "value2"), - )), + bindings: []*triggersv1.TriggerBinding{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "", + Namespace: "", + }, + Spec: triggersv1.TriggerBindingSpec{ + Params: []triggersv1.Param{{ + Name: "param1", + Value: "value1", + }, { + Name: "param2", + Value: "value2", + }}, + }, + }, }, want: []triggersv1.Param{{ Name: "param1", @@ -715,12 +734,20 @@ func TestMergeBindingParams(t *testing.T) { }}, }, { name: "single cluster type binding with multiple params", - clusterBindings: []*triggersv1.ClusterTriggerBinding{ - bldr.ClusterTriggerBinding("", bldr.ClusterTriggerBindingSpec( - bldr.TriggerBindingParam("param1", "value1"), - bldr.TriggerBindingParam("param2", "value2"), - )), - }, + clusterBindings: []*triggersv1.ClusterTriggerBinding{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "ctb", + }, + Spec: triggersv1.TriggerBindingSpec{ + Params: []triggersv1.Param{{ + Name: "param1", + Value: "value1", + }, { + Name: "param2", + Value: "value2", + }}, + }, + }}, bindings: []*triggersv1.TriggerBinding{}, want: []triggersv1.Param{{ Name: "param1", @@ -731,22 +758,49 @@ func TestMergeBindingParams(t *testing.T) { }}, }, { name: "multiple bindings each with multiple params", - clusterBindings: []*triggersv1.ClusterTriggerBinding{ - bldr.ClusterTriggerBinding("", bldr.ClusterTriggerBindingSpec( - bldr.TriggerBindingParam("param5", "value1"), - bldr.TriggerBindingParam("param6", "value2"), - )), - }, - bindings: []*triggersv1.TriggerBinding{ - bldr.TriggerBinding("", "", bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "value1"), - bldr.TriggerBindingParam("param2", "value2"), - )), - bldr.TriggerBinding("", "", bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param3", "value3"), - bldr.TriggerBindingParam("param4", "value4"), - )), - }, + clusterBindings: []*triggersv1.ClusterTriggerBinding{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "ctb", + }, + Spec: triggersv1.TriggerBindingSpec{ + Params: []triggersv1.Param{{ + Name: "param5", + Value: "value1", + }, { + Name: "param6", + Value: "value2", + }}, + }, + }}, + bindings: []*triggersv1.TriggerBinding{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tb", + Namespace: "ns", + }, + Spec: triggersv1.TriggerBindingSpec{ + Params: []triggersv1.Param{{ + Name: "param1", + Value: "value1", + }, { + Name: "param2", + Value: "value2", + }}, + }, + }, { + ObjectMeta: metav1.ObjectMeta{ + Name: "", + Namespace: "", + }, + Spec: triggersv1.TriggerBindingSpec{ + Params: []triggersv1.Param{{ + Name: "param3", + Value: "value3", + }, { + Name: "param4", + Value: "value4", + }}, + }, + }}, want: []triggersv1.Param{{ Name: "param1", Value: "value1", @@ -769,24 +823,52 @@ func TestMergeBindingParams(t *testing.T) { }, { name: "multiple bindings with duplicate params", clusterBindings: []*triggersv1.ClusterTriggerBinding{}, - bindings: []*triggersv1.TriggerBinding{ - bldr.TriggerBinding("", "", bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "value1"), - )), - bldr.TriggerBinding("", "", bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "value3"), - bldr.TriggerBindingParam("param4", "value4"), - )), + bindings: []*triggersv1.TriggerBinding{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tb", + Namespace: "ns", + }, + Spec: triggersv1.TriggerBindingSpec{ + Params: []triggersv1.Param{{ + Name: "param1", + Value: "value1", + }}, + }, + }, { + ObjectMeta: metav1.ObjectMeta{ + Name: "tb", + Namespace: "ns", + }, + Spec: triggersv1.TriggerBindingSpec{ + Params: []triggersv1.Param{{ + Name: "param1", + Value: "value1", + }, { + Name: "param4", + Value: "value4", + }}, + }, + }, }, wantErr: true, }, { name: "single binding with duplicate params", clusterBindings: []*triggersv1.ClusterTriggerBinding{}, - bindings: []*triggersv1.TriggerBinding{ - bldr.TriggerBinding("", "", bldr.TriggerBindingSpec( - bldr.TriggerBindingParam("param1", "value1"), - bldr.TriggerBindingParam("param1", "value3"), - )), + bindings: []*triggersv1.TriggerBinding{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tb", + Namespace: "ns", + }, + Spec: triggersv1.TriggerBindingSpec{ + Params: []triggersv1.Param{{ + Name: "param1", + Value: "value1", + }, { + Name: "param1", + Value: "value3", + }}, + }, + }, }, wantErr: true, }}