github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/pkg/utils/tekton/matchers.go (about) 1 package tekton 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "strings" 8 9 "github.com/onsi/gomega" 10 "github.com/onsi/gomega/types" 11 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" 12 "k8s.io/client-go/util/jsonpath" 13 "knative.dev/pkg/apis" 14 ) 15 16 type TaskRunResultMatcher struct { 17 name string 18 jsonPath *string 19 value *string 20 jsonValue *interface{} 21 jsonMatcher types.GomegaMatcher 22 } 23 24 func (matcher *TaskRunResultMatcher) FailureMessage(actual interface{}) (message string) { 25 if matcher.value != nil { 26 return fmt.Sprintf("%v to equal %v", actual, v1beta1.TaskRunResult{ 27 Name: matcher.name, 28 Value: *v1beta1.NewArrayOrString(*matcher.value), 29 }) 30 } 31 32 return matcher.jsonMatcher.FailureMessage(actual) 33 } 34 35 func (matcher *TaskRunResultMatcher) Match(actual interface{}) (success bool, err error) { 36 if tr, ok := actual.(v1beta1.TaskRunResult); !ok { 37 return false, fmt.Errorf("not given TaskRunResult") 38 } else { 39 if tr.Name != matcher.name { 40 return false, nil 41 } 42 43 given := tr.Value 44 if matcher.jsonPath != nil { 45 p := jsonpath.New("test") 46 p.EnableJSONOutput(true) 47 if err := p.Parse(*matcher.jsonPath); err != nil { 48 return false, err 49 } 50 51 var v interface{} 52 if err := json.Unmarshal([]byte(given.StringVal), &v); err != nil { 53 return false, err 54 } 55 56 results, err := p.FindResults(v) 57 if err != nil { 58 return false, err 59 } 60 var values []interface{} 61 for _, result := range results { 62 var buffy bytes.Buffer 63 if err := p.PrintResults(&buffy, result); err != nil { 64 return false, err 65 } 66 67 var value interface{} 68 if err := json.Unmarshal(buffy.Bytes(), &value); err != nil { 69 return false, err 70 } 71 values = append(values, value) 72 } 73 if len(values) == 1 { 74 if b, err := json.Marshal(values[0]); err != nil { 75 return false, err 76 } else { 77 given = *v1beta1.NewArrayOrString(string(b)) 78 } 79 } else if b, err := json.Marshal(values); err != nil { 80 return false, err 81 } else { 82 given = *v1beta1.NewArrayOrString(string(b)) 83 } 84 } 85 86 if matcher.value != nil { 87 return strings.TrimSpace(given.StringVal) == *matcher.value, nil 88 } else { 89 matcher.jsonMatcher = gomega.MatchJSON(*matcher.jsonValue) 90 return matcher.jsonMatcher.Match(given.StringVal) 91 } 92 } 93 } 94 95 func (matcher *TaskRunResultMatcher) NegatedFailureMessage(actual interface{}) (message string) { 96 if matcher.jsonPath != nil && matcher.jsonValue != nil { 97 return fmt.Sprintf("value `%s` for JSONPath `%s` not to equal `%s`", actual, *matcher.jsonPath, *matcher.jsonValue) 98 } 99 if matcher.value != nil { 100 return fmt.Sprintf("%v not to equal %v", actual, v1beta1.TaskRunResult{ 101 Name: matcher.name, 102 Value: *v1beta1.NewArrayOrString(strings.TrimSpace(*matcher.value)), 103 }) 104 } 105 106 return matcher.jsonMatcher.NegatedFailureMessage(actual) 107 } 108 109 func MatchTaskRunResult(name, value string) types.GomegaMatcher { 110 return &TaskRunResultMatcher{name: name, value: &value} 111 } 112 113 func MatchTaskRunResultWithJSONValue(name string, json interface{}) types.GomegaMatcher { 114 return &TaskRunResultMatcher{name: name, jsonValue: &json} 115 } 116 117 func MatchTaskRunResultWithJSONPathValue(name, path string, json interface{}) types.GomegaMatcher { 118 return &TaskRunResultMatcher{name: name, jsonPath: &path, jsonValue: &json} 119 } 120 121 func DidTaskSucceed(tr interface{}) bool { 122 switch tr := tr.(type) { 123 case *v1beta1.PipelineRunTaskRunStatus: 124 return tr.Status.GetCondition(apis.ConditionSucceeded).IsTrue() 125 case *v1beta1.TaskRunStatus: 126 return tr.Status.GetCondition(apis.ConditionSucceeded).IsTrue() 127 } 128 return false 129 }