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 48e531b18e..40182c98f5 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/trigger_binding_validation_test.go b/pkg/apis/triggers/v1alpha1/trigger_binding_validation_test.go index 97528b21db..9bef3413d1 100644 --- a/pkg/apis/triggers/v1alpha1/trigger_binding_validation_test.go +++ b/pkg/apis/triggers/v1alpha1/trigger_binding_validation_test.go @@ -20,9 +20,8 @@ import ( "context" "testing" - "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 +30,52 @@ 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: "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)"), - )), - }, { - 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)", + }, { + Name: "PARAM1", + Value: "$(body.input2)", + }, { + Name: "param3", + Value: "$(body.input3)", + }}, + }, + }, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -67,49 +88,34 @@ func Test_TriggerBindingValidate(t *testing.T) { func Test_TriggerBindingValidate_error(t *testing.T) { tests := []struct { - name string - tb *v1alpha1.TriggerBinding - errMsg string + name string + tb *v1alpha1.TriggerBinding }{{ 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)"), - )), - 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))"), - )), - 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))"), - )), - 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)))"), - )), - errMsg: "invalid value: $($($(body.param1))): spec.params[0].value", + 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)", + }}, + }, + }, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := tt.tb.Validate(context.Background()) - if err == nil { + if err := tt.tb.Validate(context.Background()); err == nil { t.Errorf("TriggerBinding.Validate() expected error for TriggerBinding: %v", tt.tb) } - if diff := cmp.Diff(tt.errMsg, err.Error()); diff != "" { - t.Errorf("-want +got: %s", diff) - } }) } } diff --git a/pkg/apis/triggers/v1alpha1/trigger_template_validation_test.go b/pkg/apis/triggers/v1alpha1/trigger_template_validation_test.go index fc349ea3bb..91885c2855 100644 --- a/pkg/apis/triggers/v1alpha1/trigger_template_validation_test.go +++ b/pkg/apis/triggers/v1alpha1/trigger_template_validation_test.go @@ -22,13 +22,12 @@ import ( 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" ) @@ -96,134 +95,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 fc86990679..03fff78da8 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{ @@ -151,7 +195,7 @@ func Test_TriggerValidate(t *testing.T) { ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ RawExtension: test.RawExtension(t, pipelinev1.PipelineRun{ TypeMeta: metav1.TypeMeta{ - APIVersion: "tekton.dev/v1beta1", + APIVersion: "tekton.dev/v1alpha1", Kind: "PipelineRun", }, }), @@ -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{ @@ -442,7 +369,7 @@ func TestTriggerValidate_error(t *testing.T) { ResourceTemplates: []v1alpha1.TriggerResourceTemplate{{ RawExtension: test.RawExtension(t, pipelinev1.PipelineRun{ TypeMeta: metav1.TypeMeta{ - APIVersion: "tekton.dev/v1beta1", + APIVersion: "tekton.dev/v1alpha1", Kind: "PipelineRun", }, }), diff --git a/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go b/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go index 95ddac56ff..72c763603d 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 0dad0d19d3..d80525c13e 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,107 @@ 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", + Description: "", + 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", + Description: "", + 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", + Description: "", + 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", + Description: "", + Default: ptr.String(""), + }, { + Name: "param2", + Description: "", + 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 +488,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 +523,31 @@ 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", + Description: "desc", + Default: ptr.String(""), + }, { + Name: "p2", + Description: "", + Default: ptr.String(""), + }}, + 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 +555,28 @@ 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", + Description: "desc", + Default: ptr.String(""), + }, { + Name: "p2", + Description: "", + Default: ptr.String(""), + }}, + 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 +584,59 @@ 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", + Description: "desc", + Default: ptr.String(""), + }}, + 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 c0c1c029ea..077f8106a3 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, }}