Skip to content

Commit 61c4839

Browse files
Merge pull request #1295 from theobarberbany/machineset-fix-flake
NO-JIRA: Updates MachineSet tests to use GenerateName
2 parents 57b7917 + fc37dbf commit 61c4839

File tree

17 files changed

+1235
-222
lines changed

17 files changed

+1235
-222
lines changed

pkg/controller/machine/machine_controller_suite_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/klog/v2/textlogger"
3636
"sigs.k8s.io/controller-runtime/pkg/client"
3737
"sigs.k8s.io/controller-runtime/pkg/envtest"
38+
"sigs.k8s.io/controller-runtime/pkg/envtest/komega"
3839
logf "sigs.k8s.io/controller-runtime/pkg/log"
3940
)
4041

@@ -61,13 +62,19 @@ func TestMachineController(t *testing.T) {
6162
RunSpecs(t, "Machine Controller Suite")
6263
}
6364

64-
var _ = BeforeSuite(func(ctx SpecContext) {
65+
var _ = BeforeSuite(func() {
6566
By("bootstrapping test environment")
6667
var err error
6768
cfg, testEnv, err = StartEnvTest()
6869
Expect(err).ToNot(HaveOccurred())
6970
Expect(cfg).ToNot(BeNil())
7071

72+
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
73+
Expect(err).ToNot(HaveOccurred())
74+
75+
komega.SetContext(ctx)
76+
komega.SetClient(k8sClient)
77+
7178
})
7279

7380
var _ = AfterSuite(func() {

pkg/controller/machine/machine_controller_test.go

Lines changed: 101 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ import (
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/utils/ptr"
3030

31+
testutils "github.com/openshift/cluster-api-actuator-pkg/testutils"
3132
machinev1resourcebuilder "github.com/openshift/cluster-api-actuator-pkg/testutils/resourcebuilder/machine/v1beta1"
3233
"github.com/openshift/cluster-control-plane-machine-set-operator/test/e2e/framework"
33-
testutils "github.com/openshift/machine-api-operator/pkg/util/testing"
34+
"github.com/openshift/machine-api-operator/pkg/util/testing"
3435

3536
"sigs.k8s.io/controller-runtime/pkg/client"
3637
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -55,11 +56,10 @@ var _ = Describe("Machine Reconciler", func() {
5556
})
5657
Expect(err).NotTo(HaveOccurred())
5758

58-
k8sClient = mgr.GetClient()
5959
k = komega.New(k8sClient)
6060

6161
By("Setting up feature gates")
62-
gate, err := testutils.NewDefaultMutableFeatureGate()
62+
gate, err := testing.NewDefaultMutableFeatureGate()
6363
Expect(err).NotTo(HaveOccurred())
6464

6565
By("Setting up a new reconciler")
@@ -96,15 +96,15 @@ var _ = Describe("Machine Reconciler", func() {
9696
})
9797

9898
AfterEach(func() {
99-
By("Deleting the machines")
100-
Expect(cleanResources(namespace.Name)).To(Succeed())
101-
102-
By("Deleting the namespace")
103-
Expect(k8sClient.Delete(ctx, namespace)).To(Succeed())
104-
10599
By("Closing the manager")
106100
mgrCtxCancel()
107101
Eventually(mgrStopped, timeout).WithTimeout(20 * time.Second).Should(BeClosed())
102+
103+
By("Cleaning up test resources")
104+
testutils.CleanupResources(Default, ctx, cfg, k8sClient, namespace.GetName(),
105+
&machinev1.Machine{},
106+
)
107+
108108
})
109109

110110
It("Should reconcile a Machine", func() {
@@ -127,99 +127,98 @@ var _ = Describe("Machine Reconciler", func() {
127127

128128
})
129129

130-
It("Should set the Paused condition appropriately", func() {
131-
instance := machineBuilder.Build()
132-
133-
By("Creating the Machine")
134-
Expect(k8sClient.Create(ctx, instance)).To(Succeed())
135-
136-
By("Setting the AuthoritativeAPI to ClusterAPI")
137-
Eventually(k.UpdateStatus(instance, func() {
138-
instance.Status.AuthoritativeAPI = machinev1.MachineAuthorityClusterAPI
139-
})).Should(Succeed())
140-
141-
By("Verifying that the AuthoritativeAPI is set to Cluster API")
142-
Eventually(k.Object(instance), timeout).Should(HaveField("Status.AuthoritativeAPI", Equal(machinev1.MachineAuthorityClusterAPI)))
143-
144-
By("Verifying the paused condition is approproately set to true")
145-
Eventually(k.Object(instance), timeout).Should(HaveField("Status.Conditions", ContainElement(SatisfyAll(
146-
HaveField("Type", Equal(PausedCondition)),
147-
HaveField("Status", Equal(corev1.ConditionTrue)),
148-
))))
149-
150-
// The condition should remain true whilst transitioning through 'Migrating'
151-
// Run this in a goroutine so we don't block
152-
153-
// Copy the instance before starting the goroutine, to avoid data races
154-
instanceCopy := instance.DeepCopy()
155-
go func() {
156-
defer GinkgoRecover()
157-
framework.RunCheckUntil(
158-
ctx,
159-
// Check that we consistently have the Paused condition true
160-
func(_ context.Context, g framework.GomegaAssertions) bool {
161-
By("Checking that the paused condition is consistently true whilst migrating to MachineAPI")
162-
163-
localInstance := instanceCopy.DeepCopy()
164-
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(localInstance), localInstance); err != nil {
165-
return g.Expect(err).Should(WithTransform(testutils.IsRetryableAPIError, BeTrue()), "expected temporary error while getting machine: %v", err)
166-
}
167-
168-
return g.Expect(localInstance.Status.Conditions).Should(ContainElement(SatisfyAll(
169-
HaveField("Type", Equal(PausedCondition)),
170-
HaveField("Status", Equal(corev1.ConditionTrue)),
171-
)))
172-
173-
},
174-
// Condition / until function: until we observe the MachineAuthority being MAPI
175-
func(_ context.Context, g framework.GomegaAssertions) bool {
176-
By("Checking that the AuthoritativeAPI is not MachineAPI")
177-
178-
localInstance := instanceCopy.DeepCopy()
179-
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(localInstance), localInstance); err != nil {
180-
return g.Expect(err).Should(WithTransform(testutils.IsRetryableAPIError, BeTrue()), "expected temporary error while getting machine: %v", err)
181-
}
182-
183-
return g.Expect(localInstance.Status.AuthoritativeAPI).To(Equal(machinev1.MachineAuthorityMachineAPI))
184-
})
185-
}()
186-
187-
By("Transitioning the AuthoritativeAPI though 'Migrating' to MachineAPI")
188-
Eventually(k.UpdateStatus(instance, func() {
189-
instance.Status.AuthoritativeAPI = machinev1.MachineAuthorityMigrating
190-
})).Should(Succeed())
191-
192-
By("Updating the AuthoritativeAPI from Migrating to MachineAPI")
193-
Eventually(k.UpdateStatus(instance, func() {
194-
instance.Status.AuthoritativeAPI = machinev1.MachineAuthorityMachineAPI
195-
})).Should(Succeed())
196-
197-
Eventually(k.Object(instance), timeout).Should(HaveField("Status.AuthoritativeAPI", Equal(machinev1.MachineAuthorityMachineAPI)))
198-
199-
By("Verifying the paused condition is approproately set to false")
200-
Eventually(k.Object(instance), timeout).Should(HaveField("Status.Conditions", ContainElement(SatisfyAll(
201-
HaveField("Type", Equal(PausedCondition)),
202-
HaveField("Status", Equal(corev1.ConditionFalse)),
203-
))))
204-
205-
By("Unsetting the AuthoritativeAPI field in the status")
206-
Eventually(k.UpdateStatus(instance, func() {
207-
instance.Status.AuthoritativeAPI = ""
208-
})).Should(Succeed())
130+
Describe("Paused condition", func() {
131+
var instance *machinev1.Machine
132+
BeforeEach(func() {
133+
instance = machineBuilder.Build()
134+
})
209135

210-
By("Verifying the paused condition is still approproately set to false")
211-
Eventually(k.Object(instance), timeout).Should(HaveField("Status.Conditions", ContainElement(SatisfyAll(
212-
HaveField("Type", Equal(PausedCondition)),
213-
HaveField("Status", Equal(corev1.ConditionFalse)),
214-
))))
136+
It("Should set the Paused condition appropriately", func() {
137+
138+
By("Creating the Machine")
139+
Expect(k8sClient.Create(ctx, instance)).To(Succeed())
140+
141+
By("Setting the AuthoritativeAPI to ClusterAPI")
142+
Eventually(k.UpdateStatus(instance, func() {
143+
instance.Status.AuthoritativeAPI = machinev1.MachineAuthorityClusterAPI
144+
})).Should(Succeed())
145+
146+
By("Verifying the paused condition is approproately set to true")
147+
Eventually(k.Object(instance), timeout).Should(SatisfyAll(
148+
HaveField("Status.Conditions", ContainElement(SatisfyAll(
149+
HaveField("Type", Equal(PausedCondition)),
150+
HaveField("Status", Equal(corev1.ConditionTrue)),
151+
))),
152+
HaveField("Status.AuthoritativeAPI", Equal(machinev1.MachineAuthorityClusterAPI)),
153+
))
154+
155+
// The condition should remain true whilst transitioning through 'Migrating'
156+
// Run this in a goroutine so we don't block
157+
158+
// Copy the instance before starting the goroutine, to avoid data races
159+
instanceCopy := instance.DeepCopy()
160+
go func() {
161+
defer GinkgoRecover()
162+
framework.RunCheckUntil(
163+
ctx,
164+
// Check that we consistently have the Paused condition true
165+
func(_ context.Context, g framework.GomegaAssertions) bool {
166+
By("Checking that the paused condition is consistently true whilst migrating to MachineAPI")
167+
168+
localInstance := instanceCopy.DeepCopy()
169+
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(localInstance), localInstance); err != nil {
170+
return g.Expect(err).Should(WithTransform(testing.IsRetryableAPIError, BeTrue()), "expected temporary error while getting machine: %v", err)
171+
}
172+
173+
return g.Expect(localInstance.Status.Conditions).Should(ContainElement(SatisfyAll(
174+
HaveField("Type", Equal(PausedCondition)),
175+
HaveField("Status", Equal(corev1.ConditionTrue)),
176+
)))
177+
178+
},
179+
// Condition / until function: until we observe the MachineAuthority being MAPI
180+
func(_ context.Context, g framework.GomegaAssertions) bool {
181+
By("Checking that the AuthoritativeAPI is not MachineAPI")
182+
183+
localInstance := instanceCopy.DeepCopy()
184+
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(localInstance), localInstance); err != nil {
185+
return g.Expect(err).Should(WithTransform(testing.IsRetryableAPIError, BeTrue()), "expected temporary error while getting machine: %v", err)
186+
}
187+
188+
return g.Expect(localInstance.Status.AuthoritativeAPI).To(Equal(machinev1.MachineAuthorityMachineAPI))
189+
})
190+
}()
191+
192+
By("Transitioning the AuthoritativeAPI though 'Migrating' to MachineAPI")
193+
Eventually(k.UpdateStatus(instance, func() {
194+
instance.Status.AuthoritativeAPI = machinev1.MachineAuthorityMigrating
195+
})).Should(Succeed())
196+
197+
By("Updating the AuthoritativeAPI from Migrating to MachineAPI")
198+
Eventually(k.UpdateStatus(instance, func() {
199+
instance.Status.AuthoritativeAPI = machinev1.MachineAuthorityMachineAPI
200+
})).Should(Succeed())
201+
202+
By("Verifying the paused condition is approproately set to false")
203+
Eventually(k.Object(instance), timeout).Should(SatisfyAll(
204+
HaveField("Status.Conditions", ContainElement(SatisfyAll(
205+
HaveField("Type", Equal(PausedCondition)),
206+
HaveField("Status", Equal(corev1.ConditionFalse)),
207+
))),
208+
HaveField("Status.AuthoritativeAPI", Equal(machinev1.MachineAuthorityMachineAPI)),
209+
))
210+
211+
By("Unsetting the AuthoritativeAPI field in the status")
212+
Eventually(k.UpdateStatus(instance, func() {
213+
instance.Status.AuthoritativeAPI = ""
214+
})).Should(Succeed())
215+
216+
By("Verifying the paused condition is still approproately set to false")
217+
Eventually(k.Object(instance), timeout).Should(HaveField("Status.Conditions", ContainElement(SatisfyAll(
218+
HaveField("Type", Equal(PausedCondition)),
219+
HaveField("Status", Equal(corev1.ConditionFalse)),
220+
HaveField("Message", Equal("The AuthoritativeAPI is not set")),
221+
))))
222+
})
215223
})
216224
})
217-
218-
func cleanResources(namespace string) error {
219-
machine := &machinev1.Machine{}
220-
if err := k8sClient.DeleteAllOf(ctx, machine, client.InNamespace(namespace)); err != nil {
221-
return err
222-
}
223-
224-
return nil
225-
}

pkg/controller/machineset/controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ func (r *ReconcileMachineSet) Reconcile(ctx context.Context, request reconcile.R
188188
"The AuthoritativeAPI is set to %s", string(machineSet.Status.AuthoritativeAPI),
189189
))
190190

191-
_, err := updateMachineSetStatus(r.Client, machineSet, machineSetCopy.Status)
191+
machineSet, err := updateMachineSetStatus(r.Client, machineSet, machineSetCopy.Status)
192192
if err != nil {
193193
klog.Errorf("%v: error updating status: %v", machineSet.Name, err)
194194
}
195195

196-
klog.Infof("%v: machine is paused, taking no further action", machineSet.Name)
196+
klog.Infof("%v: machine set is paused, taking no further action", machineSet.Name)
197197
return reconcile.Result{}, nil
198198
}
199199

@@ -211,7 +211,7 @@ func (r *ReconcileMachineSet) Reconcile(ctx context.Context, request reconcile.R
211211
machinev1.ConditionSeverityInfo,
212212
pausedFalseReason,
213213
))
214-
_, err := updateMachineSetStatus(r.Client, machineSet, machineSetCopy.Status)
214+
machineSet, err := updateMachineSetStatus(r.Client, machineSet, machineSetCopy.Status)
215215
if err != nil {
216216
klog.Errorf("%v: error updating status: %v", machineSet.Name, err)
217217
}

pkg/controller/machineset/machineset_controller_suite_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/klog/v2/textlogger"
3636
"sigs.k8s.io/controller-runtime/pkg/client"
3737
"sigs.k8s.io/controller-runtime/pkg/envtest"
38+
"sigs.k8s.io/controller-runtime/pkg/envtest/komega"
3839
logf "sigs.k8s.io/controller-runtime/pkg/log"
3940
)
4041

@@ -57,11 +58,10 @@ var (
5758

5859
func TestMachinesetController(t *testing.T) {
5960
RegisterFailHandler(Fail)
60-
6161
RunSpecs(t, "MachineSet Controller Suite")
6262
}
6363

64-
var _ = BeforeSuite(func(ctx SpecContext) {
64+
var _ = BeforeSuite(func() {
6565
By("bootstrapping test environment")
6666

6767
testEnv = &envtest.Environment{
@@ -80,6 +80,12 @@ var _ = BeforeSuite(func(ctx SpecContext) {
8080
cfg, err = testEnv.Start()
8181
Expect(err).ToNot(HaveOccurred())
8282
Expect(cfg).ToNot(BeNil())
83+
84+
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
85+
Expect(err).ToNot(HaveOccurred())
86+
87+
komega.SetContext(ctx)
88+
komega.SetClient(k8sClient)
8389
})
8490

8591
var _ = AfterSuite(func() {

0 commit comments

Comments
 (0)