...

Source file src/edge-infra.dev/pkg/k8s/runtime/conditions/matcher.go

Documentation: edge-infra.dev/pkg/k8s/runtime/conditions

     1  package conditions
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	. "github.com/onsi/gomega" //nolint:revive
     8  	"github.com/onsi/gomega/format"
     9  	"github.com/onsi/gomega/types"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  )
    12  
    13  // HaveSameStateOf returns a custom matcher to check equality of a metav1.Condition, the condition message is checked
    14  // for a subset string match.
    15  func HaveSameStateOf(expected *metav1.Condition) types.GomegaMatcher {
    16  	return &ConditionMatcher{
    17  		Expected: expected,
    18  	}
    19  }
    20  
    21  // MatchConditions returns a custom matcher to check equality of a metav1.Condition slice, the condition messages are
    22  // checked for a subset string match.
    23  func MatchConditions(expected []metav1.Condition) types.GomegaMatcher {
    24  	return &matchConditions{
    25  		expected: expected,
    26  	}
    27  }
    28  
    29  type matchConditions struct {
    30  	expected []metav1.Condition
    31  }
    32  
    33  func (m matchConditions) Match(actual interface{}) (success bool, err error) {
    34  	elems := []interface{}{}
    35  	for _, condition := range m.expected {
    36  		elems = append(elems, MatchCondition(condition))
    37  	}
    38  	return ConsistOf(elems).Match(actual)
    39  }
    40  
    41  func (m matchConditions) FailureMessage(actual interface{}) (message string) {
    42  	return fmt.Sprintf("expected\n\t%#v\nto match\n\t%#v\n", actual, m.expected)
    43  }
    44  
    45  func (m matchConditions) NegatedFailureMessage(actual interface{}) (message string) {
    46  	return fmt.Sprintf("expected\n\t%#v\nto not match\n\t%#v\n", actual, m.expected)
    47  }
    48  
    49  // MatchCondition returns a custom matcher to check equality of metav1.Condition.
    50  func MatchCondition(expected metav1.Condition) types.GomegaMatcher {
    51  	return &matchCondition{
    52  		expected: expected,
    53  	}
    54  }
    55  
    56  type ConditionMatcher struct {
    57  	Expected *metav1.Condition
    58  }
    59  
    60  func (matcher *ConditionMatcher) Match(actual interface{}) (success bool, err error) {
    61  	actualCondition, ok := actual.(*metav1.Condition)
    62  	if !ok {
    63  		return false, errors.New("value should be a condition")
    64  	}
    65  	return hasSameState(actualCondition, matcher.Expected), nil
    66  }
    67  
    68  func (matcher *ConditionMatcher) FailureMessage(actual interface{}) (message string) {
    69  	return format.Message(actual, "to have the same state of", matcher.Expected)
    70  }
    71  
    72  func (matcher *ConditionMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    73  	return format.Message(actual, "not to have the same state of", matcher.Expected)
    74  }
    75  
    76  type matchCondition struct {
    77  	expected metav1.Condition
    78  }
    79  
    80  func (m matchCondition) Match(actual interface{}) (success bool, err error) {
    81  	actualCondition, ok := actual.(metav1.Condition)
    82  	if !ok {
    83  		return false, fmt.Errorf("actual should be of type Condition")
    84  	}
    85  
    86  	ok, err = Equal(m.expected.Type).Match(actualCondition.Type)
    87  	if !ok {
    88  		return ok, err
    89  	}
    90  	ok, err = Equal(m.expected.Status).Match(actualCondition.Status)
    91  	if !ok {
    92  		return ok, err
    93  	}
    94  	ok, err = Equal(m.expected.Reason).Match(actualCondition.Reason)
    95  	if !ok {
    96  		return ok, err
    97  	}
    98  	ok, err = ContainSubstring(m.expected.Message).Match(actualCondition.Message)
    99  	if !ok {
   100  		return ok, err
   101  	}
   102  
   103  	return ok, err
   104  }
   105  
   106  func (m matchCondition) FailureMessage(actual interface{}) (message string) {
   107  	return fmt.Sprintf("expected\n\t%#v\nto match\n\t%#v\n", actual, m.expected)
   108  }
   109  
   110  func (m matchCondition) NegatedFailureMessage(actual interface{}) (message string) {
   111  	return fmt.Sprintf("expected\n\t%#v\nto not match\n\t%#v\n", actual, m.expected)
   112  }
   113  

View as plain text