Skip to content

Commit 499a1a4

Browse files
🌱 TestReconcileMachinePhases: use apireader to directly talk to apiserver (#12819)
* flake use apireader to directly talk to apiserver * lint
1 parent 9829829 commit 499a1a4

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

internal/controllers/machine/machine_controller_status_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,7 @@ func TestReconcileMachinePhases(t *testing.T) {
20862086

20872087
// Wait until InfraMachine has the ownerReference.
20882088
g.Eventually(func(g Gomega) bool {
2089-
if err := env.Get(ctx, client.ObjectKeyFromObject(infraMachine), infraMachine); err != nil {
2089+
if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(infraMachine), infraMachine); err != nil {
20902090
return false
20912091
}
20922092
g.Expect(infraMachine.GetOwnerReferences()).To(HaveLen(1))
@@ -2129,7 +2129,7 @@ func TestReconcileMachinePhases(t *testing.T) {
21292129

21302130
// Wait until Machine was reconciled.
21312131
g.Eventually(func(g Gomega) bool {
2132-
if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
2132+
if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
21332133
return false
21342134
}
21352135
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhasePending))
@@ -2184,7 +2184,7 @@ func TestReconcileMachinePhases(t *testing.T) {
21842184

21852185
// Wait until Machine was reconciled.
21862186
g.Eventually(func(g Gomega) bool {
2187-
if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
2187+
if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
21882188
return false
21892189
}
21902190
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseProvisioning))
@@ -2274,7 +2274,7 @@ func TestReconcileMachinePhases(t *testing.T) {
22742274

22752275
// Wait until Machine was reconciled.
22762276
g.Eventually(func(g Gomega) bool {
2277-
if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
2277+
if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
22782278
return false
22792279
}
22802280
g.Expect(machine.Status.Addresses).To(HaveLen(2))
@@ -2354,7 +2354,7 @@ func TestReconcileMachinePhases(t *testing.T) {
23542354

23552355
// Wait until Machine was reconciled.
23562356
g.Eventually(func(g Gomega) bool {
2357-
if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
2357+
if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
23582358
return false
23592359
}
23602360
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseRunning))
@@ -2433,7 +2433,7 @@ func TestReconcileMachinePhases(t *testing.T) {
24332433

24342434
// Wait until Machine was reconciled.
24352435
g.Eventually(func(g Gomega) bool {
2436-
if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
2436+
if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
24372437
return false
24382438
}
24392439
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseRunning))
@@ -2496,7 +2496,7 @@ func TestReconcileMachinePhases(t *testing.T) {
24962496

24972497
// Wait until Machine was reconciled.
24982498
g.Eventually(func(g Gomega) bool {
2499-
if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
2499+
if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
25002500
return false
25012501
}
25022502
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseProvisioned))
@@ -2590,7 +2590,7 @@ func TestReconcileMachinePhases(t *testing.T) {
25902590

25912591
// Wait until Machine was reconciled.
25922592
g.Eventually(func(g Gomega) bool {
2593-
if err := env.Get(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
2593+
if err := env.DirectAPIServerGet(ctx, client.ObjectKeyFromObject(machine), machine); err != nil {
25942594
return false
25952595
}
25962596
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseDeleting))

internal/test/envtest/environment.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,13 @@ func (e *Environment) PatchAndWait(ctx context.Context, obj client.Object, opts
596596
return nil
597597
}
598598

599+
// DirectAPIServerGet gets an object directly from apiserver bypassing informer caches..
600+
//
601+
// NOTE: Bypassing cache helps in preventing test flakes due to the cache sync delays but should only be used in validation steps of testing.
602+
func (e *Environment) DirectAPIServerGet(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
603+
return e.Manager.GetAPIReader().Get(ctx, key, obj, opts...)
604+
}
605+
599606
// CreateNamespace creates a new namespace with a generated name.
600607
func (e *Environment) CreateNamespace(ctx context.Context, generateName string) (*corev1.Namespace, error) {
601608
ns := &corev1.Namespace{

0 commit comments

Comments
 (0)