Fixed potential nil dereference in hostpath unit test

This commit is contained in:
yuzhiquan 2020-07-20 20:58:36 +08:00
parent a60cbd5535
commit aca107eee5

View File

@ -52,11 +52,11 @@ func TestCanSupport(t *testing.T) {
plugMgr := volume.VolumePluginMgr{} plugMgr := volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost(t, "fake", nil, nil)) plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost(t, "fake", nil, nil))
plug, err := plugMgr.FindPluginByName("kubernetes.io/host-path") plug, err := plugMgr.FindPluginByName(hostPathPluginName)
if err != nil { if err != nil {
t.Errorf("Can't find the plugin by name") t.Fatal("Can't find the plugin by name")
} }
if plug.GetPluginName() != "kubernetes.io/host-path" { if plug.GetPluginName() != hostPathPluginName {
t.Errorf("Wrong name: %s", plug.GetPluginName()) t.Errorf("Wrong name: %s", plug.GetPluginName())
} }
if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{}}}}) { if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{}}}}) {
@ -74,9 +74,9 @@ func TestGetAccessModes(t *testing.T) {
plugMgr := volume.VolumePluginMgr{} plugMgr := volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost(t, "/tmp/fake", nil, nil)) plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost(t, "/tmp/fake", nil, nil))
plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/host-path") plug, err := plugMgr.FindPersistentPluginByName(hostPathPluginName)
if err != nil { if err != nil {
t.Errorf("Can't find the plugin by name") t.Fatal("Can't find the plugin by name")
} }
if len(plug.GetAccessModes()) != 1 || plug.GetAccessModes()[0] != v1.ReadWriteOnce { if len(plug.GetAccessModes()) != 1 || plug.GetAccessModes()[0] != v1.ReadWriteOnce {
t.Errorf("Expected %s PersistentVolumeAccessMode", v1.ReadWriteOnce) t.Errorf("Expected %s PersistentVolumeAccessMode", v1.ReadWriteOnce)
@ -109,7 +109,7 @@ func TestDeleter(t *testing.T) {
spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: tempPath}}}}} spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: tempPath}}}}}
plug, err := plugMgr.FindDeletablePluginBySpec(spec) plug, err := plugMgr.FindDeletablePluginBySpec(spec)
if err != nil { if err != nil {
t.Errorf("Can't find the plugin by name") t.Fatal("Can't find the plugin by name")
} }
deleter, err := plug.NewDeleter(spec) deleter, err := plug.NewDeleter(spec)
if err != nil { if err != nil {
@ -230,9 +230,9 @@ func TestPlugin(t *testing.T) {
plugMgr := volume.VolumePluginMgr{} plugMgr := volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost(t, "fake", nil, nil)) plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost(t, "fake", nil, nil))
plug, err := plugMgr.FindPluginByName("kubernetes.io/host-path") plug, err := plugMgr.FindPluginByName(hostPathPluginName)
if err != nil { if err != nil {
t.Errorf("Can't find the plugin by name") t.Fatal("Can't find the plugin by name")
} }
volPath := "/tmp/vol1" volPath := "/tmp/vol1"