2018-12-11 09:03:47 -05:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-29 04:28:43 -05:00
|
|
|
"github.com/docker/cli/cli/config"
|
2018-12-11 09:03:47 -05:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
|
|
"github.com/docker/cli/internal/test"
|
docker info: list CLI plugins alphabetically
Before this change, plugins were listed in a random order:
Client:
Debug Mode: false
Plugins:
doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1)
shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1)
app: Docker Application (Docker Inc., v0.8.0)
buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
With this change, plugins are listed alphabetically:
Client:
Debug Mode: false
Plugins:
app: Docker Application (Docker Inc., v0.8.0)
buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1)
shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-31 08:26:08 -05:00
|
|
|
"github.com/spf13/cobra"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/fs"
|
2018-12-11 09:03:47 -05:00
|
|
|
)
|
|
|
|
|
2018-12-11 09:50:04 -05:00
|
|
|
func TestListPluginCandidates(t *testing.T) {
|
|
|
|
// Populate a selection of directories with various shadowed and bogus/obscure plugin candidates.
|
|
|
|
// For the purposes of this test no contents is required and permissions are irrelevant.
|
|
|
|
dir := fs.NewDir(t, t.Name(),
|
|
|
|
fs.WithDir(
|
|
|
|
"plugins1",
|
|
|
|
fs.WithFile("docker-plugin1", ""), // This appears in each directory
|
|
|
|
fs.WithFile("not-a-plugin", ""), // Should be ignored
|
|
|
|
fs.WithFile("docker-symlinked1", ""), // This and ...
|
|
|
|
fs.WithSymlink("docker-symlinked2", "docker-symlinked1"), // ... this should both appear
|
|
|
|
fs.WithDir("ignored1"), // A directory should be ignored
|
|
|
|
),
|
|
|
|
fs.WithDir(
|
|
|
|
"plugins2",
|
|
|
|
fs.WithFile("docker-plugin1", ""),
|
|
|
|
fs.WithFile("also-not-a-plugin", ""),
|
|
|
|
fs.WithFile("docker-hardlink1", ""), // This and ...
|
|
|
|
fs.WithHardlink("docker-hardlink2", "docker-hardlink1"), // ... this should both appear
|
|
|
|
fs.WithDir("ignored2"),
|
|
|
|
),
|
|
|
|
fs.WithDir(
|
|
|
|
"plugins3-target", // Will be referenced as a symlink from below
|
|
|
|
fs.WithFile("docker-plugin1", ""),
|
|
|
|
fs.WithDir("ignored3"),
|
|
|
|
fs.WithSymlink("docker-brokensymlink", "broken"), // A broken symlink is still a candidate (but would fail tests later)
|
|
|
|
fs.WithFile("non-plugin-symlinked", ""), // This shouldn't appear, but ...
|
|
|
|
fs.WithSymlink("docker-symlinked", "non-plugin-symlinked"), // ... this link to it should.
|
|
|
|
),
|
|
|
|
fs.WithSymlink("plugins3", "plugins3-target"),
|
|
|
|
fs.WithFile("/plugins4", ""),
|
|
|
|
fs.WithSymlink("plugins5", "plugins5-nonexistent-target"),
|
|
|
|
)
|
|
|
|
defer dir.Remove()
|
|
|
|
|
|
|
|
var dirs []string
|
|
|
|
for _, d := range []string{"plugins1", "nonexistent", "plugins2", "plugins3", "plugins4", "plugins5"} {
|
|
|
|
dirs = append(dirs, dir.Join(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
candidates, err := listPluginCandidates(dirs)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
exp := map[string][]string{
|
|
|
|
"plugin1": {
|
|
|
|
dir.Join("plugins1", "docker-plugin1"),
|
|
|
|
dir.Join("plugins2", "docker-plugin1"),
|
|
|
|
dir.Join("plugins3", "docker-plugin1"),
|
|
|
|
},
|
|
|
|
"symlinked1": {
|
|
|
|
dir.Join("plugins1", "docker-symlinked1"),
|
|
|
|
},
|
|
|
|
"symlinked2": {
|
|
|
|
dir.Join("plugins1", "docker-symlinked2"),
|
|
|
|
},
|
|
|
|
"hardlink1": {
|
|
|
|
dir.Join("plugins2", "docker-hardlink1"),
|
|
|
|
},
|
|
|
|
"hardlink2": {
|
|
|
|
dir.Join("plugins2", "docker-hardlink2"),
|
|
|
|
},
|
|
|
|
"brokensymlink": {
|
|
|
|
dir.Join("plugins3", "docker-brokensymlink"),
|
|
|
|
},
|
|
|
|
"symlinked": {
|
|
|
|
dir.Join("plugins3", "docker-symlinked"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.DeepEqual(t, candidates, exp)
|
|
|
|
}
|
|
|
|
|
2022-02-03 04:37:55 -05:00
|
|
|
func TestGetPlugin(t *testing.T) {
|
|
|
|
dir := fs.NewDir(t, t.Name(),
|
|
|
|
fs.WithFile("docker-bbb", `
|
|
|
|
#!/bin/sh
|
2022-09-30 13:13:22 -04:00
|
|
|
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
|
2022-02-03 04:37:55 -05:00
|
|
|
fs.WithFile("docker-aaa", `
|
|
|
|
#!/bin/sh
|
2022-09-30 13:13:22 -04:00
|
|
|
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
|
2022-02-03 04:37:55 -05:00
|
|
|
)
|
|
|
|
defer dir.Remove()
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(nil)
|
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{CLIPluginsExtraDirs: []string{dir.Path()}})
|
|
|
|
|
|
|
|
plugin, err := GetPlugin("bbb", cli, &cobra.Command{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, plugin.Name, "bbb")
|
|
|
|
|
|
|
|
_, err = GetPlugin("ccc", cli, &cobra.Command{})
|
|
|
|
assert.Error(t, err, "Error: No such CLI plugin: ccc")
|
|
|
|
assert.Assert(t, IsNotFound(err))
|
|
|
|
}
|
|
|
|
|
docker info: list CLI plugins alphabetically
Before this change, plugins were listed in a random order:
Client:
Debug Mode: false
Plugins:
doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1)
shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1)
app: Docker Application (Docker Inc., v0.8.0)
buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
With this change, plugins are listed alphabetically:
Client:
Debug Mode: false
Plugins:
app: Docker Application (Docker Inc., v0.8.0)
buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1)
shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-31 08:26:08 -05:00
|
|
|
func TestListPluginsIsSorted(t *testing.T) {
|
|
|
|
dir := fs.NewDir(t, t.Name(),
|
|
|
|
fs.WithFile("docker-bbb", `
|
|
|
|
#!/bin/sh
|
2022-09-30 13:13:22 -04:00
|
|
|
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
|
docker info: list CLI plugins alphabetically
Before this change, plugins were listed in a random order:
Client:
Debug Mode: false
Plugins:
doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1)
shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1)
app: Docker Application (Docker Inc., v0.8.0)
buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
With this change, plugins are listed alphabetically:
Client:
Debug Mode: false
Plugins:
app: Docker Application (Docker Inc., v0.8.0)
buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1)
shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-31 08:26:08 -05:00
|
|
|
fs.WithFile("docker-aaa", `
|
|
|
|
#!/bin/sh
|
2022-09-30 13:13:22 -04:00
|
|
|
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
|
docker info: list CLI plugins alphabetically
Before this change, plugins were listed in a random order:
Client:
Debug Mode: false
Plugins:
doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1)
shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1)
app: Docker Application (Docker Inc., v0.8.0)
buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
With this change, plugins are listed alphabetically:
Client:
Debug Mode: false
Plugins:
app: Docker Application (Docker Inc., v0.8.0)
buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
doodle: Docker Doodles all around! 🐳 🎃 (thaJeztah, v0.0.1)
shell: Open a browser shell on the Docker Host. (thaJeztah, v0.0.1)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-12-31 08:26:08 -05:00
|
|
|
)
|
|
|
|
defer dir.Remove()
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(nil)
|
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{CLIPluginsExtraDirs: []string{dir.Path()}})
|
|
|
|
|
|
|
|
plugins, err := ListPlugins(cli, &cobra.Command{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// We're only interested in the plugins we created for testing this, and only
|
|
|
|
// if they appear in the expected order
|
|
|
|
var names []string
|
|
|
|
for _, p := range plugins {
|
|
|
|
if p.Name == "aaa" || p.Name == "bbb" {
|
|
|
|
names = append(names, p.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.DeepEqual(t, names, []string{"aaa", "bbb"})
|
|
|
|
}
|
|
|
|
|
2018-12-11 09:03:47 -05:00
|
|
|
func TestErrPluginNotFound(t *testing.T) {
|
|
|
|
var err error = errPluginNotFound("test")
|
|
|
|
err.(errPluginNotFound).NotFound()
|
|
|
|
assert.Error(t, err, "Error: No such CLI plugin: test")
|
|
|
|
assert.Assert(t, IsNotFound(err))
|
|
|
|
assert.Assert(t, !IsNotFound(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPluginDirs(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(nil)
|
|
|
|
|
2019-03-07 09:28:42 -05:00
|
|
|
pluginDir, err := config.Path("cli-plugins")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
expected := append([]string{pluginDir}, defaultSystemPluginDirs...)
|
2018-12-11 09:03:47 -05:00
|
|
|
|
2019-03-07 09:28:42 -05:00
|
|
|
var pluginDirs []string
|
|
|
|
pluginDirs, err = getPluginDirs(cli)
|
|
|
|
assert.Equal(t, strings.Join(expected, ":"), strings.Join(pluginDirs, ":"))
|
|
|
|
assert.NilError(t, err)
|
2018-12-11 09:03:47 -05:00
|
|
|
|
|
|
|
extras := []string{
|
|
|
|
"foo", "bar", "baz",
|
|
|
|
}
|
|
|
|
expected = append(extras, expected...)
|
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
|
|
|
CLIPluginsExtraDirs: extras,
|
|
|
|
})
|
2019-03-07 09:28:42 -05:00
|
|
|
pluginDirs, err = getPluginDirs(cli)
|
|
|
|
assert.DeepEqual(t, expected, pluginDirs)
|
|
|
|
assert.NilError(t, err)
|
2018-12-11 09:03:47 -05:00
|
|
|
}
|