Skip to content

Commit de675e0

Browse files
authored
Handle nil HostPath type in GetVolumeHostPathType and add unit tests. (#3965)
Signed-off-by: daiping8 <[email protected]>
1 parent e37b70c commit de675e0

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

apiserver/pkg/model/volumes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ func GetVolumeMountPropagation(mount *corev1.VolumeMount) api.Volume_MountPropag
130130
}
131131

132132
func GetVolumeHostPathType(vol *corev1.Volume) api.Volume_HostPathType {
133+
if vol.VolumeSource.HostPath.Type == nil {
134+
return api.Volume_DIRECTORY
135+
}
133136
if *vol.VolumeSource.HostPath.Type == corev1.HostPathFile {
134137
return api.Volume_FILE
135138
}

apiserver/pkg/model/volumes_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,46 @@ func TestPopulateVolumes(t *testing.T) {
199199
}
200200
}
201201
}
202+
203+
func TestGetVolumeHostPathType(t *testing.T) {
204+
vol1 := corev1.Volume{
205+
VolumeSource: corev1.VolumeSource{
206+
HostPath: &corev1.HostPathVolumeSource{
207+
Path: "/tmp",
208+
Type: nil,
209+
},
210+
},
211+
}
212+
result1 := GetVolumeHostPathType(&vol1)
213+
if result1 != api.Volume_DIRECTORY {
214+
t.Errorf("Expected FILE type, got %v", result1)
215+
}
216+
217+
typeFile := corev1.HostPathFile
218+
vol2 := corev1.Volume{
219+
VolumeSource: corev1.VolumeSource{
220+
HostPath: &corev1.HostPathVolumeSource{
221+
Path: "/tmp",
222+
Type: &typeFile,
223+
},
224+
},
225+
}
226+
result2 := GetVolumeHostPathType(&vol2)
227+
if result2 != api.Volume_FILE {
228+
t.Errorf("Expected FILE type, got %v", result2)
229+
}
230+
231+
typeDir := corev1.HostPathDirectory
232+
vol3 := corev1.Volume{
233+
VolumeSource: corev1.VolumeSource{
234+
HostPath: &corev1.HostPathVolumeSource{
235+
Path: "/tmp",
236+
Type: &typeDir,
237+
},
238+
},
239+
}
240+
result3 := GetVolumeHostPathType(&vol3)
241+
if result3 != api.Volume_DIRECTORY {
242+
t.Errorf("Expected DIRECTORY type, got %v", result3)
243+
}
244+
}

0 commit comments

Comments
 (0)