Skip to content

Commit

Permalink
fix: attempt race fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobmoellerdev committed Sep 13, 2023
1 parent 99c6473 commit 0bc51e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
47 changes: 24 additions & 23 deletions pkg/vgmanager/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package vgmanager

import (
"context"
"log"
"os/user"
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
lsblkmocks "github.com/openshift/lvm-operator/pkg/lsblk/mocks"
lvmmocks "github.com/openshift/lvm-operator/pkg/lvm/mocks"
"github.com/openshift/lvm-operator/pkg/lvmd"

configv1 "github.com/openshift/api/config/v1"
secv1 "github.com/openshift/api/security/v1"
Expand All @@ -44,10 +45,6 @@ import (

lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1"
"github.com/openshift/lvm-operator/pkg/filter"
lsblkmocks "github.com/openshift/lvm-operator/pkg/lsblk/mocks"
lvmmocks "github.com/openshift/lvm-operator/pkg/lvm/mocks"
"github.com/openshift/lvm-operator/pkg/lvmd"

topolvmv1 "github.com/topolvm/topolvm/api/v1"
//+kubebuilder:scaffold:imports
)
Expand All @@ -62,9 +59,7 @@ var (
ctx context.Context
cancel context.CancelFunc
testNodeSelector corev1.NodeSelector
testLVMDFile string
mockLSBLK *lsblkmocks.MockLSBLK
mockLVM *lvmmocks.MockLVM
testVGReconciler *VGReconciler
)

func TestAPIs(t *testing.T) {
Expand Down Expand Up @@ -147,20 +142,15 @@ var _ = BeforeSuite(func() {
}}}
Expect(k8sClient.Create(ctx, testNode)).Should(Succeed())

testLVMD := lvmd.NewFileConfigurator(filepath.Join(GinkgoT().TempDir(), "lvmd.yaml"))
mockLSBLK = lsblkmocks.NewMockLSBLK(GinkgoT())
mockLVM = lvmmocks.NewMockLVM(GinkgoT())
err = (&VGReconciler{
testVGReconciler = &VGReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
EventRecorder: k8sManager.GetEventRecorderFor(ControllerName),
LVM: mockLVM,
LSBLK: mockLSBLK,
LVMD: testLVMD,
Namespace: testNamespaceName,
NodeName: testNodeName,
Filters: filter.DefaultFilters,
}).SetupWithManager(k8sManager)
}
err = (testVGReconciler).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

go func() {
Expand All @@ -177,10 +167,21 @@ var _ = AfterSuite(func() {
Expect(testEnv.Stop()).To(Succeed())
})

func isRoot() bool {
currentUser, err := user.Current()
if err != nil {
log.Fatalf("[isRoot] Unable to get current user: %s", err)
}
return currentUser.Username == "root" || currentUser.Uid == "0"
func setupMocks() (*lvmmocks.MockLVM, *lsblkmocks.MockLSBLK) {
t := GinkgoT()
t.Helper()
mockLSBLK := lsblkmocks.NewMockLSBLK(t)
mockLVM := lvmmocks.NewMockLVM(t)
testLVMD := lvmd.NewFileConfigurator(filepath.Join(t.TempDir(), "lvmd.yaml"))

testVGReconciler.LSBLK = mockLSBLK
testVGReconciler.LVM = mockLVM
testVGReconciler.LVMD = testLVMD
DeferCleanup(func() {
testVGReconciler.LVMD = nil
testVGReconciler.LSBLK = nil
testVGReconciler.LVM = nil
})

return mockLVM, mockLSBLK
}
6 changes: 2 additions & 4 deletions pkg/vgmanager/vgmanager_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ var _ = Describe("vgmanager controller", func() {
})

func testMockedBlockDeviceOnHost(ctx context.Context) {
DeferCleanup(func() {
mockLVM.AssertExpectations(GinkgoT())
mockLSBLK.AssertExpectations(GinkgoT())
})
By("injecting mocked LVM and LSBLK")
mockLVM, mockLSBLK := setupMocks()

By("setting up the disk as a block device with losetup")
device := filepath.Join(GinkgoT().TempDir(), "mock0")
Expand Down

0 comments on commit 0bc51e7

Please sign in to comment.