Skip to content

Commit

Permalink
Adds custom matcher for MHC status
Browse files Browse the repository at this point in the history
  • Loading branch information
Gab Satchi committed Jun 25, 2020
1 parent a9b58bf commit ff76970
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 48 deletions.
112 changes: 64 additions & 48 deletions controllers/machinehealthcheck_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,17 @@ var _ = Describe("MachineHealthCheck", func() {
targetMachines[i] = m.Name
}
// Make sure the status matches.
Eventually(func() int32 {
Eventually(func() *clusterv1.MachineHealthCheckStatus {
err := testEnv.Get(ctx, util.ObjectKey(mhc), mhc)
if err != nil {
return -1
return nil
}
return mhc.Status.CurrentHealthy
}).Should(Equal(int32(2)))
Expect(mhc.Status.ExpectedMachines).To(Equal(int32(2)))
Expect(mhc.Status.Targets).To(ConsistOf(targetMachines))
return &mhc.Status
}).Should(Equal(&clusterv1.MachineHealthCheckStatus{
ExpectedMachines: 2,
CurrentHealthy: 2,
Targets: targetMachines},
))
})

Specify("when there is one unhealthy Machine", func() {
Expand All @@ -332,15 +334,17 @@ var _ = Describe("MachineHealthCheck", func() {
}

// Make sure the status matches.
Eventually(func() int32 {
Eventually(func() *clusterv1.MachineHealthCheckStatus {
err := testEnv.Get(ctx, util.ObjectKey(mhc), mhc)
if err != nil {
return -1
return nil
}
return mhc.Status.CurrentHealthy
}).Should(Equal(int32(2)))
Expect(mhc.Status.ExpectedMachines).To(Equal(int32(3)))
Expect(mhc.Status.Targets).To(ConsistOf(targetMachines))
return &mhc.Status
}).Should(MatchMachineHealthCheckStatus(&clusterv1.MachineHealthCheckStatus{
ExpectedMachines: 3,
CurrentHealthy: 2,
Targets: targetMachines,
}))
})

Specify("when the unhealthy Machines exceed MaxUnhealthy", func() {
Expand All @@ -359,15 +363,17 @@ var _ = Describe("MachineHealthCheck", func() {
}

// Make sure the status matches.
Eventually(func() int32 {
Eventually(func() *clusterv1.MachineHealthCheckStatus {
err := testEnv.Get(ctx, util.ObjectKey(mhc), mhc)
if err != nil {
return -1
return nil
}
return mhc.Status.CurrentHealthy
}).Should(Equal(int32(1)))
Expect(mhc.Status.ExpectedMachines).To(Equal(int32(3)))
Expect(mhc.Status.Targets).To(ConsistOf(targetMachines))
return &mhc.Status
}).Should(MatchMachineHealthCheckStatus(&clusterv1.MachineHealthCheckStatus{
ExpectedMachines: 3,
CurrentHealthy: 1,
Targets: targetMachines},
))

// Calculate how many Machines have health check succeeded = false.
Eventually(func() (unhealthy int) {
Expand Down Expand Up @@ -421,15 +427,17 @@ var _ = Describe("MachineHealthCheck", func() {
}

// Make sure the status matches.
Eventually(func() int32 {
Eventually(func() *clusterv1.MachineHealthCheckStatus {
err := testEnv.Get(ctx, util.ObjectKey(mhc), mhc)
if err != nil {
return -1
return nil
}
return mhc.Status.CurrentHealthy
}).Should(Equal(int32(2)))
Expect(mhc.Status.ExpectedMachines).To(Equal(int32(3)))
Expect(mhc.Status.Targets).To(ConsistOf(targetMachines))
return &mhc.Status
}).Should(MatchMachineHealthCheckStatus(&clusterv1.MachineHealthCheckStatus{
ExpectedMachines: 3,
CurrentHealthy: 2,
Targets: targetMachines},
))

// Calculate how many Machines have health check succeeded = false.
Eventually(func() (unhealthy int) {
Expand Down Expand Up @@ -483,15 +491,17 @@ var _ = Describe("MachineHealthCheck", func() {
}

// Make sure the status matches.
Eventually(func() int32 {
Eventually(func() *clusterv1.MachineHealthCheckStatus {
err := testEnv.Get(ctx, util.ObjectKey(mhc), mhc)
if err != nil {
return -1
return nil
}
return mhc.Status.CurrentHealthy
}).Should(Equal(int32(2)))
Expect(mhc.Status.ExpectedMachines).To(Equal(int32(3)))
Expect(mhc.Status.Targets).To(ConsistOf(targetMachines))
return &mhc.Status
}).Should(MatchMachineHealthCheckStatus(&clusterv1.MachineHealthCheckStatus{
ExpectedMachines: 3,
CurrentHealthy: 2,
Targets: targetMachines},
))

// Calculate how many Machines have health check succeeded = false.
Eventually(func() (unhealthy int) {
Expand Down Expand Up @@ -551,15 +561,17 @@ var _ = Describe("MachineHealthCheck", func() {
}).Should(BeTrue())

// Make sure the status matches.
Eventually(func() int32 {
Eventually(func() *clusterv1.MachineHealthCheckStatus {
err := testEnv.Get(ctx, util.ObjectKey(mhc), mhc)
if err != nil {
return -1
return nil
}
return mhc.Status.CurrentHealthy
}).Should(Equal(int32(2)))
Expect(mhc.Status.ExpectedMachines).To(Equal(int32(3)))
Expect(mhc.Status.Targets).To(ConsistOf(targetMachines))
return &mhc.Status
}).Should(MatchMachineHealthCheckStatus(&clusterv1.MachineHealthCheckStatus{
ExpectedMachines: 3,
CurrentHealthy: 2,
Targets: targetMachines},
))

// Calculate how many Machines have health check succeeded = false.
Eventually(func() (unhealthy int) {
Expand Down Expand Up @@ -610,15 +622,17 @@ var _ = Describe("MachineHealthCheck", func() {
}

// Make sure the status matches.
Eventually(func() int32 {
Eventually(func() *clusterv1.MachineHealthCheckStatus {
err := testEnv.Get(ctx, util.ObjectKey(mhc), mhc)
if err != nil {
return -1
return nil
}
return mhc.Status.CurrentHealthy
}).Should(Equal(int32(1)))
Expect(mhc.Status.ExpectedMachines).To(Equal(int32(1)))
Expect(mhc.Status.Targets).To(ConsistOf(targetMachines))
return &mhc.Status
}).Should(MatchMachineHealthCheckStatus(&clusterv1.MachineHealthCheckStatus{
ExpectedMachines: 1,
CurrentHealthy: 1,
Targets: targetMachines},
))

// Transition the node to unhealthy.
node := nodes[0]
Expand All @@ -633,15 +647,17 @@ var _ = Describe("MachineHealthCheck", func() {
Expect(testEnv.Status().Patch(ctx, node, nodePatch)).To(Succeed())

// Make sure the status matches.
Eventually(func() int32 {
Eventually(func() *clusterv1.MachineHealthCheckStatus {
err := testEnv.Get(ctx, util.ObjectKey(mhc), mhc)
if err != nil {
return -1
return nil
}
return mhc.Status.CurrentHealthy
}).Should(Equal(int32(0)))
Expect(mhc.Status.ExpectedMachines).To(Equal(int32(1)))
Expect(mhc.Status.Targets).To(ConsistOf(targetMachines))
return &mhc.Status
}).Should(MatchMachineHealthCheckStatus(&clusterv1.MachineHealthCheckStatus{
ExpectedMachines: 1,
CurrentHealthy: 0,
Targets: targetMachines},
))

// Calculate how many Machines have health check succeeded = false.
Eventually(func() (unhealthy int) {
Expand Down
62 changes: 62 additions & 0 deletions controllers/machinehealthcheck_status_matcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers

import (
"fmt"

. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
)

// MatchMachineHealthCheckStatus returns a custom matcher to check equality of clusterv1.MachineHealthCheckStatus
func MatchMachineHealthCheckStatus(expected *clusterv1.MachineHealthCheckStatus) types.GomegaMatcher {
return &machineHealthCheckStatusMatcher{
expected: expected,
}
}

type machineHealthCheckStatusMatcher struct {
expected *clusterv1.MachineHealthCheckStatus
}

func (m machineHealthCheckStatusMatcher) Match(actual interface{}) (success bool, err error) {
actualStatus, ok := actual.(*clusterv1.MachineHealthCheckStatus)
if !ok {
return false, fmt.Errorf("actual should be of type MachineHealthCheckStatus")
}

ok, err = Equal(m.expected.CurrentHealthy).Match(actualStatus.CurrentHealthy)
if !ok {
return ok, err
}
ok, err = Equal(m.expected.ExpectedMachines).Match(actualStatus.ExpectedMachines)
if !ok {
return ok, err
}
ok, err = ConsistOf(m.expected.Targets).Match(actualStatus.Targets)
return ok, err
}

func (m machineHealthCheckStatusMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("expected\n\t%#v\nto match\n\t%#v\n", actual, m.expected)
}

func (m machineHealthCheckStatusMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("expected\n\t%#v\nto not match\n\t%#v\n", actual, m.expected)
}

0 comments on commit ff76970

Please sign in to comment.