Skip to content
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
62 changes: 62 additions & 0 deletions integrationtests/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integrationtests

import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
Expand All @@ -23,6 +24,7 @@ import (

"github.com/kubernetes-csi/csi-proxy/pkg/server"
srvtypes "github.com/kubernetes-csi/csi-proxy/pkg/server/types"
"github.com/kubernetes-csi/csi-proxy/pkg/volume"
)

// startServer starts the proxy's GRPC servers, and returns a function to shut them down when done with testing
Expand Down Expand Up @@ -274,6 +276,17 @@ func diskInit(t *testing.T) (*VirtualHardDisk, func()) {
return vhd, cleanup
}

// sizeIsAround returns true if the actual size is around the expected size
// (considers the fact that some bytes were lost)
func sizeIsAround(t *testing.T, actualSize, expectedSize int64) bool {
// An upper bound on the number of bytes that are lost when creating or resizing a partition
var volumeSizeBytesLoss int64 = (20 * 1024 * 1024)
var lowerBound = expectedSize - volumeSizeBytesLoss
var upperBound = expectedSize
t.Logf("Checking that the size is inside the bounds: %d < (actual) %d < %d", lowerBound, actualSize, upperBound)
return lowerBound <= actualSize && actualSize <= upperBound
}

func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
Expand All @@ -284,3 +297,52 @@ func pathExists(path string) (bool, error) {
}
return false, err
}

// volumeInit initializes a volume, it creates a VHD, initializes it,
// creates a partition with the max size and formats the volume corresponding to that partition
func volumeInit(volumeClient volume.Interface, t *testing.T) (*VirtualHardDisk, string, func()) {
vhd, vhdCleanup := diskInit(t)

listRequest := &volume.ListVolumesOnDiskRequest{
DiskNumber: vhd.DiskNumber,
}
listResponse, err := volumeClient.ListVolumesOnDisk(context.TODO(), listRequest)
if err != nil {
t.Fatalf("List response: %v", err)
}

volumeIDsLen := len(listResponse.VolumeIds)
if volumeIDsLen != 1 {
t.Fatalf("Number of volumes not equal to 1: %d", volumeIDsLen)
}
volumeID := listResponse.VolumeIds[0]
t.Logf("VolumeId %v", volumeID)

isVolumeFormattedRequest := &volume.IsVolumeFormattedRequest{
VolumeId: volumeID,
}
isVolumeFormattedResponse, err := volumeClient.IsVolumeFormatted(context.TODO(), isVolumeFormattedRequest)
if err != nil {
t.Fatalf("Is volume formatted request error: %v", err)
}
if isVolumeFormattedResponse.Formatted {
t.Fatal("Volume formatted. Unexpected !!")
}

formatVolumeRequest := &volume.FormatVolumeRequest{
VolumeId: volumeID,
}
_, err = volumeClient.FormatVolume(context.TODO(), formatVolumeRequest)
if err != nil {
t.Fatalf("Volume format failed. Error: %v", err)
}

isVolumeFormattedResponse, err = volumeClient.IsVolumeFormatted(context.TODO(), isVolumeFormattedRequest)
if err != nil {
t.Fatalf("Is volume formatted request error: %v", err)
}
if !isVolumeFormattedResponse.Formatted {
t.Fatal("Volume should be formatted. Unexpected !!")
}
return vhd, volumeID, vhdCleanup
}
Loading