Skip to content

Commit

Permalink
TEP-0118: Add exported functions for validating Matrix.Include and Ma…
Browse files Browse the repository at this point in the history
…trix.Params

This commit adds functions that will be used to validate if MatrixHasInclude or MatrixHasParams(), which will be used in subsequent PRs as validation and implementation logic is built out.
  • Loading branch information
EmmaMunley committed Feb 24, 2023
1 parent 58712bb commit e18d70b
Show file tree
Hide file tree
Showing 4 changed files with 344 additions and 52 deletions.
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
186 changes: 161 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,149 @@ 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) {
matrix := tc.arg.Matrix
matrixHasParams := 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) {
matrix := tc.arg.Matrix
matrixHasInclude := 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

0 comments on commit e18d70b

Please sign in to comment.