@@ -27,6 +27,7 @@ import (
27
27
clientset "k8s.io/client-go/kubernetes"
28
28
"k8s.io/kubernetes/test/e2e/framework"
29
29
e2elog "k8s.io/kubernetes/test/e2e/framework/log"
30
+ e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
30
31
"k8s.io/kubernetes/test/e2e/storage/testpatterns"
31
32
)
32
33
@@ -94,6 +95,11 @@ func (t *volumeModeTestSuite) defineTests(driver TestDriver, pattern testpattern
94
95
// Now do the more expensive test initialization.
95
96
l .config , l .testCleanup = driver .PrepareTest (f )
96
97
l .intreeOps , l .migratedOps = getMigrationVolumeOpCounts (f .ClientSet , dInfo .InTreePluginName )
98
+ }
99
+
100
+ // manualInit initializes l.genericVolumeTestResource without creating the PV & PVC objects.
101
+ manualInit := func () {
102
+ init ()
97
103
98
104
fsType := pattern .FsType
99
105
volBindMode := storagev1 .VolumeBindingImmediate
@@ -167,7 +173,7 @@ func (t *volumeModeTestSuite) defineTests(driver TestDriver, pattern testpattern
167
173
case testpatterns .PreprovisionedPV :
168
174
if pattern .VolMode == v1 .PersistentVolumeBlock && ! isBlockSupported {
169
175
ginkgo .It ("should fail to create pod by failing to mount volume [Slow]" , func () {
170
- init ()
176
+ manualInit ()
171
177
defer cleanup ()
172
178
173
179
var err error
@@ -196,13 +202,12 @@ func (t *volumeModeTestSuite) defineTests(driver TestDriver, pattern testpattern
196
202
}()
197
203
framework .ExpectError (err )
198
204
})
199
- // TODO(mkimuram): Add more tests
200
205
}
201
206
202
207
case testpatterns .DynamicPV :
203
208
if pattern .VolMode == v1 .PersistentVolumeBlock && ! isBlockSupported {
204
209
ginkgo .It ("should fail in binding dynamic provisioned PV to PVC [Slow]" , func () {
205
- init ()
210
+ manualInit ()
206
211
defer cleanup ()
207
212
208
213
var err error
@@ -218,12 +223,36 @@ func (t *volumeModeTestSuite) defineTests(driver TestDriver, pattern testpattern
218
223
err = framework .WaitForPersistentVolumeClaimPhase (v1 .ClaimBound , l .cs , l .pvc .Namespace , l .pvc .Name , framework .Poll , framework .ClaimProvisionTimeout )
219
224
framework .ExpectError (err )
220
225
})
221
- // TODO(mkimuram): Add more tests
222
226
}
223
227
default :
224
228
e2elog .Failf ("Volume mode test doesn't support volType: %v" , pattern .VolType )
225
229
}
226
230
231
+ ginkgo .It ("should fail to use a volume in a pod with mismatched mode [Slow]" , func () {
232
+ skipBlockTest (driver )
233
+ init ()
234
+ l .genericVolumeTestResource = * createGenericVolumeTestResource (driver , l .config , pattern )
235
+ defer cleanup ()
236
+
237
+ ginkgo .By ("Creating pod" )
238
+ var err error
239
+ pod := framework .MakeSecPod (l .ns .Name , []* v1.PersistentVolumeClaim {l .pvc }, false , "" , false , false , framework .SELinuxLabel , nil )
240
+ // Change volumeMounts to volumeDevices and the other way around
241
+ pod = swapVolumeMode (pod )
242
+
243
+ // Run the pod
244
+ pod , err = l .cs .CoreV1 ().Pods (l .ns .Name ).Create (pod )
245
+ framework .ExpectNoError (err )
246
+ defer func () {
247
+ framework .ExpectNoError (framework .DeletePodWithWait (f , l .cs , pod ))
248
+ }()
249
+
250
+ // TODO: find a faster way how to check the pod can't start,
251
+ // perhaps when https://github.com/kubernetes/kubernetes/issues/79794 is fixed.
252
+ err = e2epod .WaitTimeoutForPodRunningInNamespace (l .cs , pod .Name , l .ns .Name , framework .PodStartTimeout )
253
+ framework .ExpectError (err , "pod with mismatched block/filesystem volumes should not start" )
254
+ })
255
+
227
256
}
228
257
229
258
func generateConfigsForPreprovisionedPVTest (scName string , volBindMode storagev1.VolumeBindingMode ,
@@ -254,3 +283,29 @@ func generateConfigsForPreprovisionedPVTest(scName string, volBindMode storagev1
254
283
255
284
return scConfig , pvConfig , pvcConfig
256
285
}
286
+
287
+ // swapVolumeMode changes volumeMounts to volumeDevices and the other way around
288
+ func swapVolumeMode (podTemplate * v1.Pod ) * v1.Pod {
289
+ pod := podTemplate .DeepCopy ()
290
+ for c := range pod .Spec .Containers {
291
+ container := & pod .Spec .Containers [c ]
292
+ container .VolumeDevices = []v1.VolumeDevice {}
293
+ container .VolumeMounts = []v1.VolumeMount {}
294
+
295
+ // Change VolumeMounts to VolumeDevices
296
+ for _ , volumeMount := range podTemplate .Spec .Containers [c ].VolumeMounts {
297
+ container .VolumeDevices = append (container .VolumeDevices , v1.VolumeDevice {
298
+ Name : volumeMount .Name ,
299
+ DevicePath : volumeMount .MountPath ,
300
+ })
301
+ }
302
+ // Change VolumeDevices to VolumeMounts
303
+ for _ , volumeDevice := range podTemplate .Spec .Containers [c ].VolumeDevices {
304
+ container .VolumeMounts = append (container .VolumeMounts , v1.VolumeMount {
305
+ Name : volumeDevice .Name ,
306
+ MountPath : volumeDevice .DevicePath ,
307
+ })
308
+ }
309
+ }
310
+ return pod
311
+ }
0 commit comments