@@ -71,6 +71,9 @@ func TestDropDisabledSnapshotDataSource(t *testing.T) {
71
71
},
72
72
}
73
73
74
+ // Ensure that any data sources aren't enabled for this test
75
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .AnyVolumeDataSource , false )()
76
+
74
77
for _ , enabled := range []bool {true , false } {
75
78
for _ , oldpvcInfo := range pvcInfo {
76
79
for _ , newpvcInfo := range pvcInfo {
@@ -169,6 +172,9 @@ func TestPVCDataSourceSpecFilter(t *testing.T) {
169
172
},
170
173
}
171
174
175
+ // Ensure that any data sources aren't enabled for this test
176
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .AnyVolumeDataSource , false )()
177
+
172
178
for testName , test := range tests {
173
179
t .Run (testName , func (t * testing.T ) {
174
180
DropDisabledFields (& test .spec , nil )
@@ -181,3 +187,108 @@ func TestPVCDataSourceSpecFilter(t *testing.T) {
181
187
}
182
188
183
189
}
190
+
191
+ // TestAnyDataSourceFilter checks to ensure the AnyVolumeDataSource feature gate works
192
+ func TestAnyDataSourceFilter (t * testing.T ) {
193
+ makeDataSource := func (apiGroup , kind , name string ) * core.TypedLocalObjectReference {
194
+ return & core.TypedLocalObjectReference {
195
+ APIGroup : & apiGroup ,
196
+ Kind : kind ,
197
+ Name : name ,
198
+ }
199
+ }
200
+
201
+ volumeDataSource := makeDataSource ("" , "PersistentVolumeClaim" , "my-vol" )
202
+ snapshotDataSource := makeDataSource ("snapshot.storage.k8s.io" , "VolumeSnapshot" , "my-snap" )
203
+ genericDataSource := makeDataSource ("generic.storage.k8s.io" , "Generic" , "my-foo" )
204
+
205
+ var tests = map [string ]struct {
206
+ spec core.PersistentVolumeClaimSpec
207
+ snapshotEnabled bool
208
+ anyEnabled bool
209
+ want * core.TypedLocalObjectReference
210
+ }{
211
+ "both disabled with empty ds" : {
212
+ spec : core.PersistentVolumeClaimSpec {},
213
+ want : nil ,
214
+ },
215
+ "both disabled with volume ds" : {
216
+ spec : core.PersistentVolumeClaimSpec {DataSource : volumeDataSource },
217
+ want : volumeDataSource ,
218
+ },
219
+ "both disabled with snapshot ds" : {
220
+ spec : core.PersistentVolumeClaimSpec {DataSource : snapshotDataSource },
221
+ want : nil ,
222
+ },
223
+ "both disabled with generic ds" : {
224
+ spec : core.PersistentVolumeClaimSpec {DataSource : genericDataSource },
225
+ want : nil ,
226
+ },
227
+ "any enabled with empty ds" : {
228
+ spec : core.PersistentVolumeClaimSpec {},
229
+ anyEnabled : true ,
230
+ want : nil ,
231
+ },
232
+ "any enabled with volume ds" : {
233
+ spec : core.PersistentVolumeClaimSpec {DataSource : volumeDataSource },
234
+ anyEnabled : true ,
235
+ want : volumeDataSource ,
236
+ },
237
+ "any enabled with snapshot ds" : {
238
+ spec : core.PersistentVolumeClaimSpec {DataSource : snapshotDataSource },
239
+ anyEnabled : true ,
240
+ want : snapshotDataSource ,
241
+ },
242
+ "any enabled with generic ds" : {
243
+ spec : core.PersistentVolumeClaimSpec {DataSource : genericDataSource },
244
+ anyEnabled : true ,
245
+ want : genericDataSource ,
246
+ },
247
+ "snapshot enabled with snapshot ds" : {
248
+ spec : core.PersistentVolumeClaimSpec {DataSource : snapshotDataSource },
249
+ snapshotEnabled : true ,
250
+ want : snapshotDataSource ,
251
+ },
252
+ "snapshot enabled with generic ds" : {
253
+ spec : core.PersistentVolumeClaimSpec {DataSource : genericDataSource },
254
+ snapshotEnabled : true ,
255
+ want : nil ,
256
+ },
257
+ "both enabled with empty ds" : {
258
+ spec : core.PersistentVolumeClaimSpec {},
259
+ snapshotEnabled : true ,
260
+ anyEnabled : true ,
261
+ want : nil ,
262
+ },
263
+ "both enabled with volume ds" : {
264
+ spec : core.PersistentVolumeClaimSpec {DataSource : volumeDataSource },
265
+ snapshotEnabled : true ,
266
+ anyEnabled : true ,
267
+ want : volumeDataSource ,
268
+ },
269
+ "both enabled with snapshot ds" : {
270
+ spec : core.PersistentVolumeClaimSpec {DataSource : snapshotDataSource },
271
+ snapshotEnabled : true ,
272
+ anyEnabled : true ,
273
+ want : snapshotDataSource ,
274
+ },
275
+ "both enabled with generic ds" : {
276
+ spec : core.PersistentVolumeClaimSpec {DataSource : genericDataSource },
277
+ snapshotEnabled : true ,
278
+ anyEnabled : true ,
279
+ want : genericDataSource ,
280
+ },
281
+ }
282
+
283
+ for testName , test := range tests {
284
+ t .Run (testName , func (t * testing.T ) {
285
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .VolumeSnapshotDataSource , test .snapshotEnabled )()
286
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .AnyVolumeDataSource , test .anyEnabled )()
287
+ DropDisabledFields (& test .spec , nil )
288
+ if test .spec .DataSource != test .want {
289
+ t .Errorf ("expected condition was not met, test: %s, snapshotEnabled: %v, anyEnabled: %v, spec: %v, expected: %v" ,
290
+ testName , test .snapshotEnabled , test .anyEnabled , test .spec , test .want )
291
+ }
292
+ })
293
+ }
294
+ }
0 commit comments