8
8
"net/url"
9
9
"strconv"
10
10
"strings"
11
+ "time"
11
12
12
13
. "github.com/onsi/ginkgo/v2"
13
14
. "github.com/onsi/gomega"
@@ -16,6 +17,7 @@ import (
16
17
17
18
corev1 "k8s.io/api/core/v1"
18
19
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
20
+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
19
21
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20
22
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
21
23
"k8s.io/apimachinery/pkg/runtime"
@@ -42,20 +44,25 @@ var (
42
44
var _ = Describe ("Forklift populator tests" , func () {
43
45
var (
44
46
reconciler * ForkliftPopulatorReconciler
47
+ recorder * record.FakeRecorder
45
48
)
46
49
50
+ BeforeEach (func () {
51
+ recorder = nil
52
+ })
53
+
54
+ AfterEach (func () {
55
+ if recorder != nil {
56
+ close (recorder .Events )
57
+ }
58
+ })
59
+
47
60
const (
48
61
samplePopulatorName = "forklift-populator-test"
49
62
targetPvcName = "test-forklift-populator-pvc"
50
63
scName = "testsc"
51
64
)
52
65
53
- AfterEach (func () {
54
- if reconciler != nil && reconciler .recorder != nil {
55
- close (reconciler .recorder .(* record.FakeRecorder ).Events )
56
- }
57
- })
58
-
59
66
sc := CreateStorageClassWithProvisioner (scName , map [string ]string {
60
67
AnnDefaultStorageClass : "true" ,
61
68
}, map [string ]string {}, "csi-plugin" )
@@ -66,6 +73,7 @@ var _ = Describe("Forklift populator tests", func() {
66
73
Name : PVCPrimeName (pvc ),
67
74
Namespace : pvc .Namespace ,
68
75
Annotations : annotations ,
76
+ Finalizers : []string {"test/finalizer" },
69
77
},
70
78
Spec : corev1.PersistentVolumeClaimSpec {
71
79
AccessModes : pvc .Spec .AccessModes ,
@@ -402,6 +410,111 @@ var _ = Describe("Forklift populator tests", func() {
402
410
Expect (err ).ToNot (HaveOccurred ())
403
411
Expect (targetPvc .Annotations [AnnPopulatorProgress ]).To (BeEquivalentTo ("13.45%" ))
404
412
})
413
+
414
+ It ("should remove the populator pod after pvcPrime is marked for deletion" , func () {
415
+ targetPvc := CreatePvcInStorageClass (targetPvcName , metav1 .NamespaceDefault , & sc .Name , nil , nil , corev1 .ClaimPending )
416
+ targetPvc .Spec .DataSourceRef = dataSourceRef
417
+ pvcPrime := getPVCPrime (targetPvc , make (map [string ]string ))
418
+ pvcPrime .DeletionTimestamp = & metav1.Time {Time : time .Now ()}
419
+
420
+ populatorPod := getPopulatorPod (targetPvc , pvcPrime )
421
+
422
+ By ("Reconcile" )
423
+ reconciler = createForkliftPopulatorReconciler (targetPvc , pvcPrime , sc , populatorPod )
424
+
425
+ result , err := reconciler .Reconcile (context .TODO (), reconcile.Request {NamespacedName : types.NamespacedName {Name : targetPvcName , Namespace : metav1 .NamespaceDefault }})
426
+ Expect (err ).To (Not (HaveOccurred ()))
427
+ Expect (result ).To (Not (BeNil ()))
428
+
429
+ By ("Checking if the populator pod is deleted" )
430
+ pod := & corev1.Pod {}
431
+ err = reconciler .client .Get (context .TODO (), types.NamespacedName {Name : populatorPod .Name , Namespace : populatorPod .Namespace }, pod )
432
+ Expect (err ).To (HaveOccurred ())
433
+ Expect (k8serrors .IsNotFound (err )).To (BeTrue ())
434
+ })
435
+
436
+ It ("should create the populator pod with expected specifications" , func () {
437
+ targetPvc := CreatePvcInStorageClass (targetPvcName , metav1 .NamespaceDefault , & sc .Name , nil , nil , corev1 .ClaimPending )
438
+ targetPvc .Spec .DataSourceRef = dataSourceRef
439
+ pvcPrime := getPVCPrime (targetPvc , make (map [string ]string ))
440
+
441
+ By ("Reconcile" )
442
+ reconciler = createForkliftPopulatorReconciler (targetPvc , pvcPrime , sc , ovirtCr )
443
+
444
+ // Call createPopulatorPod directly
445
+ err := reconciler .createPopulatorPod (pvcPrime , targetPvc )
446
+ Expect (err ).To (Not (HaveOccurred ()))
447
+
448
+ By ("Checking if the populator pod is created with the expected specifications" )
449
+ pod := & corev1.Pod {}
450
+ podName := fmt .Sprintf ("%s-%s" , populatorPodPrefix , targetPvc .UID )
451
+ err = reconciler .client .Get (context .TODO (), types.NamespacedName {Name : podName , Namespace : targetPvc .Namespace }, pod )
452
+ Expect (err ).To (Not (HaveOccurred ()))
453
+ Expect (pod .Name ).To (Equal (podName ))
454
+ Expect (pod .Namespace ).To (Equal (targetPvc .Namespace ))
455
+ Expect (pod .Spec .Containers ).To (HaveLen (1 ))
456
+ container := pod .Spec .Containers [0 ]
457
+ Expect (container .Name ).To (Equal ("populate" ))
458
+ Expect (container .Image ).To (Equal (reconciler .ovirtPopulatorImage ))
459
+ Expect (container .Command ).To (Equal ([]string {"ovirt-populator" }))
460
+ Expect (container .Args ).To (ContainElements (
461
+ fmt .Sprintf ("--owner-uid=%s" , string (targetPvc .UID )),
462
+ fmt .Sprintf ("--pvc-size=%d" , targetPvc .Spec .Resources .Requests .Storage ().Value ()),
463
+ "--volume-path=/mnt/disk.img" ,
464
+ "--secret-name=ovirt-engine-secret" ,
465
+ "--disk-id=12345678-1234-1234-1234-123456789012" ,
466
+ "--engine-url=https://ovirt-engine.example.com" ,
467
+ ))
468
+ })
469
+
470
+ It ("should correctly identify a PVC as Forklift kind" , func () {
471
+ validPVC := & corev1.PersistentVolumeClaim {
472
+ ObjectMeta : metav1.ObjectMeta {
473
+ Name : "valid-pvc" ,
474
+ Namespace : metav1 .NamespaceDefault ,
475
+ },
476
+ Spec : corev1.PersistentVolumeClaimSpec {
477
+ DataSourceRef : & corev1.TypedObjectReference {
478
+ APIGroup : & apiGroup ,
479
+ Kind : "OvirtVolumePopulator" ,
480
+ Name : "sample-populator" ,
481
+ },
482
+ },
483
+ }
484
+
485
+ invalidPVC := & corev1.PersistentVolumeClaim {
486
+ ObjectMeta : metav1.ObjectMeta {
487
+ Name : "invalid-pvc" ,
488
+ Namespace : metav1 .NamespaceDefault ,
489
+ },
490
+ Spec : corev1.PersistentVolumeClaimSpec {
491
+ DataSourceRef : & corev1.TypedObjectReference {
492
+ APIGroup : & apiGroup ,
493
+ Kind : "UnknownPopulator" ,
494
+ Name : "sample-populator" ,
495
+ },
496
+ },
497
+ }
498
+
499
+ noDataSourceRefPVC := & corev1.PersistentVolumeClaim {
500
+ ObjectMeta : metav1.ObjectMeta {
501
+ Name : "no-datasource-pvc" ,
502
+ Namespace : metav1 .NamespaceDefault ,
503
+ },
504
+ }
505
+
506
+ By ("Validating PVC with correct DataSourceRef" )
507
+ isValid := isPVCForkliftKind (validPVC )
508
+ Expect (isValid ).To (BeTrue ())
509
+
510
+ By ("Validating PVC with incorrect DataSourceRef kind" )
511
+ isInvalid := isPVCForkliftKind (invalidPVC )
512
+ Expect (isInvalid ).To (BeFalse ())
513
+
514
+ By ("Validating PVC with no DataSourceRef" )
515
+ isNoDataSourceRef := isPVCForkliftKind (noDataSourceRefPVC )
516
+ Expect (isNoDataSourceRef ).To (BeFalse ())
517
+ })
405
518
})
406
519
})
407
520
0 commit comments