Skip to content

Commit

Permalink
Plugin system testing (#1255)
Browse files Browse the repository at this point in the history
* first round of tests for plugin

* missing yaml should be malformed yaml, implement that test

* fix malformed plugin test and add test for loading multiple plugins of
different types

* add parallel testdata for windows due to the different folder layout and
modify the tests to take that into account

* remove windows specific test file

* Change require.NotEmpty to assert.Equal(1, ...)

* Move all test data into `testdata` and fix plugin tests to point to the
new place.
  • Loading branch information
br-lewis authored and bamarni committed Jul 3, 2018
1 parent 3ef1378 commit ea43fd8
Show file tree
Hide file tree
Showing 30 changed files with 258 additions and 5 deletions.
7 changes: 5 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/plugin/manager.go
Expand Up @@ -165,7 +165,7 @@ func (m *Manager) loadPluginFromPackage(plugin *Plugin) error {
cmdExe := filepath.Join(plugin.BinDir, binary.Name())
infoCmd, err := exec.Command(cmdExe, commandName, "--info").Output()
if err != nil {
m.Logger.Fatal(err)
m.Logger.Warning(err)
}
cmd.Description = strings.TrimSpace(string(infoCmd))

Expand All @@ -188,14 +188,14 @@ func (m *Manager) loadPluginFromPackage(plugin *Plugin) error {
func (m *Manager) persist(plugin *Plugin) error {
// We need an env directory for `plugin.yaml`.
envFilePath := filepath.Join(plugin.dir, "env")
if _, err := os.Stat(envFilePath); os.IsNotExist(err) {
if _, err := m.Fs.Stat(envFilePath); os.IsNotExist(err) {
return errors.New(envFilePath + " does not exist")
}

// We should not overwrite `plugin.yaml`, this method should only be used
// to create a minimal `plugin.yaml` if the user has an old plugin.
pluginFilePath := filepath.Join(plugin.dir, "env", "plugin.yaml")
if _, err := os.Stat(pluginFilePath); err == nil {
if _, err := m.Fs.Stat(pluginFilePath); err == nil {
return errors.New(pluginFilePath + " already exists")
}

Expand Down
119 changes: 119 additions & 0 deletions pkg/plugin/plugin_test.go
@@ -0,0 +1,119 @@
package plugin_test

import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/dcos/dcos-cli/pkg/plugin"
)

func TestLoadNoPlugin(t *testing.T) {
subcommandsDir := targetSubcommandDir(t, "no_plugins")

m := plugin.Manager{
Fs: afero.NewMemMapFs(),
Dir: subcommandsDir,
}

plugins := m.Plugins()
require.Empty(t, plugins)
}

func TestLoadNewPlugin(t *testing.T) {
subcommandsDir := targetSubcommandDir(t, "only_new_plugin")
logger, _ := test.NewNullLogger()

m := plugin.Manager{
Fs: roFs(),
Dir: subcommandsDir,
Logger: logger,
}

plugins := m.Plugins()
assert.Equal(t, 1, len(plugins))
plugin := plugins[0]

assert.Equal(t, "new-test", plugin.Name)
}

func TestLoadOldPlugin(t *testing.T) {
subcommandsDir := targetSubcommandDir(t, "only_old_plugin")

logger, _ := test.NewNullLogger()

m := plugin.Manager{
Fs: roFs(),
Dir: subcommandsDir,
Logger: logger,
}

plugins := m.Plugins()
assert.Equal(t, 1, len(plugins))

plugin := plugins[0]

assert.Equal(t, "old-test", plugin.Name, "plugin name does not match name found in package.json")

pluginExists, err := afero.Exists(m.Fs, filepath.Join(subcommandsDir, "old", "env", "plugin.yaml"))
require.NoError(t, err)

assert.True(t, pluginExists, "plugin.yaml was not created")
}

func TestIgnoreMalformedYaml(t *testing.T) {
dir := targetSubcommandDir(t, "malformed_yaml")
logger, _ := test.NewNullLogger()

m := plugin.Manager{
Fs: roFs(),
Dir: dir,
Logger: logger,
}
plugins := m.Plugins()

assert.Empty(t, plugins)
}

func TestLoadingMultiple(t *testing.T) {
dir := targetSubcommandDir(t, "correct_and_malformed")
logger, _ := test.NewNullLogger()

m := plugin.Manager{
Fs: roFs(),
Dir: dir,
Logger: logger,
}

plugins := m.Plugins()
// 3 directories, one new plugin, one old, and one malformed
// malformed should be ignored, leaving 2 loaded plugins
assert.Equal(t, 2, len(plugins))
}

func targetSubcommandDir(t *testing.T, name string) string {
wd, err := os.Getwd()
require.NoError(t, err)

var testdataDir string
if runtime.GOOS == "windows" {
testdataDir = filepath.Join(wd, "testdata", "windows")
} else {
testdataDir = filepath.Join(wd, "testdata", "unix")
}
return filepath.Join(testdataDir, name)
}

func roFs() afero.Fs {
base := afero.NewOsFs()
roBase := afero.NewReadOnlyFs(base)
overlay := afero.NewCopyOnWriteFs(roBase, afero.NewMemMapFs())

return overlay
}
2 changes: 2 additions & 0 deletions pkg/plugin/testdata/.gitignore
@@ -0,0 +1,2 @@
# env is ignored in the root gitignore, this resets that so the testdata can be tracked
!env/
Empty file.
@@ -0,0 +1 @@
not valid yaml
Empty file.
@@ -0,0 +1,2 @@
name: new-test
description: plugin test with the new schema
Empty file.
@@ -0,0 +1,5 @@
{
"name": "old-test",
"description": "barebones old style plugin",
"version": "0.0.1"
}
Empty file.
@@ -0,0 +1 @@
not valid yaml
Empty file.
2 changes: 2 additions & 0 deletions pkg/plugin/testdata/unix/only_new_plugin/new/env/plugin.yaml
@@ -0,0 +1,2 @@
name: new-test
description: plugin test with the new schema
Empty file.
5 changes: 5 additions & 0 deletions pkg/plugin/testdata/unix/only_old_plugin/old/package.json
@@ -0,0 +1,5 @@
{
"name": "old-test",
"description": "barebones old style plugin",
"version": "0.0.1"
}
2 changes: 2 additions & 0 deletions pkg/plugin/testdata/windows/.gitignore
@@ -0,0 +1,2 @@
# env is ignored in the root gitignore, this resets that so the testdata can be tracked
!env/
Empty file.
@@ -0,0 +1 @@
not valid yaml
Empty file.
@@ -0,0 +1,2 @@
name: new-test
description: plugin test with the new schema
Empty file.
@@ -0,0 +1,5 @@
{
"name": "old-test",
"description": "barebones old style plugin",
"version": "0.0.1"
}
Empty file.
@@ -0,0 +1 @@
not valid yaml
Empty file.
@@ -0,0 +1,2 @@
name: new-test
description: plugin test with the new schema
Empty file.
5 changes: 5 additions & 0 deletions pkg/plugin/testdata/windows/only_old_plugin/old/package.json
@@ -0,0 +1,5 @@
{
"name": "old-test",
"description": "barebones old style plugin",
"version": "0.0.1"
}
95 changes: 95 additions & 0 deletions vendor/github.com/sirupsen/logrus/hooks/test/test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ea43fd8

Please sign in to comment.