Skip to content

Commit

Permalink
Add ability to use other mounters for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
msau42 committed Aug 15, 2019
1 parent d74ee25 commit 7f33fbb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
8 changes: 5 additions & 3 deletions pkg/nfs/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package nfs
import (
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/util/mount"
)

type nfsDriver struct {
Expand Down Expand Up @@ -61,9 +62,10 @@ func NewNFSdriver(nodeID, endpoint string) *nfsDriver {
return n
}

func NewNodeServer(n *nfsDriver) *nodeServer {
func NewNodeServer(n *nfsDriver, mounter mount.Interface) *nodeServer {
return &nodeServer{
Driver: n,
Driver: n,
mounter: mounter,
}
}

Expand All @@ -74,7 +76,7 @@ func (n *nfsDriver) Run() {
// NFS plugin has not implemented ControllerServer
// using default controllerserver.
NewControllerServer(n),
NewNodeServer(n))
NewNodeServer(n, mount.New("")))
s.Wait()
}

Expand Down
15 changes: 8 additions & 7 deletions pkg/nfs/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package nfs

import (
"fmt"
"github.com/golang/glog"
"os"
"strings"

"github.com/golang/glog"

"github.com/container-storage-interface/spec/lib/go/csi"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
Expand All @@ -30,12 +31,13 @@ import (
)

type nodeServer struct {
Driver *nfsDriver
Driver *nfsDriver
mounter mount.Interface
}

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

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

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

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

err = mount.CleanupMountPoint(req.GetTargetPath(), mount.New(""), false)
err = mount.CleanupMountPoint(req.GetTargetPath(), ns.mounter, false)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down

0 comments on commit 7f33fbb

Please sign in to comment.