sigs.k8s.io/cluster-api@v1.7.1/internal/controllers/machinehealthcheck/machinehealthcheck_status_matcher_test.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 machinehealthcheck 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 "sigs.k8s.io/cluster-api/util/conditions" 27 ) 28 29 // MatchMachineHealthCheckStatus returns a custom matcher to check equality of clusterv1.MachineHealthCheckStatus. 30 func MatchMachineHealthCheckStatus(expected *clusterv1.MachineHealthCheckStatus) types.GomegaMatcher { 31 return &machineHealthCheckStatusMatcher{ 32 expected: expected, 33 } 34 } 35 36 type machineHealthCheckStatusMatcher struct { 37 expected *clusterv1.MachineHealthCheckStatus 38 } 39 40 func (m machineHealthCheckStatusMatcher) Match(actual interface{}) (success bool, err error) { 41 actualStatus, ok := actual.(*clusterv1.MachineHealthCheckStatus) 42 if !ok { 43 return false, fmt.Errorf("actual should be of type MachineHealthCheckStatus") 44 } 45 46 ok, err = Equal(m.expected.CurrentHealthy).Match(actualStatus.CurrentHealthy) 47 if !ok { 48 return ok, err 49 } 50 ok, err = Equal(m.expected.ExpectedMachines).Match(actualStatus.ExpectedMachines) 51 if !ok { 52 return ok, err 53 } 54 ok, err = Equal(m.expected.RemediationsAllowed).Match(actualStatus.RemediationsAllowed) 55 if !ok { 56 return ok, err 57 } 58 ok, err = Equal(m.expected.Targets).Match(actualStatus.Targets) 59 if !ok { 60 return ok, err 61 } 62 ok, err = conditions.MatchConditions(m.expected.Conditions).Match(actualStatus.Conditions) 63 return ok, err 64 } 65 66 func (m machineHealthCheckStatusMatcher) FailureMessage(actual interface{}) (message string) { 67 return fmt.Sprintf("expected\n\t%#v\nto match\n\t%#v\n", actual, m.expected) 68 } 69 70 func (m machineHealthCheckStatusMatcher) NegatedFailureMessage(actual interface{}) (message string) { 71 return fmt.Sprintf("expected\n\t%#v\nto not match\n\t%#v\n", actual, m.expected) 72 }