Skip to content

Add unit test runner script. Fixed some errors caught by unit tests #51

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 1 commit into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 26 additions & 8 deletions pkg/gce-pd-csi-driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,38 @@ const (
attachableDiskTypePersistent = "PERSISTENT"
)

func getRequestCapacity(capRange *csi.CapacityRange) (capBytes int64) {
func getRequestCapacity(capRange *csi.CapacityRange) (int64, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You may want to check out what I did for this (in fact, it should probably be a utility function that can be shared by multiple drivers), and my test cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrote some tests for this function in a followup PR. Lets work on a canonical getRequestCapacity.

// 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) {
Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions pkg/gce-pd-csi-driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand All @@ -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,
},
},
Expand All @@ -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,
},
},
Expand All @@ -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,
},
},
Expand All @@ -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,
},
},
Expand Down Expand Up @@ -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,
},
},
Expand Down
11 changes: 11 additions & 0 deletions test/run-unit.sh
Original file line number Diff line number Diff line change
@@ -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"