Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
Update tests with MHC events
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Mular <[email protected]>
  • Loading branch information
ondrejmular committed Oct 8, 2019
1 parent 8c614fd commit 3138a4f
Showing 1 changed file with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package machinehealthcheck
import (
"context"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -82,11 +83,15 @@ func TestHasMatchingLabels(t *testing.T) {

// newFakeReconciler returns a new reconcile.Reconciler with a fake client
func newFakeReconciler(initObjects ...runtime.Object) *ReconcileMachineHealthCheck {
return newFakeReconcilerCustomRecorder(record.NewFakeRecorder(10), initObjects...)
}

func newFakeReconcilerCustomRecorder(recorder record.EventRecorder, initObjects ...runtime.Object) *ReconcileMachineHealthCheck {
fakeClient := fake.NewFakeClient(initObjects...)
return &ReconcileMachineHealthCheck{
client: fakeClient,
namespace: consts.NamespaceOpenshiftMachineAPI,
recorder: record.NewFakeRecorder(10),
recorder: recorder,
}
}

Expand All @@ -95,6 +100,28 @@ type expectedReconcile struct {
error bool
}

func assertEvents(t *testing.T, testCase string, expectedEvents []string, realEvents chan string) {
if len(expectedEvents) != len(realEvents) {
t.Errorf(
"Test case: %s. Number of expected events (%v) differs from number of real events (%v)",
testCase,
len(expectedEvents),
len(realEvents),
)
} else {
for _, eventType := range expectedEvents {
select {
case event := <-realEvents:
if !strings.Contains(event, eventType) {
t.Errorf("Test case: %s. Expected %s event, got: %v", testCase, eventType, event)
}
default:
t.Errorf("Test case: %s. Expected %s event, but no event occured", testCase, eventType)
}
}
}
}

func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects ...runtime.Object) {
// healthy node
nodeHealthy := mrotesting.NewNode("healthy", true, "machineWithNodehealthy")
Expand Down Expand Up @@ -139,6 +166,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
node *corev1.Node
remediationStrategy mrv1.RemediationStrategyType
expected expectedReconcile
expectedEvents []string
}{
{
machine: machineUnhealthyForTooLong,
Expand All @@ -148,6 +176,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
error: false,
},
remediationStrategy: mrv1.RemediationStrategyTypeReboot,
expectedEvents: []string{"MachineRemediationCreated"},
},
{
machine: machineWithNodeHealthy,
Expand All @@ -156,6 +185,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
result: reconcile.Result{},
error: false,
},
expectedEvents: []string{},
},
{
machine: machineWithNodeRecentlyUnhealthy,
Expand All @@ -167,6 +197,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
},
error: false,
},
expectedEvents: []string{"MachineRemediationWaiting"},
},
{
machine: nil,
Expand All @@ -175,6 +206,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
result: reconcile.Result{},
error: false,
},
expectedEvents: []string{},
},
{
machine: nil,
Expand All @@ -183,6 +215,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
result: reconcile.Result{},
error: false,
},
expectedEvents: []string{},
},
{
machine: machineWithoutOwnerController,
Expand All @@ -191,6 +224,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
result: reconcile.Result{},
error: false,
},
expectedEvents: []string{},
},
{
machine: machineWithoutNodeRef,
Expand All @@ -199,6 +233,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
result: reconcile.Result{},
error: true,
},
expectedEvents: []string{},
},
{
machine: machineWithRemediationDisabled,
Expand All @@ -207,6 +242,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
result: reconcile.Result{},
error: false,
},
expectedEvents: []string{},
},
}

Expand All @@ -219,7 +255,8 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
objects = append(objects, tc.machine)
}
objects = append(objects, tc.node)
r := newFakeReconciler(objects...)
recorder := record.NewFakeRecorder(10)
r := newFakeReconcilerCustomRecorder(recorder, objects...)

request := reconcile.Request{
NamespacedName: types.NamespacedName{
Expand All @@ -228,6 +265,7 @@ func testReconcile(t *testing.T, remediationWaitTime time.Duration, initObjects
},
}
result, err := r.Reconcile(request)
assertEvents(t, tc.node.Name, tc.expectedEvents, recorder.Events)
if tc.expected.error != (err != nil) {
var errorExpectation string
if !tc.expected.error {
Expand Down Expand Up @@ -330,12 +368,20 @@ func TestApplyRemediationReboot(t *testing.T) {
nodeUnhealthyForTooLong := mrotesting.NewNode("nodeUnhealthyForTooLong", false, "machineUnhealthyForTooLong")
machineUnhealthyForTooLong := mrotesting.NewMachine("machineUnhealthyForTooLong", nodeUnhealthyForTooLong.Name, "")
machineHealthCheck := mrotesting.NewMachineHealthCheck("machineHealthCheck")
r := newFakeReconciler(nodeUnhealthyForTooLong, machineUnhealthyForTooLong, machineHealthCheck)
recorder := record.NewFakeRecorder(1)
r := newFakeReconcilerCustomRecorder(
recorder,
nodeUnhealthyForTooLong,
machineUnhealthyForTooLong,
machineHealthCheck,
)
_, err := r.remediationStrategyReboot(machineUnhealthyForTooLong, nodeUnhealthyForTooLong)
if err != nil {
t.Fatalf("unexpected error %v", err)
}

assertEvents(t, "TestApplyRemediationReboot", []string{"MachineRemediationCreated"}, recorder.Events)

machineRemediations := &mrv1.MachineRemediationList{}
if err := r.client.List(context.TODO(), machineRemediations); err != nil {
t.Errorf("Expected: no error, got: %v", err)
Expand Down

0 comments on commit 3138a4f

Please sign in to comment.