Skip to content

Commit f413f91

Browse files
author
Power Cloud Robot
authored
Merge pull request #93 from Madhan-SWE/missing_tests
Node PublishVolume tests added
2 parents 66633ef + 1cad806 commit f413f91

File tree

1 file changed

+285
-0
lines changed

1 file changed

+285
-0
lines changed

pkg/driver/node_test.go

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ import (
3030
"google.golang.org/grpc/status"
3131
)
3232

33+
// constants of keys in PublishContext
34+
const (
35+
// devicePathKey represents key for device path in PublishContext
36+
// devicePath is the device path where the volume is attached to
37+
DevicePathKey = "devicePath"
38+
)
39+
40+
// constants of keys in VolumeContext
41+
const (
42+
// VolumeAttributePartition represents key for partition config in VolumeContext
43+
// this represents the partition number on a device used to mount
44+
VolumeAttributePartition = "partition"
45+
)
46+
3347
var (
3448
volumeID = "voltest"
3549
)
@@ -324,6 +338,277 @@ func TestNodeExpandVolume(t *testing.T) {
324338
}
325339
}
326340

341+
func TestNodePublishVolume(t *testing.T) {
342+
targetPath := "/test/path"
343+
stagingTargetPath := "/test/staging/path"
344+
devicePath := "/dev/fake"
345+
sourcePath := "/test/src"
346+
wwnValue := "testwwn12"
347+
stdVolCap := &csi.VolumeCapability{
348+
AccessType: &csi.VolumeCapability_Block{
349+
Block: &csi.VolumeCapability_BlockVolume{},
350+
},
351+
AccessMode: &csi.VolumeCapability_AccessMode{
352+
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
353+
},
354+
}
355+
356+
stdVolContext := map[string]string{"partition": "1"}
357+
testCases := []struct {
358+
name string
359+
testFunc func(t *testing.T)
360+
}{
361+
{
362+
name: "success normal [raw block]",
363+
testFunc: func(t *testing.T) {
364+
mockCtl := gomock.NewController(t)
365+
defer mockCtl.Finish()
366+
367+
mockMounter := mocks.NewMockMounter(mockCtl)
368+
369+
powervsDriver := &nodeService{
370+
mounter: mockMounter,
371+
volumeLocks: util.NewVolumeLocks(),
372+
}
373+
374+
mockMounter.EXPECT().GetDevicePath(gomock.Any()).Return(sourcePath, nil)
375+
mockMounter.EXPECT().ExistsPath(gomock.Any()).Return(true, nil)
376+
mockMounter.EXPECT().MakeFile(targetPath).Return(nil)
377+
mockMounter.EXPECT().Mount(sourcePath, targetPath, "", gomock.Any()).Return(nil)
378+
379+
req := &csi.NodePublishVolumeRequest{
380+
PublishContext: map[string]string{DevicePathKey: devicePath, WWNKey: wwnValue},
381+
StagingTargetPath: stagingTargetPath,
382+
TargetPath: targetPath,
383+
VolumeCapability: stdVolCap,
384+
VolumeId: volumeID,
385+
}
386+
387+
_, err := powervsDriver.NodePublishVolume(context.TODO(), req)
388+
if err != nil {
389+
t.Fatalf("Expect no error but got: %v", err)
390+
}
391+
},
392+
},
393+
{
394+
name: "success normal with partition [raw block]",
395+
testFunc: func(t *testing.T) {
396+
mockCtl := gomock.NewController(t)
397+
defer mockCtl.Finish()
398+
399+
mockMounter := mocks.NewMockMounter(mockCtl)
400+
401+
powervsDriver := &nodeService{
402+
mounter: mockMounter,
403+
volumeLocks: util.NewVolumeLocks(),
404+
}
405+
406+
mockMounter.EXPECT().GetDevicePath(gomock.Any()).Return(sourcePath, nil)
407+
mockMounter.EXPECT().ExistsPath(gomock.Any()).Return(true, nil)
408+
mockMounter.EXPECT().MakeFile(targetPath).Return(nil)
409+
mockMounter.EXPECT().Mount(sourcePath, targetPath, "", gomock.Any()).Return(nil)
410+
411+
req := &csi.NodePublishVolumeRequest{
412+
PublishContext: map[string]string{DevicePathKey: devicePath, WWNKey: wwnValue},
413+
StagingTargetPath: stagingTargetPath,
414+
TargetPath: targetPath,
415+
VolumeCapability: stdVolCap,
416+
VolumeId: volumeID,
417+
VolumeContext: stdVolContext,
418+
}
419+
420+
_, err := powervsDriver.NodePublishVolume(context.TODO(), req)
421+
if err != nil {
422+
t.Fatalf("Expect no error but got: %v", err)
423+
}
424+
},
425+
},
426+
{
427+
name: "success normal with invalid partition config, will ignore the config [raw block]",
428+
testFunc: func(t *testing.T) {
429+
mockCtl := gomock.NewController(t)
430+
defer mockCtl.Finish()
431+
432+
mockMounter := mocks.NewMockMounter(mockCtl)
433+
434+
powervsDriver := &nodeService{
435+
mounter: mockMounter,
436+
volumeLocks: util.NewVolumeLocks(),
437+
}
438+
439+
mockMounter.EXPECT().GetDevicePath(gomock.Any()).Return(sourcePath, nil)
440+
mockMounter.EXPECT().ExistsPath(gomock.Any()).Return(true, nil)
441+
mockMounter.EXPECT().MakeFile(targetPath).Return(nil)
442+
mockMounter.EXPECT().Mount(sourcePath, targetPath, "", gomock.Any()).Return(nil)
443+
444+
req := &csi.NodePublishVolumeRequest{
445+
PublishContext: map[string]string{DevicePathKey: devicePath, WWNKey: wwnValue},
446+
StagingTargetPath: stagingTargetPath,
447+
TargetPath: targetPath,
448+
VolumeCapability: stdVolCap,
449+
VolumeId: volumeID,
450+
VolumeContext: map[string]string{VolumeAttributePartition: "0"},
451+
}
452+
453+
_, err := powervsDriver.NodePublishVolume(context.TODO(), req)
454+
if err != nil {
455+
t.Fatalf("Expect no error but got: %v", err)
456+
}
457+
},
458+
},
459+
{
460+
name: "fail no device path [raw block]",
461+
testFunc: func(t *testing.T) {
462+
mockCtl := gomock.NewController(t)
463+
defer mockCtl.Finish()
464+
465+
mockMounter := mocks.NewMockMounter(mockCtl)
466+
467+
powervsDriver := &nodeService{
468+
mounter: mockMounter,
469+
volumeLocks: util.NewVolumeLocks(),
470+
}
471+
472+
req := &csi.NodePublishVolumeRequest{
473+
StagingTargetPath: stagingTargetPath,
474+
TargetPath: targetPath,
475+
VolumeCapability: stdVolCap,
476+
VolumeId: volumeID,
477+
VolumeContext: map[string]string{VolumeAttributePartition: "partition1"},
478+
}
479+
480+
_, err := powervsDriver.NodePublishVolume(context.TODO(), req)
481+
expectErr(t, err, codes.InvalidArgument)
482+
},
483+
},
484+
{
485+
name: "fail no VolumeId",
486+
testFunc: func(t *testing.T) {
487+
mockCtl := gomock.NewController(t)
488+
defer mockCtl.Finish()
489+
490+
mockMounter := mocks.NewMockMounter(mockCtl)
491+
492+
powervsDriver := &nodeService{
493+
mounter: mockMounter,
494+
volumeLocks: util.NewVolumeLocks(),
495+
}
496+
req := &csi.NodePublishVolumeRequest{
497+
PublishContext: map[string]string{DevicePathKey: devicePath, WWNKey: wwnValue},
498+
StagingTargetPath: stagingTargetPath,
499+
TargetPath: targetPath,
500+
VolumeCapability: stdVolCap,
501+
}
502+
503+
_, err := powervsDriver.NodePublishVolume(context.TODO(), req)
504+
expectErr(t, err, codes.InvalidArgument)
505+
},
506+
},
507+
{
508+
name: "fail no StagingTargetPath",
509+
testFunc: func(t *testing.T) {
510+
mockCtl := gomock.NewController(t)
511+
defer mockCtl.Finish()
512+
513+
mockMounter := mocks.NewMockMounter(mockCtl)
514+
515+
powervsDriver := &nodeService{
516+
mounter: mockMounter,
517+
volumeLocks: util.NewVolumeLocks(),
518+
}
519+
req := &csi.NodePublishVolumeRequest{
520+
PublishContext: map[string]string{DevicePathKey: devicePath, WWNKey: wwnValue},
521+
TargetPath: targetPath,
522+
VolumeCapability: stdVolCap,
523+
VolumeId: volumeID,
524+
}
525+
526+
_, err := powervsDriver.NodePublishVolume(context.TODO(), req)
527+
expectErr(t, err, codes.InvalidArgument)
528+
},
529+
},
530+
{
531+
name: "fail no VolumeCapability",
532+
testFunc: func(t *testing.T) {
533+
mockCtl := gomock.NewController(t)
534+
defer mockCtl.Finish()
535+
536+
mockMounter := mocks.NewMockMounter(mockCtl)
537+
538+
powervsDriver := &nodeService{
539+
mounter: mockMounter,
540+
volumeLocks: util.NewVolumeLocks(),
541+
}
542+
req := &csi.NodePublishVolumeRequest{
543+
PublishContext: map[string]string{DevicePathKey: devicePath, WWNKey: wwnValue},
544+
TargetPath: targetPath,
545+
StagingTargetPath: stagingTargetPath,
546+
VolumeId: volumeID,
547+
}
548+
549+
_, err := powervsDriver.NodePublishVolume(context.TODO(), req)
550+
expectErr(t, err, codes.InvalidArgument)
551+
},
552+
},
553+
{
554+
name: "fail no TargetPath",
555+
testFunc: func(t *testing.T) {
556+
mockCtl := gomock.NewController(t)
557+
defer mockCtl.Finish()
558+
559+
mockMounter := mocks.NewMockMounter(mockCtl)
560+
561+
powervsDriver := &nodeService{
562+
mounter: mockMounter,
563+
volumeLocks: util.NewVolumeLocks(),
564+
}
565+
req := &csi.NodePublishVolumeRequest{
566+
PublishContext: map[string]string{DevicePathKey: devicePath, WWNKey: wwnValue},
567+
StagingTargetPath: stagingTargetPath,
568+
VolumeCapability: stdVolCap,
569+
VolumeId: volumeID,
570+
}
571+
572+
_, err := powervsDriver.NodePublishVolume(context.TODO(), req)
573+
expectErr(t, err, codes.InvalidArgument)
574+
},
575+
},
576+
{
577+
name: "fail invalid VolumeCapability",
578+
testFunc: func(t *testing.T) {
579+
mockCtl := gomock.NewController(t)
580+
defer mockCtl.Finish()
581+
582+
mockMounter := mocks.NewMockMounter(mockCtl)
583+
584+
powervsDriver := &nodeService{
585+
mounter: mockMounter,
586+
volumeLocks: util.NewVolumeLocks(),
587+
}
588+
req := &csi.NodePublishVolumeRequest{
589+
PublishContext: map[string]string{DevicePathKey: devicePath, WWNKey: wwnValue},
590+
TargetPath: targetPath,
591+
StagingTargetPath: stagingTargetPath,
592+
VolumeCapability: &csi.VolumeCapability{
593+
AccessMode: &csi.VolumeCapability_AccessMode{
594+
Mode: csi.VolumeCapability_AccessMode_UNKNOWN,
595+
},
596+
},
597+
VolumeId: volumeID,
598+
}
599+
600+
_, err := powervsDriver.NodePublishVolume(context.TODO(), req)
601+
expectErr(t, err, codes.InvalidArgument)
602+
},
603+
},
604+
}
605+
606+
for _, tc := range testCases {
607+
t.Run(tc.name, tc.testFunc)
608+
}
609+
610+
}
611+
327612
func TestNodeUnpublishVolume(t *testing.T) {
328613
targetPath := "/test/path"
329614

0 commit comments

Comments
 (0)