-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_zone_e2e_test.go
2020 lines (1707 loc) · 73.2 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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/deviceutils"
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
testutils "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e/utils"
"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/v2"
. "github.com/onsi/gomega"
compute "google.golang.org/api/compute/v1"
"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
defaultExtremeSizeGb int64 = 500
defaultHdBSizeGb int64 = 100
defaultHdXSizeGb int64 = 100
defaultHdTSizeGb int64 = 2048
defaultHdmlSizeGb int64 = 200
defaultRepdSizeGb int64 = 200
defaultMwSizeGb int64 = 200
defaultVolumeLimit int64 = 127
invalidSizeGb int64 = 66000
readyState = "READY"
standardDiskType = "pd-standard"
ssdDiskType = "pd-ssd"
extremeDiskType = "pd-extreme"
hdbDiskType = "hyperdisk-balanced"
hdxDiskType = "hyperdisk-extreme"
hdtDiskType = "hyperdisk-throughput"
hdmlDiskType = "hyperdisk-ml"
hdhaDiskType = "hyperdisk-balanced-high-availability"
provisionedIOPSOnCreate = "12345"
provisionedIOPSOnCreateInt = int64(12345)
provisionedIOPSOnCreateDefaultInt = int64(100000)
provisionedIOPSOnCreateHdb = "3000"
provisionedIOPSOnCreateHdbInt = int64(3000)
provisionedIOPSOnCreateHdx = "200"
provisionedIOPSOnCreateHdxInt = int64(200)
provisionedThroughputOnCreate = "66Mi"
provisionedThroughputOnCreateInt = int64(66)
provisionedThroughputOnCreateHdb = "150Mi"
provisionedThroughputOnCreateHdbInt = int64(150)
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("[NVMe] 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, standardDiskType)
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 automatically fix the symlink between /dev/* and /dev/by-id if the disk does not match", func() {
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
instance := testContext.Instance
// Create Disk
volName, volID := createAndValidateUniqueZonalDisk(client, p, z, standardDiskType)
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 := client.ControllerPublishVolumeReadWrite(volID, instance.GetNodeID(), false /* forceAttach */)
Expect(err).To(BeNil(), "ControllerPublishVolume failed with error for disk %v on node %v: %v", volID, instance.GetNodeID())
defer func() {
// Detach Disk
err = client.ControllerUnpublishVolume(volID, instance.GetNodeID())
if err != nil {
klog.Errorf("Failed to detach disk: %v", err)
}
}()
// MESS UP THE symlink
devicePaths := deviceutils.NewDeviceUtils().GetDiskByIdPaths(volName, "")
for _, devicePath := range devicePaths {
err = testutils.RmAll(instance, devicePath)
Expect(err).To(BeNil(), "failed to remove /dev/by-id folder")
err = testutils.Symlink(instance, "/dev/null", devicePath)
Expect(err).To(BeNil(), "failed to add invalid symlink /dev/by-id folder")
}
// Stage Disk
stageDir := filepath.Join("/tmp/", volName, "stage")
err = client.NodeStageExt4Volume(volID, stageDir)
Expect(err).To(BeNil(), "failed to repair /dev/by-id symlink and stage volume")
// Validate that the link is correct
var validated bool
for _, devicePath := range devicePaths {
validated, err = testutils.ValidateLogicalLinkIsDisk(instance, devicePath, volName)
Expect(err).To(BeNil(), "failed to validate link %s is disk %s: %v", stageDir, volName, err)
if validated {
break
}
}
Expect(validated).To(BeTrue(), "could not find device in %v that links to volume %s", devicePaths, volName)
defer func() {
// Unstage Disk
err = client.NodeUnstageVolume(volID, stageDir)
if err != nil {
klog.Errorf("Failed to unstage volume: %v", err)
}
fp := filepath.Join("/tmp/", volName)
err = testutils.RmAll(instance, fp)
if err != nil {
klog.Errorf("Failed to rm file path %s: %v", fp, err)
}
}()
})
It("[NVMe] Should automatically add a symlink between /dev/* and /dev/by-id if disk is not found", func() {
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
instance := testContext.Instance
// Create Disk
volName, volID := createAndValidateUniqueZonalDisk(client, p, z, standardDiskType)
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 := client.ControllerPublishVolumeReadWrite(volID, instance.GetNodeID(), false /* forceAttach */)
Expect(err).To(BeNil(), "ControllerPublishVolume failed with error for disk %v on node %v: %v", volID, instance.GetNodeID())
defer func() {
// Detach Disk
err = client.ControllerUnpublishVolume(volID, instance.GetNodeID())
if err != nil {
klog.Errorf("Failed to detach disk: %v", err)
}
}()
// DELETE THE symlink
devicePaths := deviceutils.NewDeviceUtils().GetDiskByIdPaths(volName, "")
for _, devicePath := range devicePaths {
err = testutils.RmAll(instance, devicePath)
Expect(err).To(BeNil(), "failed to remove /dev/by-id folder")
}
// Stage Disk
stageDir := filepath.Join("/tmp/", volName, "stage")
err = client.NodeStageExt4Volume(volID, stageDir)
Expect(err).To(BeNil(), "failed to repair /dev/by-id symlink and stage volume")
// Validate that the link is correct
var validated bool
for _, devicePath := range devicePaths {
validated, err = testutils.ValidateLogicalLinkIsDisk(instance, devicePath, volName)
Expect(err).To(BeNil(), "failed to validate link %s is disk %s: %v", stageDir, volName, err)
if validated {
break
}
}
Expect(validated).To(BeTrue(), "could not find device in %v that links to volume %s", devicePaths, volName)
defer func() {
// Unstage Disk
err = client.NodeUnstageVolume(volID, stageDir)
if err != nil {
klog.Errorf("Failed to unstage volume: %v", err)
}
fp := filepath.Join("/tmp/", volName)
err = testutils.RmAll(instance, fp)
if err != nil {
klog.Errorf("Failed to rm file path %s: %v", fp, err)
}
}()
})
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},
},
},
}
volume, err := testContext.Client.CreateVolume(volName, nil, defaultSizeGb, topReq, nil)
Expect(err).To(BeNil(), "Failed to create volume")
defer func() {
err = testContext.Client.DeleteVolume(volume.VolumeId)
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")
}
})
// TODO(hime): Enable this test once all release branches contain the fix from PR#1708.
// It("Should return InvalidArgument when disk size exceeds limit", func() {
// // If this returns a different error code (like Unknown), the error wrapping logic in #1708 has regressed.
// Expect(testContexts).ToNot(BeEmpty())
// testContext := getRandomTestContext()
// 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},
// },
// },
// }
// volume, err := testContext.Client.CreateVolume(volName, nil, invalidSizeGb, topReq, nil)
// Expect(err).ToNot(BeNil(), "Failed to fetch error from create volume.")
// Expect(err.Error()).To(ContainSubstring("InvalidArgument"), "Failed to verify error code matches InvalidArgument.")
// defer func() {
// if volume != nil {
// testContext.Client.DeleteVolume(volume.VolumeId)
// }
// }()
// }
// })
DescribeTable("Should complete entire disk lifecycle with underspecified volume ID",
func(diskType string) {
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
instance := testContext.Instance
volName, _ := createAndValidateUniqueZonalDisk(client, p, z, diskType)
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")
},
Entry("on pd-standard", standardDiskType),
Entry("on pd-extreme", extremeDiskType),
Entry("on hyperdisk-throughput", hdtDiskType),
Entry("on pd-ssd", ssdDiskType),
)
DescribeTable("[NVMe] Should complete publish/unpublish lifecycle with underspecified volume ID and missing volume",
func(diskType string) {
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
instance := testContext.Instance
// Create Disk
volName, _ := createAndValidateUniqueZonalDisk(client, p, z, diskType)
underSpecifiedID := common.GenerateUnderspecifiedVolumeID(volName, true /* isZonal */)
defer func() {
// Detach Disk
err := instance.DetachDisk(volName)
Expect(err).To(BeNil(), "DetachDisk failed")
// 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")
// Unpublish Disk
err = client.ControllerUnpublishVolume(underSpecifiedID, instance.GetNodeID())
Expect(err).To(BeNil(), "ControllerUnpublishVolume failed")
}()
// Attach Disk
err := client.ControllerPublishVolumeReadWrite(underSpecifiedID, instance.GetNodeID(), false /* forceAttach */)
Expect(err).To(BeNil(), "ControllerPublishVolume failed")
},
Entry("on pd-standard", standardDiskType),
Entry("on pd-extreme", extremeDiskType),
)
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())
volume, err := controllerClient.CreateVolume(volName, map[string]string{
common.ParameterKeyReplicationType: "regional-pd",
}, defaultRepdSizeGb, nil, 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 {
actualZone := zoneFromURL(replicaZone)
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(volume.VolumeId)
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")
}()
})
DescribeTable("Should create and delete disk with default zone",
func(diskType string) {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
// Create Disk
disk := typeToDisk[diskType]
volName := testNamePrefix + string(uuid.NewUUID())
diskSize := defaultSizeGb
if diskType == extremeDiskType {
diskSize = defaultExtremeSizeGb
}
volume, err := client.CreateVolume(volName, disk.params, diskSize, nil, 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.Status).To(Equal(readyState))
Expect(cloudDisk.SizeGb).To(Equal(diskSize))
Expect(cloudDisk.Name).To(Equal(volName))
disk.validate(cloudDisk)
defer func() {
// Delete Disk
client.DeleteVolume(volume.VolumeId)
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")
}()
},
Entry("on pd-standard", standardDiskType),
Entry("on pd-extreme", extremeDiskType),
)
DescribeTable("Should create and delete pd-extreme disk with default iops",
func(diskType string) {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
// Create Disk
diskParams := map[string]string{
common.ParameterKeyType: diskType,
}
volName := testNamePrefix + string(uuid.NewUUID())
diskSize := defaultExtremeSizeGb
volume, err := client.CreateVolume(volName, diskParams, diskSize, nil, 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.Status).To(Equal(readyState))
Expect(cloudDisk.SizeGb).To(Equal(defaultExtremeSizeGb))
Expect(cloudDisk.Type).To(ContainSubstring(extremeDiskType))
Expect(cloudDisk.ProvisionedIops).To(Equal(provisionedIOPSOnCreateDefaultInt))
Expect(cloudDisk.Name).To(Equal(volName))
defer func() {
// Delete Disk
client.DeleteVolume(volume.VolumeId)
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")
}()
},
Entry("on pd-extreme", extremeDiskType),
)
DescribeTable("Should create and delete disk with labels",
func(diskType string) {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
// Create Disk
disk := typeToDisk[diskType]
volName := testNamePrefix + string(uuid.NewUUID())
params := merge(disk.params, map[string]string{
common.ParameterKeyLabels: "key1=value1,key2=value2",
})
diskSize := defaultSizeGb
if diskType == extremeDiskType {
diskSize = defaultExtremeSizeGb
}
volume, err := client.CreateVolume(volName, params, diskSize, nil, 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.Status).To(Equal(readyState))
Expect(cloudDisk.SizeGb).To(Equal(diskSize))
Expect(cloudDisk.Labels).To(Equal(map[string]string{
"key1": "value1",
"key2": "value2",
// The label below is added as an --extra-label driver command line argument.
testutils.DiskLabelKey: testutils.DiskLabelValue,
}))
Expect(cloudDisk.Name).To(Equal(volName))
disk.validate(cloudDisk)
defer func() {
// Delete Disk
err := client.DeleteVolume(volume.VolumeId)
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")
}()
},
Entry("on pd-standard", standardDiskType),
Entry("on pd-extreme", extremeDiskType),
)
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, standardDiskType)
// 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")
}()
})
DescribeTable("Should create CMEK key, go through volume lifecycle, validate behavior on key revoke and restore",
func(diskType string) {
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"
key, keyVersions := setupKeyRing(ctx, parentName, keyRingId)
// 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
disk := typeToDisk[diskType]
volName := testNamePrefix + string(uuid.NewUUID())
params := merge(disk.params, map[string]string{
common.ParameterKeyDiskEncryptionKmsKey: key.Name,
})
topology := &csi.TopologyRequirement{
Requisite: []*csi.Topology{
{
Segments: map[string]string{common.TopologyKeyZone: z},
},
},
}
diskSize := defaultSizeGb
if diskType == extremeDiskType {
diskSize = defaultExtremeSizeGb
}
volume, err := controllerClient.CreateVolume(volName, params, diskSize, topology, 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.Status).To(Equal(readyState))
Expect(cloudDisk.SizeGb).To(Equal(diskSize))
Expect(cloudDisk.Name).To(Equal(volName))
disk.validate(cloudDisk)
defer func() {
// Delete Disk
err = controllerClient.DeleteVolume(volume.VolumeId)
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(volume.VolumeId, 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(volume.VolumeId, 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")
}
// The controller publish failure in above step would set a backoff condition on the node. Wait suffcient amount of time for the driver to accept new controller publish requests.
time.Sleep(time.Second)
// Make sure attach of PD succeeds
err = testAttachWriteReadDetach(volume.VolumeId, volName, controllerInstance, controllerClient, false /* readOnly */)
Expect(err).To(BeNil(), "Failed to go through volume lifecycle after restoring CMEK key")
},
Entry("on pd-standard", standardDiskType),
Entry("on pd-extreme", extremeDiskType),
)
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, standardDiskType)
defer deleteVolumeOrError(client, volID)
_, secondVolID := createAndValidateUniqueZonalDisk(client, p, z, standardDiskType)
defer deleteVolumeOrError(client, secondVolID)
// Attach volID to current instance
err := client.ControllerPublishVolumeReadWrite(volID, nodeID, false /* forceAttach */)
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())
volume, err := controllerClient.CreateVolume(volName, map[string]string{
common.ParameterKeyReplicationType: "regional-pd",
}, defaultRepdSizeGb, nil, 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 {
actualZone := zoneFromURL(replicaZone)
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, volume.VolumeId, 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(volume.VolumeId)
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, standardDiskType)
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.Error())
}
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, standardDiskType)
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.Error())
}
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")
})
// Pending while multi-writer feature is in Alpha
PIt("Should create and delete multi-writer disk", func() {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
p, _, _ := testContext.Instance.GetIdentity()
client := testContext.Client
// Hardcode to us-east1-a while feature is in alpha
zone := "us-east1-a"
// Create and Validate Disk
volName, volID := createAndValidateUniqueZonalMultiWriterDisk(client, p, zone, standardDiskType)
defer func() {
// Delete Disk
err := client.DeleteVolume(volID)
Expect(err).To(BeNil(), "DeleteVolume failed")
// Validate Disk Deleted
_, err = computeAlphaService.Disks.Get(p, zone, volName).Do()
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
}()
})
// Pending while multi-writer feature is in Alpha
PIt("Should complete entire disk lifecycle with multi-writer disk", func() {
testContext := getRandomTestContext()
p, z, _ := testContext.Instance.GetIdentity()
client := testContext.Client
instance := testContext.Instance
// Create and Validate Disk
volName, volID := createAndValidateUniqueZonalMultiWriterDisk(client, p, z, standardDiskType)
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
testFileContents := "test"
writeFunc := func(a *verifyArgs) error {
err := testutils.WriteBlock(instance, a.publishDir, testFileContents)
if err != nil {
return fmt.Errorf("Failed to write file: %v", err.Error())
}
return nil
}
verifyReadFunc := func(a *verifyArgs) error {
readContents, err := testutils.ReadBlock(instance, a.publishDir, len(testFileContents))
if err != nil {
return fmt.Errorf("ReadFile failed with error: %v", err.Error())
}
if strings.TrimSpace(string(readContents)) != testFileContents {
return fmt.Errorf("wanted test file content: %s, got content: %s", testFileContents, readContents)
}
return nil
}
err := testLifecycleWithVerify(volID, volName, instance, client, false /* readOnly */, true /* block */, writeFunc, verifyReadFunc)
Expect(err).To(BeNil(), "Failed to go through volume lifecycle")
})
DescribeTable("Should successfully create disk with PVC/PV tags",
func(diskType string) {
Expect(testContexts).ToNot(BeEmpty())
testContext := getRandomTestContext()
controllerInstance := testContext.Instance
controllerClient := testContext.Client
diskSize := defaultSizeGb
if diskType == extremeDiskType {
diskSize = defaultExtremeSizeGb
}
p, z, _ := controllerInstance.GetIdentity()
// Create Disk
disk := typeToDisk[diskType]
volName := testNamePrefix + string(uuid.NewUUID())
params := merge(disk.params, map[string]string{
common.ParameterKeyPVCName: "test-pvc",
common.ParameterKeyPVCNamespace: "test-pvc-namespace",
common.ParameterKeyPVName: "test-pv-name",
})
volume, err := controllerClient.CreateVolume(volName, params, diskSize, nil /* topReq */, nil)