Skip to content

Commit f928344

Browse files
Updates machine controller tests
1 parent 1ea945c commit f928344

File tree

3 files changed

+173
-149
lines changed

3 files changed

+173
-149
lines changed

pkg/controller/machine/controller_test.go

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
. "github.com/onsi/gomega"
2828
machinev1 "github.com/openshift/api/machine/v1beta1"
2929
"github.com/openshift/machine-api-operator/pkg/util/conditions"
30+
testutils "github.com/openshift/machine-api-operator/pkg/util/testing"
31+
3032
corev1 "k8s.io/api/core/v1"
3133
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3234
"k8s.io/apimachinery/pkg/runtime"
@@ -534,6 +536,10 @@ func TestReconcileRequest(t *testing.T) {
534536

535537
for _, tc := range testCases {
536538
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+
}
537543
act := newTestActuator()
538544
act.ExistsValue = tc.existsValue
539545
r := &ReconcileMachine{
@@ -551,6 +557,7 @@ func TestReconcileRequest(t *testing.T) {
551557
).WithStatusSubresource(&machinev1.Machine{}).Build(),
552558
scheme: scheme.Scheme,
553559
actuator: act,
560+
gate: gate,
554561
}
555562

556563
result, err := r.Reconcile(ctx, tc.request)
@@ -596,9 +603,6 @@ func TestReconcileRequest(t *testing.T) {
596603
}
597604

598605
func TestUpdateStatus(t *testing.T) {
599-
cleanupFn := StartEnvTest(t)
600-
defer cleanupFn(t)
601-
602606
drainableTrue := conditions.TrueCondition(machinev1.MachineDrainable)
603607
terminableTrue := conditions.TrueCondition(machinev1.MachineTerminable)
604608
defaultLifecycleConditions := []machinev1.Condition{*drainableTrue, *terminableTrue}
@@ -711,12 +715,19 @@ func TestUpdateStatus(t *testing.T) {
711715
},
712716
}
713717

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+
714726
for _, tc := range testCases {
715727
t.Run(tc.name, func(t *testing.T) {
716-
g := NewWithT(t)
717-
728+
gs := NewWithT(t)
718729
k8sClient, err := client.New(cfg, client.Options{})
719-
g.Expect(err).ToNot(HaveOccurred())
730+
gs.Expect(err).ToNot(HaveOccurred())
720731
reconciler := &ReconcileMachine{
721732
Client: k8sClient,
722733
scheme: scheme.Scheme,
@@ -729,7 +740,7 @@ func TestUpdateStatus(t *testing.T) {
729740
GenerateName: name,
730741
},
731742
}
732-
g.Expect(k8sClient.Create(ctx, namespace)).To(Succeed())
743+
gs.Expect(k8sClient.Create(ctx, namespace)).To(Succeed())
733744

734745
// Set up the test machine
735746
machine := &machinev1.Machine{
@@ -739,7 +750,7 @@ func TestUpdateStatus(t *testing.T) {
739750
},
740751
}
741752

742-
g.Expect(k8sClient.Create(ctx, machine)).To(Succeed())
753+
gs.Expect(k8sClient.Create(ctx, machine)).To(Succeed())
743754
defer func() {
744755
if err := k8sClient.Delete(ctx, machine); err != nil {
745756
t.Fatalf("error deleting machine: %v", err)
@@ -752,7 +763,7 @@ func TestUpdateStatus(t *testing.T) {
752763
}
753764
}
754765

755-
g.Expect(k8sClient.Status().Update(ctx, machine)).To(Succeed())
766+
gs.Expect(k8sClient.Status().Update(ctx, machine)).To(Succeed())
756767

757768
namespacedName := types.NamespacedName{
758769
Namespace: machine.Namespace,
@@ -765,20 +776,20 @@ func TestUpdateStatus(t *testing.T) {
765776
}
766777

767778
// 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())
769780
// validate persisted object
770781
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))
774785
lastUpdated := got.Status.LastUpdated
775786
gotConditions := got.Status.Conditions
776-
g.Expect(lastUpdated).ToNot(BeNil())
787+
gs.Expect(lastUpdated).ToNot(BeNil())
777788
// 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))
780791
objectLastUpdated := machine.Status.LastUpdated
781-
g.Expect(objectLastUpdated).ToNot(BeNil())
792+
gs.Expect(objectLastUpdated).ToNot(BeNil())
782793

783794
// Set the time func so that we can check lastUpdated is set correctly
784795
reconciler.nowFunc = func() time.Time {
@@ -790,38 +801,38 @@ func TestUpdateStatus(t *testing.T) {
790801
c := cond
791802
conditions.Set(machine, &c)
792803
}
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())
794805
// validate the persisted object
795806
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())
797808

798809
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()))
801812
} 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()))
804815
}
805816

806817
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()))
811822
}
812823

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))
815826

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))
818829

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))
821832

822833
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))
825836
}
826837
})
827838
}

pkg/controller/machine/machine_controller_suite_test.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"context"
2121
"flag"
2222
"log"
23+
"os"
2324
"path/filepath"
2425
"testing"
2526
"time"
2627

2728
. "github.com/onsi/ginkgo/v2"
2829
. "github.com/onsi/gomega"
2930

31+
configv1 "github.com/openshift/api/config/v1"
3032
machinev1 "github.com/openshift/api/machine/v1beta1"
3133
"k8s.io/client-go/kubernetes/scheme"
3234
"k8s.io/client-go/rest"
@@ -40,9 +42,6 @@ func init() {
4042
textLoggerConfig := textlogger.NewConfig()
4143
textLoggerConfig.AddFlags(flag.CommandLine)
4244
logf.SetLogger(textlogger.NewLogger(textLoggerConfig))
43-
44-
// Register required object kinds with global scheme.
45-
_ = machinev1.Install(scheme.Scheme)
4645
}
4746

4847
const (
@@ -64,17 +63,8 @@ func TestMachineController(t *testing.T) {
6463

6564
var _ = BeforeSuite(func(ctx SpecContext) {
6665
By("bootstrapping test environment")
67-
testEnv = &envtest.Environment{
68-
69-
CRDInstallOptions: envtest.CRDInstallOptions{
70-
Paths: []string{
71-
filepath.Join("..", "..", "..", "vendor", "github.com", "openshift", "api", "machine", "v1beta1", "zz_generated.crd-manifests", "0000_10_machine-api_01_machines-CustomNoUpgrade.crd.yaml"),
72-
},
73-
},
74-
}
75-
7666
var err error
77-
cfg, err = testEnv.Start()
67+
cfg, testEnv, err = StartEnvTest()
7868
Expect(err).ToNot(HaveOccurred())
7969
Expect(cfg).ToNot(BeNil())
8070

@@ -85,7 +75,19 @@ var _ = AfterSuite(func() {
8575
Expect(testEnv.Stop()).To(Succeed())
8676
})
8777

88-
func StartEnvTest(t *testing.T) func(t *testing.T) {
78+
func TestMain(m *testing.M) {
79+
// Register required object kinds with global scheme.
80+
if err := machinev1.Install(scheme.Scheme); err != nil {
81+
log.Fatalf("cannot add scheme: %v", err)
82+
}
83+
if err := configv1.Install(scheme.Scheme); err != nil {
84+
log.Fatalf("cannot add scheme: %v", err)
85+
}
86+
exitVal := m.Run()
87+
os.Exit(exitVal)
88+
}
89+
90+
func StartEnvTest() (*rest.Config, *envtest.Environment, error) {
8991
testEnv := &envtest.Environment{
9092

9193
CRDInstallOptions: envtest.CRDInstallOptions{
@@ -94,18 +96,11 @@ func StartEnvTest(t *testing.T) func(t *testing.T) {
9496
},
9597
},
9698
}
97-
if err := machinev1.Install(scheme.Scheme); err != nil {
98-
log.Fatalf("cannot add scheme: %v", err)
99-
}
10099

101100
var err error
102101
if cfg, err = testEnv.Start(); err != nil {
103-
log.Fatal(err)
102+
return nil, nil, err
104103
}
105104

106-
return func(t *testing.T) {
107-
if err = testEnv.Stop(); err != nil {
108-
log.Fatal(err)
109-
}
110-
}
105+
return cfg, testEnv, nil
111106
}

0 commit comments

Comments
 (0)