github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/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  	tektonpipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
    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  // FailureMessage returns failure message for a TaskRunResult matcher.
    25  func (matcher *TaskRunResultMatcher) FailureMessage(actual interface{}) (message string) {
    26  	if matcher.value != nil {
    27  		return fmt.Sprintf("%v to equal %v", actual, tektonpipeline.TaskRunResult{
    28  			Name:  matcher.name,
    29  			Value: *tektonpipeline.NewStructuredValues(*matcher.value),
    30  		})
    31  	}
    32  
    33  	return matcher.jsonMatcher.FailureMessage(actual)
    34  }
    35  
    36  // Match matches the matcher with a given taskRun.
    37  func (matcher *TaskRunResultMatcher) Match(actual interface{}) (success bool, err error) {
    38  	if tr, ok := actual.(tektonpipeline.TaskRunResult); !ok {
    39  		return false, fmt.Errorf("not given TaskRunResult")
    40  	} else {
    41  		if tr.Name != matcher.name {
    42  			return false, nil
    43  		}
    44  
    45  		given := tr.Value
    46  		if matcher.jsonPath != nil {
    47  			p := jsonpath.New("test")
    48  			p.EnableJSONOutput(true)
    49  			if err := p.Parse(*matcher.jsonPath); err != nil {
    50  				return false, err
    51  			}
    52  
    53  			var v interface{}
    54  			if err := json.Unmarshal([]byte(given.StringVal), &v); err != nil {
    55  				return false, err
    56  			}
    57  
    58  			results, err := p.FindResults(v)
    59  			if err != nil {
    60  				return false, err
    61  			}
    62  			var values []interface{}
    63  			for _, result := range results {
    64  				var buffy bytes.Buffer
    65  				if err := p.PrintResults(&buffy, result); err != nil {
    66  					return false, err
    67  				}
    68  
    69  				var value interface{}
    70  				if err := json.Unmarshal(buffy.Bytes(), &value); err != nil {
    71  					return false, err
    72  				}
    73  				values = append(values, value)
    74  			}
    75  			if len(values) == 1 {
    76  				if b, err := json.Marshal(values[0]); err != nil {
    77  					return false, err
    78  				} else {
    79  					given = *tektonpipeline.NewStructuredValues(string(b))
    80  				}
    81  			} else if b, err := json.Marshal(values); err != nil {
    82  				return false, err
    83  			} else {
    84  				given = *tektonpipeline.NewStructuredValues(string(b))
    85  			}
    86  		}
    87  
    88  		if matcher.value != nil {
    89  			return strings.TrimSpace(given.StringVal) == *matcher.value, nil
    90  		} else {
    91  			matcher.jsonMatcher = gomega.MatchJSON(*matcher.jsonValue)
    92  			return matcher.jsonMatcher.Match(given.StringVal)
    93  		}
    94  	}
    95  }
    96  
    97  // NegatedFailureMessage returns negated failure message for a TaskRunResult matcher.
    98  func (matcher *TaskRunResultMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    99  	if matcher.jsonPath != nil && matcher.jsonValue != nil {
   100  		return fmt.Sprintf("value `%s` for JSONPath `%s` not to equal `%s`", actual, *matcher.jsonPath, *matcher.jsonValue)
   101  	}
   102  	if matcher.value != nil {
   103  		return fmt.Sprintf("%v not to equal %v", actual, tektonpipeline.TaskRunResult{
   104  			Name:  matcher.name,
   105  			Value: *tektonpipeline.NewStructuredValues(strings.TrimSpace(*matcher.value)),
   106  		})
   107  	}
   108  
   109  	return matcher.jsonMatcher.NegatedFailureMessage(actual)
   110  }
   111  
   112  // MatchTaskRunResult returns a taskRunResult matcher.
   113  func MatchTaskRunResult(name, value string) types.GomegaMatcher {
   114  	return &TaskRunResultMatcher{name: name, value: &value}
   115  }
   116  
   117  // MatchTaskRunResultWithJSONValue returns a taskRunResult with a JSONValue matcher.
   118  func MatchTaskRunResultWithJSONValue(name string, json interface{}) types.GomegaMatcher {
   119  	return &TaskRunResultMatcher{name: name, jsonValue: &json}
   120  }
   121  
   122  // MatchTaskRunResultWithJSONPathValue returns a taskRunResult with a JSONPath and JSONvalue matcher.
   123  func MatchTaskRunResultWithJSONPathValue(name, path string, json interface{}) types.GomegaMatcher {
   124  	return &TaskRunResultMatcher{name: name, jsonPath: &path, jsonValue: &json}
   125  }
   126  
   127  func DidTaskSucceed(tr interface{}) bool {
   128  	switch tr := tr.(type) {
   129  	case *tektonpipeline.PipelineRunTaskRunStatus:
   130  		return tr.Status.GetCondition(apis.ConditionSucceeded).IsTrue()
   131  	case *tektonpipeline.TaskRunStatus:
   132  		return tr.Status.GetCondition(apis.ConditionSucceeded).IsTrue()
   133  	}
   134  	return false
   135  }