diff --git a/pkg/apis/pipeline/v1/pipeline_types.go b/pkg/apis/pipeline/v1/pipeline_types.go index c01696305fc..e2bf7eaa58c 100644 --- a/pkg/apis/pipeline/v1/pipeline_types.go +++ b/pkg/apis/pipeline/v1/pipeline_types.go @@ -293,7 +293,17 @@ func (pt PipelineTask) validateTask(ctx context.Context) (errs *apis.FieldError) // IsMatrixed return whether pipeline task is matrixed func (pt *PipelineTask) IsMatrixed() bool { - return pt.Matrix != nil && (len(pt.Matrix.Params) > 0 || len(pt.Matrix.Include) > 0) + return pt.Matrix != nil && (pt.Matrix.MatrixHasParams() || pt.Matrix.MatrixHasInclude()) +} + +// MatrixHasInclude return whether matrix has Include +func (matrix *Matrix) MatrixHasInclude() bool { + return matrix != nil && len(matrix.Include) > 0 +} + +// MatrixHasParams return whether matrix has Params +func (matrix *Matrix) MatrixHasParams() bool { + return matrix != nil && len(matrix.Params) > 0 } func (pt *PipelineTask) validateMatrix(ctx context.Context) (errs *apis.FieldError) { diff --git a/pkg/apis/pipeline/v1/pipeline_types_test.go b/pkg/apis/pipeline/v1/pipeline_types_test.go index f94c955db0b..993f1f484a5 100644 --- a/pkg/apis/pipeline/v1/pipeline_types_test.go +++ b/pkg/apis/pipeline/v1/pipeline_types_test.go @@ -903,23 +903,13 @@ func TestPipelineTask_IsMatrixed(t *testing.T) { name: "matrixed with include", arg: arg{ Matrix: &Matrix{ - Include: []MatrixInclude{ - {Name: "build-1"}, - {Params: []Param{ - {Name: "IMAGE", Value: ParamValue{StringVal: "image-1"}}, - {Name: "DOCKERFILE", Value: ParamValue{StringVal: "path/to/Dockerfile1"}}, - }}, - {Name: "build-2"}, - {Params: []Param{ - {Name: "IMAGE", Value: ParamValue{StringVal: "image-2"}}, - {Name: "DOCKERFILE", Value: ParamValue{StringVal: "path/to/Dockerfile2"}}, - }}, - {Name: "build-3"}, - {Params: []Param{ - {Name: "IMAGE", Value: ParamValue{StringVal: "image-3"}}, - {Name: "DOCKERFILE", Value: ParamValue{StringVal: "path/to/Dockerfile3"}}, - }}, - }, + Include: []MatrixInclude{{ + Name: "build-1", + Params: []Param{{ + Name: "IMAGE", Value: ParamValue{Type: ParamTypeString, StringVal: "image-1"}, + }, { + Name: "DOCKERFILE", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/Dockerfile1"}}}, + }}, }, }, expected: true, @@ -927,14 +917,14 @@ func TestPipelineTask_IsMatrixed(t *testing.T) { name: "matrixed with params and include", arg: arg{ Matrix: &Matrix{ - Params: []Param{{Name: "GOARCH", Value: ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}}}}, - Include: []MatrixInclude{ - {Name: "s390x-no-race"}, - {Params: []Param{ - {Name: "GOARCH", Value: ParamValue{StringVal: "linux/s390x"}}, - {Name: "flags", Value: ParamValue{StringVal: "-cover -v"}}, - }}, - }, + Params: []Param{{ + Name: "GOARCH", Value: ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}}, + }}, + Include: []MatrixInclude{{ + Name: "common-package", + Params: []Param{{ + Name: "package", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/common/package/"}}}, + }}, }, }, expected: true, @@ -952,3 +942,147 @@ func TestPipelineTask_IsMatrixed(t *testing.T) { }) } } + +func TestPipelineTask_MatrixHasParams(t *testing.T) { + type arg struct { + *Matrix + } + testCases := []struct { + name string + arg arg + expected bool + }{ + { + name: "nil matrix", + arg: arg{ + Matrix: nil, + }, + expected: false, + }, + { + name: "empty matrix", + arg: arg{ + Matrix: &Matrix{}, + }, + expected: false, + }, + { + name: "matrixed with params", + arg: arg{ + Matrix: &Matrix{ + Params: []Param{{Name: "platform", Value: ParamValue{ArrayVal: []string{"linux", "windows"}}}}, + }, + }, + expected: true, + }, { + name: "matrixed with include", + arg: arg{ + Matrix: &Matrix{ + Include: []MatrixInclude{{ + Name: "build-1", + Params: []Param{{ + Name: "IMAGE", Value: ParamValue{Type: ParamTypeString, StringVal: "image-1"}, + }, { + Name: "DOCKERFILE", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/Dockerfile1"}}}, + }}, + }, + }, + expected: false, + }, { + name: "matrixed with params and include", + arg: arg{ + Matrix: &Matrix{ + Params: []Param{{ + Name: "GOARCH", Value: ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}}, + }}, + Include: []MatrixInclude{{ + Name: "common-package", + Params: []Param{{ + Name: "package", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/common/package/"}}}, + }}, + }, + }, + expected: true, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + matrixHasParams := tc.arg.Matrix.MatrixHasParams() + if matrixHasParams != tc.expected { + t.Errorf("Matrix.MatrixHasParams() return bool: %v, but wanted: %v", matrixHasParams, tc.expected) + } + }) + } +} + +func TestPipelineTask_MatrixHasInclude(t *testing.T) { + type arg struct { + *Matrix + } + testCases := []struct { + name string + arg arg + expected bool + }{ + { + name: "nil matrix", + arg: arg{ + Matrix: nil, + }, + expected: false, + }, + { + name: "empty matrix", + arg: arg{ + Matrix: &Matrix{}, + }, + expected: false, + }, + { + name: "matrixed with params", + arg: arg{ + Matrix: &Matrix{ + Params: []Param{{Name: "platform", Value: ParamValue{ArrayVal: []string{"linux", "windows"}}}}, + }, + }, + expected: false, + }, { + name: "matrixed with include", + arg: arg{ + Matrix: &Matrix{ + Include: []MatrixInclude{{ + Name: "build-1", + Params: []Param{{ + Name: "IMAGE", Value: ParamValue{Type: ParamTypeString, StringVal: "image-1"}, + }, { + Name: "DOCKERFILE", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/Dockerfile1"}}}, + }}, + }, + }, + expected: true, + }, { + name: "matrixed with params and include", + arg: arg{ + Matrix: &Matrix{ + Params: []Param{{ + Name: "GOARCH", Value: ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}}, + }}, + Include: []MatrixInclude{{ + Name: "common-package", + Params: []Param{{ + Name: "package", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/common/package/"}}}, + }}, + }, + }, + expected: true, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + matrixHasInclude := tc.arg.Matrix.MatrixHasInclude() + if matrixHasInclude != tc.expected { + t.Errorf("Matrix.MatrixHasInclude() return bool: %v, but wanted: %v", matrixHasInclude, tc.expected) + } + }) + } +} diff --git a/pkg/apis/pipeline/v1beta1/pipeline_types.go b/pkg/apis/pipeline/v1beta1/pipeline_types.go index b0e1bc93f00..13e557e5156 100644 --- a/pkg/apis/pipeline/v1beta1/pipeline_types.go +++ b/pkg/apis/pipeline/v1beta1/pipeline_types.go @@ -323,7 +323,17 @@ func (pt PipelineTask) validateTask(ctx context.Context) (errs *apis.FieldError) // IsMatrixed return whether pipeline task is matrixed func (pt *PipelineTask) IsMatrixed() bool { - return pt.Matrix != nil && (len(pt.Matrix.Params) > 0 || len(pt.Matrix.Include) > 0) + return pt.Matrix != nil && (pt.Matrix.MatrixHasParams() || pt.Matrix.MatrixHasInclude()) +} + +// MatrixHasInclude return whether matrix has Include +func (matrix *Matrix) MatrixHasInclude() bool { + return matrix != nil && len(matrix.Include) > 0 +} + +// MatrixHasParams return whether matrix has Params +func (matrix *Matrix) MatrixHasParams() bool { + return matrix != nil && len(matrix.Params) > 0 } func (pt *PipelineTask) validateMatrix(ctx context.Context) (errs *apis.FieldError) { diff --git a/pkg/apis/pipeline/v1beta1/pipeline_types_test.go b/pkg/apis/pipeline/v1beta1/pipeline_types_test.go index cbfd6cf6fbe..4a268003f3b 100644 --- a/pkg/apis/pipeline/v1beta1/pipeline_types_test.go +++ b/pkg/apis/pipeline/v1beta1/pipeline_types_test.go @@ -1090,23 +1090,13 @@ func TestPipelineTask_IsMatrixed(t *testing.T) { name: "matrixed with include", arg: arg{ Matrix: &Matrix{ - Include: []MatrixInclude{ - {Name: "build-1"}, - {Params: []Param{ - {Name: "IMAGE", Value: ParamValue{StringVal: "image-1"}}, - {Name: "DOCKERFILE", Value: ParamValue{StringVal: "path/to/Dockerfile1"}}, - }}, - {Name: "build-2"}, - {Params: []Param{ - {Name: "IMAGE", Value: ParamValue{StringVal: "image-2"}}, - {Name: "DOCKERFILE", Value: ParamValue{StringVal: "path/to/Dockerfile2"}}, - }}, - {Name: "build-3"}, - {Params: []Param{ - {Name: "IMAGE", Value: ParamValue{StringVal: "image-3"}}, - {Name: "DOCKERFILE", Value: ParamValue{StringVal: "path/to/Dockerfile3"}}, - }}, - }, + Include: []MatrixInclude{{ + Name: "build-1", + Params: []Param{{ + Name: "IMAGE", Value: ParamValue{Type: ParamTypeString, StringVal: "image-1"}, + }, { + Name: "DOCKERFILE", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/Dockerfile1"}}}, + }}, }, }, expected: true, @@ -1114,14 +1104,14 @@ func TestPipelineTask_IsMatrixed(t *testing.T) { name: "matrixed with params and include", arg: arg{ Matrix: &Matrix{ - Params: []Param{{Name: "GOARCH", Value: ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}}}}, - Include: []MatrixInclude{ - {Name: "s390x-no-race"}, - {Params: []Param{ - {Name: "GOARCH", Value: ParamValue{StringVal: "linux/s390x"}}, - {Name: "flags", Value: ParamValue{StringVal: "-cover -v"}}, - }}, - }, + Params: []Param{{ + Name: "GOARCH", Value: ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}}, + }}, + Include: []MatrixInclude{{ + Name: "common-package", + Params: []Param{{ + Name: "package", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/common/package/"}}}, + }}, }, }, expected: true, @@ -1139,3 +1129,147 @@ func TestPipelineTask_IsMatrixed(t *testing.T) { }) } } + +func TestPipelineTask_MatrixHasParams(t *testing.T) { + type arg struct { + *Matrix + } + testCases := []struct { + name string + arg arg + expected bool + }{ + { + name: "nil matrix", + arg: arg{ + Matrix: nil, + }, + expected: false, + }, + { + name: "empty matrix", + arg: arg{ + Matrix: &Matrix{}, + }, + expected: false, + }, + { + name: "matrixed with params", + arg: arg{ + Matrix: &Matrix{ + Params: []Param{{Name: "platform", Value: ParamValue{ArrayVal: []string{"linux", "windows"}}}}, + }, + }, + expected: true, + }, { + name: "matrixed with include", + arg: arg{ + Matrix: &Matrix{ + Include: []MatrixInclude{{ + Name: "build-1", + Params: []Param{{ + Name: "IMAGE", Value: ParamValue{Type: ParamTypeString, StringVal: "image-1"}, + }, { + Name: "DOCKERFILE", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/Dockerfile1"}}}, + }}, + }, + }, + expected: false, + }, { + name: "matrixed with params and include", + arg: arg{ + Matrix: &Matrix{ + Params: []Param{{ + Name: "GOARCH", Value: ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}}, + }}, + Include: []MatrixInclude{{ + Name: "common-package", + Params: []Param{{ + Name: "package", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/common/package/"}}}, + }}, + }, + }, + expected: true, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + matrixHasParams := tc.arg.Matrix.MatrixHasParams() + if matrixHasParams != tc.expected { + t.Errorf("Matrix.MatrixHasParams() return bool: %v, but wanted: %v", matrixHasParams, tc.expected) + } + }) + } +} + +func TestPipelineTask_MatrixHasInclude(t *testing.T) { + type arg struct { + *Matrix + } + testCases := []struct { + name string + arg arg + expected bool + }{ + { + name: "nil matrix", + arg: arg{ + Matrix: nil, + }, + expected: false, + }, + { + name: "empty matrix", + arg: arg{ + Matrix: &Matrix{}, + }, + expected: false, + }, + { + name: "matrixed with params", + arg: arg{ + Matrix: &Matrix{ + Params: []Param{{Name: "platform", Value: ParamValue{ArrayVal: []string{"linux", "windows"}}}}, + }, + }, + expected: false, + }, { + name: "matrixed with include", + arg: arg{ + Matrix: &Matrix{ + Include: []MatrixInclude{{ + Name: "build-1", + Params: []Param{{ + Name: "IMAGE", Value: ParamValue{Type: ParamTypeString, StringVal: "image-1"}, + }, { + Name: "DOCKERFILE", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/Dockerfile1"}}}, + }}, + }, + }, + expected: true, + }, { + name: "matrixed with params and include", + arg: arg{ + Matrix: &Matrix{ + Params: []Param{{ + Name: "GOARCH", Value: ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}}, + }}, + Include: []MatrixInclude{{ + Name: "common-package", + Params: []Param{{ + Name: "package", Value: ParamValue{Type: ParamTypeString, StringVal: "path/to/common/package/"}}}, + }}, + }, + }, + expected: true, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + matrixHasInclude := tc.arg.Matrix.MatrixHasInclude() + if matrixHasInclude != tc.expected { + t.Errorf("Matrix.MatrixHasInclude() return bool: %v, but wanted: %v", matrixHasInclude, tc.expected) + } + }) + } +}