sigs.k8s.io/cluster-api@v1.7.1/util/conditions/matcher.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 "fmt" 21 22 "github.com/onsi/gomega" 23 "github.com/onsi/gomega/types" 24 25 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 26 ) 27 28 // MatchConditions returns a custom matcher to check equality of clusterv1.Conditions. 29 func MatchConditions(expected clusterv1.Conditions) types.GomegaMatcher { 30 return &matchConditions{ 31 expected: expected, 32 } 33 } 34 35 type matchConditions struct { 36 expected clusterv1.Conditions 37 } 38 39 func (m matchConditions) Match(actual interface{}) (success bool, err error) { 40 elems := []interface{}{} 41 for _, condition := range m.expected { 42 elems = append(elems, MatchCondition(condition)) 43 } 44 45 return gomega.ConsistOf(elems...).Match(actual) 46 } 47 48 func (m matchConditions) FailureMessage(actual interface{}) (message string) { 49 return fmt.Sprintf("expected\n\t%#v\nto match\n\t%#v\n", actual, m.expected) 50 } 51 52 func (m matchConditions) NegatedFailureMessage(actual interface{}) (message string) { 53 return fmt.Sprintf("expected\n\t%#v\nto not match\n\t%#v\n", actual, m.expected) 54 } 55 56 // MatchCondition returns a custom matcher to check equality of clusterv1.Condition. 57 func MatchCondition(expected clusterv1.Condition) types.GomegaMatcher { 58 return &matchCondition{ 59 expected: expected, 60 } 61 } 62 63 type matchCondition struct { 64 expected clusterv1.Condition 65 } 66 67 func (m matchCondition) Match(actual interface{}) (success bool, err error) { 68 actualCondition, ok := actual.(clusterv1.Condition) 69 if !ok { 70 return false, fmt.Errorf("actual should be of type Condition") 71 } 72 73 ok, err = gomega.Equal(m.expected.Type).Match(actualCondition.Type) 74 if !ok { 75 return ok, err 76 } 77 ok, err = gomega.Equal(m.expected.Status).Match(actualCondition.Status) 78 if !ok { 79 return ok, err 80 } 81 ok, err = gomega.Equal(m.expected.Severity).Match(actualCondition.Severity) 82 if !ok { 83 return ok, err 84 } 85 ok, err = gomega.Equal(m.expected.Reason).Match(actualCondition.Reason) 86 if !ok { 87 return ok, err 88 } 89 ok, err = gomega.Equal(m.expected.Message).Match(actualCondition.Message) 90 if !ok { 91 return ok, err 92 } 93 94 return ok, err 95 } 96 97 func (m matchCondition) FailureMessage(actual interface{}) (message string) { 98 return fmt.Sprintf("expected\n\t%#v\nto match\n\t%#v\n", actual, m.expected) 99 } 100 101 func (m matchCondition) NegatedFailureMessage(actual interface{}) (message string) { 102 return fmt.Sprintf("expected\n\t%#v\nto not match\n\t%#v\n", actual, m.expected) 103 }