Skip to content

Commit adb36fc

Browse files
authored
Merge pull request #16 from msau42/fake-mounter
Add ability to use other mounters for unit testing
2 parents 48ce196 + a7d05cc commit adb36fc

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

pkg/nfs/nfs.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package nfs
1919
import (
2020
"github.com/container-storage-interface/spec/lib/go/csi"
2121
"github.com/golang/glog"
22+
"k8s.io/kubernetes/pkg/util/mount"
2223
)
2324

2425
type nfsDriver struct {
@@ -61,14 +62,15 @@ func NewNFSdriver(nodeID, endpoint string) *nfsDriver {
6162
return n
6263
}
6364

64-
func NewNodeServer(n *nfsDriver) *nodeServer {
65+
func NewNodeServer(n *nfsDriver, mounter mount.Interface) *nodeServer {
6566
return &nodeServer{
66-
Driver: n,
67+
Driver: n,
68+
mounter: mounter,
6769
}
6870
}
6971

7072
func (n *nfsDriver) Run() {
71-
n.ns = NewNodeServer(n)
73+
n.ns = NewNodeServer(n, mount.New(""))
7274
s := NewNonBlockingGRPCServer()
7375
s.Start(n.endpoint,
7476
NewDefaultIdentityServer(n),

pkg/nfs/nodeserver.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ package nfs
1818

1919
import (
2020
"fmt"
21-
"github.com/golang/glog"
2221
"os"
2322
"strings"
2423

24+
"github.com/golang/glog"
25+
2526
"github.com/container-storage-interface/spec/lib/go/csi"
2627
"golang.org/x/net/context"
2728
"google.golang.org/grpc/codes"
@@ -30,12 +31,13 @@ import (
3031
)
3132

3233
type nodeServer struct {
33-
Driver *nfsDriver
34+
Driver *nfsDriver
35+
mounter mount.Interface
3436
}
3537

3638
func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
3739
targetPath := req.GetTargetPath()
38-
notMnt, err := mount.New("").IsLikelyNotMountPoint(targetPath)
40+
notMnt, err := ns.mounter.IsLikelyNotMountPoint(targetPath)
3941
if err != nil {
4042
if os.IsNotExist(err) {
4143
if err := os.MkdirAll(targetPath, 0750); err != nil {
@@ -60,8 +62,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
6062
ep := req.GetVolumeContext()["share"]
6163
source := fmt.Sprintf("%s:%s", s, ep)
6264

63-
mounter := mount.New("")
64-
err = mounter.Mount(source, targetPath, "nfs", mo)
65+
err = ns.mounter.Mount(source, targetPath, "nfs", mo)
6566
if err != nil {
6667
if os.IsPermission(err) {
6768
return nil, status.Error(codes.PermissionDenied, err.Error())
@@ -77,7 +78,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
7778

7879
func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
7980
targetPath := req.GetTargetPath()
80-
notMnt, err := mount.New("").IsLikelyNotMountPoint(targetPath)
81+
notMnt, err := ns.mounter.IsLikelyNotMountPoint(targetPath)
8182

8283
if err != nil {
8384
if os.IsNotExist(err) {
@@ -90,7 +91,7 @@ func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
9091
return nil, status.Error(codes.NotFound, "Volume not mounted")
9192
}
9293

93-
err = mount.CleanupMountPoint(req.GetTargetPath(), mount.New(""), false)
94+
err = mount.CleanupMountPoint(req.GetTargetPath(), ns.mounter, false)
9495
if err != nil {
9596
return nil, status.Error(codes.Internal, err.Error())
9697
}

0 commit comments

Comments
 (0)