Skip to content

Commit

Permalink
Split Pipeline validation tests into separate test classes
Browse files Browse the repository at this point in the history
Some parts of Pipeline validation happen only when extra validation is performed
after propagating parameters and workspaces. This commit splits existing Pipeline
tests in TestValidatePipelineParameterVariables_Failure into tests for functionality
that happens before propagation and tests for functionality that happens after propagation.
This commit is solely for refactoring purposes to make subsequent cleanup easier.
No functional changes.
  • Loading branch information
lbernick authored and tekton-robot committed May 12, 2023
1 parent 584f2e6 commit 42d1901
Show file tree
Hide file tree
Showing 2 changed files with 348 additions and 304 deletions.
326 changes: 174 additions & 152 deletions pkg/apis/pipeline/v1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ func TestValidatePipelineParameterVariables_Success(t *testing.T) {
}
}

func TestValidatePipelineParameterVariables_Failure(t *testing.T) {
func TestValidatePipelineDeclaredParameterUsage_Failure(t *testing.T) {
tests := []struct {
name string
params []ParamSpec
Expand Down Expand Up @@ -1422,154 +1422,6 @@ func TestValidatePipelineParameterVariables_Failure(t *testing.T) {
Message: `non-existent variable in "$(params.does-not-exist)"`,
Paths: []string{"[0].params[b-param]"},
},
}, {
name: "invalid parameter type",
params: []ParamSpec{{
Name: "foo", Type: "invalidtype",
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `invalid value: invalidtype`,
Paths: []string{"params.foo.type"},
},
}, {
name: "array parameter mismatching default type",
params: []ParamSpec{{
Name: "foo", Type: ParamTypeArray, Default: &ParamValue{Type: ParamTypeString, StringVal: "astring"},
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `"array" type does not match default value's type: "string"`,
Paths: []string{"params.foo.default.type", "params.foo.type"},
},
}, {
name: "string parameter mismatching default type",
params: []ParamSpec{{
Name: "foo", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.foo.default.type", "params.foo.type"},
},
}, {
name: "array parameter used as string",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "bar",
TaskRef: &TaskRef{Name: "bar-task"},
Params: Params{{
Name: "a-param", Value: ParamValue{Type: ParamTypeString, StringVal: "$(params.baz)"},
}},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.baz.default.type", "params.baz.type"},
},
}, {
name: "star array parameter used as string",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "bar",
TaskRef: &TaskRef{Name: "bar-task"},
Params: Params{{
Name: "a-param", Value: ParamValue{Type: ParamTypeString, StringVal: "$(params.baz[*])"},
}},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.baz.default.type", "params.baz.type"},
},
}, {
name: "array parameter string template not isolated",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "bar",
TaskRef: &TaskRef{Name: "bar-task"},
Params: Params{{
Name: "a-param", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{"value: $(params.baz)", "last"}},
}},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.baz.default.type", "params.baz.type"},
},
}, {
name: "star array parameter string template not isolated",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "bar",
TaskRef: &TaskRef{Name: "bar-task"},
Params: Params{{
Name: "a-param", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{"value: $(params.baz[*])", "last"}},
}},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.baz.default.type", "params.baz.type"},
},
}, {
name: "multiple string parameters with the same name",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString,
}, {
Name: "baz", Type: ParamTypeString,
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `parameter appears more than once`,
Paths: []string{"params[baz]"},
},
}, {
name: "multiple array parameters with the same name",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeArray,
}, {
Name: "baz", Type: ParamTypeArray,
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `parameter appears more than once`,
Paths: []string{"params[baz]"},
},
}, {
name: "multiple different type parameters with the same name",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeArray,
}, {
Name: "baz", Type: ParamTypeString,
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `parameter appears more than once`,
Paths: []string{"params[baz]"},
},
}, {
name: "invalid pipeline task with a matrix parameter which is missing from the param declarations",
tasks: []PipelineTask{{
Expand Down Expand Up @@ -1785,6 +1637,179 @@ func TestValidatePipelineParameterVariables_Failure(t *testing.T) {
Paths: []string{"[0].matrix.params[b-param].value[0]"},
},
api: "alpha",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
if tt.api == "alpha" {
ctx = config.EnableAlphaAPIFields(context.Background())
}
ctx = config.SkipValidationDueToPropagatedParametersAndWorkspaces(ctx, false)
err := ValidatePipelineParameterVariables(ctx, tt.tasks, tt.params)
if err == nil {
t.Errorf("Pipeline.ValidatePipelineParameterVariables() did not return error for invalid pipeline parameters")
}
if d := cmp.Diff(tt.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
t.Errorf("PipelineSpec.Validate() errors diff %s", diff.PrintWantGot(d))
}
})
}
}

func TestValidatePipelineParameterVariables_Failure(t *testing.T) {
tests := []struct {
name string
params []ParamSpec
tasks []PipelineTask
expectedError apis.FieldError
}{{
name: "invalid parameter type",
params: []ParamSpec{{
Name: "foo", Type: "invalidtype",
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `invalid value: invalidtype`,
Paths: []string{"params.foo.type"},
},
}, {
name: "array parameter mismatching default type",
params: []ParamSpec{{
Name: "foo", Type: ParamTypeArray, Default: &ParamValue{Type: ParamTypeString, StringVal: "astring"},
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `"array" type does not match default value's type: "string"`,
Paths: []string{"params.foo.default.type", "params.foo.type"},
},
}, {
name: "string parameter mismatching default type",
params: []ParamSpec{{
Name: "foo", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.foo.default.type", "params.foo.type"},
},
}, {
name: "array parameter used as string",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "bar",
TaskRef: &TaskRef{Name: "bar-task"},
Params: Params{{
Name: "a-param", Value: ParamValue{Type: ParamTypeString, StringVal: "$(params.baz)"},
}},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.baz.default.type", "params.baz.type"},
},
}, {
name: "star array parameter used as string",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "bar",
TaskRef: &TaskRef{Name: "bar-task"},
Params: Params{{
Name: "a-param", Value: ParamValue{Type: ParamTypeString, StringVal: "$(params.baz[*])"},
}},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.baz.default.type", "params.baz.type"},
},
}, {
name: "array parameter string template not isolated",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "bar",
TaskRef: &TaskRef{Name: "bar-task"},
Params: Params{{
Name: "a-param", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{"value: $(params.baz)", "last"}},
}},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.baz.default.type", "params.baz.type"},
},
}, {
name: "star array parameter string template not isolated",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString, Default: &ParamValue{Type: ParamTypeArray, ArrayVal: []string{"anarray", "elements"}},
}},
tasks: []PipelineTask{{
Name: "bar",
TaskRef: &TaskRef{Name: "bar-task"},
Params: Params{{
Name: "a-param", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{"value: $(params.baz[*])", "last"}},
}},
}},
expectedError: apis.FieldError{
Message: `"string" type does not match default value's type: "array"`,
Paths: []string{"params.baz.default.type", "params.baz.type"},
},
}, {
name: "multiple string parameters with the same name",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeString,
}, {
Name: "baz", Type: ParamTypeString,
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `parameter appears more than once`,
Paths: []string{"params[baz]"},
},
}, {
name: "multiple array parameters with the same name",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeArray,
}, {
Name: "baz", Type: ParamTypeArray,
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `parameter appears more than once`,
Paths: []string{"params[baz]"},
},
}, {
name: "multiple different type parameters with the same name",
params: []ParamSpec{{
Name: "baz", Type: ParamTypeArray,
}, {
Name: "baz", Type: ParamTypeString,
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo-task"},
}},
expectedError: apis.FieldError{
Message: `parameter appears more than once`,
Paths: []string{"params[baz]"},
},
}, {
name: "invalid task use duplicate parameters",
tasks: []PipelineTask{{
Expand All @@ -1806,9 +1831,6 @@ func TestValidatePipelineParameterVariables_Failure(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
if tt.api == "alpha" {
ctx = config.EnableAlphaAPIFields(context.Background())
}
ctx = config.SkipValidationDueToPropagatedParametersAndWorkspaces(ctx, false)
err := ValidatePipelineParameterVariables(ctx, tt.tasks, tt.params)
if err == nil {
Expand Down
Loading

0 comments on commit 42d1901

Please sign in to comment.