Skip to content

Commit 15300a4

Browse files
authored
Merge pull request kubernetes-sigs#3028 from sedefsavas/e2e-machine-status-check
[e2e] add machine helper for status check.
2 parents 69ebae6 + 59ceb80 commit 15300a4

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

test/framework/machine_helpers.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,50 @@ func PatchNodeCondition(ctx context.Context, input PatchNodeConditionInput) {
207207
node.Status.Conditions = append(node.Status.Conditions, input.NodeCondition)
208208
Expect(patchHelper.Patch(ctx, node)).To(Succeed())
209209
}
210+
211+
// MachineStatusCheck is a type that operates a status check on a Machine
212+
type MachineStatusCheck func(p *clusterv1.Machine) error
213+
214+
// WaitForMachineStatusCheckInput is the input for WaitForMachineStatusCheck.
215+
type WaitForMachineStatusCheckInput struct {
216+
Machine *clusterv1.Machine
217+
StatusChecks []MachineStatusCheck
218+
WaitForMachineIntervals []interface{}
219+
}
220+
221+
// WaitForMachineStatusCheck waits for the specified status to be true for the machine.
222+
func WaitForMachineStatusCheck(ctx context.Context, input WaitForMachineStatusCheckInput) {
223+
Expect(ctx).NotTo(BeNil(), "ctx is required for WaitForMachineStatusCheck")
224+
Expect(input.Machine).ToNot(BeNil(), "Invalid argument. input.Machine can't be nil when calling WaitForMachineStatusCheck")
225+
Expect(input.StatusChecks).ToNot(BeEmpty(), "Invalid argument. input.StatusCheck can't be empty when calling WaitForMachineStatusCheck")
226+
227+
Eventually(func() (bool, error) {
228+
for _, statusCheck := range input.StatusChecks {
229+
err := statusCheck(input.Machine)
230+
if err != nil {
231+
return false, err
232+
}
233+
}
234+
return true, nil
235+
}, input.WaitForMachineIntervals...).Should(BeTrue())
236+
}
237+
238+
// MachineNodeRefCheck is a MachineStatusCheck ensuring that a NodeRef is assigned to the machine
239+
func MachineNodeRefCheck() MachineStatusCheck {
240+
return func(machine *clusterv1.Machine) error {
241+
if machine.Status.NodeRef == nil {
242+
return errors.Errorf("NodeRef is not assigned to the machine %s/%s", machine.Namespace, machine.Name)
243+
}
244+
return nil
245+
}
246+
}
247+
248+
// MachinePhaseCheck is a MachineStatusCheck ensuring that a machines is in the expected phase
249+
func MachinePhaseCheck(expectedPhase string) MachineStatusCheck {
250+
return func(machine *clusterv1.Machine) error {
251+
if machine.Status.Phase != expectedPhase {
252+
return errors.Errorf("Machine %s/%s is not in phase %s", machine.Namespace, machine.Name, expectedPhase)
253+
}
254+
return nil
255+
}
256+
}

0 commit comments

Comments
 (0)