Skip to content

Commit

Permalink
Validation for invalid Task Result expressions in Pipeline Result
Browse files Browse the repository at this point in the history
Prior to this commit, the validation for invalid `Task Result` expressions
in `Pipeline` `Result`'s was not extensive.

This commit adds the case where a `Pipeline` would be invalid if there is
no expression following the valid form `$(tasks,<task-name>.results.<result-name>)`.

Please reference this [doc](https://github.com/tektoncd/pipeline/blob/main/docs/pipelines.md#emitting-results-from-a-pipeline) for more information.

Fixes bug [#4922](#4922)

/kind bug
/cc @jerop
  • Loading branch information
Varun Singhai authored and tekton-robot committed Jun 9, 2022
1 parent 4621a66 commit af98ffe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pkg/apis/pipeline/v1beta1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,20 @@ func validatePipelineResults(results []PipelineResult) (errs *apis.FieldError) {
for idx, result := range results {
expressions, ok := GetVarSubstitutionExpressionsForPipelineResult(result)
if !ok {
errs = errs.Also(apis.ErrInvalidValue("expected pipeline results to be task result expressions but no expressions were found",
return errs.Also(apis.ErrInvalidValue("expected pipeline results to be task result expressions but no expressions were found",
"value").ViaFieldIndex("results", idx))
}

if LooksLikeContainsResultRefs(expressions) {
expressions = filter(expressions, looksLikeResultRef)
resultRefs := NewResultRefs(expressions)
if len(expressions) != len(resultRefs) {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("expected all of the expressions %v to be result expressions but only %v were", expressions, resultRefs),
"value").ViaFieldIndex("results", idx))
}
if !LooksLikeContainsResultRefs(expressions) {
return errs.Also(apis.ErrInvalidValue("expected pipeline results to be task result expressions but an invalid expressions was found",
"value").ViaFieldIndex("results", idx))
}

expressions = filter(expressions, looksLikeResultRef)
resultRefs := NewResultRefs(expressions)
if len(expressions) != len(resultRefs) {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("expected all of the expressions %v to be result expressions but only %v were", expressions, resultRefs),
"value").ViaFieldIndex("results", idx))
}

}
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,17 @@ func TestValidatePipelineResults_Failure(t *testing.T) {
Message: `invalid value: expected pipeline results to be task result expressions but no expressions were found`,
Paths: []string{"results[0].value"},
},
}, {
desc: "invalid pipeline result value with invalid expression",
results: []PipelineResult{{
Name: "my-pipeline-result",
Description: "this is my pipeline result",
Value: "$(foo.bar)",
}},
expectedError: apis.FieldError{
Message: `invalid value: expected pipeline results to be task result expressions but an invalid expressions was found`,
Paths: []string{"results[0].value"},
},
}}
for _, tt := range tests {
err := validatePipelineResults(tt.results)
Expand Down

0 comments on commit af98ffe

Please sign in to comment.