Skip to content
Closed
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
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
mount "k8s.io/mount-utils"
"k8s.io/utils/exec"
cloud "sigs.k8s.io/gcp-filestore-csi-driver/pkg/cloud_provider"
"sigs.k8s.io/gcp-filestore-csi-driver/pkg/cloud_provider/metadata"
metadataservice "sigs.k8s.io/gcp-filestore-csi-driver/pkg/cloud_provider/metadata"
Expand Down Expand Up @@ -205,7 +206,7 @@ func main() {
},
}

mounter := mount.New("")
mounter := mount.NewSafeFormatAndMount(mount.New(""), exec.New())
config := &driver.GCFSDriverConfig{
Name: driverName,
Version: version,
Expand Down
14 changes: 7 additions & 7 deletions pkg/csi_driver/gcfs_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ const (
)

type GCFSDriverConfig struct {
Name string // Driver name
Version string // Driver version
NodeName string // Node name
RunController bool // Run CSI controller service
RunNode bool // Run CSI node service
Mounter mount.Interface // Mount library
Cloud *cloud.Cloud // Cloud provider
Name string // Driver name
Version string // Driver version
NodeName string // Node name
RunController bool // Run CSI controller service
RunNode bool // Run CSI node service
Mounter *mount.SafeFormatAndMount // Mount library
Cloud *cloud.Cloud // Cloud provider
MetadataService metadataservice.Service
EnableMultishare bool
Reconciler *MultishareReconciler
Expand Down
4 changes: 2 additions & 2 deletions pkg/csi_driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ var (
// TODO(b/375481562): refactor config map utils & remove node driver's dependency on lockReleaseController
type nodeServer struct {
driver *GCFSDriver
mounter mount.Interface
mounter *mount.SafeFormatAndMount
metaService metadata.Service
volumeLocks *util.VolumeLocks
lockReleaseController *lockrelease.LockReleaseController
features *GCFSDriverFeatureOptions
}

func newNodeServer(driver *GCFSDriver, mounter mount.Interface, metaService metadata.Service, featureOptions *GCFSDriverFeatureOptions) (csi.NodeServer, error) {
func newNodeServer(driver *GCFSDriver, mounter *mount.SafeFormatAndMount, metaService metadata.Service, featureOptions *GCFSDriverFeatureOptions) (csi.NodeServer, error) {
ns := &nodeServer{
driver: driver,
mounter: mounter,
Expand Down
67 changes: 50 additions & 17 deletions pkg/csi_driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

csi "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/google/go-cmp/cmp"
Expand All @@ -33,6 +34,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
mount "k8s.io/mount-utils"
"k8s.io/utils/exec"
"sigs.k8s.io/gcp-filestore-csi-driver/pkg/cloud_provider/metadata"
lockrelease "sigs.k8s.io/gcp-filestore-csi-driver/pkg/releaselock"
"sigs.k8s.io/gcp-filestore-csi-driver/pkg/util"
Expand Down Expand Up @@ -75,12 +77,11 @@ var (

type nodeServerTestEnv struct {
ns csi.NodeServer
fm *mount.FakeMounter
fm *mount.SafeFormatAndMount
}

func initTestNodeServer(t *testing.T) *nodeServerTestEnv {
// TODO: make a constructor in FakeMmounter library
mounter := &mount.FakeMounter{MountPoints: []mount.MountPoint{}}
mounter := NewFakeSafeBlockingMounter(nil)
metaserice, err := metadata.NewFakeService()
if err != nil {
t.Fatalf("Failed to init metadata service")
Expand All @@ -96,7 +97,7 @@ func initTestNodeServer(t *testing.T) *nodeServerTestEnv {
}

func initTestNodeServerWithKubeClient(t *testing.T, client kubernetes.Interface) *nodeServer {
mounter := &mount.FakeMounter{MountPoints: []mount.MountPoint{}}
mounter := NewFakeSafeBlockingMounter(nil)
metaserice, err := metadata.NewFakeService()
if err != nil {
t.Fatalf("Failed to init metadata service")
Expand Down Expand Up @@ -351,7 +352,11 @@ func TestNodePublishVolume(t *testing.T) {
for _, test := range cases {
testEnv := initTestNodeServer(t)
if test.mounts != nil {
testEnv.fm.MountPoints = test.mounts
if _, ok := testEnv.fm.Interface.(*FakeBlockingMounter); ok {
testEnv.fm.Interface.(*FakeBlockingMounter).MountPoints = test.mounts
} else {
t.Errorf("no FakeBlockingMounter present")
}
}

_, err = testEnv.ns.NodePublishVolume(context.TODO(), test.req)
Expand All @@ -361,8 +366,11 @@ func TestNodePublishVolume(t *testing.T) {
if test.expectErr && err == nil {
t.Errorf("test %q failed: got success", test.name)
}

validateMountPoint(t, test.name, testEnv.fm, test.expectedMount)
mounter, ok := testEnv.fm.Interface.(*FakeBlockingMounter)
if !ok {
t.Errorf("no FakeBlockingMounter present")
}
validateMountPoint(t, test.name, mounter, test.expectedMount)
// TODO: ValidateMountActions if possible.
}
}
Expand Down Expand Up @@ -450,7 +458,11 @@ func testWindowsNodePublishVolume(t *testing.T) {
for _, test := range cases {
testEnv := initTestNodeServer(t)
if test.mounts != nil {
testEnv.fm.MountPoints = test.mounts
if _, ok := testEnv.fm.Interface.(*FakeBlockingMounter); ok {
testEnv.fm.Interface.(*FakeBlockingMounter).MountPoints = test.mounts
} else {
t.Errorf("no FakeBlockingMounter present")
}
}

_, err = testEnv.ns.NodePublishVolume(context.TODO(), test.req)
Expand All @@ -460,8 +472,11 @@ func testWindowsNodePublishVolume(t *testing.T) {
if test.expectErr && err == nil {
t.Errorf("test %q failed: got success", test.name)
}

validateMountPoint(t, test.name, testEnv.fm, test.expectedMount)
mounter, ok := testEnv.fm.Interface.(*FakeBlockingMounter)
if !ok {
t.Errorf("no FakeBlockingMounter present")
}
validateMountPoint(t, test.name, mounter, test.expectedMount)
// TODO: ValidateMountActions if possible.
}
goOs = defaultOsString
Expand Down Expand Up @@ -527,7 +542,11 @@ func TestNodeUnpublishVolume(t *testing.T) {
for _, test := range cases {
testEnv := initTestNodeServer(t)
if test.mounts != nil {
testEnv.fm.MountPoints = test.mounts
if _, ok := testEnv.fm.Interface.(*FakeBlockingMounter); ok {
testEnv.fm.Interface.(*FakeBlockingMounter).MountPoints = test.mounts
} else {
t.Errorf("no FakeBlockingMounter present")
}
}

_, err = testEnv.ns.NodeUnpublishVolume(context.TODO(), test.req)
Expand All @@ -538,7 +557,11 @@ func TestNodeUnpublishVolume(t *testing.T) {
t.Errorf("test %q failed: got success", test.name)
}

validateMountPoint(t, test.name, testEnv.fm, test.expectedMount)
mounter, ok := testEnv.fm.Interface.(*FakeBlockingMounter)
if !ok {
t.Errorf("no FakeBlockingMounter present")
}
validateMountPoint(t, test.name, mounter, test.expectedMount)
// TODO: ValidateMountActions if possible.
}
}
Expand Down Expand Up @@ -668,7 +691,7 @@ func TestNodeGetVolumeStats(t *testing.T) {

}

func validateMountPoint(t *testing.T, name string, fm *mount.FakeMounter, e *mount.MountPoint) {
func validateMountPoint(t *testing.T, name string, fm *FakeBlockingMounter, e *mount.MountPoint) {
if e == nil {
if len(fm.MountPoints) != 0 {
t.Errorf("test %q failed: got mounts %+v, expected none", name, fm.MountPoints)
Expand Down Expand Up @@ -721,15 +744,25 @@ func NewFakeBlockingMounter(operationUnblocker chan chan struct{}) *FakeBlocking
}
}

func NewFakeSafeBlockingMounter(operationUnblocker chan chan struct{}) *mount.SafeFormatAndMount {
fakeBlockingMounter := &FakeBlockingMounter{
FakeMounter: &mount.FakeMounter{MountPoints: []mount.MountPoint{}},
OperationUnblocker: operationUnblocker,
}
return mount.NewSafeFormatAndMount(fakeBlockingMounter, exec.New(), mount.WithMaxConcurrentFormat(1, 1*time.Minute))
}

func (m *FakeBlockingMounter) Mount(source string, target string, fstype string, options []string) error {
execute := make(chan struct{})
m.OperationUnblocker <- execute
<-execute
if m.OperationUnblocker != nil {
execute := make(chan struct{})
m.OperationUnblocker <- execute
<-execute
}
return m.FakeMounter.Mount(source, target, fstype, options)
}

func initBlockingTestNodeServer(t *testing.T, operationUnblocker chan chan struct{}) *nodeServerTestEnv {
mounter := NewFakeBlockingMounter(operationUnblocker)
mounter := NewFakeSafeBlockingMounter(operationUnblocker)
metaserice, err := metadata.NewFakeService()
if err != nil {
t.Fatalf("Failed to init metadata service")
Expand Down
5 changes: 4 additions & 1 deletion test/sanity/sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func TestSanity(t *testing.T) {
if err != nil {
t.Fatalf("Failed to get cloud provider: %v", err)
}
mounter := &mount.FakeMounter{MountPoints: []mount.MountPoint{}}
fakeMounter := &mount.FakeMounter{MountPoints: []mount.MountPoint{}}
mounter := &mount.SafeFormatAndMount{
Interface: fakeMounter,
}

meta, err := metadata.NewFakeService()
if err != nil {
Expand Down