generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 61
/
volume_test.go
378 lines (322 loc) · 12.2 KB
/
volume_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package integrationtests
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
disk "github.com/kubernetes-csi/csi-proxy/v2/pkg/disk"
diskapi "github.com/kubernetes-csi/csi-proxy/v2/pkg/disk/hostapi"
volume "github.com/kubernetes-csi/csi-proxy/v2/pkg/volume"
volumeapi "github.com/kubernetes-csi/csi-proxy/v2/pkg/volume/hostapi"
"github.com/stretchr/testify/require"
)
func TestVolume(t *testing.T) {
t.Run("NegativeVolumeTests", func(t *testing.T) {
negativeVolumeTests(t)
})
// TODO: These tests will fail on Github Actions because Hyper-V is disabled
// see https://github.com/actions/virtual-environments/pull/2525
// these tests should be considered frozen from the API point of view
volumeClient, err := volume.New(volumeapi.New())
require.Nil(t, err)
diskClient, err := disk.New(diskapi.New())
require.Nil(t, err)
t.Run("MountVolume", func(t *testing.T) {
skipTestOnCondition(t, isRunningOnGhActions())
mountVolumeTests(diskClient, volumeClient, t)
})
t.Run("GetClosestVolumeFromTargetPath", func(t *testing.T) {
skipTestOnCondition(t, isRunningOnGhActions())
getClosestVolumeFromTargetPathTests(diskClient, volumeClient, t)
})
}
func runNegativeListVolumeRequest(t *testing.T, client volume.Interface, diskNum uint32) {
listRequest := &volume.ListVolumesOnDiskRequest{
DiskNumber: diskNum,
}
_, err := client.ListVolumesOnDisk(context.TODO(), listRequest)
if err == nil {
t.Fatalf("Empty error. Expected error for disknum:%d", diskNum)
}
}
func runNegativeIsVolumeFormattedRequest(t *testing.T, client volume.Interface, volumeID string) {
isVolumeFormattedRequest := &volume.IsVolumeFormattedRequest{
VolumeID: volumeID,
}
_, err := client.IsVolumeFormatted(context.TODO(), isVolumeFormattedRequest)
if err == nil {
t.Fatalf("Empty error. Expected error for volumeID: %s", volumeID)
}
}
func runNegativeFormatVolumeRequest(t *testing.T, client volume.Interface, volumeID string) {
formatVolumeRequest := &volume.FormatVolumeRequest{
VolumeID: volumeID,
}
_, err := client.FormatVolume(context.TODO(), formatVolumeRequest)
if err == nil {
t.Fatalf("Empty error. Expected error for volume id: %s", volumeID)
}
}
func runNegativeResizeVolumeRequest(t *testing.T, client volume.Interface, volumeID string, size int64) {
resizeVolumeRequest := &volume.ResizeVolumeRequest{
VolumeID: volumeID,
SizeBytes: size,
}
_, err := client.ResizeVolume(context.TODO(), resizeVolumeRequest)
if err == nil {
t.Fatalf("Error empty for volume resize. Volume: %s, Size: %d", volumeID, size)
}
}
func runNegativeMountVolumeRequest(t *testing.T, client volume.Interface, volumeID, targetPath string) {
// Mount the volume
mountVolumeRequest := &volume.MountVolumeRequest{
VolumeID: volumeID,
TargetPath: targetPath,
}
_, err := client.MountVolume(context.TODO(), mountVolumeRequest)
if err == nil {
t.Fatalf("Error empty for volume(%s) mount to path %s.", volumeID, targetPath)
}
}
func runNegativeUnmountVolumeRequest(t *testing.T, client volume.Interface, volumeID, targetPath string) {
// Unmount the volume
unmountVolumeRequest := &volume.UnmountVolumeRequest{
VolumeID: volumeID,
TargetPath: targetPath,
}
_, err := client.UnmountVolume(context.TODO(), unmountVolumeRequest)
if err == nil {
t.Fatalf("Empty error. Volume id %s dismount from path %s ", volumeID, targetPath)
}
}
func runNegativeVolumeStatsRequest(t *testing.T, client volume.Interface, volumeID string) {
// Get VolumeStats
volumeStatsRequest := &volume.GetVolumeStatsRequest{
VolumeID: volumeID,
}
_, err := client.GetVolumeStats(context.TODO(), volumeStatsRequest)
if err == nil {
t.Errorf("Empty error. VolumeStats for id %s", volumeID)
}
}
func negativeVolumeTests(t *testing.T) {
client, err := volume.New(volumeapi.New())
require.Nil(t, err)
// Empty volume test
runNegativeIsVolumeFormattedRequest(t, client, "")
// -ve volume id
runNegativeIsVolumeFormattedRequest(t, client, "-1")
// Format volume negative tests
runNegativeFormatVolumeRequest(t, client, "")
runNegativeFormatVolumeRequest(t, client, "-1")
// Resize volume negative tests
runNegativeResizeVolumeRequest(t, client, "", 2*1024*1024)
runNegativeResizeVolumeRequest(t, client, "-1", 2*1024*1024)
// Mount volume negative tests
runNegativeMountVolumeRequest(t, client, "", "")
runNegativeMountVolumeRequest(t, client, "-1", "")
// Unmount volume negative tests
runNegativeUnmountVolumeRequest(t, client, "", "")
runNegativeUnmountVolumeRequest(t, client, "-1", "")
runNegativeVolumeStatsRequest(t, client, "")
runNegativeVolumeStatsRequest(t, client, "-1")
}
func getClosestVolumeFromTargetPathTests(diskClient disk.Interface, volumeClient volume.Interface, t *testing.T) {
t.Run("DriveLetterVolume", func(t *testing.T) {
vhd, _, vhdCleanup := volumeInit(volumeClient, t)
defer vhdCleanup()
// vhd.Mount dir exists, because there are no volumes above it should return the C:\ volume
request := &volume.GetClosestVolumeIDFromTargetPathRequest{
TargetPath: vhd.Mount,
}
response, err := volumeClient.GetClosestVolumeIDFromTargetPath(context.TODO(), request)
if err != nil {
t.Fatalf("GetClosestVolumeIDFromTargetPath request error, err=%v", err)
}
// the C drive volume
targetb, err := runPowershellCmd(t, `(Get-Partition -DriveLetter C | Get-Volume).UniqueId`)
if err != nil {
t.Fatalf("Failed to get the C: drive volume")
}
cDriveVolume := strings.TrimSpace(string(targetb))
if response.VolumeID != cDriveVolume {
t.Fatalf("The volume from GetClosestVolumeIDFromTargetPath doesn't match the C: drive volume")
}
})
t.Run("AncestorVolumeFromNestedDirectory", func(t *testing.T) {
var err error
vhd, volumeID, vhdCleanup := volumeInit(volumeClient, t)
defer vhdCleanup()
// Mount the volume
mountVolumeRequest := &volume.MountVolumeRequest{
VolumeID: volumeID,
TargetPath: vhd.Mount,
}
_, err = volumeClient.MountVolume(context.TODO(), mountVolumeRequest)
if err != nil {
t.Fatalf("Volume id %s mount to path %s failed. Error: %v", volumeID, vhd.Mount, err)
}
// Unmount the volume
defer func() {
unmountVolumeRequest := &volume.UnmountVolumeRequest{
VolumeID: volumeID,
TargetPath: vhd.Mount,
}
_, err = volumeClient.UnmountVolume(context.TODO(), unmountVolumeRequest)
if err != nil {
t.Fatalf("Volume id %s mount to path %s failed. Error: %v", volumeID, vhd.Mount, err)
}
}()
nestedDirectory := filepath.Join(vhd.Mount, "foo/bar")
err = os.MkdirAll(nestedDirectory, os.ModeDir)
if err != nil {
t.Fatalf("Failed to create directory=%s", nestedDirectory)
}
// the volume returned should be the VHD volume
request := &volume.GetClosestVolumeIDFromTargetPathRequest{
TargetPath: nestedDirectory,
}
response, err := volumeClient.GetClosestVolumeIDFromTargetPath(context.TODO(), request)
if err != nil {
t.Fatalf("GetClosestVolumeIDFromTargetPath request error, err=%v", err)
}
if response.VolumeID != volumeID {
t.Fatalf("The volume from GetClosestVolumeIDFromTargetPath doesn't match the VHD volume=%s", volumeID)
}
})
t.Run("SymlinkToVolume", func(t *testing.T) {
var err error
vhd, volumeID, vhdCleanup := volumeInit(volumeClient, t)
defer vhdCleanup()
// Mount the volume
mountVolumeRequest := &volume.MountVolumeRequest{
VolumeID: volumeID,
TargetPath: vhd.Mount,
}
_, err = volumeClient.MountVolume(context.TODO(), mountVolumeRequest)
if err != nil {
t.Fatalf("Volume id %s mount to path %s failed. Error: %v", volumeID, vhd.Mount, err)
}
// Unmount the volume
defer func() {
unmountVolumeRequest := &volume.UnmountVolumeRequest{
VolumeID: volumeID,
TargetPath: vhd.Mount,
}
_, err = volumeClient.UnmountVolume(context.TODO(), unmountVolumeRequest)
if err != nil {
t.Fatalf("Volume id %s mount to path %s failed. Error: %v", volumeID, vhd.Mount, err)
}
}()
testPluginPath, _ := getTestPluginPath()
err = os.MkdirAll(testPluginPath, os.ModeDir)
if err != nil {
t.Fatalf("Failed to create directory=%s", testPluginPath)
}
sourceSymlink := filepath.Join(testPluginPath, "source")
err = os.Symlink(vhd.Mount, sourceSymlink)
if err != nil {
t.Fatalf("Failed to create the symlink=%s", sourceSymlink)
}
// the volume returned should be the VHD volume
var request *volume.GetClosestVolumeIDFromTargetPathRequest
var response *volume.GetClosestVolumeIDFromTargetPathResponse
request = &volume.GetClosestVolumeIDFromTargetPathRequest{
TargetPath: sourceSymlink,
}
response, err = volumeClient.GetClosestVolumeIDFromTargetPath(context.TODO(), request)
if err != nil {
t.Fatalf("GetClosestVolumeIDFromTargetPath request error, err=%v", err)
}
if response.VolumeID != volumeID {
t.Fatalf("The volume from GetClosestVolumeIDFromTargetPath doesn't match the VHD volume=%s", volumeID)
}
})
}
func mountVolumeTests(diskClient disk.Interface, volumeClient volume.Interface, t *testing.T) {
vhd, volumeID, vhdCleanup := volumeInit(volumeClient, t)
defer vhdCleanup()
volumeStatsRequest := &volume.GetVolumeStatsRequest{
VolumeID: volumeID,
}
volumeStatsResponse, err := volumeClient.GetVolumeStats(context.TODO(), volumeStatsRequest)
if err != nil {
t.Fatalf("VolumeStats request error: %v", err)
}
// For a volume formatted with 1GB it should be around 1GB, in practice it was 1056947712 bytes or 0.9844GB
// let's compare with a range of +- 20MB
if !sizeIsAround(t, volumeStatsResponse.TotalBytes, vhd.InitialSize) {
t.Fatalf("volumeStatsResponse.TotalBytes reported is not valid, it is %v", volumeStatsResponse.TotalBytes)
}
// Resize the disk to twice its size (from 1GB to 2GB)
// To resize a volume we need to resize the virtual hard disk first and then the partition
cmd := fmt.Sprintf("Resize-VHD -Path %s -SizeBytes %d", vhd.Path, int64(vhd.InitialSize*2))
if out, err := runPowershellCmd(t, cmd); err != nil {
t.Fatalf("Error: %v. Command: %q. Out: %s.", err, cmd, out)
}
// Resize the volume to 1.5GB
oldVolumeSize := volumeStatsResponse.TotalBytes
newVolumeSize := int64(float32(oldVolumeSize) * 1.5)
// This is the max partition size when doing a resize to 2GB
//
// Get-PartitionSupportedSize -DiskNumber 7 -PartitionNumber 2 | ConvertTo-Json
// {
// "SizeMin": 404725760,
// "SizeMax": 2130689536
// }
resizeVolumeRequest := &volume.ResizeVolumeRequest{
VolumeID: volumeID,
// resize the partition to 1.5x times instead
SizeBytes: newVolumeSize,
}
t.Logf("Attempt to resize volume from sizeBytes=%d to sizeBytes=%d", oldVolumeSize, newVolumeSize)
_, err = volumeClient.ResizeVolume(context.TODO(), resizeVolumeRequest)
if err != nil {
t.Fatalf("Volume resize request failed. Error: %v", err)
}
volumeStatsResponse, err = volumeClient.GetVolumeStats(context.TODO(), volumeStatsRequest)
if err != nil {
t.Fatalf("VolumeStats request after resize error: %v", err)
}
// resizing from 1GB to approximately 1.5GB
if !sizeIsAround(t, volumeStatsResponse.TotalBytes, newVolumeSize) {
t.Fatalf("VolumeSize reported should be greater than the old size, it is %v", volumeStatsResponse.TotalBytes)
}
volumeDiskNumberRequest := &volume.GetDiskNumberFromVolumeIDRequest{
VolumeID: volumeID,
}
volumeDiskNumberResponse, err := volumeClient.GetDiskNumberFromVolumeID(context.TODO(), volumeDiskNumberRequest)
if err != nil {
t.Fatalf("GetDiskNumberFromVolumeID failed: %v", err)
}
diskStatsRequest := &disk.GetDiskStatsRequest{
DiskNumber: volumeDiskNumberResponse.DiskNumber,
}
diskStatsResponse, err := diskClient.GetDiskStats(context.TODO(), diskStatsRequest)
if err != nil {
t.Fatalf("DiskStats request error: %v", err)
}
if diskStatsResponse.TotalBytes < 0 {
t.Fatalf("Invalid disk size was returned %v", diskStatsResponse.TotalBytes)
}
// Mount the volume
mountVolumeRequest := &volume.MountVolumeRequest{
VolumeID: volumeID,
TargetPath: vhd.Mount,
}
_, err = volumeClient.MountVolume(context.TODO(), mountVolumeRequest)
if err != nil {
t.Fatalf("Volume id %s mount to path %s failed. Error: %v", volumeID, vhd.Mount, err)
}
// Unmount the volume
unmountVolumeRequest := &volume.UnmountVolumeRequest{
VolumeID: volumeID,
TargetPath: vhd.Mount,
}
_, err = volumeClient.UnmountVolume(context.TODO(), unmountVolumeRequest)
if err != nil {
t.Fatalf("Volume id %s mount to path %s failed. Error: %v", volumeID, vhd.Mount, err)
}
}