@@ -446,20 +446,96 @@ func TestGetPortIDs(t *testing.T) {
446446 }
447447}
448448
449- func TestReconcileMachineState (t * testing.T ) {
449+ func TestReconcileMachineState (t * testing.T ) { //nolint:gocyclo,cyclop // this is test code
450450 tests := []struct {
451451 name string
452- instanceState infrav1.InstanceState
452+ instanceState * infrav1.InstanceState
453453 machineHasNodeRef bool
454+ serverConditions []clusterv1beta1.Condition
454455 expectRequeue bool
455456 expectedInstanceReadyCondition * clusterv1beta1.Condition
456457 expectedReadyCondition * clusterv1beta1.Condition
457458 expectInitializationProvisioned bool
458459 expectFailureSet bool
459460 }{
461+ {
462+ name : "Nil InstanceState with DependencyFailed condition propagates error to machine" ,
463+ instanceState : nil ,
464+ serverConditions : []clusterv1beta1.Condition {
465+ {
466+ Type : infrav1 .InstanceReadyCondition ,
467+ Status : corev1 .ConditionFalse ,
468+ Severity : clusterv1beta1 .ConditionSeverityError ,
469+ Reason : infrav1 .DependencyFailedReason ,
470+ Message : "Failed to resolve server spec: image not found" ,
471+ },
472+ },
473+ expectRequeue : true ,
474+ expectedInstanceReadyCondition : & clusterv1beta1.Condition {
475+ Type : infrav1 .InstanceReadyCondition ,
476+ Status : corev1 .ConditionFalse ,
477+ Severity : clusterv1beta1 .ConditionSeverityError ,
478+ Reason : infrav1 .DependencyFailedReason ,
479+ Message : "Failed to resolve server spec: image not found" ,
480+ },
481+ expectedReadyCondition : & clusterv1beta1.Condition {
482+ Type : clusterv1beta1 .ReadyCondition ,
483+ Status : corev1 .ConditionFalse ,
484+ Severity : clusterv1beta1 .ConditionSeverityError ,
485+ Reason : infrav1 .DependencyFailedReason ,
486+ Message : "Failed to resolve server spec: image not found" ,
487+ },
488+ },
489+ {
490+ name : "Nil InstanceState with InstanceNotReady condition propagates info to machine" ,
491+ instanceState : nil ,
492+ serverConditions : []clusterv1beta1.Condition {
493+ {
494+ Type : infrav1 .InstanceReadyCondition ,
495+ Status : corev1 .ConditionFalse ,
496+ Severity : clusterv1beta1 .ConditionSeverityInfo ,
497+ Reason : infrav1 .InstanceNotReadyReason ,
498+ Message : "Waiting for dependencies" ,
499+ },
500+ },
501+ expectRequeue : true ,
502+ expectedInstanceReadyCondition : & clusterv1beta1.Condition {
503+ Type : infrav1 .InstanceReadyCondition ,
504+ Status : corev1 .ConditionFalse ,
505+ Severity : clusterv1beta1 .ConditionSeverityInfo ,
506+ Reason : infrav1 .InstanceNotReadyReason ,
507+ Message : "Waiting for dependencies" ,
508+ },
509+ expectedReadyCondition : & clusterv1beta1.Condition {
510+ Type : clusterv1beta1 .ReadyCondition ,
511+ Status : corev1 .ConditionFalse ,
512+ Severity : clusterv1beta1 .ConditionSeverityInfo ,
513+ Reason : infrav1 .InstanceNotReadyReason ,
514+ Message : "Waiting for dependencies" ,
515+ },
516+ },
517+ {
518+ name : "Nil InstanceState with no server condition sets default waiting conditions" ,
519+ instanceState : nil ,
520+ expectRequeue : true ,
521+ expectedInstanceReadyCondition : & clusterv1beta1.Condition {
522+ Type : infrav1 .InstanceReadyCondition ,
523+ Status : corev1 .ConditionFalse ,
524+ Severity : clusterv1beta1 .ConditionSeverityInfo ,
525+ Reason : infrav1 .InstanceNotReadyReason ,
526+ Message : "Waiting for instance to be created" ,
527+ },
528+ expectedReadyCondition : & clusterv1beta1.Condition {
529+ Type : clusterv1beta1 .ReadyCondition ,
530+ Status : corev1 .ConditionFalse ,
531+ Severity : clusterv1beta1 .ConditionSeverityInfo ,
532+ Reason : infrav1 .InstanceNotReadyReason ,
533+ Message : "Waiting for instance to be created" ,
534+ },
535+ },
460536 {
461537 name : "Instance state ACTIVE sets conditions to True and initialization.provisioned" ,
462- instanceState : infrav1 .InstanceStateActive ,
538+ instanceState : ptr . To ( infrav1 .InstanceStateActive ) ,
463539 expectRequeue : false ,
464540 expectedInstanceReadyCondition : & clusterv1beta1.Condition {
465541 Type : infrav1 .InstanceReadyCondition ,
@@ -473,7 +549,7 @@ func TestReconcileMachineState(t *testing.T) {
473549 },
474550 {
475551 name : "Instance state ERROR sets conditions to False without NodeRef" ,
476- instanceState : infrav1 .InstanceStateError ,
552+ instanceState : ptr . To ( infrav1 .InstanceStateError ) ,
477553 machineHasNodeRef : false ,
478554 expectRequeue : true ,
479555 expectedInstanceReadyCondition : & clusterv1beta1.Condition {
@@ -492,7 +568,7 @@ func TestReconcileMachineState(t *testing.T) {
492568 },
493569 {
494570 name : "Instance state ERROR with NodeRef does not set failure" ,
495- instanceState : infrav1 .InstanceStateError ,
571+ instanceState : ptr . To ( infrav1 .InstanceStateError ) ,
496572 machineHasNodeRef : true ,
497573 expectRequeue : true ,
498574 expectedInstanceReadyCondition : & clusterv1beta1.Condition {
@@ -509,9 +585,39 @@ func TestReconcileMachineState(t *testing.T) {
509585 },
510586 expectFailureSet : false ,
511587 },
588+ {
589+ name : "Instance state ERROR propagates error message from server condition" ,
590+ instanceState : ptr .To (infrav1 .InstanceStateError ),
591+ machineHasNodeRef : false ,
592+ serverConditions : []clusterv1beta1.Condition {
593+ {
594+ Type : infrav1 .InstanceReadyCondition ,
595+ Status : corev1 .ConditionFalse ,
596+ Severity : clusterv1beta1 .ConditionSeverityError ,
597+ Reason : infrav1 .InstanceStateErrorReason ,
598+ Message : "Server entered ERROR state: No valid host was found" ,
599+ },
600+ },
601+ expectRequeue : true ,
602+ expectedInstanceReadyCondition : & clusterv1beta1.Condition {
603+ Type : infrav1 .InstanceReadyCondition ,
604+ Status : corev1 .ConditionFalse ,
605+ Severity : clusterv1beta1 .ConditionSeverityError ,
606+ Reason : infrav1 .InstanceStateErrorReason ,
607+ Message : "Server entered ERROR state: No valid host was found" ,
608+ },
609+ expectedReadyCondition : & clusterv1beta1.Condition {
610+ Type : clusterv1beta1 .ReadyCondition ,
611+ Status : corev1 .ConditionFalse ,
612+ Severity : clusterv1beta1 .ConditionSeverityError ,
613+ Reason : infrav1 .InstanceStateErrorReason ,
614+ Message : "Server entered ERROR state: No valid host was found" ,
615+ },
616+ expectFailureSet : true ,
617+ },
512618 {
513619 name : "Instance state DELETED sets conditions to False" ,
514- instanceState : infrav1 .InstanceStateDeleted ,
620+ instanceState : ptr . To ( infrav1 .InstanceStateDeleted ) ,
515621 expectRequeue : true ,
516622 expectedInstanceReadyCondition : & clusterv1beta1.Condition {
517623 Type : infrav1 .InstanceReadyCondition ,
@@ -528,7 +634,7 @@ func TestReconcileMachineState(t *testing.T) {
528634 },
529635 {
530636 name : "Instance state BUILD sets ReadyCondition to False" ,
531- instanceState : infrav1 .InstanceStateBuild ,
637+ instanceState : ptr . To ( infrav1 .InstanceStateBuild ) ,
532638 expectRequeue : true ,
533639 expectedReadyCondition : & clusterv1beta1.Condition {
534640 Type : clusterv1beta1 .ReadyCondition ,
@@ -539,7 +645,7 @@ func TestReconcileMachineState(t *testing.T) {
539645 },
540646 {
541647 name : "Instance state SHUTOFF sets conditions to Unknown" ,
542- instanceState : infrav1 .InstanceStateShutoff ,
648+ instanceState : ptr . To ( infrav1 .InstanceStateShutoff ) ,
543649 expectRequeue : true ,
544650 expectedInstanceReadyCondition : & clusterv1beta1.Condition {
545651 Type : infrav1 .InstanceReadyCondition ,
@@ -590,10 +696,15 @@ func TestReconcileMachineState(t *testing.T) {
590696 },
591697 Status : infrav1alpha1.OpenStackServerStatus {
592698 InstanceID : ptr .To (testInstanceID ),
593- InstanceState : ptr . To ( tt .instanceState ) ,
699+ InstanceState : tt .instanceState ,
594700 },
595701 }
596702
703+ // Set any pre-existing conditions on the OpenStackServer
704+ for i := range tt .serverConditions {
705+ v1beta1conditions .Set (openStackServer , & tt .serverConditions [i ])
706+ }
707+
597708 r := & OpenStackMachineReconciler {}
598709 result := r .reconcileMachineState (scope .NewWithLogger (nil , logr .Discard ()), openStackMachine , machine , openStackServer )
599710
@@ -620,6 +731,9 @@ func TestReconcileMachineState(t *testing.T) {
620731 if tt .expectedInstanceReadyCondition .Severity != "" && condition .Severity != tt .expectedInstanceReadyCondition .Severity {
621732 t .Errorf ("expected %s severity %s, got %s" , tt .expectedInstanceReadyCondition .Type , tt .expectedInstanceReadyCondition .Severity , condition .Severity )
622733 }
734+ if tt .expectedInstanceReadyCondition .Message != "" && condition .Message != tt .expectedInstanceReadyCondition .Message {
735+ t .Errorf ("expected %s message %q, got %q" , tt .expectedInstanceReadyCondition .Type , tt .expectedInstanceReadyCondition .Message , condition .Message )
736+ }
623737 }
624738 }
625739
@@ -638,6 +752,9 @@ func TestReconcileMachineState(t *testing.T) {
638752 if tt .expectedReadyCondition .Severity != "" && condition .Severity != tt .expectedReadyCondition .Severity {
639753 t .Errorf ("expected %s severity %s, got %s" , tt .expectedReadyCondition .Type , tt .expectedReadyCondition .Severity , condition .Severity )
640754 }
755+ if tt .expectedReadyCondition .Message != "" && condition .Message != tt .expectedReadyCondition .Message {
756+ t .Errorf ("expected %s message %q, got %q" , tt .expectedReadyCondition .Type , tt .expectedReadyCondition .Message , condition .Message )
757+ }
641758 }
642759 }
643760
0 commit comments