@@ -27,6 +27,8 @@ import (
27
27
. "github.com/onsi/gomega"
28
28
machinev1 "github.com/openshift/api/machine/v1beta1"
29
29
"github.com/openshift/machine-api-operator/pkg/util/conditions"
30
+ testutils "github.com/openshift/machine-api-operator/pkg/util/testing"
31
+
30
32
corev1 "k8s.io/api/core/v1"
31
33
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
34
"k8s.io/apimachinery/pkg/runtime"
@@ -534,6 +536,10 @@ func TestReconcileRequest(t *testing.T) {
534
536
535
537
for _ , tc := range testCases {
536
538
t .Run (tc .request .Name , func (t * testing.T ) {
539
+ gate , err := testutils .NewDefaultMutableFeatureGate ()
540
+ if err != nil {
541
+ t .Errorf ("Case: %s. Unexpected error setting up feature gates: %v" , tc .request .Name , err )
542
+ }
537
543
act := newTestActuator ()
538
544
act .ExistsValue = tc .existsValue
539
545
r := & ReconcileMachine {
@@ -551,6 +557,7 @@ func TestReconcileRequest(t *testing.T) {
551
557
).WithStatusSubresource (& machinev1.Machine {}).Build (),
552
558
scheme : scheme .Scheme ,
553
559
actuator : act ,
560
+ gate : gate ,
554
561
}
555
562
556
563
result , err := r .Reconcile (ctx , tc .request )
@@ -596,9 +603,6 @@ func TestReconcileRequest(t *testing.T) {
596
603
}
597
604
598
605
func TestUpdateStatus (t * testing.T ) {
599
- cleanupFn := StartEnvTest (t )
600
- defer cleanupFn (t )
601
-
602
606
drainableTrue := conditions .TrueCondition (machinev1 .MachineDrainable )
603
607
terminableTrue := conditions .TrueCondition (machinev1 .MachineTerminable )
604
608
defaultLifecycleConditions := []machinev1.Condition {* drainableTrue , * terminableTrue }
@@ -711,12 +715,19 @@ func TestUpdateStatus(t *testing.T) {
711
715
},
712
716
}
713
717
718
+ // We don't need to recreate the test environment for every case.
719
+ g := NewWithT (t )
720
+ _ , testEnv , err := StartEnvTest ()
721
+ g .Expect (err ).ToNot (HaveOccurred ())
722
+ defer func () {
723
+ g .Expect (testEnv .Stop ()).To (Succeed ())
724
+ }()
725
+
714
726
for _ , tc := range testCases {
715
727
t .Run (tc .name , func (t * testing.T ) {
716
- g := NewWithT (t )
717
-
728
+ gs := NewWithT (t )
718
729
k8sClient , err := client .New (cfg , client.Options {})
719
- g .Expect (err ).ToNot (HaveOccurred ())
730
+ gs .Expect (err ).ToNot (HaveOccurred ())
720
731
reconciler := & ReconcileMachine {
721
732
Client : k8sClient ,
722
733
scheme : scheme .Scheme ,
@@ -729,7 +740,7 @@ func TestUpdateStatus(t *testing.T) {
729
740
GenerateName : name ,
730
741
},
731
742
}
732
- g .Expect (k8sClient .Create (ctx , namespace )).To (Succeed ())
743
+ gs .Expect (k8sClient .Create (ctx , namespace )).To (Succeed ())
733
744
734
745
// Set up the test machine
735
746
machine := & machinev1.Machine {
@@ -739,7 +750,7 @@ func TestUpdateStatus(t *testing.T) {
739
750
},
740
751
}
741
752
742
- g .Expect (k8sClient .Create (ctx , machine )).To (Succeed ())
753
+ gs .Expect (k8sClient .Create (ctx , machine )).To (Succeed ())
743
754
defer func () {
744
755
if err := k8sClient .Delete (ctx , machine ); err != nil {
745
756
t .Fatalf ("error deleting machine: %v" , err )
@@ -752,7 +763,7 @@ func TestUpdateStatus(t *testing.T) {
752
763
}
753
764
}
754
765
755
- g .Expect (k8sClient .Status ().Update (ctx , machine )).To (Succeed ())
766
+ gs .Expect (k8sClient .Status ().Update (ctx , machine )).To (Succeed ())
756
767
757
768
namespacedName := types.NamespacedName {
758
769
Namespace : machine .Namespace ,
@@ -765,20 +776,20 @@ func TestUpdateStatus(t *testing.T) {
765
776
}
766
777
767
778
// Set the phase to Running initially
768
- g .Expect (reconciler .updateStatus (context .TODO (), machine , machinev1 .PhaseRunning , nil , []machinev1.Condition {})).To (Succeed ())
779
+ gs .Expect (reconciler .updateStatus (context .TODO (), machine , machinev1 .PhaseRunning , nil , []machinev1.Condition {})).To (Succeed ())
769
780
// validate persisted object
770
781
got := machinev1.Machine {}
771
- g .Expect (reconciler .Client .Get (context .TODO (), namespacedName , & got )).To (Succeed ())
772
- g .Expect (got .Status .Phase ).ToNot (BeNil ())
773
- g .Expect (* got .Status .Phase ).To (Equal (machinev1 .PhaseRunning ))
782
+ gs .Expect (reconciler .Client .Get (context .TODO (), namespacedName , & got )).To (Succeed ())
783
+ gs .Expect (got .Status .Phase ).ToNot (BeNil ())
784
+ gs .Expect (* got .Status .Phase ).To (Equal (machinev1 .PhaseRunning ))
774
785
lastUpdated := got .Status .LastUpdated
775
786
gotConditions := got .Status .Conditions
776
- g .Expect (lastUpdated ).ToNot (BeNil ())
787
+ gs .Expect (lastUpdated ).ToNot (BeNil ())
777
788
// validate passed object
778
- g .Expect (machine .Status .Phase ).ToNot (BeNil ())
779
- g .Expect (* machine .Status .Phase ).To (Equal (machinev1 .PhaseRunning ))
789
+ gs .Expect (machine .Status .Phase ).ToNot (BeNil ())
790
+ gs .Expect (* machine .Status .Phase ).To (Equal (machinev1 .PhaseRunning ))
780
791
objectLastUpdated := machine .Status .LastUpdated
781
- g .Expect (objectLastUpdated ).ToNot (BeNil ())
792
+ gs .Expect (objectLastUpdated ).ToNot (BeNil ())
782
793
783
794
// Set the time func so that we can check lastUpdated is set correctly
784
795
reconciler .nowFunc = func () time.Time {
@@ -790,38 +801,38 @@ func TestUpdateStatus(t *testing.T) {
790
801
c := cond
791
802
conditions .Set (machine , & c )
792
803
}
793
- g .Expect (reconciler .updateStatus (context .TODO (), machine , tc .phase , tc .err , gotConditions )).To (Succeed ())
804
+ gs .Expect (reconciler .updateStatus (context .TODO (), machine , tc .phase , tc .err , gotConditions )).To (Succeed ())
794
805
// validate the persisted object
795
806
got = machinev1.Machine {}
796
- g .Expect (reconciler .Client .Get (context .TODO (), namespacedName , & got )).To (Succeed ())
807
+ gs .Expect (reconciler .Client .Get (context .TODO (), namespacedName , & got )).To (Succeed ())
797
808
798
809
if tc .updated {
799
- g .Expect (got .Status .LastUpdated .UnixNano ()).ToNot (Equal (lastUpdated .UnixNano ()))
800
- g .Expect (machine .Status .LastUpdated .UnixNano ()).ToNot (Equal (objectLastUpdated .UnixNano ()))
810
+ gs .Expect (got .Status .LastUpdated .UnixNano ()).ToNot (Equal (lastUpdated .UnixNano ()))
811
+ gs .Expect (machine .Status .LastUpdated .UnixNano ()).ToNot (Equal (objectLastUpdated .UnixNano ()))
801
812
} else {
802
- g .Expect (got .Status .LastUpdated .UnixNano ()).To (Equal (lastUpdated .UnixNano ()))
803
- g .Expect (machine .Status .LastUpdated .UnixNano ()).To (Equal (objectLastUpdated .UnixNano ()))
813
+ gs .Expect (got .Status .LastUpdated .UnixNano ()).To (Equal (lastUpdated .UnixNano ()))
814
+ gs .Expect (machine .Status .LastUpdated .UnixNano ()).To (Equal (objectLastUpdated .UnixNano ()))
804
815
}
805
816
806
817
if tc .err != nil {
807
- g .Expect (got .Status .ErrorMessage ).ToNot (BeNil ())
808
- g .Expect (* got .Status .ErrorMessage ).To (Equal (tc .err .Error ()))
809
- g .Expect (machine .Status .ErrorMessage ).ToNot (BeNil ())
810
- g .Expect (* machine .Status .ErrorMessage ).To (Equal (tc .err .Error ()))
818
+ gs .Expect (got .Status .ErrorMessage ).ToNot (BeNil ())
819
+ gs .Expect (* got .Status .ErrorMessage ).To (Equal (tc .err .Error ()))
820
+ gs .Expect (machine .Status .ErrorMessage ).ToNot (BeNil ())
821
+ gs .Expect (* machine .Status .ErrorMessage ).To (Equal (tc .err .Error ()))
811
822
}
812
823
813
- g .Expect (* got .Status .Phase ).To (Equal (tc .phase ))
814
- g .Expect (* machine .Status .Phase ).To (Equal (tc .phase ))
824
+ gs .Expect (* got .Status .Phase ).To (Equal (tc .phase ))
825
+ gs .Expect (* machine .Status .Phase ).To (Equal (tc .phase ))
815
826
816
- g .Expect (got .Status .Conditions ).To (conditions .MatchConditions (tc .conditions ))
817
- g .Expect (machine .Status .Conditions ).To (conditions .MatchConditions (tc .conditions ))
827
+ gs .Expect (got .Status .Conditions ).To (conditions .MatchConditions (tc .conditions ))
828
+ gs .Expect (machine .Status .Conditions ).To (conditions .MatchConditions (tc .conditions ))
818
829
819
- g .Expect (got .GetAnnotations ()).To (Equal (tc .annotations ))
820
- g .Expect (machine .GetAnnotations ()).To (Equal (tc .annotations ))
830
+ gs .Expect (got .GetAnnotations ()).To (Equal (tc .annotations ))
831
+ gs .Expect (machine .GetAnnotations ()).To (Equal (tc .annotations ))
821
832
822
833
if tc .existingProviderStatus != "" {
823
- g .Expect (got .Status .ProviderStatus ).ToNot (BeNil ())
824
- g .Expect (got .Status .ProviderStatus .Raw ).To (BeEquivalentTo (tc .expectedProviderStatus ))
834
+ gs .Expect (got .Status .ProviderStatus ).ToNot (BeNil ())
835
+ gs .Expect (got .Status .ProviderStatus .Raw ).To (BeEquivalentTo (tc .expectedProviderStatus ))
825
836
}
826
837
})
827
838
}
0 commit comments