Skip to content

Add unit test for Node #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions pkg/gce-pd-csi-driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
269 changes: 269 additions & 0 deletions pkg/gce-pd-csi-driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use a valid volume ID here of the form projects/{project}/zones/{zone}/disks/{name}
technically I think we want to validate the ID format and return an invalidargument error if it is not in that format (although right now we don't do that)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto for the other tests as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I created several const variables for default testing volume ID, staging path and target path.

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)
}
}