Skip to content

Commit

Permalink
fix the remaining things
Browse files Browse the repository at this point in the history
  • Loading branch information
vinamra28 committed Oct 23, 2022
1 parent 5ad6bc1 commit de04679
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
34 changes: 13 additions & 21 deletions pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,30 +560,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
}
}

Expand Down
37 changes: 28 additions & 9 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -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{
Expand All @@ -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},
},
},
},
Expand All @@ -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{
Expand Down Expand Up @@ -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")
}

})
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/reconciler/taskrun/validate_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,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)
Expand Down

0 comments on commit de04679

Please sign in to comment.