Skip to content

Commit 1e7bd18

Browse files
chore: move volumeInit to utils
1 parent 6602735 commit 1e7bd18

File tree

2 files changed

+64
-66
lines changed

2 files changed

+64
-66
lines changed

integrationtests/utils.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package integrationtests
22

33
import (
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
@@ -295,3 +297,52 @@ func pathExists(path string) (bool, error) {
295297
}
296298
return false, err
297299
}
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+
}

integrationtests/volume_test.go

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -158,55 +158,6 @@ func TestVolumeAPIs(t *testing.T) {
158158
})
159159
}
160160

161-
// volumeInit initializes a volume, it creates a VHD, initializes it,
162-
// creates a partition with the max size and formats the volume corresponding to that partition
163-
func volumeInit(volumeClient volume.Interface, t *testing.T) (*VirtualHardDisk, string, func()) {
164-
vhd, vhdCleanup := diskInit(t)
165-
166-
listRequest := &volume.ListVolumesOnDiskRequest{
167-
DiskNumber: vhd.DiskNumber,
168-
}
169-
listResponse, err := volumeClient.ListVolumesOnDisk(context.TODO(), listRequest)
170-
if err != nil {
171-
t.Fatalf("List response: %v", err)
172-
}
173-
174-
volumeIDsLen := len(listResponse.VolumeIds)
175-
if volumeIDsLen != 1 {
176-
t.Fatalf("Number of volumes not equal to 1: %d", volumeIDsLen)
177-
}
178-
volumeID := listResponse.VolumeIds[0]
179-
t.Logf("VolumeId %v", volumeID)
180-
181-
isVolumeFormattedRequest := &volume.IsVolumeFormattedRequest{
182-
VolumeId: volumeID,
183-
}
184-
isVolumeFormattedResponse, err := volumeClient.IsVolumeFormatted(context.TODO(), isVolumeFormattedRequest)
185-
if err != nil {
186-
t.Fatalf("Is volume formatted request error: %v", err)
187-
}
188-
if isVolumeFormattedResponse.Formatted {
189-
t.Fatal("Volume formatted. Unexpected !!")
190-
}
191-
192-
formatVolumeRequest := &volume.FormatVolumeRequest{
193-
VolumeId: volumeID,
194-
}
195-
_, err = volumeClient.FormatVolume(context.TODO(), formatVolumeRequest)
196-
if err != nil {
197-
t.Fatalf("Volume format failed. Error: %v", err)
198-
}
199-
200-
isVolumeFormattedResponse, err = volumeClient.IsVolumeFormatted(context.TODO(), isVolumeFormattedRequest)
201-
if err != nil {
202-
t.Fatalf("Is volume formatted request error: %v", err)
203-
}
204-
if !isVolumeFormattedResponse.Formatted {
205-
t.Fatal("Volume should be formatted. Unexpected !!")
206-
}
207-
return vhd, volumeID, vhdCleanup
208-
}
209-
210161
func getClosestVolumeFromTargetPathTests(diskClient disk.Interface, volumeClient volume.Interface, t *testing.T) {
211162
t.Run("DriveLetterVolume", func(t *testing.T) {
212163
vhd, _, vhdCleanup := volumeInit(volumeClient, t)
@@ -436,21 +387,6 @@ func mountVolumeTests(diskClient disk.Interface, volumeClient volume.Interface,
436387
}
437388
}
438389

439-
func volumeTests(t *testing.T) {
440-
volumeClient, err := volume.New(volumeapi.New())
441-
require.Nil(t, err)
442-
443-
diskClient, err := disk.New(diskapi.New())
444-
require.Nil(t, err)
445-
446-
t.Run("MountVolume", func(t *testing.T) {
447-
mountVolumeTests(diskClient, volumeClient, t)
448-
})
449-
t.Run("GetClosestVolumeFromTargetPath", func(t *testing.T) {
450-
getClosestVolumeFromTargetPathTests(diskClient, volumeClient, t)
451-
})
452-
}
453-
454390
func TestVolume(t *testing.T) {
455391
t.Run("NegativeDiskTests", func(t *testing.T) {
456392
negativeDiskTests(t)
@@ -463,8 +399,19 @@ func TestVolume(t *testing.T) {
463399
// see https://github.com/actions/virtual-environments/pull/2525
464400

465401
// these tests should be considered frozen from the API point of view
466-
t.Run("volumeTests", func(t *testing.T) {
402+
volumeClient, err := volume.New(volumeapi.New())
403+
require.Nil(t, err)
404+
405+
diskClient, err := disk.New(diskapi.New())
406+
require.Nil(t, err)
407+
408+
t.Run("MountVolume", func(t *testing.T) {
467409
skipTestOnCondition(t, isRunningOnGhActions())
468-
volumeTests(t)
410+
mountVolumeTests(diskClient, volumeClient, t)
411+
})
412+
413+
t.Run("GetClosestVolumeFromTargetPath", func(t *testing.T) {
414+
skipTestOnCondition(t, isRunningOnGhActions())
415+
getClosestVolumeFromTargetPathTests(diskClient, volumeClient, t)
469416
})
470417
}

0 commit comments

Comments
 (0)