Skip to content

Commit

Permalink
Migrate from test builders to structs
Browse files Browse the repository at this point in the history
This is helpful as we migrate to the v1beta1 types since we won't have
to maintain separate test builders for v1beta1 types.

Ref tektoncd#1067, tektoncd#735

Signed-off-by: Dibyo Mukherjee <dibyo@google.com>
  • Loading branch information
dibyom committed May 7, 2021
1 parent 50293ad commit 72e2b54
Show file tree
Hide file tree
Showing 7 changed files with 1,002 additions and 646 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
122 changes: 64 additions & 58 deletions pkg/apis/triggers/v1alpha1/trigger_binding_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}
})
}
}
Loading

0 comments on commit 72e2b54

Please sign in to comment.