Skip to content

Commit 890790c

Browse files
authored
Merge pull request #58 from davidz627/feature/controllerTests
Added controller unit tests
2 parents f0b6d63 + 2ccf1ac commit 890790c

File tree

5 files changed

+362
-140
lines changed

5 files changed

+362
-140
lines changed

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

+58-59
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)
@@ -371,31 +337,6 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
371337
return &csi.ControllerUnpublishVolumeResponse{}, nil
372338
}
373339

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-
399340
func (gceCS *GCEControllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
400341
// TODO: Factor out the volume capability functionality and use as validation in all other functions as well
401342
glog.V(5).Infof("Using default ValidateVolumeCapabilities")
@@ -459,3 +400,61 @@ func (gceCS *GCEControllerServer) DeleteSnapshot(ctx context.Context, req *csi.D
459400
func (gceCS *GCEControllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
460401
return nil, status.Error(codes.Unimplemented, "")
461402
}
403+
404+
func getRequestCapacity(capRange *csi.CapacityRange) (int64, error) {
405+
// TODO: Take another look at these casts/caps. Make sure this func is correct
406+
var capBytes int64
407+
// Default case where nothing is set
408+
if capRange == nil {
409+
capBytes = MinimumVolumeSizeInBytes
410+
return capBytes, nil
411+
}
412+
413+
rBytes := capRange.GetRequiredBytes()
414+
rSet := rBytes > 0
415+
lBytes := capRange.GetLimitBytes()
416+
lSet := lBytes > 0
417+
418+
if lSet && rSet && lBytes < rBytes {
419+
return 0, fmt.Errorf("Limit bytes %v is less than required bytes %v", lBytes, rBytes)
420+
}
421+
if lSet && lBytes < MinimumVolumeSizeInBytes {
422+
return 0, fmt.Errorf("Limit bytes %v is less than minimum volume size: %v", lBytes, MinimumVolumeSizeInBytes)
423+
}
424+
425+
// If Required set just set capacity to that which is Required
426+
if rSet {
427+
capBytes = rBytes
428+
}
429+
430+
// Limit is more than Required, but larger than Minimum. So we just set capcity to Minimum
431+
// Too small, default
432+
if capBytes < MinimumVolumeSizeInBytes {
433+
capBytes = MinimumVolumeSizeInBytes
434+
}
435+
return capBytes, nil
436+
}
437+
438+
func diskIsAttached(volume *compute.Disk, instance *compute.Instance) bool {
439+
for _, disk := range instance.Disks {
440+
if disk.DeviceName == volume.Name {
441+
// Disk is attached to node
442+
return true
443+
}
444+
}
445+
return false
446+
}
447+
448+
func diskIsAttachedAndCompatible(volume *compute.Disk, instance *compute.Instance, volumeCapability *csi.VolumeCapability, readWrite string) (bool, error) {
449+
for _, disk := range instance.Disks {
450+
if disk.DeviceName == volume.Name {
451+
// Disk is attached to node
452+
if disk.Mode != readWrite {
453+
return true, fmt.Errorf("disk mode does not match. Got %v. Want %v", disk.Mode, readWrite)
454+
}
455+
// TODO: Check volume_capability.
456+
return true, nil
457+
}
458+
}
459+
return false, nil
460+
}

0 commit comments

Comments
 (0)