diff --git a/pkg/gce-pd-csi-driver/controller.go b/pkg/gce-pd-csi-driver/controller.go index d85ddbe59..249fe3644 100644 --- a/pkg/gce-pd-csi-driver/controller.go +++ b/pkg/gce-pd-csi-driver/controller.go @@ -52,23 +52,38 @@ const ( attachableDiskTypePersistent = "PERSISTENT" ) -func getRequestCapacity(capRange *csi.CapacityRange) (capBytes int64) { +func getRequestCapacity(capRange *csi.CapacityRange) (int64, error) { // TODO: Take another look at these casts/caps. Make sure this func is correct + var capBytes int64 + // Default case where nothing is set if capRange == nil { capBytes = MinimumVolumeSizeInBytes - return + return capBytes, nil } - if tcap := capRange.GetRequiredBytes(); tcap > 0 { - capBytes = tcap - } else if tcap = capRange.GetLimitBytes(); tcap > 0 { - capBytes = tcap + rBytes := capRange.GetRequiredBytes() + rSet := rBytes > 0 + lBytes := capRange.GetLimitBytes() + lSet := lBytes > 0 + + if lSet && rSet && lBytes < rBytes { + return 0, fmt.Errorf("Limit bytes %v is less than required bytes %v", lBytes, rBytes) + } + if lSet && lBytes < MinimumVolumeSizeInBytes { + return 0, fmt.Errorf("Limit bytes %v is less than minimum volume size: %v", lBytes, MinimumVolumeSizeInBytes) + } + + // If Required set just set capacity to that which is Required + if rSet { + capBytes = rBytes } + + // Limit is more than Required, but larger than Minimum. So we just set capcity to Minimum // Too small, default if capBytes < MinimumVolumeSizeInBytes { capBytes = MinimumVolumeSizeInBytes } - return + return capBytes, nil } func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) { @@ -86,7 +101,10 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre return nil, status.Error(codes.InvalidArgument, "CreateVolume Volume capabilities must be provided") } - capBytes := getRequestCapacity(capacityRange) + capBytes, err := getRequestCapacity(capacityRange) + if err != nil { + return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("CreateVolume Request Capacity is invalid: %v", err)) + } // TODO: Validate volume capabilities diff --git a/pkg/gce-pd-csi-driver/controller_test.go b/pkg/gce-pd-csi-driver/controller_test.go index c0a4f31b7..fb4fe0a9e 100644 --- a/pkg/gce-pd-csi-driver/controller_test.go +++ b/pkg/gce-pd-csi-driver/controller_test.go @@ -71,7 +71,7 @@ func TestCreateVolumeArguments(t *testing.T) { }, expVol: &csi.Volume{ CapacityBytes: utils.GbToBytes(20), - Id: project + "/" + zone + "/" + "test-vol", + Id: zone + "/" + "test-vol", Attributes: nil, }, }, @@ -94,7 +94,7 @@ func TestCreateVolumeArguments(t *testing.T) { }, expVol: &csi.Volume{ CapacityBytes: MinimumVolumeSizeInBytes, - Id: project + "/" + zone + "/" + "test-vol", + Id: zone + "/" + "test-vol", Attributes: nil, }, }, @@ -117,7 +117,7 @@ func TestCreateVolumeArguments(t *testing.T) { }, expVol: &csi.Volume{ CapacityBytes: utils.GbToBytes(20), - Id: project + "/" + zone + "/" + "test-vol", + Id: zone + "/" + "test-vol", Attributes: nil, }, }, @@ -132,7 +132,7 @@ func TestCreateVolumeArguments(t *testing.T) { }, expVol: &csi.Volume{ CapacityBytes: utils.GbToBytes(20), - Id: project + "/" + zone + "/" + "test-vol", + Id: zone + "/" + "test-vol", Attributes: nil, }, }, @@ -149,7 +149,7 @@ func TestCreateVolumeArguments(t *testing.T) { }, expVol: &csi.Volume{ CapacityBytes: utils.GbToBytes(20), - Id: project + "/" + zone + "/" + "test-vol", + Id: zone + "/" + "test-vol", Attributes: nil, }, }, @@ -178,7 +178,7 @@ func TestCreateVolumeArguments(t *testing.T) { }, expVol: &csi.Volume{ CapacityBytes: MinimumVolumeSizeInBytes, - Id: project + "/" + zone + "/" + "test-vol", + Id: zone + "/" + "test-vol", Attributes: nil, }, }, diff --git a/test/run-unit.sh b/test/run-unit.sh new file mode 100755 index 000000000..f3485a5ca --- /dev/null +++ b/test/run-unit.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e +set -x + +readonly PKGDIR=sigs.k8s.io/gcp-compute-persistent-disk-csi-driver + +go test -timeout 30s "${PKGDIR}/pkg/gce-pd-csi-driver" +# The following have no unit tests yet +#go test -timeout 30s "${PKGDIR}/pkg/mount-manager" +#go test -timeout 30s "${PKGDIR}/pkg/gce-cloud-provider" \ No newline at end of file