11package integrationtests
22
33import (
4+ "context"
45 "crypto/md5"
56 "encoding/hex"
67 "fmt"
@@ -23,6 +24,7 @@ import (
2324
2425 "github.com/kubernetes-csi/csi-proxy/pkg/server"
2526 srvtypes "github.com/kubernetes-csi/csi-proxy/pkg/server/types"
27+ "github.com/kubernetes-csi/csi-proxy/pkg/volume"
2628)
2729
2830// startServer starts the proxy's GRPC servers, and returns a function to shut them down when done with testing
@@ -274,6 +276,17 @@ func diskInit(t *testing.T) (*VirtualHardDisk, func()) {
274276 return vhd , cleanup
275277}
276278
279+ // sizeIsAround returns true if the actual size is around the expected size
280+ // (considers the fact that some bytes were lost)
281+ func sizeIsAround (t * testing.T , actualSize , expectedSize int64 ) bool {
282+ // An upper bound on the number of bytes that are lost when creating or resizing a partition
283+ var volumeSizeBytesLoss int64 = (20 * 1024 * 1024 )
284+ var lowerBound = expectedSize - volumeSizeBytesLoss
285+ var upperBound = expectedSize
286+ t .Logf ("Checking that the size is inside the bounds: %d < (actual) %d < %d" , lowerBound , actualSize , upperBound )
287+ return lowerBound <= actualSize && actualSize <= upperBound
288+ }
289+
277290func pathExists (path string ) (bool , error ) {
278291 _ , err := os .Stat (path )
279292 if err == nil {
@@ -284,3 +297,52 @@ func pathExists(path string) (bool, error) {
284297 }
285298 return false , err
286299}
300+
301+ // volumeInit initializes a volume, it creates a VHD, initializes it,
302+ // creates a partition with the max size and formats the volume corresponding to that partition
303+ func volumeInit (volumeClient volume.Interface , t * testing.T ) (* VirtualHardDisk , string , func ()) {
304+ vhd , vhdCleanup := diskInit (t )
305+
306+ listRequest := & volume.ListVolumesOnDiskRequest {
307+ DiskNumber : vhd .DiskNumber ,
308+ }
309+ listResponse , err := volumeClient .ListVolumesOnDisk (context .TODO (), listRequest )
310+ if err != nil {
311+ t .Fatalf ("List response: %v" , err )
312+ }
313+
314+ volumeIDsLen := len (listResponse .VolumeIds )
315+ if volumeIDsLen != 1 {
316+ t .Fatalf ("Number of volumes not equal to 1: %d" , volumeIDsLen )
317+ }
318+ volumeID := listResponse .VolumeIds [0 ]
319+ t .Logf ("VolumeId %v" , volumeID )
320+
321+ isVolumeFormattedRequest := & volume.IsVolumeFormattedRequest {
322+ VolumeId : volumeID ,
323+ }
324+ isVolumeFormattedResponse , err := volumeClient .IsVolumeFormatted (context .TODO (), isVolumeFormattedRequest )
325+ if err != nil {
326+ t .Fatalf ("Is volume formatted request error: %v" , err )
327+ }
328+ if isVolumeFormattedResponse .Formatted {
329+ t .Fatal ("Volume formatted. Unexpected !!" )
330+ }
331+
332+ formatVolumeRequest := & volume.FormatVolumeRequest {
333+ VolumeId : volumeID ,
334+ }
335+ _ , err = volumeClient .FormatVolume (context .TODO (), formatVolumeRequest )
336+ if err != nil {
337+ t .Fatalf ("Volume format failed. Error: %v" , err )
338+ }
339+
340+ isVolumeFormattedResponse , err = volumeClient .IsVolumeFormatted (context .TODO (), isVolumeFormattedRequest )
341+ if err != nil {
342+ t .Fatalf ("Is volume formatted request error: %v" , err )
343+ }
344+ if ! isVolumeFormattedResponse .Formatted {
345+ t .Fatal ("Volume should be formatted. Unexpected !!" )
346+ }
347+ return vhd , volumeID , vhdCleanup
348+ }
0 commit comments