Skip to content

Commit

Permalink
Merge pull request kubernetes-csi#1118 from leonardoce/vgs-unit-tests
Browse files Browse the repository at this point in the history
Add group snapshot controller unit tests
  • Loading branch information
k8s-ci-robot authored Aug 28, 2024
2 parents 723dc22 + 3c2470c commit 0a1fa5b
Show file tree
Hide file tree
Showing 10 changed files with 1,435 additions and 26 deletions.
603 changes: 583 additions & 20 deletions pkg/common-controller/framework_test.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/common-controller/groupsnapshot_controller_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ func (ctrl *csiSnapshotCommonController) getClaimsFromVolumeGroupSnapshot(groupS
// Get PVC that has group snapshot label applied.
pvcList, err := ctrl.client.CoreV1().PersistentVolumeClaims(groupSnapshot.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labels.Set(labelSelector.MatchLabels).String()})
if err != nil {
return nil, fmt.Errorf("failed to list PVCs with label selector %s: %q", labelSelector.String(), err)
return nil, fmt.Errorf("failed to list PVCs with label selector %s: %q", metav1.FormatLabelSelector(labelSelector), err)
}
if len(pvcList.Items) == 0 {
return nil, fmt.Errorf("label selector %s for group snapshot not applied to any PVC", labelSelector.String())
return nil, fmt.Errorf("label selector %s for group snapshot not applied to any PVC", metav1.FormatLabelSelector(labelSelector))
}
return pvcList.Items, nil
}
Expand Down
380 changes: 380 additions & 0 deletions pkg/common-controller/groupsnapshot_create_test.go

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions pkg/common-controller/groupsnapshot_delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
Copyright 2024 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 common_controller

import (
"testing"

crdv1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
"github.com/kubernetes-csi/external-snapshotter/v8/pkg/utils"
v1 "k8s.io/api/core/v1"
)

func TestDeleteGroupSnapshotSync(t *testing.T) {
tests := []controllerTest{
{
name: "2-1 - group snapshot have been deleted, but no content was present - no op",
initialGroupSnapshots: newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
),
expectedGroupSnapshots: newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
),
initialGroupContents: nil,
expectedGroupContents: nil,
initialClaims: withClaimLabels(
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
map[string]string{
"app.kubernetes.io/name": "postgresql",
}),
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
errors: noerrors,
test: testSyncGroupSnapshot,
expectSuccess: true,
},
{
name: "2-2 - dynamic group snapshot have been deleted, was not provisioned, no-op",
initialGroupSnapshots: newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "", nil, nil, nil, true, false, &timeNowMetav1,
),
expectedGroupSnapshots: newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "", nil, nil, nil, true, false, &timeNowMetav1,
),
initialGroupContents: nil,
expectedGroupContents: nil,
initialClaims: withClaimLabels(
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
map[string]string{
"app.kubernetes.io/name": "postgresql",
}),
initialVolumes: nil,
errors: noerrors,
test: testSyncGroupSnapshot,
expectSuccess: true,
},
{
name: "2-3 - pre-provisioned group snapshot have been deleted, retention policy set to retain - set 'being deleted' annotation",
initialGroupSnapshots: withGroupSnapshotFinalizers(
newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
),
utils.VolumeGroupSnapshotBoundFinalizer,
),
expectedGroupSnapshots: newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
),
initialGroupContents: newGroupSnapshotContentArray(
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
"1-pv-handle6-1",
"2-pv-handle6-1",
}, "", crdv1.VolumeSnapshotContentRetain, nil, false, false,
),
expectedGroupContents: withGroupContentAnnotations(
newGroupSnapshotContentArray(
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
"1-pv-handle6-1",
"2-pv-handle6-1",
}, "", crdv1.VolumeSnapshotContentRetain, nil, false, false,
),
map[string]string{
utils.AnnVolumeGroupSnapshotBeingDeleted: "yes",
},
),
initialClaims: withClaimLabels(
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
map[string]string{
"app.kubernetes.io/name": "postgresql",
}),
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
errors: noerrors,
test: testSyncGroupSnapshot,
expectSuccess: true,
},
{
name: "2-4 - pre-provisioned snapshot have been deleted, retention policy set to delete - volume snapshot content will be deleted",
initialGroupSnapshots: withGroupSnapshotFinalizers(
newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
),
utils.VolumeGroupSnapshotBoundFinalizer,
),
expectedGroupSnapshots: withGroupSnapshotFinalizers(
newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
),
utils.VolumeGroupSnapshotBoundFinalizer,
),
initialGroupContents: newGroupSnapshotContentArray(
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
"1-pv-handle6-1",
"2-pv-handle6-1",
}, "", crdv1.VolumeSnapshotContentDelete, nil, false, false,
),
expectedGroupContents: nil,
initialClaims: withClaimLabels(
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
map[string]string{
"app.kubernetes.io/name": "postgresql",
}),
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
errors: noerrors,
test: testSyncGroupSnapshot,
expectSuccess: true,
},
}
runSyncTests(t, tests, nil, groupSnapshotClasses)
}
153 changes: 153 additions & 0 deletions pkg/common-controller/groupsnapshot_finalizer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
Copyright 2024 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 common_controller

import (
"testing"

crdv1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
"github.com/kubernetes-csi/external-snapshotter/v8/pkg/utils"
v1 "k8s.io/api/core/v1"
)

func TestGroupSnapshotFinalizer(t *testing.T) {
tests := []controllerTest{
{
name: "3-1 - finalizer is added on dynamically provisioned group contents if the volume group snapshot class specify a Deletion retain policy",
initialGroupSnapshots: newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, nil,
),
expectedGroupSnapshots: withGroupSnapshotFinalizers(
newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, nil,
),
utils.VolumeGroupSnapshotBoundFinalizer,
),
initialGroupContents: newGroupSnapshotContentArray(
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
"1-pv-handle6-1",
"2-pv-handle6-1",
}, "", deletionPolicy, nil, false, false,
),
expectedGroupContents: newGroupSnapshotContentArray(
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
"1-pv-handle6-1",
"2-pv-handle6-1",
}, "", deletionPolicy, nil, false, false,
),
initialClaims: withClaimLabels(
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
map[string]string{
"app.kubernetes.io/name": "postgresql",
}),
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
errors: noerrors,
test: testSyncGroupSnapshot,
expectSuccess: true,
},
{
name: "3-2 - finalizer is not added on dynamically provisioned group contents if the volume group snapshot class specify the Retain retain policy",
initialGroupSnapshots: newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classSilver, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, nil,
),
expectedGroupSnapshots: withGroupSnapshotFinalizers(
newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classSilver, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, nil,
),
utils.VolumeGroupSnapshotBoundFinalizer,
),
initialGroupContents: newGroupSnapshotContentArray(
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classSilver, []string{
"1-pv-handle6-1",
"2-pv-handle6-1",
}, "", deletionPolicy, nil, false, false,
),
expectedGroupContents: newGroupSnapshotContentArray(
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classSilver, []string{
"1-pv-handle6-1",
"2-pv-handle6-1",
}, "", deletionPolicy, nil, false, false,
),
initialClaims: withClaimLabels(
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classSilver),
map[string]string{
"app.kubernetes.io/name": "postgresql",
}),
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classSilver),
errors: noerrors,
test: testSyncGroupSnapshot,
expectSuccess: true,
},
{
name: "3-3 - dynamic group snapshot have been deleted, retention policy set to Delete - the finalizer will be removed",
initialGroupSnapshots: withGroupSnapshotFinalizers(
newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
),
utils.VolumeGroupSnapshotBoundFinalizer,
),
expectedGroupSnapshots: newGroupSnapshotArray(
"group-snap-1-1", "group-snapuid1-1", map[string]string{
"app.kubernetes.io/name": "postgresql",
},
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
),
initialGroupContents: newGroupSnapshotContentArray(
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
"1-pv-handle6-1",
"2-pv-handle6-1",
}, "", crdv1.VolumeSnapshotContentRetain, nil, false, false,
),
expectedGroupContents: withGroupContentAnnotations(
newGroupSnapshotContentArray(
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
"1-pv-handle6-1",
"2-pv-handle6-1",
}, "", crdv1.VolumeSnapshotContentRetain, nil, false, false,
),
map[string]string{
utils.AnnVolumeGroupSnapshotBeingDeleted: "yes",
},
),
initialClaims: withClaimLabels(
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
map[string]string{
"app.kubernetes.io/name": "postgresql",
}),
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
errors: noerrors,
test: testSyncGroupSnapshot,
expectSuccess: true,
},
}
runSyncTests(t, tests, nil, groupSnapshotClasses)
}
Loading

0 comments on commit 0a1fa5b

Please sign in to comment.