From 00731ecaa2b3a1bbbd3a971e015046024cdc7b00 Mon Sep 17 00:00:00 2001 From: Sai Diliyaer Date: Mon, 16 Dec 2024 12:13:28 -0500 Subject: [PATCH] Fix race in WebConsoleRequest controller tests --- .../v1alpha1/webconsolerequest_intg_test.go | 6 ++++-- .../v1alpha1/webconsolerequest_unit_test.go | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_intg_test.go b/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_intg_test.go index a843614af..856a99b12 100644 --- a/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_intg_test.go +++ b/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_intg_test.go @@ -97,12 +97,10 @@ func intgTestsReconcile() { }, } - intgFakeVMProvider.Lock() intgFakeVMProvider.GetVirtualMachineWebMKSTicketFn = func(ctx context.Context, vm *vmopv1.VirtualMachine, pubKey string) (string, error) { v1a2ProviderCalled = true return v1a2Ticket, nil } - intgFakeVMProvider.Unlock() }) AfterEach(func() { @@ -147,7 +145,11 @@ func intgTestsReconcile() { g.Expect(wcr.Status.Response).ToNot(BeEmpty()) }).Should(Succeed(), "waiting response to be set") + // Lock to prevent race conditions when reading v1a2ProviderCalled, + // as the fake provider might write to it again if CR is reconciled. + intgFakeVMProvider.Lock() Expect(v1a2ProviderCalled).Should(BeTrue()) + intgFakeVMProvider.Unlock() Expect(wcr.Status.ProxyAddr).To(Equal("192.168.0.1")) Expect(wcr.Status.Response).To(Equal(v1a2Ticket)) Expect(wcr.Status.ExpiryTime.Time).To(BeTemporally("~", time.Now(), webconsolerequest.DefaultExpiryTime)) diff --git a/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_unit_test.go b/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_unit_test.go index 7a75d87aa..04fac8ab3 100644 --- a/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_unit_test.go +++ b/controllers/virtualmachinewebconsolerequest/v1alpha1/webconsolerequest_unit_test.go @@ -137,7 +137,11 @@ func unitTestsReconcile() { err := reconciler.ReconcileNormal(wcrCtx) Expect(err).ToNot(HaveOccurred()) + // Lock to prevent race conditions when reading v1a2ProviderCalled, + // as the fake provider might write to it again if CR is reconciled. + fakeVMProvider.Lock() Expect(v1a2ProviderCalled).Should(BeTrue()) + fakeVMProvider.Unlock() Expect(wcrCtx.WebConsoleRequest.Status.ProxyAddr).To(Equal("dummy-proxy-ip")) Expect(wcrCtx.WebConsoleRequest.Status.Response).To(Equal(v1a2Ticket)) Expect(wcrCtx.WebConsoleRequest.Status.ExpiryTime.Time).To(BeTemporally("~", time.Now(), webconsolerequest.DefaultExpiryTime))