diff --git a/pkg/volume/host_path/host_path_test.go b/pkg/volume/host_path/host_path_test.go index 2189f1e1cbb..9e7fe52c642 100644 --- a/pkg/volume/host_path/host_path_test.go +++ b/pkg/volume/host_path/host_path_test.go @@ -63,7 +63,8 @@ func TestGetAccessModes(t *testing.T) { func TestRecycler(t *testing.T) { plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins([]volume.VolumePlugin{&hostPathPlugin{nil, newMockRecycler}}, volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + volumeHost := volume.NewFakeVolumeHost("/tmp/fake", nil, nil) + plugMgr.InitPlugins([]volume.VolumePlugin{&hostPathPlugin{nil, volume.NewFakeRecycler}}, volumeHost) spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/foo"}}}}} plug, err := plugMgr.FindRecyclablePluginBySpec(spec) @@ -82,26 +83,6 @@ func TestRecycler(t *testing.T) { } } -func newMockRecycler(spec *volume.Spec, host volume.VolumeHost) (volume.Recycler, error) { - return &mockRecycler{ - path: spec.PersistentVolume.Spec.HostPath.Path, - }, nil -} - -type mockRecycler struct { - path string - host volume.VolumeHost -} - -func (r *mockRecycler) GetPath() string { - return r.path -} - -func (r *mockRecycler) Recycle() error { - // return nil means recycle passed - return nil -} - func TestPlugin(t *testing.T) { plugMgr := volume.VolumePluginMgr{} plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost("fake", nil, nil)) diff --git a/pkg/volume/testing.go b/pkg/volume/testing.go index 7ceb94d4f25..b347f5461f3 100644 --- a/pkg/volume/testing.go +++ b/pkg/volume/testing.go @@ -17,6 +17,7 @@ limitations under the License. package volume import ( + "fmt" "os" "path" @@ -125,7 +126,7 @@ func (plugin *FakeVolumePlugin) NewCleaner(volName string, podUID types.UID, mou } func (plugin *FakeVolumePlugin) NewRecycler(spec *Spec) (Recycler, error) { - return &FakeRecycler{"/attributesTransferredFromSpec"}, nil + return &fakeRecycler{"/attributesTransferredFromSpec"}, nil } func (plugin *FakeVolumePlugin) GetAccessModes() []api.PersistentVolumeAccessMode { @@ -162,15 +163,24 @@ func (fv *FakeVolume) TearDownAt(dir string) error { return os.RemoveAll(dir) } -type FakeRecycler struct { +type fakeRecycler struct { path string } -func (fr *FakeRecycler) Recycle() error { +func (fr *fakeRecycler) Recycle() error { // nil is success, else error return nil } -func (fr *FakeRecycler) GetPath() string { +func (fr *fakeRecycler) GetPath() string { return fr.path } + +func NewFakeRecycler(spec *Spec, host VolumeHost) (Recycler, error) { + if spec.PersistentVolume == nil || spec.PersistentVolume.Spec.HostPath == nil { + return nil, fmt.Errorf("fakeRecycler only supports spec.PersistentVolume.Spec.HostPath") + } + return &fakeRecycler{ + path: spec.PersistentVolume.Spec.HostPath.Path, + }, nil +}