Skip to content

Commit 0a1fa5b

Browse files
authored
Merge pull request kubernetes-csi#1118 from leonardoce/vgs-unit-tests
Add group snapshot controller unit tests
2 parents 723dc22 + 3c2470c commit 0a1fa5b

10 files changed

+1435
-26
lines changed

pkg/common-controller/framework_test.go

Lines changed: 583 additions & 20 deletions
Large diffs are not rendered by default.

pkg/common-controller/groupsnapshot_controller_helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ func (ctrl *csiSnapshotCommonController) getClaimsFromVolumeGroupSnapshot(groupS
223223
// Get PVC that has group snapshot label applied.
224224
pvcList, err := ctrl.client.CoreV1().PersistentVolumeClaims(groupSnapshot.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labels.Set(labelSelector.MatchLabels).String()})
225225
if err != nil {
226-
return nil, fmt.Errorf("failed to list PVCs with label selector %s: %q", labelSelector.String(), err)
226+
return nil, fmt.Errorf("failed to list PVCs with label selector %s: %q", metav1.FormatLabelSelector(labelSelector), err)
227227
}
228228
if len(pvcList.Items) == 0 {
229-
return nil, fmt.Errorf("label selector %s for group snapshot not applied to any PVC", labelSelector.String())
229+
return nil, fmt.Errorf("label selector %s for group snapshot not applied to any PVC", metav1.FormatLabelSelector(labelSelector))
230230
}
231231
return pvcList.Items, nil
232232
}

pkg/common-controller/groupsnapshot_create_test.go

Lines changed: 380 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package common_controller
18+
19+
import (
20+
"testing"
21+
22+
crdv1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
23+
"github.com/kubernetes-csi/external-snapshotter/v8/pkg/utils"
24+
v1 "k8s.io/api/core/v1"
25+
)
26+
27+
func TestDeleteGroupSnapshotSync(t *testing.T) {
28+
tests := []controllerTest{
29+
{
30+
name: "2-1 - group snapshot have been deleted, but no content was present - no op",
31+
initialGroupSnapshots: newGroupSnapshotArray(
32+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
33+
"app.kubernetes.io/name": "postgresql",
34+
},
35+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
36+
),
37+
expectedGroupSnapshots: newGroupSnapshotArray(
38+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
39+
"app.kubernetes.io/name": "postgresql",
40+
},
41+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
42+
),
43+
initialGroupContents: nil,
44+
expectedGroupContents: nil,
45+
initialClaims: withClaimLabels(
46+
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
47+
map[string]string{
48+
"app.kubernetes.io/name": "postgresql",
49+
}),
50+
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
51+
errors: noerrors,
52+
test: testSyncGroupSnapshot,
53+
expectSuccess: true,
54+
},
55+
{
56+
name: "2-2 - dynamic group snapshot have been deleted, was not provisioned, no-op",
57+
initialGroupSnapshots: newGroupSnapshotArray(
58+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
59+
"app.kubernetes.io/name": "postgresql",
60+
},
61+
"", classGold, "", nil, nil, nil, true, false, &timeNowMetav1,
62+
),
63+
expectedGroupSnapshots: newGroupSnapshotArray(
64+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
65+
"app.kubernetes.io/name": "postgresql",
66+
},
67+
"", classGold, "", nil, nil, nil, true, false, &timeNowMetav1,
68+
),
69+
initialGroupContents: nil,
70+
expectedGroupContents: nil,
71+
initialClaims: withClaimLabels(
72+
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
73+
map[string]string{
74+
"app.kubernetes.io/name": "postgresql",
75+
}),
76+
initialVolumes: nil,
77+
errors: noerrors,
78+
test: testSyncGroupSnapshot,
79+
expectSuccess: true,
80+
},
81+
{
82+
name: "2-3 - pre-provisioned group snapshot have been deleted, retention policy set to retain - set 'being deleted' annotation",
83+
initialGroupSnapshots: withGroupSnapshotFinalizers(
84+
newGroupSnapshotArray(
85+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
86+
"app.kubernetes.io/name": "postgresql",
87+
},
88+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
89+
),
90+
utils.VolumeGroupSnapshotBoundFinalizer,
91+
),
92+
expectedGroupSnapshots: newGroupSnapshotArray(
93+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
94+
"app.kubernetes.io/name": "postgresql",
95+
},
96+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
97+
),
98+
initialGroupContents: newGroupSnapshotContentArray(
99+
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
100+
"1-pv-handle6-1",
101+
"2-pv-handle6-1",
102+
}, "", crdv1.VolumeSnapshotContentRetain, nil, false, false,
103+
),
104+
expectedGroupContents: withGroupContentAnnotations(
105+
newGroupSnapshotContentArray(
106+
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
107+
"1-pv-handle6-1",
108+
"2-pv-handle6-1",
109+
}, "", crdv1.VolumeSnapshotContentRetain, nil, false, false,
110+
),
111+
map[string]string{
112+
utils.AnnVolumeGroupSnapshotBeingDeleted: "yes",
113+
},
114+
),
115+
initialClaims: withClaimLabels(
116+
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
117+
map[string]string{
118+
"app.kubernetes.io/name": "postgresql",
119+
}),
120+
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
121+
errors: noerrors,
122+
test: testSyncGroupSnapshot,
123+
expectSuccess: true,
124+
},
125+
{
126+
name: "2-4 - pre-provisioned snapshot have been deleted, retention policy set to delete - volume snapshot content will be deleted",
127+
initialGroupSnapshots: withGroupSnapshotFinalizers(
128+
newGroupSnapshotArray(
129+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
130+
"app.kubernetes.io/name": "postgresql",
131+
},
132+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
133+
),
134+
utils.VolumeGroupSnapshotBoundFinalizer,
135+
),
136+
expectedGroupSnapshots: withGroupSnapshotFinalizers(
137+
newGroupSnapshotArray(
138+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
139+
"app.kubernetes.io/name": "postgresql",
140+
},
141+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
142+
),
143+
utils.VolumeGroupSnapshotBoundFinalizer,
144+
),
145+
initialGroupContents: newGroupSnapshotContentArray(
146+
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
147+
"1-pv-handle6-1",
148+
"2-pv-handle6-1",
149+
}, "", crdv1.VolumeSnapshotContentDelete, nil, false, false,
150+
),
151+
expectedGroupContents: nil,
152+
initialClaims: withClaimLabels(
153+
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
154+
map[string]string{
155+
"app.kubernetes.io/name": "postgresql",
156+
}),
157+
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
158+
errors: noerrors,
159+
test: testSyncGroupSnapshot,
160+
expectSuccess: true,
161+
},
162+
}
163+
runSyncTests(t, tests, nil, groupSnapshotClasses)
164+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package common_controller
18+
19+
import (
20+
"testing"
21+
22+
crdv1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
23+
"github.com/kubernetes-csi/external-snapshotter/v8/pkg/utils"
24+
v1 "k8s.io/api/core/v1"
25+
)
26+
27+
func TestGroupSnapshotFinalizer(t *testing.T) {
28+
tests := []controllerTest{
29+
{
30+
name: "3-1 - finalizer is added on dynamically provisioned group contents if the volume group snapshot class specify a Deletion retain policy",
31+
initialGroupSnapshots: newGroupSnapshotArray(
32+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
33+
"app.kubernetes.io/name": "postgresql",
34+
},
35+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, nil,
36+
),
37+
expectedGroupSnapshots: withGroupSnapshotFinalizers(
38+
newGroupSnapshotArray(
39+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
40+
"app.kubernetes.io/name": "postgresql",
41+
},
42+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, nil,
43+
),
44+
utils.VolumeGroupSnapshotBoundFinalizer,
45+
),
46+
initialGroupContents: newGroupSnapshotContentArray(
47+
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
48+
"1-pv-handle6-1",
49+
"2-pv-handle6-1",
50+
}, "", deletionPolicy, nil, false, false,
51+
),
52+
expectedGroupContents: newGroupSnapshotContentArray(
53+
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
54+
"1-pv-handle6-1",
55+
"2-pv-handle6-1",
56+
}, "", deletionPolicy, nil, false, false,
57+
),
58+
initialClaims: withClaimLabels(
59+
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
60+
map[string]string{
61+
"app.kubernetes.io/name": "postgresql",
62+
}),
63+
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
64+
errors: noerrors,
65+
test: testSyncGroupSnapshot,
66+
expectSuccess: true,
67+
},
68+
{
69+
name: "3-2 - finalizer is not added on dynamically provisioned group contents if the volume group snapshot class specify the Retain retain policy",
70+
initialGroupSnapshots: newGroupSnapshotArray(
71+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
72+
"app.kubernetes.io/name": "postgresql",
73+
},
74+
"", classSilver, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, nil,
75+
),
76+
expectedGroupSnapshots: withGroupSnapshotFinalizers(
77+
newGroupSnapshotArray(
78+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
79+
"app.kubernetes.io/name": "postgresql",
80+
},
81+
"", classSilver, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, nil,
82+
),
83+
utils.VolumeGroupSnapshotBoundFinalizer,
84+
),
85+
initialGroupContents: newGroupSnapshotContentArray(
86+
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classSilver, []string{
87+
"1-pv-handle6-1",
88+
"2-pv-handle6-1",
89+
}, "", deletionPolicy, nil, false, false,
90+
),
91+
expectedGroupContents: newGroupSnapshotContentArray(
92+
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classSilver, []string{
93+
"1-pv-handle6-1",
94+
"2-pv-handle6-1",
95+
}, "", deletionPolicy, nil, false, false,
96+
),
97+
initialClaims: withClaimLabels(
98+
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classSilver),
99+
map[string]string{
100+
"app.kubernetes.io/name": "postgresql",
101+
}),
102+
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classSilver),
103+
errors: noerrors,
104+
test: testSyncGroupSnapshot,
105+
expectSuccess: true,
106+
},
107+
{
108+
name: "3-3 - dynamic group snapshot have been deleted, retention policy set to Delete - the finalizer will be removed",
109+
initialGroupSnapshots: withGroupSnapshotFinalizers(
110+
newGroupSnapshotArray(
111+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
112+
"app.kubernetes.io/name": "postgresql",
113+
},
114+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
115+
),
116+
utils.VolumeGroupSnapshotBoundFinalizer,
117+
),
118+
expectedGroupSnapshots: newGroupSnapshotArray(
119+
"group-snap-1-1", "group-snapuid1-1", map[string]string{
120+
"app.kubernetes.io/name": "postgresql",
121+
},
122+
"", classGold, "groupsnapcontent-group-snapuid1-1", &False, nil, nil, false, false, &timeNowMetav1,
123+
),
124+
initialGroupContents: newGroupSnapshotContentArray(
125+
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
126+
"1-pv-handle6-1",
127+
"2-pv-handle6-1",
128+
}, "", crdv1.VolumeSnapshotContentRetain, nil, false, false,
129+
),
130+
expectedGroupContents: withGroupContentAnnotations(
131+
newGroupSnapshotContentArray(
132+
"groupsnapcontent-group-snapuid1-1", "group-snapuid1-1", "group-snap-1-1", "group-snapshot-handle", classGold, []string{
133+
"1-pv-handle6-1",
134+
"2-pv-handle6-1",
135+
}, "", crdv1.VolumeSnapshotContentRetain, nil, false, false,
136+
),
137+
map[string]string{
138+
utils.AnnVolumeGroupSnapshotBeingDeleted: "yes",
139+
},
140+
),
141+
initialClaims: withClaimLabels(
142+
newClaimCoupleArray("claim1-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classGold),
143+
map[string]string{
144+
"app.kubernetes.io/name": "postgresql",
145+
}),
146+
initialVolumes: newVolumeCoupleArray("volume6-1", "pv-uid6-1", "pv-handle6-1", "1Gi", "pvc-uid6-1", "claim1-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold),
147+
errors: noerrors,
148+
test: testSyncGroupSnapshot,
149+
expectSuccess: true,
150+
},
151+
}
152+
runSyncTests(t, tests, nil, groupSnapshotClasses)
153+
}

0 commit comments

Comments
 (0)