github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli-plugins/manager/manager_test.go (about) 1 package manager 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/docker/cli/cli/config" 8 "github.com/docker/cli/cli/config/configfile" 9 "github.com/docker/cli/internal/test" 10 "gotest.tools/assert" 11 "gotest.tools/fs" 12 ) 13 14 func TestListPluginCandidates(t *testing.T) { 15 // Populate a selection of directories with various shadowed and bogus/obscure plugin candidates. 16 // For the purposes of this test no contents is required and permissions are irrelevant. 17 dir := fs.NewDir(t, t.Name(), 18 fs.WithDir( 19 "plugins1", 20 fs.WithFile("docker-plugin1", ""), // This appears in each directory 21 fs.WithFile("not-a-plugin", ""), // Should be ignored 22 fs.WithFile("docker-symlinked1", ""), // This and ... 23 fs.WithSymlink("docker-symlinked2", "docker-symlinked1"), // ... this should both appear 24 fs.WithDir("ignored1"), // A directory should be ignored 25 ), 26 fs.WithDir( 27 "plugins2", 28 fs.WithFile("docker-plugin1", ""), 29 fs.WithFile("also-not-a-plugin", ""), 30 fs.WithFile("docker-hardlink1", ""), // This and ... 31 fs.WithHardlink("docker-hardlink2", "docker-hardlink1"), // ... this should both appear 32 fs.WithDir("ignored2"), 33 ), 34 fs.WithDir( 35 "plugins3-target", // Will be referenced as a symlink from below 36 fs.WithFile("docker-plugin1", ""), 37 fs.WithDir("ignored3"), 38 fs.WithSymlink("docker-brokensymlink", "broken"), // A broken symlink is still a candidate (but would fail tests later) 39 fs.WithFile("non-plugin-symlinked", ""), // This shouldn't appear, but ... 40 fs.WithSymlink("docker-symlinked", "non-plugin-symlinked"), // ... this link to it should. 41 ), 42 fs.WithSymlink("plugins3", "plugins3-target"), 43 fs.WithFile("/plugins4", ""), 44 fs.WithSymlink("plugins5", "plugins5-nonexistent-target"), 45 ) 46 defer dir.Remove() 47 48 var dirs []string 49 for _, d := range []string{"plugins1", "nonexistent", "plugins2", "plugins3", "plugins4", "plugins5"} { 50 dirs = append(dirs, dir.Join(d)) 51 } 52 53 candidates, err := listPluginCandidates(dirs) 54 assert.NilError(t, err) 55 exp := map[string][]string{ 56 "plugin1": { 57 dir.Join("plugins1", "docker-plugin1"), 58 dir.Join("plugins2", "docker-plugin1"), 59 dir.Join("plugins3", "docker-plugin1"), 60 }, 61 "symlinked1": { 62 dir.Join("plugins1", "docker-symlinked1"), 63 }, 64 "symlinked2": { 65 dir.Join("plugins1", "docker-symlinked2"), 66 }, 67 "hardlink1": { 68 dir.Join("plugins2", "docker-hardlink1"), 69 }, 70 "hardlink2": { 71 dir.Join("plugins2", "docker-hardlink2"), 72 }, 73 "brokensymlink": { 74 dir.Join("plugins3", "docker-brokensymlink"), 75 }, 76 "symlinked": { 77 dir.Join("plugins3", "docker-symlinked"), 78 }, 79 } 80 81 assert.DeepEqual(t, candidates, exp) 82 } 83 84 func TestErrPluginNotFound(t *testing.T) { 85 var err error = errPluginNotFound("test") 86 err.(errPluginNotFound).NotFound() 87 assert.Error(t, err, "Error: No such CLI plugin: test") 88 assert.Assert(t, IsNotFound(err)) 89 assert.Assert(t, !IsNotFound(nil)) 90 } 91 92 func TestGetPluginDirs(t *testing.T) { 93 cli := test.NewFakeCli(nil) 94 95 pluginDir, err := config.Path("cli-plugins") 96 assert.NilError(t, err) 97 expected := append([]string{pluginDir}, defaultSystemPluginDirs...) 98 99 var pluginDirs []string 100 pluginDirs, err = getPluginDirs(cli) 101 assert.Equal(t, strings.Join(expected, ":"), strings.Join(pluginDirs, ":")) 102 assert.NilError(t, err) 103 104 extras := []string{ 105 "foo", "bar", "baz", 106 } 107 expected = append(extras, expected...) 108 cli.SetConfigFile(&configfile.ConfigFile{ 109 CLIPluginsExtraDirs: extras, 110 }) 111 pluginDirs, err = getPluginDirs(cli) 112 assert.DeepEqual(t, expected, pluginDirs) 113 assert.NilError(t, err) 114 }