@@ -21,6 +21,7 @@ import (
21
21
"errors"
22
22
"fmt"
23
23
"os"
24
+ "path/filepath"
24
25
"reflect"
25
26
"runtime"
26
27
"syscall"
@@ -34,6 +35,7 @@ import (
34
35
"github.com/stretchr/testify/assert"
35
36
36
37
mount "k8s.io/mount-utils"
38
+ utilexec "k8s.io/utils/exec"
37
39
testingexec "k8s.io/utils/exec/testing"
38
40
)
39
41
@@ -246,6 +248,52 @@ func TestNodePublishVolume(t *testing.T) {
246
248
assert .NoError (t , err )
247
249
}
248
250
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
+
249
297
func TestNodeUnpublishVolume (t * testing.T ) {
250
298
tests := []struct {
251
299
desc string
0 commit comments