From f205d62555a0c98a64e89e92d6364578ea785f4c Mon Sep 17 00:00:00 2001 From: Mockery-Li <249406560@qq.com> Date: Fri, 19 Apr 2019 10:26:26 +0800 Subject: [PATCH 1/2] Test publish --- pkg/gce-pd-csi-driver/node.go | 5 +- pkg/gce-pd-csi-driver/node_test.go | 269 +++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+), 4 deletions(-) diff --git a/pkg/gce-pd-csi-driver/node.go b/pkg/gce-pd-csi-driver/node.go index 080261aaf..25a29d396 100644 --- a/pkg/gce-pd-csi-driver/node.go +++ b/pkg/gce-pd-csi-driver/node.go @@ -296,10 +296,7 @@ func (ns *GCENodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRe volumeLimits, err := ns.GetVolumeLimits() resp := &csi.NodeGetInfoResponse{ - NodeId: nodeID, - // TODO(#19): Set MaxVolumesPerNode based on Node Type - // Default of 0 means that CO Decides how many nodes can be published - // Can get from metadata server "machine-type" + NodeId: nodeID, MaxVolumesPerNode: volumeLimits, AccessibleTopology: top, } diff --git a/pkg/gce-pd-csi-driver/node_test.go b/pkg/gce-pd-csi-driver/node_test.go index 96c79d4ff..dd4de63d7 100644 --- a/pkg/gce-pd-csi-driver/node_test.go +++ b/pkg/gce-pd-csi-driver/node_test.go @@ -19,6 +19,8 @@ import ( csi "github.com/container-storage-interface/spec/lib/go/csi" "golang.org/x/net/context" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata" mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager" ) @@ -86,3 +88,270 @@ func TestNodeGetVolumeLimits(t *testing.T) { } } } + +func TestNodePublishVolume(t *testing.T) { + gceDriver := getTestGCEDriver(t) + ns := gceDriver.ns + testCases := []struct { + name string + req *csi.NodePublishVolumeRequest + expErrCode codes.Code + }{ + { + name: "Valid request", + req: &csi.NodePublishVolumeRequest{ + VolumeId: "1", + TargetPath: "/mnt/test", + StagingTargetPath: "/staging", + Readonly: false, + VolumeCapability: &csi.VolumeCapability{}, + }, + }, + { + name: "Invalid request (No VolumeId)", + req: &csi.NodePublishVolumeRequest{ + TargetPath: "/mnt/test", + StagingTargetPath: "/staging", + Readonly: false, + VolumeCapability: &csi.VolumeCapability{}, + }, + expErrCode: codes.InvalidArgument, + }, + { + name: "Invalid request (No TargetPath)", + req: &csi.NodePublishVolumeRequest{ + VolumeId: "1", + StagingTargetPath: "/staging", + Readonly: false, + VolumeCapability: &csi.VolumeCapability{}, + }, + expErrCode: codes.InvalidArgument, + }, + { + name: "Invalid request (No StagingTargetPath)", + req: &csi.NodePublishVolumeRequest{ + VolumeId: "1", + TargetPath: "/mnt/test", + Readonly: false, + VolumeCapability: &csi.VolumeCapability{}, + }, + expErrCode: codes.InvalidArgument, + }, + { + name: "Invalid request (Nil VolumeCapability)", + req: &csi.NodePublishVolumeRequest{ + VolumeId: "1", + TargetPath: "/mnt/test", + StagingTargetPath: "/staging", + Readonly: false, + VolumeCapability: nil, + }, + expErrCode: codes.InvalidArgument, + }, + } + for _, tc := range testCases { + t.Logf("Test case: %s", tc.name) + _, err := ns.NodePublishVolume(context.Background(), tc.req) + if err != nil { + serverError, ok := status.FromError(err) + if !ok { + t.Fatalf("Could not get error status code from err: %v", err) + } + if serverError.Code() != tc.expErrCode { + t.Fatalf("Expected error code: %v, got: %v. err : %v", tc.expErrCode, serverError.Code(), err) + } + continue + } + if tc.expErrCode != codes.OK { + t.Fatalf("Expected error: %v, got no error", tc.expErrCode) + } + } +} + +func TestNodeUnpublishVolume(t *testing.T) { + gceDriver := getTestGCEDriver(t) + ns := gceDriver.ns + testCases := []struct { + name string + req *csi.NodeUnpublishVolumeRequest + expErrCode codes.Code + }{ + { + name: "Valid request", + req: &csi.NodeUnpublishVolumeRequest{ + VolumeId: "1", + TargetPath: "/mnt/test", + }, + }, + { + name: "Invalid request (No VolumeId)", + req: &csi.NodeUnpublishVolumeRequest{ + TargetPath: "/mnt/test", + }, + expErrCode: codes.InvalidArgument, + }, + { + name: "Invalid request (No TargetPath)", + req: &csi.NodeUnpublishVolumeRequest{ + VolumeId: "1", + }, + expErrCode: codes.InvalidArgument, + }, + } + for _, tc := range testCases { + t.Logf("Test case: %s", tc.name) + _, err := ns.NodeUnpublishVolume(context.Background(), tc.req) + if err != nil { + serverError, ok := status.FromError(err) + if !ok { + t.Fatalf("Could not get error status code from err: %v", err) + } + if serverError.Code() != tc.expErrCode { + t.Fatalf("Expected error code: %v, got: %v. err : %v", tc.expErrCode, serverError.Code(), err) + } + continue + } + if tc.expErrCode != codes.OK { + t.Fatalf("Expected error: %v, got no error", tc.expErrCode) + } + } +} + +func TestNodeStageVolume(t *testing.T) { + gceDriver := getTestGCEDriver(t) + ns := gceDriver.ns + volumeID := "project/test001/zones/c1/disks/testDisk" + blockCap := &csi.VolumeCapability_Block{ + Block: &csi.VolumeCapability_BlockVolume{}, + } + cap := &csi.VolumeCapability{ + AccessType: blockCap, + } + + testCases := []struct { + name string + req *csi.NodeStageVolumeRequest + expErrCode codes.Code + }{ + { + name: "Valid request", + req: &csi.NodeStageVolumeRequest{ + VolumeId: volumeID, + StagingTargetPath: "/staging", + VolumeCapability: &csi.VolumeCapability{}, + }, + }, + { + name: "Invalid request (No VolumeId)", + req: &csi.NodeStageVolumeRequest{ + StagingTargetPath: "/staging", + VolumeCapability: &csi.VolumeCapability{}, + }, + expErrCode: codes.InvalidArgument, + }, + { + name: "Invalid request (No StagingTargetPath)", + req: &csi.NodeStageVolumeRequest{ + VolumeId: volumeID, + VolumeCapability: &csi.VolumeCapability{}, + }, + expErrCode: codes.InvalidArgument, + }, + { + name: "Invalid request (Nil VolumeCapability)", + req: &csi.NodeStageVolumeRequest{ + VolumeId: volumeID, + StagingTargetPath: "/staging", + VolumeCapability: nil, + }, + expErrCode: codes.InvalidArgument, + }, + { + name: "Invalid request (No Mount in capability)", + req: &csi.NodeStageVolumeRequest{ + VolumeId: volumeID, + StagingTargetPath: "/staging", + VolumeCapability: cap, + }, + expErrCode: codes.Unimplemented, + }, + // Capability Mount. codes.Unimplemented + } + for _, tc := range testCases { + t.Logf("Test case: %s", tc.name) + _, err := ns.NodeStageVolume(context.Background(), tc.req) + if err != nil { + serverError, ok := status.FromError(err) + if !ok { + t.Fatalf("Could not get error status code from err: %v", err) + } + if serverError.Code() != tc.expErrCode { + t.Fatalf("Expected error code: %v, got: %v. err : %v", tc.expErrCode, serverError.Code(), err) + } + continue + } + if tc.expErrCode != codes.OK { + t.Fatalf("Expected error: %v, got no error", tc.expErrCode) + } + } +} + +func TestNodeUnstageVolume(t *testing.T) { + gceDriver := getTestGCEDriver(t) + ns := gceDriver.ns + testCases := []struct { + name string + req *csi.NodeUnstageVolumeRequest + expErrCode codes.Code + }{ + { + name: "Valid request", + req: &csi.NodeUnstageVolumeRequest{ + VolumeId: "1", + StagingTargetPath: "/staging", + }, + }, + { + name: "Invalid request (No VolumeId)", + req: &csi.NodeUnstageVolumeRequest{ + StagingTargetPath: "/staging", + }, + expErrCode: codes.InvalidArgument, + }, + { + name: "Invalid request (No StagingTargetPath)", + req: &csi.NodeUnstageVolumeRequest{ + VolumeId: "1", + }, + expErrCode: codes.InvalidArgument, + }, + } + for _, tc := range testCases { + t.Logf("Test case: %s", tc.name) + _, err := ns.NodeUnstageVolume(context.Background(), tc.req) + if err != nil { + serverError, ok := status.FromError(err) + if !ok { + t.Fatalf("Could not get error status code from err: %v", err) + } + if serverError.Code() != tc.expErrCode { + t.Fatalf("Expected error code: %v, got: %v. err : %v", tc.expErrCode, serverError.Code(), err) + } + continue + } + if tc.expErrCode != codes.OK { + t.Fatalf("Expected error: %v, got no error", tc.expErrCode) + } + } +} + +func TestNodeGetCapabilities(t *testing.T) { + gceDriver := getTestGCEDriver(t) + ns := gceDriver.ns + req := &csi.NodeGetCapabilitiesRequest{} + + _, err := ns.NodeGetCapabilities(context.Background(), req) + if err != nil { + t.Fatalf("Unexpedted error: %v", err) + } +} From b0d15e9c38b9a67c14fcaac0db6bce73b336576c Mon Sep 17 00:00:00 2001 From: Mockery-Li <249406560@qq.com> Date: Fri, 19 Apr 2019 12:11:55 +0800 Subject: [PATCH 2/2] Modify default volumeID --- pkg/gce-pd-csi-driver/node_test.go | 52 ++++++++++++++++-------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/pkg/gce-pd-csi-driver/node_test.go b/pkg/gce-pd-csi-driver/node_test.go index dd4de63d7..939e44004 100644 --- a/pkg/gce-pd-csi-driver/node_test.go +++ b/pkg/gce-pd-csi-driver/node_test.go @@ -25,6 +25,10 @@ import ( mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager" ) +const defaultVolumeID = "project/test001/zones/c1/disks/testDisk" +const defaultTargetPath = "/mnt/test" +const defaultStagingPath = "/staging" + func getTestGCEDriver(t *testing.T) *GCEDriver { gceDriver := GetGCEDriver() err := gceDriver.SetupGCEDriver(nil, mountmanager.NewFakeSafeMounter(), mountmanager.NewFakeDeviceUtils(), metadataservice.NewFakeService(), driver, "test-vendor") @@ -100,9 +104,9 @@ func TestNodePublishVolume(t *testing.T) { { name: "Valid request", req: &csi.NodePublishVolumeRequest{ - VolumeId: "1", - TargetPath: "/mnt/test", - StagingTargetPath: "/staging", + VolumeId: defaultVolumeID, + TargetPath: defaultTargetPath, + StagingTargetPath: defaultStagingPath, Readonly: false, VolumeCapability: &csi.VolumeCapability{}, }, @@ -110,8 +114,8 @@ func TestNodePublishVolume(t *testing.T) { { name: "Invalid request (No VolumeId)", req: &csi.NodePublishVolumeRequest{ - TargetPath: "/mnt/test", - StagingTargetPath: "/staging", + TargetPath: defaultTargetPath, + StagingTargetPath: defaultStagingPath, Readonly: false, VolumeCapability: &csi.VolumeCapability{}, }, @@ -120,8 +124,8 @@ func TestNodePublishVolume(t *testing.T) { { name: "Invalid request (No TargetPath)", req: &csi.NodePublishVolumeRequest{ - VolumeId: "1", - StagingTargetPath: "/staging", + VolumeId: defaultVolumeID, + StagingTargetPath: defaultStagingPath, Readonly: false, VolumeCapability: &csi.VolumeCapability{}, }, @@ -130,8 +134,8 @@ func TestNodePublishVolume(t *testing.T) { { name: "Invalid request (No StagingTargetPath)", req: &csi.NodePublishVolumeRequest{ - VolumeId: "1", - TargetPath: "/mnt/test", + VolumeId: defaultVolumeID, + TargetPath: defaultTargetPath, Readonly: false, VolumeCapability: &csi.VolumeCapability{}, }, @@ -140,9 +144,9 @@ func TestNodePublishVolume(t *testing.T) { { name: "Invalid request (Nil VolumeCapability)", req: &csi.NodePublishVolumeRequest{ - VolumeId: "1", - TargetPath: "/mnt/test", - StagingTargetPath: "/staging", + VolumeId: defaultVolumeID, + TargetPath: defaultTargetPath, + StagingTargetPath: defaultStagingPath, Readonly: false, VolumeCapability: nil, }, @@ -179,21 +183,21 @@ func TestNodeUnpublishVolume(t *testing.T) { { name: "Valid request", req: &csi.NodeUnpublishVolumeRequest{ - VolumeId: "1", - TargetPath: "/mnt/test", + VolumeId: defaultVolumeID, + TargetPath: defaultTargetPath, }, }, { name: "Invalid request (No VolumeId)", req: &csi.NodeUnpublishVolumeRequest{ - TargetPath: "/mnt/test", + TargetPath: defaultTargetPath, }, expErrCode: codes.InvalidArgument, }, { name: "Invalid request (No TargetPath)", req: &csi.NodeUnpublishVolumeRequest{ - VolumeId: "1", + VolumeId: defaultVolumeID, }, expErrCode: codes.InvalidArgument, }, @@ -237,14 +241,14 @@ func TestNodeStageVolume(t *testing.T) { name: "Valid request", req: &csi.NodeStageVolumeRequest{ VolumeId: volumeID, - StagingTargetPath: "/staging", + StagingTargetPath: defaultStagingPath, VolumeCapability: &csi.VolumeCapability{}, }, }, { name: "Invalid request (No VolumeId)", req: &csi.NodeStageVolumeRequest{ - StagingTargetPath: "/staging", + StagingTargetPath: defaultStagingPath, VolumeCapability: &csi.VolumeCapability{}, }, expErrCode: codes.InvalidArgument, @@ -261,7 +265,7 @@ func TestNodeStageVolume(t *testing.T) { name: "Invalid request (Nil VolumeCapability)", req: &csi.NodeStageVolumeRequest{ VolumeId: volumeID, - StagingTargetPath: "/staging", + StagingTargetPath: defaultStagingPath, VolumeCapability: nil, }, expErrCode: codes.InvalidArgument, @@ -270,7 +274,7 @@ func TestNodeStageVolume(t *testing.T) { name: "Invalid request (No Mount in capability)", req: &csi.NodeStageVolumeRequest{ VolumeId: volumeID, - StagingTargetPath: "/staging", + StagingTargetPath: defaultStagingPath, VolumeCapability: cap, }, expErrCode: codes.Unimplemented, @@ -307,21 +311,21 @@ func TestNodeUnstageVolume(t *testing.T) { { name: "Valid request", req: &csi.NodeUnstageVolumeRequest{ - VolumeId: "1", - StagingTargetPath: "/staging", + VolumeId: defaultVolumeID, + StagingTargetPath: defaultStagingPath, }, }, { name: "Invalid request (No VolumeId)", req: &csi.NodeUnstageVolumeRequest{ - StagingTargetPath: "/staging", + StagingTargetPath: defaultStagingPath, }, expErrCode: codes.InvalidArgument, }, { name: "Invalid request (No StagingTargetPath)", req: &csi.NodeUnstageVolumeRequest{ - VolumeId: "1", + VolumeId: defaultVolumeID, }, expErrCode: codes.InvalidArgument, },