diff --git a/pkg/reconciler/taskrun/taskrun.go b/pkg/reconciler/taskrun/taskrun.go index d1c33447d8e..e80f00e54d9 100644 --- a/pkg/reconciler/taskrun/taskrun.go +++ b/pkg/reconciler/taskrun/taskrun.go @@ -559,30 +559,22 @@ func (c *Reconciler) reconcile(ctx context.Context, tr *v1beta1.TaskRun, rtr *re } if string(tr.Status.Conditions[0].Type) == string(apis.ConditionSucceeded) && string(tr.Status.Conditions[0].Status) != string(corev1.ConditionUnknown) { - if len(tr.Status.TaskRunResults) == 0 && len(rtr.TaskSpec.Results) != 0 { - noValueErr := fmt.Errorf("%s TaskRun was expected to produce result but didn't", tr.Name) - logger.Errorf("%s TaskRun was expected to produce result but didn't", tr.Name) - tr.Status.MarkResourceFailed(v1beta1.TaskRunReasonFailed, noValueErr) - return noValueErr - } - - for _, trResult := range tr.Status.TaskRunResults { - noValueErr := fmt.Errorf("expected value for Result %s but found nil", trResult.Name) - if trResult.Type == v1beta1.ResultsTypeString && trResult.Value.StringVal == "" { - tr.Status.MarkResourceFailed(v1beta1.TaskRunReasonFailed, noValueErr) - return noValueErr - } - if trResult.Type == v1beta1.ResultsTypeArray && len(trResult.Value.ArrayVal) == 0 { - tr.Status.MarkResourceFailed(v1beta1.TaskRunReasonFailed, noValueErr) - return noValueErr - } + specResults := []v1beta1.TaskResult{} + if tr.Spec.TaskSpec != nil { + specResults = append(specResults, tr.Spec.TaskSpec.Results...) + } - if trResult.Type == v1beta1.ResultsTypeObject && len(trResult.Value.ObjectVal) == 0 { - tr.Status.MarkResourceFailed(v1beta1.TaskRunReasonFailed, noValueErr) - return noValueErr - } + if rtr.TaskSpec != nil { + specResults = append(specResults, rtr.TaskSpec.Results...) + } + // When get the results, check if the type of result is the expected one + if missingResults := missingResultValues(tr, specResults); len(missingResults) != 0 { + missingErr := fmt.Errorf("%s TaskRun missing following results: %v", tr.Name, missingResults) + logger.Errorf("%s TaskRun missing following results: %v", tr.Name, missingResults) + tr.Status.MarkResourceFailed(v1beta1.TaskRunReasonFailed, missingErr) + return missingErr } } diff --git a/pkg/reconciler/taskrun/taskrun_test.go b/pkg/reconciler/taskrun/taskrun_test.go index fb36eee6653..6261a79ff43 100644 --- a/pkg/reconciler/taskrun/taskrun_test.go +++ b/pkg/reconciler/taskrun/taskrun_test.go @@ -25,6 +25,7 @@ import ( "net/url" "os" "path/filepath" + "reflect" "strconv" "strings" "testing" @@ -4983,6 +4984,10 @@ status: func TestReconcile_TestDefaultResults(t *testing.T) { + defaultStringResult := "default-result" + defaultArrayResult := []string{"default-result-a", "default-result-b"} + defaultObjectResult := map[string]string{"url": "foobar", "commit": "commit"} + defaultResultsTask := &v1beta1.Task{ ObjectMeta: objectMeta("test-results-task", "foo"), Spec: v1beta1.TaskSpec{ @@ -4991,18 +4996,18 @@ func TestReconcile_TestDefaultResults(t *testing.T) { { Name: "aResult", Type: v1beta1.ResultsTypeString, - Default: &v1beta1.ParamValue{StringVal: "default-result"}, + Default: &v1beta1.ParamValue{StringVal: defaultStringResult}, }, { Name: "bResult", Type: v1beta1.ResultsTypeArray, - Default: &v1beta1.ParamValue{ArrayVal: []string{"default-result-a", "default-result-b"}}, + Default: &v1beta1.ParamValue{ArrayVal: defaultArrayResult}, }, { Name: "cResult", Type: v1beta1.ResultsTypeObject, Properties: map[string]v1beta1.PropertySpec{"url": {Type: "string"}, "commit": {Type: "string"}}, - Default: &v1beta1.ParamValue{ObjectVal: map[string]string{"url": "foobar", "commit": "commit"}}, + Default: &v1beta1.ParamValue{ObjectVal: defaultObjectResult}, }, }, }, @@ -5019,7 +5024,17 @@ status: taskResults: - name: aResult type: string - value: aResultValue + value: default-result + - name: bResult + type: array + value: + - default-result-a + - default-result-b + - name: cResult + type: object + value: + url: foobar + commit: commit `) d := test.Data{ @@ -5061,13 +5076,17 @@ status: t.Errorf("Expected TaskRun to succeed but it did not. Final conditions were:\n%#v", tr.Status.Conditions) } - fmt.Println(tr.Status.TaskRunResults) + if !strings.EqualFold(tr.Status.TaskRunResults[0].Value.StringVal, defaultStringResult) { + t.Errorf("Expected default result but found something else") + } - fmt.Println(tr.Status.TaskRunResults[0].Value.StringVal) + if !reflect.DeepEqual(tr.Status.TaskRunResults[1].Value.ArrayVal, defaultArrayResult) { + t.Errorf("Expected default result but found something else") + } - // if !strings.EqualFold(tr.Status.TaskRunResults[0].Value.StringVal, "default-result") { - // t.Errorf("Expected default result but found something else") - // } + if !reflect.DeepEqual(tr.Status.TaskRunResults[2].Value.ObjectVal, defaultObjectResult) { + t.Errorf("Expected default result but found something else") + } }) } diff --git a/pkg/reconciler/taskrun/validate_resources.go b/pkg/reconciler/taskrun/validate_resources.go index d413ff52599..a77dc9739d1 100644 --- a/pkg/reconciler/taskrun/validate_resources.go +++ b/pkg/reconciler/taskrun/validate_resources.go @@ -327,6 +327,22 @@ func validateTaskRunResults(tr *v1beta1.TaskRun, resolvedTaskSpec *v1beta1.TaskS return nil } +func missingResultValues(tr *v1beta1.TaskRun, specResults []v1beta1.TaskResult) []string { + neededResults := make([]string, 0) + providedResults := make([]string, 0) + // collect needed keys for object results + for _, r := range specResults { + neededResults = append(neededResults, string(r.Name)) + } + + // collect provided keys for object results + for _, trr := range tr.Status.TaskRunResults { + providedResults = append(providedResults, string(trr.Name)) + } + + return list.DiffLeft(neededResults, providedResults) +} + // mismatchedTypesResults checks and returns all the mismatched types of emitted results against specified results. func mismatchedTypesResults(tr *v1beta1.TaskRun, specResults []v1beta1.TaskResult) map[string][]string { neededTypes := make(map[string][]string)