Skip to content

Commit e2bdaca

Browse files
committed
Add unit test runner script. Fixed some errors caught by unit tests
1 parent 53270e2 commit e2bdaca

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

pkg/gce-pd-csi-driver/controller.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,38 @@ const (
5252
attachableDiskTypePersistent = "PERSISTENT"
5353
)
5454

55-
func getRequestCapacity(capRange *csi.CapacityRange) (capBytes int64) {
55+
func getRequestCapacity(capRange *csi.CapacityRange) (int64, error) {
5656
// TODO: Take another look at these casts/caps. Make sure this func is correct
57+
var capBytes int64
58+
// Default case where nothing is set
5759
if capRange == nil {
5860
capBytes = MinimumVolumeSizeInBytes
59-
return
61+
return capBytes, nil
6062
}
6163

62-
if tcap := capRange.GetRequiredBytes(); tcap > 0 {
63-
capBytes = tcap
64-
} else if tcap = capRange.GetLimitBytes(); tcap > 0 {
65-
capBytes = tcap
64+
rBytes := capRange.GetRequiredBytes()
65+
rSet := rBytes > 0
66+
lBytes := capRange.GetLimitBytes()
67+
lSet := lBytes > 0
68+
69+
if lSet && rSet && lBytes < rBytes {
70+
return 0, fmt.Errorf("Limit bytes %v is less than required bytes %v", lBytes, rBytes)
71+
}
72+
if lSet && lBytes < MinimumVolumeSizeInBytes {
73+
return 0, fmt.Errorf("Limit bytes %v is less than minimum volume size: %v", lBytes, MinimumVolumeSizeInBytes)
74+
}
75+
76+
// If Required set just set capacity to that which is Required
77+
if rSet {
78+
capBytes = rBytes
6679
}
80+
81+
// Limit is more than Required, but larger than Minimum. So we just set capcity to Minimum
6782
// Too small, default
6883
if capBytes < MinimumVolumeSizeInBytes {
6984
capBytes = MinimumVolumeSizeInBytes
7085
}
71-
return
86+
return capBytes, nil
7287
}
7388

7489
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
86101
return nil, status.Error(codes.InvalidArgument, "CreateVolume Volume capabilities must be provided")
87102
}
88103

89-
capBytes := getRequestCapacity(capacityRange)
104+
capBytes, err := getRequestCapacity(capacityRange)
105+
if err != nil {
106+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("CreateVolume Request Capacity is invalid: %v", err))
107+
}
90108

91109
// TODO: Validate volume capabilities
92110

pkg/gce-pd-csi-driver/controller_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestCreateVolumeArguments(t *testing.T) {
7171
},
7272
expVol: &csi.Volume{
7373
CapacityBytes: utils.GbToBytes(20),
74-
Id: project + "/" + zone + "/" + "test-vol",
74+
Id: zone + "/" + "test-vol",
7575
Attributes: nil,
7676
},
7777
},
@@ -94,7 +94,7 @@ func TestCreateVolumeArguments(t *testing.T) {
9494
},
9595
expVol: &csi.Volume{
9696
CapacityBytes: MinimumVolumeSizeInBytes,
97-
Id: project + "/" + zone + "/" + "test-vol",
97+
Id: zone + "/" + "test-vol",
9898
Attributes: nil,
9999
},
100100
},
@@ -117,7 +117,7 @@ func TestCreateVolumeArguments(t *testing.T) {
117117
},
118118
expVol: &csi.Volume{
119119
CapacityBytes: utils.GbToBytes(20),
120-
Id: project + "/" + zone + "/" + "test-vol",
120+
Id: zone + "/" + "test-vol",
121121
Attributes: nil,
122122
},
123123
},
@@ -132,7 +132,7 @@ func TestCreateVolumeArguments(t *testing.T) {
132132
},
133133
expVol: &csi.Volume{
134134
CapacityBytes: utils.GbToBytes(20),
135-
Id: project + "/" + zone + "/" + "test-vol",
135+
Id: zone + "/" + "test-vol",
136136
Attributes: nil,
137137
},
138138
},
@@ -149,7 +149,7 @@ func TestCreateVolumeArguments(t *testing.T) {
149149
},
150150
expVol: &csi.Volume{
151151
CapacityBytes: utils.GbToBytes(20),
152-
Id: project + "/" + zone + "/" + "test-vol",
152+
Id: zone + "/" + "test-vol",
153153
Attributes: nil,
154154
},
155155
},
@@ -178,7 +178,7 @@ func TestCreateVolumeArguments(t *testing.T) {
178178
},
179179
expVol: &csi.Volume{
180180
CapacityBytes: MinimumVolumeSizeInBytes,
181-
Id: project + "/" + zone + "/" + "test-vol",
181+
Id: zone + "/" + "test-vol",
182182
Attributes: nil,
183183
},
184184
},

test/run-unit.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -x
5+
6+
readonly PKGDIR=sigs.k8s.io/gcp-compute-persistent-disk-csi-driver
7+
8+
go test -timeout 30s "${PKGDIR}/pkg/gce-pd-csi-driver"
9+
# The following have no unit tests yet
10+
#go test -timeout 30s "${PKGDIR}/pkg/mount-manager"
11+
#go test -timeout 30s "${PKGDIR}/pkg/gce-cloud-provider"

0 commit comments

Comments
 (0)