github.com/operator-framework/operator-lifecycle-manager@v0.30.0/test/e2e/like_metric_matcher_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/onsi/gomega/format"
     7  	"github.com/onsi/gomega/types"
     8  )
     9  
    10  type Metric struct {
    11  	Family string
    12  	Labels map[string][]string
    13  	Value  float64 // Zero unless type is Untypted, Gauge, or Counter!
    14  }
    15  
    16  type MetricPredicate struct {
    17  	f    func(m Metric) bool
    18  	name string
    19  }
    20  
    21  func (mp MetricPredicate) String() string {
    22  	return mp.name
    23  }
    24  
    25  func WithFamily(f string) MetricPredicate {
    26  	return MetricPredicate{
    27  		name: fmt.Sprintf("WithFamily(%s)", f),
    28  		f: func(m Metric) bool {
    29  			return m.Family == f
    30  		},
    31  	}
    32  }
    33  
    34  func WithLabel(n, v string) MetricPredicate {
    35  	return MetricPredicate{
    36  		name: fmt.Sprintf("WithLabel(%s=%s)", n, v),
    37  		f: func(m Metric) bool {
    38  			for name, values := range m.Labels {
    39  				for _, value := range values {
    40  					if name == n && value == v {
    41  						return true
    42  					}
    43  				}
    44  			}
    45  			return false
    46  		},
    47  	}
    48  }
    49  
    50  func WithName(name string) MetricPredicate {
    51  	return WithLabel("name", name)
    52  }
    53  
    54  func WithNamespace(namespace string) MetricPredicate {
    55  	return WithLabel("namespace", namespace)
    56  }
    57  
    58  func WithChannel(channel string) MetricPredicate {
    59  	return WithLabel("channel", channel)
    60  }
    61  
    62  func WithPackage(pkg string) MetricPredicate {
    63  	return WithLabel("package", pkg)
    64  }
    65  
    66  func WithPhase(phase string) MetricPredicate {
    67  	return WithLabel("phase", phase)
    68  }
    69  
    70  func WithReason(reason string) MetricPredicate {
    71  	return WithLabel("reason", reason)
    72  }
    73  
    74  func WithApproval(approvalStrategy string) MetricPredicate {
    75  	return WithLabel("approval", approvalStrategy)
    76  }
    77  
    78  func WithVersion(version string) MetricPredicate {
    79  	return WithLabel("version", version)
    80  }
    81  
    82  func WithValue(v float64) MetricPredicate {
    83  	return MetricPredicate{
    84  		name: fmt.Sprintf("WithValue(%g)", v),
    85  		f: func(m Metric) bool {
    86  			return m.Value == v
    87  		},
    88  	}
    89  }
    90  
    91  func WithValueGreaterThan(v float64) MetricPredicate {
    92  	return MetricPredicate{
    93  		name: fmt.Sprintf("WithValueGreaterThan(%g)", v),
    94  		f: func(m Metric) bool {
    95  			return m.Value > v
    96  		},
    97  	}
    98  }
    99  
   100  type LikeMetricMatcher struct {
   101  	Predicates []MetricPredicate
   102  }
   103  
   104  func (matcher *LikeMetricMatcher) Match(actual interface{}) (bool, error) {
   105  	metric, ok := actual.(Metric)
   106  	if !ok {
   107  		return false, fmt.Errorf("LikeMetric matcher expects Metric (got %T)", actual)
   108  	}
   109  	for _, predicate := range matcher.Predicates {
   110  		if !predicate.f(metric) {
   111  			return false, nil
   112  		}
   113  	}
   114  	return true, nil
   115  }
   116  
   117  func (matcher *LikeMetricMatcher) FailureMessage(actual interface{}) string {
   118  	return format.Message(actual, "to satisfy", matcher.Predicates)
   119  }
   120  
   121  func (matcher *LikeMetricMatcher) NegatedFailureMessage(actual interface{}) string {
   122  	return format.Message(actual, "not to satisfy", matcher.Predicates)
   123  }
   124  
   125  func LikeMetric(preds ...MetricPredicate) types.GomegaMatcher {
   126  	return &LikeMetricMatcher{
   127  		Predicates: preds,
   128  	}
   129  }