From b6f12e181a5a2db8e09965d42c70b8978606f0b9 Mon Sep 17 00:00:00 2001 From: liubo02 Date: Fri, 9 Jun 2023 18:28:52 +0800 Subject: [PATCH] fix test --- pkg/manager/volumes/phase_test.go | 28 +++++- pkg/manager/volumes/pod_vol_modifier_test.go | 90 ++++++++++++------- .../volumes/sync_volume_status_test.go | 21 +++-- 3 files changed, 94 insertions(+), 45 deletions(-) diff --git a/pkg/manager/volumes/phase_test.go b/pkg/manager/volumes/phase_test.go index 75bc5e1aa2b..5ae9843638b 100644 --- a/pkg/manager/volumes/phase_test.go +++ b/pkg/manager/volumes/phase_test.go @@ -43,6 +43,11 @@ func newTestPVCForGetVolumePhase(size string, sc *string, annotations map[string }, StorageClassName: sc, }, + Status: corev1.PersistentVolumeClaimStatus{ + Capacity: corev1.ResourceList{ + corev1.ResourceStorage: q, + }, + }, } } @@ -169,13 +174,22 @@ func TestGetVolumePhase(t *testing.T) { expected: VolumePhasePreparing, }, { - desc: "invalid sc", + desc: "sc is not set", pvc: newTestPVCForGetVolumePhase(oldSize, &oldScName, nil), oldSc: newStorageClassForGetVolumePhase(oldScName, "ebs.csi.aws.com", true), sc: nil, size: oldSize, - expected: VolumePhaseCannotModify, + expected: VolumePhaseModified, + }, + { + desc: "sc is not set, but size is changed", + pvc: newTestPVCForGetVolumePhase(oldSize, &oldScName, nil), + oldSc: newStorageClassForGetVolumePhase(oldScName, "ebs.csi.aws.com", true), + sc: nil, + size: newSize, + + expected: VolumePhasePreparing, }, { desc: "invalid size", @@ -217,14 +231,22 @@ func TestGetVolumePhase(t *testing.T) { actual := ActualVolume{ PVC: c.pvc, StorageClass: c.oldSc, + PV: &corev1.PersistentVolume{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + }, + }, } if !c.noDesired { actual.Desired = &DesiredVolume{ StorageClass: c.sc, Size: resource.MustParse(c.size), } + if c.sc != nil { + actual.Desired.StorageClassName = &c.sc.Name + } } phase := pvm.getVolumePhase(&actual) - g.Expect(phase).Should(Equal(c.expected), c.desc) + g.Expect(phase.String()).Should(Equal(c.expected.String()), c.desc) } } diff --git a/pkg/manager/volumes/pod_vol_modifier_test.go b/pkg/manager/volumes/pod_vol_modifier_test.go index a1b0e6335b3..62b4c8abf4c 100644 --- a/pkg/manager/volumes/pod_vol_modifier_test.go +++ b/pkg/manager/volumes/pod_vol_modifier_test.go @@ -15,6 +15,7 @@ package volumes import ( "context" + "fmt" "testing" "time" @@ -80,17 +81,18 @@ func TestModify(t *testing.T) { oldSize := "10Gi" newSize := "20Gi" oldSc := "old" - // newSc := "new" + newSc := "new" provisioner := "test" cases := []struct { desc string - pvc *corev1.PersistentVolumeClaim - pv *corev1.PersistentVolume - sc *storagev1.StorageClass - size string + pvc *corev1.PersistentVolumeClaim + pv *corev1.PersistentVolume + oldSc *storagev1.StorageClass + sc *storagev1.StorageClass + size string isModifyVolumeFinished bool @@ -98,72 +100,92 @@ func TestModify(t *testing.T) { expectedHasErr bool }{ { - desc: "volume is not changed", - pvc: newTestPVCForModify(&oldSc, oldSize, oldSize, nil), - pv: newTestPVForModify(), - sc: newTestSCForModify(oldSc, provisioner), - size: oldSize, + desc: "volume is not changed", + pvc: newTestPVCForModify(&oldSc, oldSize, oldSize, nil), + pv: newTestPVForModify(), + oldSc: newTestSCForModify(oldSc, provisioner), + sc: newTestSCForModify(oldSc, provisioner), + size: oldSize, expectedPVC: newTestPVCForModify(&oldSc, oldSize, oldSize, nil), }, { - desc: "volume size is changed, and revision has not been upgraded", + desc: "only volume size is changed", - pvc: newTestPVCForModify(&oldSc, oldSize, oldSize, nil), - pv: newTestPVForModify(), - sc: newTestSCForModify(oldSc, provisioner), - size: newSize, + pvc: newTestPVCForModify(&oldSc, oldSize, oldSize, nil), + pv: newTestPVForModify(), + oldSc: newTestSCForModify(oldSc, provisioner), + sc: newTestSCForModify(oldSc, provisioner), + size: newSize, + + expectedPVC: newTestPVCForModify(&oldSc, newSize, oldSize, map[string]string{ + annoKeyPVCSpecRevision: "1", + annoKeyPVCSpecStorageClass: oldSc, + annoKeyPVCSpecStorageSize: newSize, + }), + expectedHasErr: true, + }, + { + desc: "volume is changed, and revision has not been upgraded", + + pvc: newTestPVCForModify(&oldSc, oldSize, oldSize, nil), + pv: newTestPVForModify(), + oldSc: newTestSCForModify(oldSc, provisioner), + sc: newTestSCForModify(newSc, provisioner), + size: newSize, isModifyVolumeFinished: false, expectedPVC: newTestPVCForModify(&oldSc, oldSize, oldSize, map[string]string{ annoKeyPVCSpecRevision: "1", - annoKeyPVCSpecStorageClass: oldSc, + annoKeyPVCSpecStorageClass: newSc, annoKeyPVCSpecStorageSize: newSize, }), expectedHasErr: true, }, { - desc: "volume size is changed, and delegate modification is finished", + desc: "volume is changed, and delegate modification is finished", pvc: newTestPVCForModify(&oldSc, oldSize, oldSize, map[string]string{ annoKeyPVCSpecRevision: "1", - annoKeyPVCSpecStorageClass: oldSc, + annoKeyPVCSpecStorageClass: newSc, annoKeyPVCSpecStorageSize: newSize, }), - pv: newTestPVForModify(), - sc: newTestSCForModify(oldSc, provisioner), - size: newSize, + pv: newTestPVForModify(), + oldSc: newTestSCForModify(oldSc, provisioner), + sc: newTestSCForModify(newSc, provisioner), + size: newSize, isModifyVolumeFinished: true, expectedPVC: newTestPVCForModify(&oldSc, newSize, oldSize, map[string]string{ annoKeyPVCSpecRevision: "1", - annoKeyPVCSpecStorageClass: oldSc, + annoKeyPVCSpecStorageClass: newSc, annoKeyPVCSpecStorageSize: newSize, }), expectedHasErr: true, }, { - desc: "volume size is changed, and fs resize is finished", + desc: "volume is changed, and fs resize is finished", pvc: newTestPVCForModify(&oldSc, newSize, newSize, map[string]string{ annoKeyPVCSpecRevision: "1", - annoKeyPVCSpecStorageClass: oldSc, + annoKeyPVCSpecStorageClass: newSc, annoKeyPVCSpecStorageSize: newSize, }), - pv: newTestPVForModify(), - sc: newTestSCForModify(oldSc, provisioner), - size: newSize, + pv: newTestPVForModify(), + oldSc: newTestSCForModify(oldSc, provisioner), + sc: newTestSCForModify(newSc, provisioner), + size: newSize, isModifyVolumeFinished: true, expectedPVC: newTestPVCForModify(&oldSc, newSize, newSize, map[string]string{ annoKeyPVCSpecRevision: "1", - annoKeyPVCSpecStorageClass: oldSc, + annoKeyPVCSpecStorageClass: newSc, annoKeyPVCSpecStorageSize: newSize, annoKeyPVCStatusRevision: "1", - annoKeyPVCStatusStorageClass: oldSc, + annoKeyPVCStatusStorageClass: newSc, annoKeyPVCStatusStorageSize: newSize, }), }, @@ -189,6 +211,7 @@ func TestModify(t *testing.T) { m := delegation.NewMockVolumeModifier(provisioner, time.Hour) m.ModifyVolumeFunc = func(_ context.Context, pvc *corev1.PersistentVolumeClaim, pv *corev1.PersistentVolume, sc *storagev1.StorageClass) (bool, error) { + fmt.Println("call modify volume") return !c.isModifyVolumeFinished, nil } @@ -206,13 +229,14 @@ func TestModify(t *testing.T) { actual := ActualVolume{ Desired: &DesiredVolume{ - Name: "test", - Size: resource.MustParse(c.size), - StorageClass: c.sc, + Name: "test", + Size: resource.MustParse(c.size), + StorageClass: c.sc, + StorageClassName: &c.sc.Name, }, PVC: c.pvc, PV: c.pv, - StorageClass: c.sc, + StorageClass: c.oldSc, } phase := pvm.getVolumePhase(&actual) diff --git a/pkg/manager/volumes/sync_volume_status_test.go b/pkg/manager/volumes/sync_volume_status_test.go index db489c70fa3..c4703f7fa54 100644 --- a/pkg/manager/volumes/sync_volume_status_test.go +++ b/pkg/manager/volumes/sync_volume_status_test.go @@ -111,19 +111,22 @@ func TestObserveVolumeStatus(t *testing.T) { desiredVolumes := []DesiredVolume{ { - Name: "vol1", - Size: resource.MustParse(desiredSize), - StorageClass: newStorageClass(desiredSC, true), + Name: "vol1", + Size: resource.MustParse(desiredSize), + StorageClass: newStorageClass(desiredSC, true), + StorageClassName: &desiredSC, }, { - Name: "vol2", - Size: resource.MustParse(desiredSize), - StorageClass: newStorageClass(desiredSC, true), + Name: "vol2", + Size: resource.MustParse(desiredSize), + StorageClass: newStorageClass(desiredSC, true), + StorageClassName: &desiredSC, }, { - Name: "vol3", - Size: resource.MustParse(desiredSize), - StorageClass: newStorageClass(desiredSC, true), + Name: "vol3", + Size: resource.MustParse(desiredSize), + StorageClass: newStorageClass(desiredSC, true), + StorageClassName: &desiredSC, }, } pvm.GetActualVolumesFunc = func(pod *corev1.Pod, vs []DesiredVolume) ([]ActualVolume, error) {