-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_zone_e2e_test.go
647 lines (525 loc) · 22.8 KB
/
single_zone_e2e_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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tests
import (
"context"
"fmt"
"strings"
"time"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/remote"
csi "github.com/container-storage-interface/spec/lib/go/csi"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"google.golang.org/api/iterator"
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
fieldmask "google.golang.org/genproto/protobuf/field_mask"
)
const (
testNamePrefix = "gcepd-csi-e2e-"
defaultSizeGb int64 = 5
defaultRepdSizeGb int64 = 200
readyState = "READY"
standardDiskType = "pd-standard"
ssdDiskType = "pd-ssd"
defaultVolumeLimit int64 = 127
defaultEpsilon = 500000000 // 500M
)
var _ = Describe("GCE PD CSI Driver", func() {
It("Should get reasonable volume limits from nodes with NodeGetInfo", func() {
testContext := getRandomTestContext()
resp, err := testContext.Client.NodeGetInfo()
Expect(err).To(BeNil())
volumeLimit := resp.GetMaxVolumesPerNode()
Expect(volumeLimit).To(Equal(defaultVolumeLimit))
})
It("Should create->attach->stage->mount volume and check if it is writable, then unmount->unstage->detach->delete and check disk is deleted", func() {
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
instance := testContext.Instance
// Create Disk
volName, volID := createAndValidateUniqueZonalDisk(client, p, z)
defer func() {
// Delete Disk
err := client.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeService.Disks.Get(p, z, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
}()
// Attach Disk
err := testAttachWriteReadDetach(volID, volName, instance, client, false /* readOnly */)
Expect(err).To(BeNil(), "Failed to go through volume lifecycle")
})
It("Should create disks in correct zones when topology is specified", func() {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
p, _, _ := testContext.Instance.GetIdentity()
zones := []string{"us-central1-c", "us-central1-b", "us-central1-a"}
for _, zone := range zones {
volName := testNamePrefix + string(uuid.NewUUID())
topReq := &csi.TopologyRequirement{
Requisite: []*csi.Topology{
{
Segments: map[string]string{common.TopologyKeyZone: zone},
},
},
}
volID, err := testContext.Client.CreateVolume(volName, nil, defaultSizeGb, topReq)
Expect(err).To(BeNil(), "Failed to create volume")
defer func() {
err = testContext.Client.DeleteVolume(volID)
Expect(err).To(BeNil(), "Failed to delete volume")
}()
_, err = computeService.Disks.Get(p, zone, volName).Do()
Expect(err).To(BeNil(), "Could not find disk in correct zone")
}
})
It("Should complete entire disk lifecycle with underspecified volume ID", func() {
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
instance := testContext.Instance
volName, _ := createAndValidateUniqueZonalDisk(client, p, z)
underSpecifiedID := common.GenerateUnderspecifiedVolumeID(volName, true /* isZonal */)
defer func() {
// Delete Disk
err := client.DeleteVolume(underSpecifiedID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeService.Disks.Get(p, z, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
}()
// Attach Disk
err := testAttachWriteReadDetach(underSpecifiedID, volName, instance, client, false /* readOnly */)
Expect(err).To(BeNil(), "Failed to go through volume lifecycle")
})
It("Should successfully create RePD in two zones in the drivers region when none are specified", func() {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
controllerInstance := testContext.Instance
controllerClient := testContext.Client
p, z, _ := controllerInstance.GetIdentity()
region, err := common.GetRegionFromZones([]string{z})
Expect(err).To(BeNil(), "Failed to get region from zones")
// Create Disk
volName := testNamePrefix + string(uuid.NewUUID())
volID, err := controllerClient.CreateVolume(volName, map[string]string{
common.ParameterKeyReplicationType: "regional-pd",
}, defaultRepdSizeGb, nil)
Expect(err).To(BeNil(), "CreateVolume failed with error: %v", err)
// Validate Disk Created
cloudDisk, err := computeService.RegionDisks.Get(p, region, volName).Do()
Expect(err).To(BeNil(), "Could not get disk from cloud directly")
Expect(cloudDisk.Type).To(ContainSubstring(standardDiskType))
Expect(cloudDisk.Status).To(Equal(readyState))
Expect(cloudDisk.SizeGb).To(Equal(defaultRepdSizeGb))
Expect(cloudDisk.Name).To(Equal(volName))
Expect(len(cloudDisk.ReplicaZones)).To(Equal(2))
for _, replicaZone := range cloudDisk.ReplicaZones {
tokens := strings.Split(replicaZone, "/")
actualZone := tokens[len(tokens)-1]
gotRegion, err := common.GetRegionFromZones([]string{actualZone})
Expect(err).To(BeNil(), "failed to get region from actual zone %v", actualZone)
Expect(gotRegion).To(Equal(region), "Got region from replica zone that did not match supplied region")
}
defer func() {
// Delete Disk
controllerClient.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeService.RegionDisks.Get(p, region, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
}()
})
It("Should create and delete disk with default zone", func() {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
// Create Disk
volName := testNamePrefix + string(uuid.NewUUID())
volID, err := client.CreateVolume(volName, nil, defaultSizeGb, nil)
Expect(err).To(BeNil(), "CreateVolume failed with error: %v", err)
// Validate Disk Created
cloudDisk, err := computeService.Disks.Get(p, z, volName).Do()
Expect(err).To(BeNil(), "Could not get disk from cloud directly")
Expect(cloudDisk.Type).To(ContainSubstring(standardDiskType))
Expect(cloudDisk.Status).To(Equal(readyState))
Expect(cloudDisk.SizeGb).To(Equal(defaultSizeGb))
Expect(cloudDisk.Name).To(Equal(volName))
defer func() {
// Delete Disk
client.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeService.Disks.Get(p, z, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
}()
})
// Test volume already exists idempotency
// Test volume with op pending
It("Should create and delete snapshot for the volume with default zone", func() {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
volName, volID := createAndValidateUniqueZonalDisk(client, p, z)
// Create Snapshot
snapshotName := testNamePrefix + string(uuid.NewUUID())
snapshotID, err := client.CreateSnapshot(snapshotName, volID, nil)
Expect(err).To(BeNil(), "CreateSnapshot failed with error: %v", err)
// Validate Snapshot Created
snapshot, err := computeService.Snapshots.Get(p, snapshotName).Do()
Expect(err).To(BeNil(), "Could not get snapshot from cloud directly")
Expect(snapshot.Name).To(Equal(snapshotName))
err = wait.Poll(10*time.Second, 3*time.Minute, func() (bool, error) {
snapshot, err := computeService.Snapshots.Get(p, snapshotName).Do()
Expect(err).To(BeNil(), "Could not get snapshot from cloud directly")
if snapshot.Status == "READY" {
return true, nil
}
return false, nil
})
Expect(err).To(BeNil(), "Could not wait for snapshot be ready")
defer func() {
// Delete Disk
err := client.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeService.Disks.Get(p, z, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
// Delete Snapshot
err = client.DeleteSnapshot(snapshotID)
Expect(err).To(BeNil(), "DeleteSnapshot failed")
// Validate Snapshot Deleted
_, err = computeService.Snapshots.Get(p, snapshotName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected snapshot to not be found")
}()
})
It("Should create CMEK key, go through volume lifecycle, validate behavior on key revoke and restore", func() {
ctx := context.Background()
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
controllerInstance := testContext.Instance
controllerClient := testContext.Client
p, z, _ := controllerInstance.GetIdentity()
locationID := "global"
// The resource name of the key rings.
parentName := fmt.Sprintf("projects/%s/locations/%s", p, locationID)
keyRingId := "gce-pd-csi-test-ring"
// Create KeyRing
ringReq := &kmspb.CreateKeyRingRequest{
Parent: parentName,
KeyRingId: keyRingId,
}
keyRing, err := kmsClient.CreateKeyRing(ctx, ringReq)
if !gce.IsGCEError(err, "alreadyExists") {
getKeyRingReq := &kmspb.GetKeyRingRequest{
Name: fmt.Sprintf("%s/keyRings/%s", parentName, keyRingId),
}
keyRing, err = kmsClient.GetKeyRing(ctx, getKeyRingReq)
}
Expect(err).To(BeNil(), "Failed to create or get key ring %v", keyRingId)
// Create CryptoKey in KeyRing
keyId := "test-key-" + string(uuid.NewUUID())
keyReq := &kmspb.CreateCryptoKeyRequest{
Parent: keyRing.Name,
CryptoKeyId: keyId,
CryptoKey: &kmspb.CryptoKey{
Purpose: kmspb.CryptoKey_ENCRYPT_DECRYPT,
VersionTemplate: &kmspb.CryptoKeyVersionTemplate{
Algorithm: kmspb.CryptoKeyVersion_GOOGLE_SYMMETRIC_ENCRYPTION,
},
},
}
key, err := kmsClient.CreateCryptoKey(ctx, keyReq)
Expect(err).To(BeNil(), "Failed to create crypto key %v in key ring %v", keyId, keyRing.Name)
keyVersions := []string{}
keyVersionReq := &kmspb.ListCryptoKeyVersionsRequest{
Parent: key.Name,
}
it := kmsClient.ListCryptoKeyVersions(ctx, keyVersionReq)
for {
keyVersion, err := it.Next()
if err == iterator.Done {
break
}
Expect(err).To(BeNil(), "Failed to list crypto key versions")
keyVersions = append(keyVersions, keyVersion.Name)
}
// Defer deletion of all key versions
// https://cloud.google.com/kms/docs/destroy-restore
defer func() {
for _, keyVersion := range keyVersions {
destroyKeyReq := &kmspb.DestroyCryptoKeyVersionRequest{
Name: keyVersion,
}
_, err = kmsClient.DestroyCryptoKeyVersion(ctx, destroyKeyReq)
Expect(err).To(BeNil(), "Failed to destroy crypto key version: %v", keyVersion)
}
}()
// Go through volume lifecycle using CMEK-ed PD
// Create Disk
volName := testNamePrefix + string(uuid.NewUUID())
volID, err := controllerClient.CreateVolume(volName, map[string]string{
common.ParameterKeyDiskEncryptionKmsKey: key.Name,
}, defaultSizeGb,
&csi.TopologyRequirement{
Requisite: []*csi.Topology{
{
Segments: map[string]string{common.TopologyKeyZone: z},
},
},
})
Expect(err).To(BeNil(), "CreateVolume failed with error: %v", err)
// Validate Disk Created
cloudDisk, err := computeService.Disks.Get(p, z, volName).Do()
Expect(err).To(BeNil(), "Could not get disk from cloud directly")
Expect(cloudDisk.Type).To(ContainSubstring(standardDiskType))
Expect(cloudDisk.Status).To(Equal(readyState))
Expect(cloudDisk.SizeGb).To(Equal(defaultSizeGb))
Expect(cloudDisk.Name).To(Equal(volName))
defer func() {
// Delete Disk
err = controllerClient.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeService.Disks.Get(p, z, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
}()
// Test disk works
err = testAttachWriteReadDetach(volID, volName, controllerInstance, controllerClient, false /* readOnly */)
Expect(err).To(BeNil(), "Failed to go through volume lifecycle before revoking CMEK key")
// Revoke CMEK key
// https://cloud.google.com/kms/docs/enable-disable
for _, keyVersion := range keyVersions {
disableReq := &kmspb.UpdateCryptoKeyVersionRequest{
CryptoKeyVersion: &kmspb.CryptoKeyVersion{
Name: keyVersion,
State: kmspb.CryptoKeyVersion_DISABLED,
},
UpdateMask: &fieldmask.FieldMask{
Paths: []string{"state"},
},
}
_, err = kmsClient.UpdateCryptoKeyVersion(ctx, disableReq)
Expect(err).To(BeNil(), "Failed to disable crypto key")
}
// Make sure attach of PD fails
err = testAttachWriteReadDetach(volID, volName, controllerInstance, controllerClient, false /* readOnly */)
Expect(err).ToNot(BeNil(), "Volume lifecycle should have failed, but succeeded")
// Restore CMEK key
for _, keyVersion := range keyVersions {
enableReq := &kmspb.UpdateCryptoKeyVersionRequest{
CryptoKeyVersion: &kmspb.CryptoKeyVersion{
Name: keyVersion,
State: kmspb.CryptoKeyVersion_ENABLED,
},
UpdateMask: &fieldmask.FieldMask{
Paths: []string{"state"},
},
}
_, err = kmsClient.UpdateCryptoKeyVersion(ctx, enableReq)
Expect(err).To(BeNil(), "Failed to enable crypto key")
}
// Make sure attach of PD succeeds
err = testAttachWriteReadDetach(volID, volName, controllerInstance, controllerClient, false /* readOnly */)
Expect(err).To(BeNil(), "Failed to go through volume lifecycle after restoring CMEK key")
})
It("Should create disks, attach them places, and verify List returns correct results", func() {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
nodeID := testContext.Instance.GetNodeID()
_, volID := createAndValidateUniqueZonalDisk(client, p, z)
defer deleteVolumeOrError(client, volID, p)
_, secondVolID := createAndValidateUniqueZonalDisk(client, p, z)
defer deleteVolumeOrError(client, secondVolID, p)
// Attach volID to current instance
err := client.ControllerPublishVolume(volID, nodeID)
Expect(err).To(BeNil(), "Failed ControllerPublishVolume")
defer client.ControllerUnpublishVolume(volID, nodeID)
// List Volumes
volsToNodes, err := client.ListVolumes()
Expect(err).To(BeNil(), "Failed ListVolumes")
// Verify
Expect(volsToNodes[volID]).ToNot(BeNil(), "Couldn't find attached nodes for vol")
Expect(volsToNodes[volID]).To(ContainElement(nodeID), "Couldn't find node in attached nodes for vol")
Expect(volsToNodes[secondVolID]).To(BeNil(), "Second vol ID attached nodes not nil")
})
It("Should create and delete snapshot for RePD in two zones ", func() {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
controllerInstance := testContext.Instance
controllerClient := testContext.Client
p, z, _ := controllerInstance.GetIdentity()
region, err := common.GetRegionFromZones([]string{z})
Expect(err).To(BeNil(), "Failed to get region from zones")
// Create Disk
volName := testNamePrefix + string(uuid.NewUUID())
volID, err := controllerClient.CreateVolume(volName, map[string]string{
common.ParameterKeyReplicationType: "regional-pd",
}, defaultRepdSizeGb, nil)
Expect(err).To(BeNil(), "CreateVolume failed with error: %v", err)
// Validate Disk Created
cloudDisk, err := computeService.RegionDisks.Get(p, region, volName).Do()
Expect(err).To(BeNil(), "Could not get disk from cloud directly")
Expect(cloudDisk.Type).To(ContainSubstring(standardDiskType))
Expect(cloudDisk.Status).To(Equal(readyState))
Expect(cloudDisk.SizeGb).To(Equal(defaultRepdSizeGb))
Expect(cloudDisk.Name).To(Equal(volName))
Expect(len(cloudDisk.ReplicaZones)).To(Equal(2))
for _, replicaZone := range cloudDisk.ReplicaZones {
tokens := strings.Split(replicaZone, "/")
actualZone := tokens[len(tokens)-1]
gotRegion, err := common.GetRegionFromZones([]string{actualZone})
Expect(err).To(BeNil(), "failed to get region from actual zone %v", actualZone)
Expect(gotRegion).To(Equal(region), "Got region from replica zone that did not match supplied region")
}
// Create Snapshot
snapshotName := testNamePrefix + string(uuid.NewUUID())
snapshotID, err := controllerClient.CreateSnapshot(snapshotName, volID, nil)
Expect(err).To(BeNil(), "CreateSnapshot failed with error: %v", err)
// Validate Snapshot Created
snapshot, err := computeService.Snapshots.Get(p, snapshotName).Do()
Expect(err).To(BeNil(), "Could not get snapshot from cloud directly")
Expect(snapshot.Name).To(Equal(snapshotName))
err = wait.Poll(10*time.Second, 3*time.Minute, func() (bool, error) {
snapshot, err := computeService.Snapshots.Get(p, snapshotName).Do()
Expect(err).To(BeNil(), "Could not get snapshot from cloud directly")
if snapshot.Status == "READY" {
return true, nil
}
return false, nil
})
Expect(err).To(BeNil(), "Could not wait for snapshot be ready")
defer func() {
// Delete Disk
err := controllerClient.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeService.RegionDisks.Get(p, region, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
// Delete Snapshot
err = controllerClient.DeleteSnapshot(snapshotID)
Expect(err).To(BeNil(), "DeleteSnapshot failed")
// Validate Snapshot Deleted
_, err = computeService.Snapshots.Get(p, snapshotName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected snapshot to not be found")
}()
})
It("Should get correct VolumeStats for Block", func() {
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
instance := testContext.Instance
volName, volID := createAndValidateUniqueZonalDisk(client, p, z)
defer func() {
// Delete Disk
err := client.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeService.Disks.Get(p, z, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
}()
verifyVolumeStats := func(a verifyArgs) error {
available, capacity, used, inodesFree, inodes, inodesUsed, err := client.NodeGetVolumeStats(volID, a.publishDir)
if err != nil {
return fmt.Errorf("failed to get node volume stats: %v", err)
}
if available != 0 || capacity != common.GbToBytes(defaultSizeGb) || used != 0 ||
inodesFree != 0 || inodes != 0 || inodesUsed != 0 {
return fmt.Errorf("got: available %v, capacity %v, used %v, inodesFree %v, inodes %v, inodesUsed %v -- expected: capacity = %v, available = 0, used = 0, inodesFree = 0, inodes = 0 , inodesUsed = 0",
available, capacity, used, inodesFree, inodes, inodesUsed, common.GbToBytes(defaultSizeGb))
}
return nil
}
// Attach Disk
err := testLifecycleWithVerify(volID, volName, instance, client, false /* readOnly */, true /* block */, verifyVolumeStats, nil)
Expect(err).To(BeNil(), "Failed to go through volume lifecycle")
})
It("Should get correct VolumeStats", func() {
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
instance := testContext.Instance
volName, volID := createAndValidateUniqueZonalDisk(client, p, z)
defer func() {
// Delete Disk
err := client.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeService.Disks.Get(p, z, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
}()
verifyVolumeStats := func(a verifyArgs) error {
available, capacity, used, inodesFree, inodes, inodesUsed, err := client.NodeGetVolumeStats(volID, a.publishDir)
if err != nil {
return fmt.Errorf("failed to get node volume stats: %v", err)
}
if !equalWithinEpsilon(available, common.GbToBytes(defaultSizeGb), defaultEpsilon) || !equalWithinEpsilon(capacity, common.GbToBytes(defaultSizeGb), defaultEpsilon) || !equalWithinEpsilon(used, 0, defaultEpsilon) ||
inodesFree == 0 || inodes == 0 || inodesUsed == 0 {
return fmt.Errorf("got: available %v, capacity %v, used %v, inodesFree %v, inodes %v, inodesUsed %v -- expected: available ~= %v, capacity ~= %v, used = 0, inodesFree != 0, inodes != 0 , inodesUsed != 0",
available, capacity, used, inodesFree, inodes, inodesUsed, common.GbToBytes(defaultSizeGb), common.GbToBytes(defaultSizeGb))
}
return nil
}
// Attach Disk
err := testLifecycleWithVerify(volID, volName, instance, client, false /* readOnly */, false /* fs */, verifyVolumeStats, nil)
Expect(err).To(BeNil(), "Failed to go through volume lifecycle")
})
})
func equalWithinEpsilon(a, b, epsiolon int64) bool {
if a > b {
return a-b < epsiolon
}
return b-a < epsiolon
}
func createAndValidateUniqueZonalDisk(client *remote.CsiClient, project, zone string) (volName, volID string) {
// Create Disk
var err error
volName = testNamePrefix + string(uuid.NewUUID())
volID, err = client.CreateVolume(volName, nil, defaultSizeGb,
&csi.TopologyRequirement{
Requisite: []*csi.Topology{
{
Segments: map[string]string{common.TopologyKeyZone: zone},
},
},
})
Expect(err).To(BeNil(), "CreateVolume failed with error: %v", err)
// Validate Disk Created
cloudDisk, err := computeService.Disks.Get(project, zone, volName).Do()
Expect(err).To(BeNil(), "Could not get disk from cloud directly")
Expect(cloudDisk.Type).To(ContainSubstring(standardDiskType))
Expect(cloudDisk.Status).To(Equal(readyState))
Expect(cloudDisk.SizeGb).To(Equal(defaultSizeGb))
Expect(cloudDisk.Name).To(Equal(volName))
return
}
func deleteVolumeOrError(client *remote.CsiClient, volID, project string) {
// Delete Disk
err := client.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
key, err := common.VolumeIDToKey(volID)
Expect(err).To(BeNil(), "Failed to conver volume ID To key")
_, err = computeService.Disks.Get(project, key.Zone, key.Name).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
}