Skip to content

Commit 3d53ff6

Browse files
committed
Added controller unit tests
1 parent f0b6d63 commit 3d53ff6

File tree

5 files changed

+364
-143
lines changed

5 files changed

+364
-143
lines changed

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

+60-62
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,6 @@ const (
5252
attachableDiskTypePersistent = "PERSISTENT"
5353
)
5454

55-
func getRequestCapacity(capRange *csi.CapacityRange) (int64, error) {
56-
// 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
59-
if capRange == nil {
60-
capBytes = MinimumVolumeSizeInBytes
61-
return capBytes, nil
62-
}
63-
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
79-
}
80-
81-
// Limit is more than Required, but larger than Minimum. So we just set capcity to Minimum
82-
// Too small, default
83-
if capBytes < MinimumVolumeSizeInBytes {
84-
capBytes = MinimumVolumeSizeInBytes
85-
}
86-
return capBytes, nil
87-
}
88-
8955
func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
9056
// TODO: Check create zone against Driver zone. They must MATCH
9157
glog.Infof("CreateVolume called with request %v", *req)
@@ -213,9 +179,7 @@ func (gceCS *GCEControllerServer) DeleteVolume(ctx context.Context, req *csi.Del
213179

214180
zone, name, err := utils.SplitZoneNameId(volumeID)
215181
if err != nil {
216-
// Cannot find volume associated with this ID because can't even get the name or zone
217-
// This is a success according to the spec
218-
return &csi.DeleteVolumeResponse{}, nil
182+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("DeleteVolume Volume ID could not be split into parts: %v", err))
219183
}
220184

221185
deleteOp, err := gceCS.CloudProvider.DeleteDisk(ctx, zone, name)
@@ -371,31 +335,6 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
371335
return &csi.ControllerUnpublishVolumeResponse{}, nil
372336
}
373337

374-
// TODO: This abstraction isn't great. We shouldn't need diskIsAttached AND diskIsAttachedAndCompatible to duplicate code
375-
func diskIsAttached(volume *compute.Disk, instance *compute.Instance) bool {
376-
for _, disk := range instance.Disks {
377-
if disk.DeviceName == volume.Name {
378-
// Disk is attached to node
379-
return true
380-
}
381-
}
382-
return false
383-
}
384-
385-
func diskIsAttachedAndCompatible(volume *compute.Disk, instance *compute.Instance, volumeCapability *csi.VolumeCapability, readWrite string) (bool, error) {
386-
for _, disk := range instance.Disks {
387-
if disk.DeviceName == volume.Name {
388-
// Disk is attached to node
389-
if disk.Mode != readWrite {
390-
return true, fmt.Errorf("disk mode does not match. Got %v. Want %v", disk.Mode, readWrite)
391-
}
392-
// TODO: Check volume_capability.
393-
return true, nil
394-
}
395-
}
396-
return false, nil
397-
}
398-
399338
func (gceCS *GCEControllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
400339
// TODO: Factor out the volume capability functionality and use as validation in all other functions as well
401340
glog.V(5).Infof("Using default ValidateVolumeCapabilities")
@@ -459,3 +398,62 @@ func (gceCS *GCEControllerServer) DeleteSnapshot(ctx context.Context, req *csi.D
459398
func (gceCS *GCEControllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
460399
return nil, status.Error(codes.Unimplemented, "")
461400
}
401+
402+
func getRequestCapacity(capRange *csi.CapacityRange) (int64, error) {
403+
// TODO: Take another look at these casts/caps. Make sure this func is correct
404+
var capBytes int64
405+
// Default case where nothing is set
406+
if capRange == nil {
407+
capBytes = MinimumVolumeSizeInBytes
408+
return capBytes, nil
409+
}
410+
411+
rBytes := capRange.GetRequiredBytes()
412+
rSet := rBytes > 0
413+
lBytes := capRange.GetLimitBytes()
414+
lSet := lBytes > 0
415+
416+
if lSet && rSet && lBytes < rBytes {
417+
return 0, fmt.Errorf("Limit bytes %v is less than required bytes %v", lBytes, rBytes)
418+
}
419+
if lSet && lBytes < MinimumVolumeSizeInBytes {
420+
return 0, fmt.Errorf("Limit bytes %v is less than minimum volume size: %v", lBytes, MinimumVolumeSizeInBytes)
421+
}
422+
423+
// If Required set just set capacity to that which is Required
424+
if rSet {
425+
capBytes = rBytes
426+
}
427+
428+
// Limit is more than Required, but larger than Minimum. So we just set capcity to Minimum
429+
// Too small, default
430+
if capBytes < MinimumVolumeSizeInBytes {
431+
capBytes = MinimumVolumeSizeInBytes
432+
}
433+
return capBytes, nil
434+
}
435+
436+
// TODO: This abstraction isn't great. We shouldn't need diskIsAttached AND diskIsAttachedAndCompatible to duplicate code
437+
func diskIsAttached(volume *compute.Disk, instance *compute.Instance) bool {
438+
for _, disk := range instance.Disks {
439+
if disk.DeviceName == volume.Name {
440+
// Disk is attached to node
441+
return true
442+
}
443+
}
444+
return false
445+
}
446+
447+
func diskIsAttachedAndCompatible(volume *compute.Disk, instance *compute.Instance, volumeCapability *csi.VolumeCapability, readWrite string) (bool, error) {
448+
for _, disk := range instance.Disks {
449+
if disk.DeviceName == volume.Name {
450+
// Disk is attached to node
451+
if disk.Mode != readWrite {
452+
return true, fmt.Errorf("disk mode does not match. Got %v. Want %v", disk.Mode, readWrite)
453+
}
454+
// TODO: Check volume_capability.
455+
return true, nil
456+
}
457+
}
458+
return false, nil
459+
}

0 commit comments

Comments
 (0)