diff --git a/pkg/volume/csi/csi_plugin_test.go b/pkg/volume/csi/csi_plugin_test.go index e115bb2596f..8cfd5db58b2 100644 --- a/pkg/volume/csi/csi_plugin_test.go +++ b/pkg/volume/csi/csi_plugin_test.go @@ -41,8 +41,22 @@ import ( volumetest "k8s.io/kubernetes/pkg/volume/testing" ) -// create a plugin mgr to load plugins and setup a fake client +const ( + volumeHostType int = iota + kubeletVolumeHostType + attachDetachVolumeHostType +) + func newTestPlugin(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, string) { + return newTestPluginWithVolumeHost(t, client, kubeletVolumeHostType) +} + +func newTestPluginWithAttachDetachVolumeHost(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, string) { + return newTestPluginWithVolumeHost(t, client, attachDetachVolumeHostType) +} + +// create a plugin mgr to load plugins and setup a fake client +func newTestPluginWithVolumeHost(t *testing.T, client *fakeclient.Clientset, hostType int) (*csiPlugin, string) { tmpDir, err := utiltesting.MkTmpdir("csi-test") if err != nil { t.Fatalf("can't create temp dir: %v", err) @@ -77,16 +91,45 @@ func newTestPlugin(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, stri } } - host := volumetest.NewFakeKubeletVolumeHostWithCSINodeName(t, - tmpDir, - client, - ProbeVolumePlugins(), - "fakeNode", - csiDriverLister, - volumeAttachmentLister, - ) + var host volume.VolumeHost + switch hostType { + case volumeHostType: + host = volumetest.NewFakeVolumeHostWithCSINodeName(t, + tmpDir, + client, + ProbeVolumePlugins(), + "fakeNode", + csiDriverLister, + nil, + ) + case kubeletVolumeHostType: + host = volumetest.NewFakeKubeletVolumeHostWithCSINodeName(t, + tmpDir, + client, + ProbeVolumePlugins(), + "fakeNode", + csiDriverLister, + volumeAttachmentLister, + ) + case attachDetachVolumeHostType: + host = volumetest.NewFakeAttachDetachVolumeHostWithCSINodeName(t, + tmpDir, + client, + ProbeVolumePlugins(), + "fakeNode", + csiDriverLister, + volumeAttachmentLister, + ) + default: + t.Fatalf("Unsupported volume host type") + } - pluginMgr := host.GetPluginMgr() + fakeHost, ok := host.(volumetest.FakeVolumeHost) + if !ok { + t.Fatalf("Unsupported volume host type") + } + + pluginMgr := fakeHost.GetPluginMgr() plug, err := pluginMgr.FindPluginByName(CSIPluginName) if err != nil { t.Fatalf("can't find plugin %v", CSIPluginName) diff --git a/pkg/volume/testing/volume_host.go b/pkg/volume/testing/volume_host.go index ac375dcec45..802d19d3e47 100644 --- a/pkg/volume/testing/volume_host.go +++ b/pkg/volume/testing/volume_host.go @@ -253,7 +253,33 @@ type fakeAttachDetachVolumeHost struct { var _ AttachDetachVolumeHost = &fakeAttachDetachVolumeHost{} var _ FakeVolumeHost = &fakeAttachDetachVolumeHost{} -// TODO: Create constructors for AttachDetachVolumeHost once it's consumed in tests. +func NewFakeAttachDetachVolumeHostWithCSINodeName(t *testing.T, rootDir string, kubeClient clientset.Interface, plugins []VolumePlugin, nodeName string, driverLister storagelistersv1.CSIDriverLister, volumeAttachLister storagelistersv1.VolumeAttachmentLister) FakeVolumeHost { + return newFakeAttachDetachVolumeHost(t, rootDir, kubeClient, plugins, nil, nil, nodeName, driverLister, volumeAttachLister) +} + +func newFakeAttachDetachVolumeHost(t *testing.T, rootDir string, kubeClient clientset.Interface, plugins []VolumePlugin, cloud cloudprovider.Interface, pathToTypeMap map[string]hostutil.FileType, nodeName string, driverLister storagelistersv1.CSIDriverLister, volumeAttachLister storagelistersv1.VolumeAttachmentLister) FakeVolumeHost { + host := &fakeAttachDetachVolumeHost{} + host.rootDir = rootDir + host.kubeClient = kubeClient + host.cloud = cloud + host.nodeName = nodeName + host.csiDriverLister = driverLister + host.volumeAttachmentLister = volumeAttachLister + host.mounter = mount.NewFakeMounter(nil) + host.hostUtil = hostutil.NewFakeHostUtil(pathToTypeMap) + host.exec = &testingexec.FakeExec{DisableScripts: true} + host.pluginMgr = &VolumePluginMgr{} + if err := host.pluginMgr.InitPlugins(plugins, nil /* prober */, host); err != nil { + t.Fatalf("Failed to init plugins while creating fake volume host: %v", err) + } + host.subpather = &subpath.FakeSubpath{} + host.informerFactory = informers.NewSharedInformerFactory(kubeClient, time.Minute) + // Wait until the InitPlugins setup is finished before returning from this setup func + if err := host.WaitForKubeletErrNil(); err != nil { + t.Fatalf("Failed to wait for kubelet err to be nil while creating fake volume host: %v", err) + } + return host +} func (f *fakeAttachDetachVolumeHost) CSINodeLister() storagelistersv1.CSINodeLister { // not needed for testing