Skip to content

Commit

Permalink
adding a unit test that checks CR is created with regular (non genera…
Browse files Browse the repository at this point in the history
…ted) machine name when using a multiple support template.

Signed-off-by: Michael Shitrit <[email protected]>
  • Loading branch information
mshitrit committed Sep 8, 2024
1 parent 7d9c383 commit ba2843b
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions controllers/machinehealthcheck_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
commonannotations "github.com/medik8s/common/pkg/annotations"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -122,16 +123,16 @@ func TestReconcile(t *testing.T) {
machineWithoutNodeRef := newMachine("machineWithoutNodeRef", nodeAnnotatedWithMachineWithoutNodeReference.Name)
machineWithoutNodeRef.Status.NodeRef = nil

machineHealthCheck := newMachineHealthCheck("machineHealthCheck")
machineHealthCheck := newMachineHealthCheck("machineHealthCheck", infraRemediationTemplateRef)
nodeStartupTimeout := 15 * time.Minute
machineHealthCheck.Spec.NodeStartupTimeout = &metav1.Duration{Duration: nodeStartupTimeout}

machineHealthCheckNegativeMaxUnhealthy := newMachineHealthCheck("machineHealthCheckNegativeMaxUnhealthy")
machineHealthCheckNegativeMaxUnhealthy := newMachineHealthCheck("machineHealthCheckNegativeMaxUnhealthy", infraRemediationTemplateRef)
negativeOne := intstr.FromInt(-1)
machineHealthCheckNegativeMaxUnhealthy.Spec.MaxUnhealthy = &negativeOne
machineHealthCheckNegativeMaxUnhealthy.Spec.NodeStartupTimeout = &metav1.Duration{Duration: nodeStartupTimeout}

machineHealthCheckPaused := newMachineHealthCheck("machineHealthCheck")
machineHealthCheckPaused := newMachineHealthCheck("machineHealthCheck", infraRemediationTemplateRef)
machineHealthCheckPaused.Annotations = make(map[string]string)
machineHealthCheckPaused.Annotations[annotations.MHCPausedAnnotation] = "test"

Expand Down Expand Up @@ -387,8 +388,11 @@ func TestReconcileExternalRemediationTemplate(t *testing.T) {
machineWithNodeUnHealthy := newMachine("Machine", nodeUnHealthy.Name)
machineWithNodeUnHealthy.APIVersion = machinev1.SchemeGroupVersion.String()

mhc := newMachineHealthCheck("machineHealthCheck")
mhc := newMachineHealthCheck("machineHealthCheck", infraRemediationTemplateRef)
mhcMultipleSupport := newMachineHealthCheck("machineHealthCheck", infraMultipleRemediationTemplateRef)

remediationTemplateCR := newTestRemediationTemplateCR(InfraRemediationKind, MachineNamespace, InfraRemediationTemplateName)
remediationMultipleSupportTemplateCR := newTestRemediationTemplateCR(InfraRemediationKind, MachineNamespace, InfraMultipleSupportRemediationTemplateName)
owner := metav1.OwnerReference{
APIVersion: mhc.APIVersion,
Kind: mhc.Kind,
Expand Down Expand Up @@ -449,7 +453,7 @@ func TestReconcileExternalRemediationTemplate(t *testing.T) {
node: nodeUnHealthy,
mhc: mhc,
remediationCR: remediationCR,
remediationTemplate: remediationTemplateCR,
remediationTemplate: remediationMultipleSupportTemplateCR,
expected: expectedReconcile{
result: reconcile.Result{},
error: false,
Expand All @@ -464,6 +468,28 @@ func TestReconcileExternalRemediationTemplate(t *testing.T) {
},
},
},

{
name: "create new multiple template supported remediation",
machine: machineWithNodeUnHealthy,
node: nodeUnHealthy,
mhc: mhcMultipleSupport,
remediationCR: nil,
remediationTemplate: remediationMultipleSupportTemplateCR,
expected: expectedReconcile{
result: reconcile.Result{},
error: false,
},
expectedEvents: []string{utils.EventReasonRemediationCreated},
expectedStatus: &machinev1.MachineHealthCheckStatus{
ExpectedMachines: pointer.Int(1),
CurrentHealthy: pointer.Int(0),
RemediationsAllowed: 0,
Conditions: machinev1.Conditions{
remediationAllowedCondition,
},
},
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -849,7 +875,7 @@ func TestMHCRequestsFromNode(t *testing.T) {
func TestGetTargetsFromMHC(t *testing.T) {
machine1 := newMachine("match1", "node1")
machine2 := newMachine("match2", "node2")
mhc := newMachineHealthCheck("findTargets")
mhc := newMachineHealthCheck("findTargets", infraRemediationTemplateRef)
testCases := []struct {
testCase string
mhc *machinev1.MachineHealthCheck
Expand Down Expand Up @@ -2485,6 +2511,10 @@ func buildRunTimeObjects(tc testCase) []runtime.Object {
objects = append(objects, newTestRemediationCRD(testRemediationKind))
objects = append(objects, newTestRemediationTemplateCR(testRemediationKind, MachineNamespace, InfraRemediationTemplateName))

templateMultiSupportCR := newTestRemediationTemplateCR(testRemediationKind, MachineNamespace, InfraMultipleSupportRemediationTemplateName)
templateMultiSupportCR.SetAnnotations(map[string]string{commonannotations.MultipleTemplatesSupportedAnnotation: "true"})
objects = append(objects, templateMultiSupportCR)

return objects
}

Expand Down Expand Up @@ -2588,7 +2618,7 @@ func newMachine(name string, nodeName string) *machinev1.Machine {
}

// newMachineHealthCheck returns new MachineHealthCheck object that can be used for testing
func newMachineHealthCheck(name string) *machinev1.MachineHealthCheck {
func newMachineHealthCheck(name string, remediationTemplate *corev1.ObjectReference) *machinev1.MachineHealthCheck {
return &machinev1.MachineHealthCheck{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -2615,7 +2645,7 @@ func newMachineHealthCheck(name string) *machinev1.MachineHealthCheck {
Timeout: metav1.Duration{Duration: 300 * time.Second},
},
},
RemediationTemplate: infraRemediationTemplateRef,
RemediationTemplate: remediationTemplate,
},
Status: machinev1.MachineHealthCheckStatus{},
}
Expand Down

0 comments on commit ba2843b

Please sign in to comment.