Skip to content

Commit d93744f

Browse files
authored
fix: nodepublishvolume idempotent issue and add a test case (#615)
* fix nodepublishvolume idempotent issue and add a test case * fix test * fix the fake ensureMountPoint test * fix lint * fix failed test cases on prow
1 parent f3d7e8c commit d93744f

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

pkg/blob/nodeserver.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"os/exec"
24+
"path/filepath"
2425
"strings"
2526
"time"
2627

@@ -464,6 +465,25 @@ func (d *Driver) ensureMountPoint(target string) (bool, error) {
464465
}
465466
}
466467

468+
// Check all the mountpoints in case IsLikelyNotMountPoint
469+
// cannot handle --bind mount
470+
mountList, err := d.mounter.List()
471+
if err != nil {
472+
return !notMnt, err
473+
}
474+
475+
targetAbs, err := filepath.Abs(target)
476+
if err != nil {
477+
return !notMnt, err
478+
}
479+
480+
for _, mountPoint := range mountList {
481+
if mountPoint.Path == targetAbs {
482+
notMnt = false
483+
break
484+
}
485+
}
486+
467487
if !notMnt {
468488
// testing original mount point, make sure the mount link is valid
469489
_, err := ioutil.ReadDir(target)

pkg/blob/nodeserver_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"os"
24+
"path/filepath"
2425
"reflect"
2526
"runtime"
2627
"syscall"
@@ -34,6 +35,7 @@ import (
3435
"github.com/stretchr/testify/assert"
3536

3637
mount "k8s.io/mount-utils"
38+
utilexec "k8s.io/utils/exec"
3739
testingexec "k8s.io/utils/exec/testing"
3840
)
3941

@@ -246,6 +248,52 @@ func TestNodePublishVolume(t *testing.T) {
246248
assert.NoError(t, err)
247249
}
248250

251+
func TestNodePublishVolumeIdempotentMount(t *testing.T) {
252+
if runtime.GOOS != "linux" || os.Getuid() != 0 {
253+
return
254+
}
255+
_ = makeDir(sourceTest)
256+
_ = makeDir(targetTest)
257+
d := NewFakeDriver()
258+
d.mounter = &mount.SafeFormatAndMount{
259+
Interface: mount.New(""),
260+
Exec: utilexec.New(),
261+
}
262+
263+
volumeCap := csi.VolumeCapability_AccessMode{Mode: csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER}
264+
req := csi.NodePublishVolumeRequest{VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
265+
VolumeId: "vol_1",
266+
TargetPath: targetTest,
267+
StagingTargetPath: sourceTest,
268+
Readonly: true}
269+
270+
_, err := d.NodePublishVolume(context.Background(), &req)
271+
assert.NoError(t, err)
272+
_, err = d.NodePublishVolume(context.Background(), &req)
273+
assert.NoError(t, err)
274+
275+
// ensure the target not be mounted twice
276+
targetAbs, err := filepath.Abs(targetTest)
277+
assert.NoError(t, err)
278+
279+
mountList, err := d.mounter.List()
280+
assert.NoError(t, err)
281+
mountPointNum := 0
282+
for _, mountPoint := range mountList {
283+
if mountPoint.Path == targetAbs {
284+
mountPointNum++
285+
}
286+
}
287+
assert.Equal(t, 1, mountPointNum)
288+
err = d.mounter.Unmount(targetTest)
289+
assert.NoError(t, err)
290+
_ = d.mounter.Unmount(targetTest)
291+
err = os.RemoveAll(sourceTest)
292+
assert.NoError(t, err)
293+
err = os.RemoveAll(targetTest)
294+
assert.NoError(t, err)
295+
}
296+
249297
func TestNodeUnpublishVolume(t *testing.T) {
250298
tests := []struct {
251299
desc string

0 commit comments

Comments
 (0)