/
data_persistence.go
585 lines (519 loc) · 25.9 KB
/
data_persistence.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
/*
Copyright 2019 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 e2e
import (
"context"
"fmt"
"strconv"
"strings"
"time"
ginkgo "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
v1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kubernetes/test/e2e/framework"
fnodes "k8s.io/kubernetes/test/e2e/framework/node"
fpod "k8s.io/kubernetes/test/e2e/framework/pod"
e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
fpv "k8s.io/kubernetes/test/e2e/framework/pv"
admissionapi "k8s.io/pod-security-admission/api"
)
// Steps
//
// 1. Create a PVC.
// 2. Create pod and wait for pod to become ready.
// 3. Verify volume is attached.
// 4. Create empty file on the volume to verify volume is writable.
// 5. Verify newly created file and previously created files exist on the volume.
// 6. Delete pod.
// 7. Create a new pod using the previously created volume and wait for pod to
// become ready.
// 8. Create another empty file on the volume .
// 9. Verify newly created file and previously created files exist on the volume.
// 10. Delete pod.
// 11. Wait for volume to be detached.
var _ = ginkgo.Describe("Data Persistence", func() {
f := framework.NewDefaultFramework("e2e-data-persistence")
f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
var (
client clientset.Interface
defaultDatacenter *object.Datacenter
defaultDatastore *object.Datastore
namespace string
scParameters map[string]string
storagePolicyName string
svcPVCName string // PVC Name in the Supervisor Cluster.
datastoreURL string
)
ginkgo.BeforeEach(func() {
client = f.ClientSet
namespace = getNamespaceToRunTests(f)
nodeList, err := fnodes.GetReadySchedulableNodes(f.ClientSet)
framework.ExpectNoError(err, "Unable to find ready and schedulable Node")
if !(len(nodeList.Items) > 0) {
framework.Failf("Unable to find ready and schedulable Node")
}
bootstrap()
scParameters = make(map[string]string)
storagePolicyName = GetAndExpectStringEnvVar(envStoragePolicyNameForSharedDatastores)
var datacenters []string
datastoreURL = GetAndExpectStringEnvVar(envSharedDatastoreURL)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
finder := find.NewFinder(e2eVSphere.Client.Client, false)
cfg, err := getConfig()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
dcList := strings.Split(cfg.Global.Datacenters, ",")
for _, dc := range dcList {
dcName := strings.TrimSpace(dc)
if dcName != "" {
datacenters = append(datacenters, dcName)
}
}
for _, dc := range datacenters {
defaultDatacenter, err = finder.Datacenter(ctx, dc)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
finder.SetDatacenter(defaultDatacenter)
defaultDatastore, err = getDatastoreByURL(ctx, datastoreURL, defaultDatacenter)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
if guestCluster {
svcClient, svNamespace := getSvcClientAndNamespace()
setResourceQuota(svcClient, svNamespace, rqLimit)
}
})
ginkgo.AfterEach(func() {
if guestCluster {
svcClient, svNamespace := getSvcClientAndNamespace()
setResourceQuota(svcClient, svNamespace, defaultrqLimit)
dumpSvcNsEventsOnTestFailure(svcClient, svNamespace)
}
if supervisorCluster {
dumpSvcNsEventsOnTestFailure(client, namespace)
}
})
ginkgo.It("[csi-block-vanilla] [csi-supervisor] [csi-guest] [csi-block-vanilla-parallelized] Should create and "+
"delete pod with the same volume source and data", ginkgo.Label(p0, block, vanilla, wcp, tkg, core), func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var sc *storagev1.StorageClass
var pvc *v1.PersistentVolumeClaim
var err error
ginkgo.By("Creating Storage Class and PVC")
// Decide which test setup is available to run.
if vanillaCluster {
ginkgo.By("CNS_TEST: Running for vanilla k8s setup")
sc, pvc, err = createPVCAndStorageClass(client, namespace, nil, nil, "", nil, "", false, "")
} else if supervisorCluster {
ginkgo.By("CNS_TEST: Running for WCP setup")
ginkgo.By(fmt.Sprintf("storagePolicyName: %s", storagePolicyName))
profileID := e2eVSphere.GetSpbmPolicyID(storagePolicyName)
scParameters[scParamStoragePolicyID] = profileID
// Create resource quota.
createResourceQuota(client, namespace, rqLimit, storagePolicyName)
sc, pvc, err = createPVCAndStorageClass(client, namespace, nil, scParameters, "", nil, "", false, "",
storagePolicyName)
} else {
ginkgo.By("CNS_TEST: Running for GC setup")
scParameters[svStorageClassName] = storagePolicyName
sc, pvc, err = createPVCAndStorageClass(client, namespace, nil, scParameters, "", nil, "", false, "")
}
gomega.Expect(err).NotTo(gomega.HaveOccurred())
defer func() {
if !supervisorCluster {
err := client.StorageV1().StorageClasses().Delete(ctx, sc.Name, *metav1.NewDeleteOptions(0))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
}()
ginkgo.By(fmt.Sprintf("Waiting for claim %s to be in bound phase", pvc.Name))
pvs, err := fpv.WaitForPVClaimBoundPhase(client, []*v1.PersistentVolumeClaim{pvc},
framework.ClaimProvisionTimeout)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(pvs).NotTo(gomega.BeEmpty())
pv := pvs[0]
volumeID := pv.Spec.CSI.VolumeHandle
if guestCluster {
// svcPVCName refers to PVC Name in the supervisor cluster.
svcPVCName = volumeID
volumeID = getVolumeIDFromSupervisorCluster(svcPVCName)
gomega.Expect(volumeID).NotTo(gomega.BeEmpty())
}
defer func() {
err := fpv.DeletePersistentVolumeClaim(client, pvc.Name, namespace)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = e2eVSphere.waitForCNSVolumeToBeDeleted(pv.Spec.CSI.VolumeHandle)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}()
ginkgo.By("Creating pod")
pod, err := createPod(client, namespace, nil, []*v1.PersistentVolumeClaim{pvc}, false, "")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Verify volume: %s is attached to the node: %s",
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName))
var vmUUID string
var exists bool
if vanillaCluster {
vmUUID = getNodeUUID(ctx, client, pod.Spec.NodeName)
} else if supervisorCluster {
annotations := pod.Annotations
vmUUID, exists = annotations[vmUUIDLabel]
gomega.Expect(exists).To(gomega.BeTrue(), fmt.Sprintf("Pod doesn't have %s annotation", vmUUIDLabel))
_, err := e2eVSphere.getVMByUUID(ctx, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
} else {
vmUUID, err = getVMUUIDFromNodeName(pod.Spec.NodeName)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
verifyCRDInSupervisorWithWait(ctx, f, pod.Spec.NodeName+"-"+svcPVCName,
crdCNSNodeVMAttachment, crdVersion, crdGroup, true)
}
isDiskAttached, err := e2eVSphere.isVolumeAttachedToVM(client, volumeID, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskAttached).To(gomega.BeTrue(), fmt.Sprintf("Volume is not attached to the node, %s", vmUUID))
var volumeFiles []string
// Create an empty file on the mounted volumes on the pod.
ginkgo.By(fmt.Sprintf("Creating an empty file on the volume mounted on: %v", pod.Name))
newEmptyFileName := fmt.Sprintf("/mnt/volume1/%v_file_A.txt", namespace)
volumeFiles = append(volumeFiles, newEmptyFileName)
createAndVerifyFilesOnVolume(namespace, pod.Name, []string{newEmptyFileName}, volumeFiles)
ginkgo.By("Deleting the pod")
err = fpod.DeletePodWithWait(client, pod)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
if supervisorCluster {
ginkgo.By(fmt.Sprintf("Verify volume: %s is detached from PodVM with vmUUID: %s",
pv.Spec.CSI.VolumeHandle, vmUUID))
_, err := e2eVSphere.getVMByUUIDWithWait(ctx, vmUUID, supervisorClusterOperationsTimeout)
gomega.Expect(err).To(gomega.HaveOccurred(),
fmt.Sprintf("PodVM with vmUUID: %s still exists. So volume: %s is not detached from the PodVM",
vmUUID, pv.Spec.CSI.VolumeHandle))
} else {
ginkgo.By("Verify volume is detached from the node")
isDiskDetached, err := e2eVSphere.waitForVolumeDetachedFromNode(client,
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskDetached).To(gomega.BeTrue(),
fmt.Sprintf("Volume %q is not detached from the node %q", pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName))
if guestCluster {
ginkgo.By("Waiting for CnsNodeVMAttachment controller to reconcile resource")
verifyCRDInSupervisorWithWait(ctx, f, pod.Spec.NodeName+"-"+svcPVCName,
crdCNSNodeVMAttachment, crdVersion, crdGroup, false)
}
}
ginkgo.By("Creating a new pod using the same volume")
pod, err = createPod(client, namespace, nil, []*v1.PersistentVolumeClaim{pvc}, false, "")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Verify volume: %s is attached to the node: %s",
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName))
if vanillaCluster {
vmUUID = getNodeUUID(ctx, client, pod.Spec.NodeName)
} else if supervisorCluster {
annotations := pod.Annotations
vmUUID, exists = annotations[vmUUIDLabel]
gomega.Expect(exists).To(gomega.BeTrue(), fmt.Sprintf("Pod doesn't have %s annotation", vmUUIDLabel))
_, err := e2eVSphere.getVMByUUID(ctx, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
} else {
vmUUID, err = getVMUUIDFromNodeName(pod.Spec.NodeName)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
verifyCRDInSupervisorWithWait(ctx, f, pod.Spec.NodeName+"-"+svcPVCName,
crdCNSNodeVMAttachment, crdVersion, crdGroup, true)
}
isDiskAttached, err = e2eVSphere.isVolumeAttachedToVM(client, volumeID, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskAttached).To(gomega.BeTrue(), "Volume is not attached to the node")
// Create another empty file on the mounted volume on the pod and
// verify newly and previously created files present on the volume
// mounted on the pod.
ginkgo.By(fmt.Sprintf("Creating a second empty file on the same volume mounted on: %v", pod.Name))
newEmptyFileName = fmt.Sprintf("/mnt/volume1/%v_file_B.txt", namespace)
volumeFiles = append(volumeFiles, newEmptyFileName)
createAndVerifyFilesOnVolume(namespace, pod.Name, []string{newEmptyFileName}, volumeFiles)
ginkgo.By("Deleting the pod")
err = fpod.DeletePodWithWait(client, pod)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
if supervisorCluster {
ginkgo.By(fmt.Sprintf("Verify volume: %s is detached from PodVM with vmUUID: %s",
pv.Spec.CSI.VolumeHandle, vmUUID))
_, err := e2eVSphere.getVMByUUIDWithWait(ctx, vmUUID, supervisorClusterOperationsTimeout)
gomega.Expect(err).To(gomega.HaveOccurred(),
fmt.Sprintf("PodVM with vmUUID: %s still exists. So volume: %s is not detached from the PodVM",
vmUUID, pv.Spec.CSI.VolumeHandle))
} else {
ginkgo.By("Verify volume is detached from the node")
isDiskDetached, err := e2eVSphere.waitForVolumeDetachedFromNode(client,
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskDetached).To(gomega.BeTrue(),
fmt.Sprintf("Volume %q is not detached from the node %q", pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName))
if guestCluster {
ginkgo.By("Waiting for 30 seconds to allow CnsNodeVMAttachment controller to reconcile resource")
time.Sleep(waitTimeForCNSNodeVMAttachmentReconciler)
verifyCRDInSupervisorWithWait(ctx, f, pod.Spec.NodeName+"-"+svcPVCName,
crdCNSNodeVMAttachment, crdVersion, crdGroup, false)
}
}
})
// Data Persistence in case of Dynamic Volume Provisioning with XFS filesystem.
// Steps
//
// 1. Create SC with fstype set to "xfs".
// 2. Create a PVC which uses SC created in step1.
// 3. Create pod and wait for pod to become ready.
// 4. Verify volume is attached and volume is mouned inside pod with xfs filesystem.
// 5. Create file at the mountpath and verify that creation is successful.
// 6. Delete pod.
// 7. Create a new pod using the previously created volume and wait for pod to
// become ready.
// 8. Verify that data written by old pod can be read successfully.
// 9. Create another file at the mountpath and verify that creation is successful.
// 10. Delete pod.
// 11. Wait for volume to be detached.
// 12. Delete PVC and SC.
ginkgo.It("[csi-block-vanilla] [csi-block-vanilla-parallelized] Dynamic volume provisioning data persistence "+
"test with XFS filesystem", ginkgo.Label(p0, block, vanilla, wcp, core), func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var sc *storagev1.StorageClass
var pvc *v1.PersistentVolumeClaim
var err error
framework.Logf("CNS_TEST: Running for vanilla k8s setup")
ginkgo.By("Creating Storage Class with XFS as fstype and PVC")
scParameters[scParamFsType] = xfsFSType
sc, pvc, err = createPVCAndStorageClass(client, namespace, nil, scParameters, "", nil, "", false, "")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
defer func() {
err := client.StorageV1().StorageClasses().Delete(ctx, sc.Name, *metav1.NewDeleteOptions(0))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}()
ginkgo.By(fmt.Sprintf("Waiting for claim %s to be in bound phase", pvc.Name))
pvs, err := fpv.WaitForPVClaimBoundPhase(client, []*v1.PersistentVolumeClaim{pvc},
framework.ClaimProvisionTimeout)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(pvs).NotTo(gomega.BeEmpty())
pv := pvs[0]
volumeID := pv.Spec.CSI.VolumeHandle
defer func() {
err := fpv.DeletePersistentVolumeClaim(client, pvc.Name, namespace)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = e2eVSphere.waitForCNSVolumeToBeDeleted(pv.Spec.CSI.VolumeHandle)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}()
ginkgo.By("Creating pod")
pod, err := createPod(client, namespace, nil, []*v1.PersistentVolumeClaim{pvc}, false, execCommand)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Verify volume: %s is attached to the node: %s",
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName))
var vmUUID string
vmUUID = getNodeUUID(ctx, client, pod.Spec.NodeName)
isDiskAttached, err := e2eVSphere.isVolumeAttachedToVM(client, volumeID, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskAttached).To(gomega.BeTrue(), "Volume is not attached to the node %s", vmUUID)
ginkgo.By("Verify that filesystem type is xfs as expected")
_, err = e2eoutput.LookForStringInPodExec(namespace, pod.Name, []string{"/bin/cat", "/mnt/volume1/fstype"},
xfsFSType, time.Minute)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
// Create file_A.txt at mountpath inside pod.
ginkgo.By(fmt.Sprintf("Creating file file_A.txt at mountpath inside pod: %v", pod.Name))
data1 := "This file file_A.txt is written by Pod1"
filePath1 := "/mnt/volume1/file_A.txt"
writeDataOnFileFromPod(namespace, pod.Name, filePath1, data1)
// Ensure that write is successful at the mountpath
ginkgo.By("Verify that data can be successfully read from file_A.txt")
output := readFileFromPod(namespace, pod.Name, filePath1)
gomega.Expect(output == data1+"\n").To(gomega.BeTrue(), "Pod1 is not able to read file_A.txt written by Pod1")
ginkgo.By("Deleting the pod")
err = fpod.DeletePodWithWait(client, pod)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Verify volume is detached from the node")
isDiskDetached, err := e2eVSphere.waitForVolumeDetachedFromNode(client,
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskDetached).To(gomega.BeTrue(),
"Volume %q is not detached from the node %q", pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName)
ginkgo.By("Creating a new pod using the same volume")
pod, err = createPod(client, namespace, nil, []*v1.PersistentVolumeClaim{pvc}, false, "")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Verify volume: %s is attached to the node: %s",
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName))
vmUUID = getNodeUUID(ctx, client, pod.Spec.NodeName)
isDiskAttached, err = e2eVSphere.isVolumeAttachedToVM(client, volumeID, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskAttached).To(gomega.BeTrue(), "Volume is not attached to the node %s", vmUUID)
// Ensure that data written by old pod can be read successfully from new pod,
// since PVC used is same.
ginkgo.By("Verify that data can be successfully read from file_A.txt from new pod")
output = readFileFromPod(namespace, pod.Name, filePath1)
gomega.Expect(output == data1+"\n").To(gomega.BeTrue(), "Pod2 is not able to read file_A.txt written by Pod1")
// Create another file file_B.txt at mountpath from new pod
ginkgo.By(fmt.Sprintf("Creating file file_B.txt at mountpath inside pod: %v", pod.Name))
data2 := "This file file_B.txt is written by Pod2"
filePath2 := "/mnt/volume1/file_B.txt"
writeDataOnFileFromPod(namespace, pod.Name, filePath2, data2)
// Ensure that write is successful at the mountpath
ginkgo.By("Verify that data can be successfully read from file_B.txt")
output = readFileFromPod(namespace, pod.Name, filePath2)
gomega.Expect(output == data2+"\n").To(gomega.BeTrue(), "Pod2 is not able to read file_B.txt written by Pod2")
ginkgo.By("Deleting the pod")
err = fpod.DeletePodWithWait(client, pod)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By("Verify volume is detached from the node")
isDiskDetached, err = e2eVSphere.waitForVolumeDetachedFromNode(client,
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskDetached).To(gomega.BeTrue(),
"Volume %q is not detached from the node %q", pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName)
})
// Data Persistence in case of Static Volume Provisioning on SVC.
// Steps
//
// 1. Create FCD with valid storage policy.
// 2. Create Resource quota.
// 3. Create CNS register volume with above created FCD.
// 4. verify PV, PVC got created , check the bidirectional referance.
// 5. Create POD, with above created PVC.
// 6. Verify volume is attached to the node and volume is accessible in the
// pod.
// 7. Create an empty file on the mounted volumes on the pod.
// 8. Delete POD.
// 9. Create a new pod using the previously created volume and wait for pod
// to become ready.
// 10. Create an empty file again and Verify newly created file and
// previously created files exist on the volume.
// 11. Delete Newly created POD.
// 12. Delete PVC.
// 13. Verify PV is deleted automatically.
// 14. Verify Volume id deleted automatically.
// 15. Verify CRD deleted automatically.
// 16. Delete resource quota.
ginkgo.It("[csi-supervisor] Data Persistence in case of Static Volume Provisioning on "+
"SVC", ginkgo.Label(p0, block, wcp), func() {
var pvc *v1.PersistentVolumeClaim
var err error
var vmUUID string
var exists bool
var volumeFiles []string
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Get a config to talk to the apiserver.
k8senv := GetAndExpectStringEnvVar("KUBECONFIG")
restConfig, err := clientcmd.BuildConfigFromFlags("", k8senv)
if err != nil {
return
}
curtime := time.Now().Unix()
curtimeinstring := strconv.FormatInt(curtime, 10)
pvcName := "cns-pvc-" + curtimeinstring
ginkgo.By("Get the Profile ID")
ginkgo.By(fmt.Sprintf("storagePolicyName: %s", storagePolicyName))
profileID := e2eVSphere.GetSpbmPolicyID(storagePolicyName)
framework.Logf("Profile ID :%s", profileID)
scParameters := make(map[string]string)
scParameters["storagePolicyID"] = profileID
ginkgo.By("Create FCD with valid storage policy.")
fcdID, err := e2eVSphere.createFCDwithValidProfileID(ctx, "staticfcd"+curtimeinstring,
profileID, diskSizeInMb, defaultDatastore.Reference())
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Sleeping for %v seconds to allow newly created FCD:%s to sync with pandora", 90, fcdID))
time.Sleep(time.Duration(90) * time.Second)
ginkgo.By("Create resource quota")
createResourceQuota(client, namespace, rqLimit, storagePolicyName)
ginkgo.By("Import above created FCD ")
cnsRegisterVolume := getCNSRegisterVolumeSpec(ctx, namespace, fcdID, "", pvcName, v1.ReadWriteOnce)
err = createCNSRegisterVolume(ctx, restConfig, cnsRegisterVolume)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
framework.ExpectNoError(waitForCNSRegisterVolumeToGetCreated(ctx,
restConfig, namespace, cnsRegisterVolume, poll, pollTimeout))
cnsRegisterVolumeName := cnsRegisterVolume.GetName()
framework.Logf("cnsRegisterVolumeName : %s", cnsRegisterVolumeName)
ginkgo.By("verify created PV, PVC and check the bidirectional referance")
pvc, err = client.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, pvcName, metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
pv := getPvFromClaim(client, namespace, pvcName)
verifyBidirectionalReferenceOfPVandPVC(ctx, client, pvc, pv, fcdID)
ginkgo.By("Creating pod")
pod, err := createPod(client, namespace, nil, []*v1.PersistentVolumeClaim{pvc}, false, "")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Verify volume: %s is attached to the node: %s",
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName))
volumeID := pv.Spec.CSI.VolumeHandle
annotations := pod.Annotations
vmUUID, exists = annotations[vmUUIDLabel]
gomega.Expect(exists).To(gomega.BeTrue(), fmt.Sprintf("Pod doesn't have %s annotation", vmUUIDLabel))
_, err = e2eVSphere.getVMByUUID(ctx, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
isDiskAttached, err := e2eVSphere.isVolumeAttachedToVM(client, volumeID, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskAttached).To(gomega.BeTrue(), fmt.Sprintf("Volume is not attached to the node, %s", vmUUID))
ginkgo.By(fmt.Sprintf("Creating an empty file on the volume mounted on: %v", pod.Name))
newEmptyFileName := fmt.Sprintf("/mnt/volume1/%v_file_A.txt", namespace)
volumeFiles = append(volumeFiles, newEmptyFileName)
createAndVerifyFilesOnVolume(namespace, pod.Name, []string{newEmptyFileName}, volumeFiles)
ginkgo.By("Deleting the pod")
err = fpod.DeletePodWithWait(client, pod)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Verify volume: %s is detached from PodVM with vmUUID: %s",
pv.Spec.CSI.VolumeHandle, vmUUID))
_, err = e2eVSphere.getVMByUUIDWithWait(ctx, vmUUID, supervisorClusterOperationsTimeout)
gomega.Expect(err).To(gomega.HaveOccurred(),
fmt.Sprintf("PodVM with vmUUID: %s still exists. So volume: %s is not detached from the PodVM",
vmUUID, pv.Spec.CSI.VolumeHandle))
ginkgo.By("Creating a new pod using the same volume")
pod, err = createPod(client, namespace, nil, []*v1.PersistentVolumeClaim{pvc}, false, "")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Verify volume: %s is attached to the node: %s",
pv.Spec.CSI.VolumeHandle, pod.Spec.NodeName))
annotations = pod.Annotations
vmUUID, exists = annotations[vmUUIDLabel]
gomega.Expect(exists).To(gomega.BeTrue(), fmt.Sprintf("Pod doesn't have %s annotation", vmUUIDLabel))
_, err = e2eVSphere.getVMByUUID(ctx, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
isDiskAttached, err = e2eVSphere.isVolumeAttachedToVM(client, volumeID, vmUUID)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(isDiskAttached).To(gomega.BeTrue(), "Volume is not attached to the node")
ginkgo.By("Create another empty file on the mounted volume on the pod and " +
"verify newly and previously created files present on the volume mounted on the pod")
ginkgo.By(fmt.Sprintf("Creating a second empty file on the same volume mounted on: %v", pod.Name))
newEmptyFileName = fmt.Sprintf("/mnt/volume1/%v_file_B.txt", namespace)
volumeFiles = append(volumeFiles, newEmptyFileName)
createAndVerifyFilesOnVolume(namespace, pod.Name, []string{newEmptyFileName}, volumeFiles)
ginkgo.By("Deleting the pod")
framework.ExpectNoError(fpod.DeletePodWithWait(client, pod), "Failed to delete pod ", pod.Name)
ginkgo.By(fmt.Sprintf("Verify volume: %s is detached from PodVM with vmUUID: %s",
pv.Spec.CSI.VolumeHandle, vmUUID))
_, err = e2eVSphere.getVMByUUIDWithWait(ctx, vmUUID, supervisorClusterOperationsTimeout)
gomega.Expect(err).To(gomega.HaveOccurred(),
fmt.Sprintf("PodVM with vmUUID: %s still exists. So volume: %s is not detached from the PodVM",
vmUUID, pv.Spec.CSI.VolumeHandle))
ginkgo.By("Deleting the PV Claim")
framework.ExpectNoError(fpv.DeletePersistentVolumeClaim(client, pvc.Name, namespace),
"Failed to delete PVC ", pvc.Name)
pvc = nil
ginkgo.By("Verify PV should be deleted automatically")
framework.ExpectNoError(fpv.WaitForPersistentVolumeDeleted(client, pv.Name, poll, pollTimeoutShort))
pv = nil
ginkgo.By("Verify CRD should be deleted automatically")
framework.ExpectNoError(waitForCNSRegisterVolumeToGetDeleted(ctx,
restConfig, namespace, cnsRegisterVolume, poll, supervisorClusterOperationsTimeout))
ginkgo.By("Delete Resource quota")
deleteResourceQuota(client, namespace)
})
})
func createAndVerifyFilesOnVolume(namespace string, podname string,
newEmptyfilesToCreate []string, filesToCheck []string) {
createEmptyFilesOnVSphereVolume(namespace, podname, newEmptyfilesToCreate)
ginkgo.By(fmt.Sprintf("Verify files exist on volume mounted on: %v", podname))
verifyFilesExistOnVSphereVolume(namespace, podname, poll, pollTimeoutShort, filesToCheck...)
}