Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEP-0118: Add exported functions for validating Matrix.Include and Matrix.Params #6229

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/apis/pipeline/v1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
184 changes: 159 additions & 25 deletions pkg/apis/pipeline/v1/pipeline_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,38 +903,28 @@ 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,
}, {
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,
Expand All @@ -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)
}
})
}
}
12 changes: 11 additions & 1 deletion pkg/apis/pipeline/v1beta1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading