sigs.k8s.io/cluster-api@v1.7.1/util/conditions/matchers.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package conditions
    18  
    19  import (
    20  	"errors"
    21  
    22  	"github.com/onsi/gomega/format"
    23  	"github.com/onsi/gomega/types"
    24  
    25  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    26  )
    27  
    28  // HaveSameStateOf matches a condition to have the same state of another.
    29  func HaveSameStateOf(expected *clusterv1.Condition) types.GomegaMatcher {
    30  	return &conditionMatcher{
    31  		Expected: expected,
    32  	}
    33  }
    34  
    35  type conditionMatcher struct {
    36  	Expected *clusterv1.Condition
    37  }
    38  
    39  func (matcher *conditionMatcher) Match(actual interface{}) (success bool, err error) {
    40  	actualCondition, ok := actual.(*clusterv1.Condition)
    41  	if !ok {
    42  		return false, errors.New("value should be a condition")
    43  	}
    44  
    45  	return hasSameState(actualCondition, matcher.Expected), nil
    46  }
    47  
    48  func (matcher *conditionMatcher) FailureMessage(actual interface{}) (message string) {
    49  	return format.Message(actual, "to have the same state of", matcher.Expected)
    50  }
    51  func (matcher *conditionMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    52  	return format.Message(actual, "not to have the same state of", matcher.Expected)
    53  }