Skip to content

Commit

Permalink
Validation for Task Result expressions in Pipeline Results
Browse files Browse the repository at this point in the history
Prior to this commit, the validation for static strings for Task
Result expressions in Pipeline Results 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)
  • Loading branch information
vsinghai committed Jun 6, 2022
1 parent 342ed52 commit 118d5bc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
20 changes: 12 additions & 8 deletions pkg/apis/pipeline/v1beta1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,20 @@ func filter(arr []string, cond func(string) bool) []string {
func validatePipelineResults(results []PipelineResult) (errs *apis.FieldError) {
for idx, result := range results {
expressions, ok := GetVarSubstitutionExpressionsForPipelineResult(result)
if ok {
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 !ok {
errs = 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))
}
}

}

return errs
Expand Down
51 changes: 35 additions & 16 deletions pkg/apis/pipeline/v1beta1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestPipeline_Validate_Success(t *testing.T) {
Results: []PipelineResult{{
Name: "pipeline-result",
Description: "this is my pipeline result",
Value: "pipeline-result-default",
Value: "$(tasks.my-task.results.my-result)",
}},
},
},
Expand Down Expand Up @@ -1204,22 +1204,41 @@ func TestValidatePipelineResults_Success(t *testing.T) {
}

func TestValidatePipelineResults_Failure(t *testing.T) {
desc := "invalid pipeline result reference"
results := []PipelineResult{{
Name: "my-pipeline-result",
Description: "this is my pipeline result",
Value: "$(tasks.a-task.results.output.output)",
tests := []struct {
desc string
results []PipelineResult
expectedError apis.FieldError
}{{
desc: "invalid pipeline result reference",
results: []PipelineResult{{
Name: "my-pipeline-result",
Description: "this is my pipeline result",
Value: "$(tasks.a-task.results.output.output)",
}},
expectedError: apis.FieldError{
Message: `invalid value: expected all of the expressions [tasks.a-task.results.output.output] to be result expressions but only [] were`,
Paths: []string{"results[0].value"},
},
}, {
desc: "invalid pipeline result value with static string",
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 no expressions were found`,
Paths: []string{"results[0].value"},
},
}}
expectedError := apis.FieldError{
Message: `invalid value: expected all of the expressions [tasks.a-task.results.output.output] to be result expressions but only [] were`,
Paths: []string{"results[0].value"},
}
err := validatePipelineResults(results)
if err == nil {
t.Errorf("Pipeline.validatePipelineResults() did not return for invalid pipeline: %s", desc)
}
if d := cmp.Diff(expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
t.Errorf("Pipeline.validateParamResults() errors diff %s", diff.PrintWantGot(d))
for _, tt := range tests {
err := validatePipelineResults(tt.results)
if err == nil {
t.Errorf("Pipeline.validatePipelineResults() did not return for invalid pipeline: %s", tt.desc)
}
if d := cmp.Diff(tt.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
t.Errorf("Pipeline.validateParamResults() errors diff %s", diff.PrintWantGot(d))
}
}
}

Expand Down

0 comments on commit 118d5bc

Please sign in to comment.