@@ -28,7 +28,9 @@ import (
28
28
v1 "k8s.io/api/core/v1"
29
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
30
31
+ "k8s.io/apimachinery/pkg/types"
31
32
_ "k8s.io/kubernetes/pkg/apis/core/install"
33
+ "k8s.io/utils/mount"
32
34
)
33
35
34
36
func validateDirExists (dir string ) error {
@@ -154,3 +156,115 @@ func TestCleanupOrphanedPodDirs(t *testing.T) {
154
156
})
155
157
}
156
158
}
159
+
160
+ func TestPodVolumesExistWithMount (t * testing.T ) {
161
+ poduid := types .UID ("poduid" )
162
+ testCases := map [string ]struct {
163
+ prepareFunc func (kubelet * Kubelet ) error
164
+ expected bool
165
+ }{
166
+ "noncsivolume-dir-not-exist" : {
167
+ prepareFunc : func (kubelet * Kubelet ) error {
168
+ return nil
169
+ },
170
+ expected : false ,
171
+ },
172
+ "noncsivolume-dir-exist-noplugins" : {
173
+ prepareFunc : func (kubelet * Kubelet ) error {
174
+ podDir := kubelet .getPodDir (poduid )
175
+ return os .MkdirAll (filepath .Join (podDir , "volumes/" ), 0750 )
176
+ },
177
+ expected : false ,
178
+ },
179
+ "noncsivolume-dir-exist-nomount" : {
180
+ prepareFunc : func (kubelet * Kubelet ) error {
181
+ podDir := kubelet .getPodDir (poduid )
182
+ return os .MkdirAll (filepath .Join (podDir , "volumes/plugin/name" ), 0750 )
183
+ },
184
+ expected : false ,
185
+ },
186
+ "noncsivolume-dir-exist-with-mount" : {
187
+ prepareFunc : func (kubelet * Kubelet ) error {
188
+ podDir := kubelet .getPodDir (poduid )
189
+ volumePath := filepath .Join (podDir , "volumes/plugin/name" )
190
+ if err := os .MkdirAll (volumePath , 0750 ); err != nil {
191
+ return err
192
+ }
193
+ fm := mount .NewFakeMounter (
194
+ []mount.MountPoint {
195
+ {Device : "/dev/sdb" , Path : volumePath },
196
+ })
197
+ kubelet .mounter = fm
198
+ return nil
199
+ },
200
+ expected : true ,
201
+ },
202
+ "noncsivolume-dir-exist-nomount-withcsimountpath" : {
203
+ prepareFunc : func (kubelet * Kubelet ) error {
204
+ podDir := kubelet .getPodDir (poduid )
205
+ volumePath := filepath .Join (podDir , "volumes/plugin/name/mount" )
206
+ if err := os .MkdirAll (volumePath , 0750 ); err != nil {
207
+ return err
208
+ }
209
+ fm := mount .NewFakeMounter (
210
+ []mount.MountPoint {
211
+ {Device : "/dev/sdb" , Path : volumePath },
212
+ })
213
+ kubelet .mounter = fm
214
+ return nil
215
+ },
216
+ expected : false ,
217
+ },
218
+ "csivolume-dir-exist-nomount" : {
219
+ prepareFunc : func (kubelet * Kubelet ) error {
220
+ podDir := kubelet .getPodDir (poduid )
221
+ volumePath := filepath .Join (podDir , "volumes/kubernetes.io~csi/name" )
222
+ return os .MkdirAll (volumePath , 0750 )
223
+ },
224
+ expected : false ,
225
+ },
226
+ "csivolume-dir-exist-mount-nocsimountpath" : {
227
+ prepareFunc : func (kubelet * Kubelet ) error {
228
+ podDir := kubelet .getPodDir (poduid )
229
+ volumePath := filepath .Join (podDir , "volumes/kubernetes.io~csi/name/mount" )
230
+ return os .MkdirAll (volumePath , 0750 )
231
+ },
232
+ expected : false ,
233
+ },
234
+ "csivolume-dir-exist-withcsimountpath" : {
235
+ prepareFunc : func (kubelet * Kubelet ) error {
236
+ podDir := kubelet .getPodDir (poduid )
237
+ volumePath := filepath .Join (podDir , "volumes/kubernetes.io~csi/name/mount" )
238
+ if err := os .MkdirAll (volumePath , 0750 ); err != nil {
239
+ return err
240
+ }
241
+ fm := mount .NewFakeMounter (
242
+ []mount.MountPoint {
243
+ {Device : "/dev/sdb" , Path : volumePath },
244
+ })
245
+ kubelet .mounter = fm
246
+ return nil
247
+ },
248
+ expected : true ,
249
+ },
250
+ }
251
+
252
+ for name , tc := range testCases {
253
+ t .Run (name , func (t * testing.T ) {
254
+ testKubelet := newTestKubelet (t , false /* controllerAttachDetachEnabled */ )
255
+ defer testKubelet .Cleanup ()
256
+ kubelet := testKubelet .kubelet
257
+
258
+ if tc .prepareFunc != nil {
259
+ if err := tc .prepareFunc (kubelet ); err != nil {
260
+ t .Fatalf ("%s failed preparation: %v" , name , err )
261
+ }
262
+ }
263
+
264
+ exist := kubelet .podVolumesExist (poduid )
265
+ if tc .expected != exist {
266
+ t .Errorf ("%s failed: expected %t, got %t" , name , tc .expected , exist )
267
+ }
268
+ })
269
+ }
270
+ }
0 commit comments