Added unit test for flex volume probe and updated DynamicPluginProber Probe() interface description

This commit is contained in:
Scott Nice 2021-12-14 10:01:02 -05:00
parent 5183513661
commit d9cbe5d314
2 changed files with 37 additions and 8 deletions

View File

@ -30,8 +30,9 @@ import (
)
const (
pluginDir = "/flexvolume"
driverName = "fake-driver"
pluginDir = "/flexvolume"
driverName = "fake-driver"
errorDriverName = "error-driver"
)
func assertPathSuffix(t *testing.T, dir1 string, dir2 string) {
@ -185,7 +186,7 @@ func TestEmptyPluginDir(t *testing.T) {
pluginDir: pluginDir,
watcher: watcher,
fs: fs,
factory: fakePluginFactory{error: false},
factory: fakePluginFactory{},
}
prober.Init()
@ -279,7 +280,7 @@ func TestProberError(t *testing.T) {
pluginDir: pluginDir,
watcher: watcher,
fs: fs,
factory: fakePluginFactory{error: true},
factory: fakePluginFactory{errorDriver: driverName},
}
installDriver(driverName, fs)
prober.Init()
@ -288,6 +289,34 @@ func TestProberError(t *testing.T) {
assert.Error(t, err)
}
func TestProberSuccessAndError(t *testing.T) {
// Arrange
fs := utilfs.NewTempFs()
watcher := newFakeWatcher()
prober := &flexVolumeProber{
pluginDir: pluginDir,
watcher: watcher,
fs: fs,
factory: fakePluginFactory{errorDriver: errorDriverName},
}
installDriver(driverName, fs)
prober.Init()
installDriver(errorDriverName, fs)
driverPath := filepath.Join(pluginDir, errorDriverName)
watcher.TriggerEvent(fsnotify.Create, filepath.Join(driverPath, errorDriverName))
// Act
events, err := prober.Probe()
// Assert
assert.Equal(t, 1, len(events))
assert.Equal(t, volume.ProbeAddOrUpdate, events[0].Op)
assert.Equal(t, driverName, events[0].PluginName)
assert.Error(t, err)
}
// Installs a mock driver (an empty file) in the mock fs.
func installDriver(driverName string, fs utilfs.Filesystem) {
driverPath := filepath.Join(pluginDir, driverName)
@ -307,7 +336,7 @@ func initTestEnvironment(t *testing.T) (
pluginDir: pluginDir,
watcher: watcher,
fs: fs,
factory: fakePluginFactory{error: false},
factory: fakePluginFactory{},
}
driverPath = filepath.Join(pluginDir, driverName)
installDriver(driverName, fs)
@ -320,13 +349,13 @@ func initTestEnvironment(t *testing.T) (
// Fake Flexvolume plugin
type fakePluginFactory struct {
error bool // Indicates whether an error should be returned.
errorDriver string // the name of the driver in error
}
var _ PluginFactory = fakePluginFactory{}
func (m fakePluginFactory) NewFlexVolumePlugin(_, driverName string, _ exec.Interface) (volume.VolumePlugin, error) {
if m.error {
if driverName == m.errorDriver {
return nil, fmt.Errorf("Flexvolume plugin error")
}
// Dummy Flexvolume plugin. Prober never interacts with the plugin.

View File

@ -132,7 +132,7 @@ type NodeResizeOptions struct {
type DynamicPluginProber interface {
Init() error
// If an error occurs, events are undefined.
// aggregates events for successful drivers and errors for failed drivers
Probe() (events []ProbeEvent, err error)
}