Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCPVE-677: fix: vgmanager unit tests failure on Darwin due to symlinked Temp Dir #420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions pkg/vgmanager/devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"runtime"
"testing"

"github.com/go-logr/logr/testr"
Expand Down Expand Up @@ -587,8 +586,5 @@ func TestAvailableDevicesForVG(t *testing.T) {
// it has /private in the beginning because /tmp symlinks are evaluated as with /private in the beginning on darwin.
func calculateDevicePath(t *testing.T, deviceName string) string {
t.Helper()
if runtime.GOOS == "darwin" {
return fmt.Sprintf("/private%s", devicePaths[deviceName])
}
return devicePaths[deviceName]
return getKNameFromDevice(devicePaths[deviceName])
}
17 changes: 17 additions & 0 deletions pkg/vgmanager/kname_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package vgmanager

import (
"path/filepath"
"runtime"
)

func getKNameFromDevice(device string) string {
// HACK: if we are on unix, we can simply use the "/tmp" path.
// if we are on darwin, then the symlink of the temp file
// will resolve to /private/var from /var, so we have to adjust
// the block device name
if runtime.GOOS == "darwin" {
return filepath.Join("/", "private", device)
}
return device
}
51 changes: 36 additions & 15 deletions pkg/vgmanager/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

configv1 "github.com/openshift/api/config/v1"
secv1 "github.com/openshift/api/security/v1"
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"

corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
Expand All @@ -36,15 +38,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"

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 @@ -58,8 +57,7 @@ var (
ctx context.Context
cancel context.CancelFunc
testNodeSelector corev1.NodeSelector
mockLSBLK *lsblkmocks.MockLSBLK
mockLVM *lvmmocks.MockLVM
testVGReconciler *VGReconciler
)

func TestAPIs(t *testing.T) {
Expand Down Expand Up @@ -144,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 @@ -173,3 +166,31 @@ var _ = AfterSuite(func() {
By("tearing down the test environment")
Expect(testEnv.Stop()).To(Succeed())
})

func setupMocks() (*lvmmocks.MockLVM, *lsblkmocks.MockLSBLK) {
t := GinkgoT()
t.Helper()

mockLSBLK := &lsblkmocks.MockLSBLK{}
mockLSBLK.Mock.Test(t)
DeferCleanup(func() {
mockLSBLK.AssertExpectations(t)
})
mockLVM := &lvmmocks.MockLVM{}
mockLVM.Mock.Test(t)
DeferCleanup(func() {
mockLVM.AssertExpectations(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
}
8 changes: 3 additions & 5 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 All @@ -52,7 +50,7 @@ func testMockedBlockDeviceOnHost(ctx context.Context) {
mockLSBLK.EXPECT().ListBlockDevices().Return([]lsblk.BlockDevice{
{
Name: "mock0",
KName: device,
KName: getKNameFromDevice(device),
Type: "mocked",
Model: "mocked",
Vendor: "mocked",
Expand Down