...

Source file src/github.com/linkerd/linkerd2/testutil/prommatch/suite.go

Documentation: github.com/linkerd/linkerd2/testutil/prommatch

     1  package prommatch
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type matcherAndMessage struct {
     8  	matcher *Matcher
     9  	message string
    10  	present bool
    11  }
    12  
    13  // Suite is a list of matchers and messages to check against a string of metrics.
    14  type Suite []matcherAndMessage
    15  
    16  // MustContain a series which will match the provided matcher.
    17  func (ms Suite) MustContain(message string, m *Matcher) Suite {
    18  	return append(ms, matcherAndMessage{
    19  		matcher: m,
    20  		message: message,
    21  		present: true,
    22  	})
    23  }
    24  
    25  // MustNotContain a series which will match the provided matcher.
    26  func (ms Suite) MustNotContain(message string, m *Matcher) Suite {
    27  	return append(ms, matcherAndMessage{
    28  		matcher: m,
    29  		message: message,
    30  		present: false,
    31  	})
    32  }
    33  
    34  // CheckString will run each assertion in the suite against the provided metrics.
    35  func (ms Suite) CheckString(metrics string) error {
    36  	for _, m := range ms {
    37  		ok, err := m.matcher.HasMatchInString(metrics)
    38  		if err != nil {
    39  			return fmt.Errorf("failed to run a check of against the provided metrics: %w", err)
    40  		}
    41  		if m.present && !ok {
    42  			return fmt.Errorf("expected to find %s\n%s", m.message, metrics)
    43  		}
    44  		if !m.present && ok {
    45  			return fmt.Errorf("expected not to find %s\n%s", m.message, metrics)
    46  		}
    47  	}
    48  	return nil
    49  }
    50  

View as plain text