...

Source file src/github.com/docker/cli/cli-plugins/manager/manager_test.go

Documentation: github.com/docker/cli/cli-plugins/manager

     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  	"github.com/spf13/cobra"
    11  	"gotest.tools/v3/assert"
    12  	"gotest.tools/v3/fs"
    13  )
    14  
    15  func TestListPluginCandidates(t *testing.T) {
    16  	// Populate a selection of directories with various shadowed and bogus/obscure plugin candidates.
    17  	// For the purposes of this test no contents is required and permissions are irrelevant.
    18  	dir := fs.NewDir(t, t.Name(),
    19  		fs.WithDir(
    20  			"plugins1",
    21  			fs.WithFile("docker-plugin1", ""),                        // This appears in each directory
    22  			fs.WithFile("not-a-plugin", ""),                          // Should be ignored
    23  			fs.WithFile("docker-symlinked1", ""),                     // This and ...
    24  			fs.WithSymlink("docker-symlinked2", "docker-symlinked1"), // ... this should both appear
    25  			fs.WithDir("ignored1"),                                   // A directory should be ignored
    26  		),
    27  		fs.WithDir(
    28  			"plugins2",
    29  			fs.WithFile("docker-plugin1", ""),
    30  			fs.WithFile("also-not-a-plugin", ""),
    31  			fs.WithFile("docker-hardlink1", ""),                     // This and ...
    32  			fs.WithHardlink("docker-hardlink2", "docker-hardlink1"), // ... this should both appear
    33  			fs.WithDir("ignored2"),
    34  		),
    35  		fs.WithDir(
    36  			"plugins3-target", // Will be referenced as a symlink from below
    37  			fs.WithFile("docker-plugin1", ""),
    38  			fs.WithDir("ignored3"),
    39  			fs.WithSymlink("docker-brokensymlink", "broken"),           // A broken symlink is still a candidate (but would fail tests later)
    40  			fs.WithFile("non-plugin-symlinked", ""),                    // This shouldn't appear, but ...
    41  			fs.WithSymlink("docker-symlinked", "non-plugin-symlinked"), // ... this link to it should.
    42  		),
    43  		fs.WithSymlink("plugins3", "plugins3-target"),
    44  		fs.WithFile("/plugins4", ""),
    45  		fs.WithSymlink("plugins5", "plugins5-nonexistent-target"),
    46  	)
    47  	defer dir.Remove()
    48  
    49  	dirs := make([]string, 0, 6)
    50  	for _, d := range []string{"plugins1", "nonexistent", "plugins2", "plugins3", "plugins4", "plugins5"} {
    51  		dirs = append(dirs, dir.Join(d))
    52  	}
    53  
    54  	candidates, err := listPluginCandidates(dirs)
    55  	assert.NilError(t, err)
    56  	exp := map[string][]string{
    57  		"plugin1": {
    58  			dir.Join("plugins1", "docker-plugin1"),
    59  			dir.Join("plugins2", "docker-plugin1"),
    60  			dir.Join("plugins3", "docker-plugin1"),
    61  		},
    62  		"symlinked1": {
    63  			dir.Join("plugins1", "docker-symlinked1"),
    64  		},
    65  		"symlinked2": {
    66  			dir.Join("plugins1", "docker-symlinked2"),
    67  		},
    68  		"hardlink1": {
    69  			dir.Join("plugins2", "docker-hardlink1"),
    70  		},
    71  		"hardlink2": {
    72  			dir.Join("plugins2", "docker-hardlink2"),
    73  		},
    74  		"brokensymlink": {
    75  			dir.Join("plugins3", "docker-brokensymlink"),
    76  		},
    77  		"symlinked": {
    78  			dir.Join("plugins3", "docker-symlinked"),
    79  		},
    80  	}
    81  
    82  	assert.DeepEqual(t, candidates, exp)
    83  }
    84  
    85  func TestGetPlugin(t *testing.T) {
    86  	dir := fs.NewDir(t, t.Name(),
    87  		fs.WithFile("docker-bbb", `
    88  #!/bin/sh
    89  echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
    90  		fs.WithFile("docker-aaa", `
    91  #!/bin/sh
    92  echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
    93  	)
    94  	defer dir.Remove()
    95  
    96  	cli := test.NewFakeCli(nil)
    97  	cli.SetConfigFile(&configfile.ConfigFile{CLIPluginsExtraDirs: []string{dir.Path()}})
    98  
    99  	plugin, err := GetPlugin("bbb", cli, &cobra.Command{})
   100  	assert.NilError(t, err)
   101  	assert.Equal(t, plugin.Name, "bbb")
   102  
   103  	_, err = GetPlugin("ccc", cli, &cobra.Command{})
   104  	assert.Error(t, err, "Error: No such CLI plugin: ccc")
   105  	assert.Assert(t, IsNotFound(err))
   106  }
   107  
   108  func TestListPluginsIsSorted(t *testing.T) {
   109  	dir := fs.NewDir(t, t.Name(),
   110  		fs.WithFile("docker-bbb", `
   111  #!/bin/sh
   112  echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
   113  		fs.WithFile("docker-aaa", `
   114  #!/bin/sh
   115  echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
   116  	)
   117  	defer dir.Remove()
   118  
   119  	cli := test.NewFakeCli(nil)
   120  	cli.SetConfigFile(&configfile.ConfigFile{CLIPluginsExtraDirs: []string{dir.Path()}})
   121  
   122  	plugins, err := ListPlugins(cli, &cobra.Command{})
   123  	assert.NilError(t, err)
   124  
   125  	// We're only interested in the plugins we created for testing this, and only
   126  	// if they appear in the expected order
   127  	var names []string
   128  	for _, p := range plugins {
   129  		if p.Name == "aaa" || p.Name == "bbb" {
   130  			names = append(names, p.Name)
   131  		}
   132  	}
   133  	assert.DeepEqual(t, names, []string{"aaa", "bbb"})
   134  }
   135  
   136  func TestErrPluginNotFound(t *testing.T) {
   137  	var err error = errPluginNotFound("test")
   138  	err.(errPluginNotFound).NotFound()
   139  	assert.Error(t, err, "Error: No such CLI plugin: test")
   140  	assert.Assert(t, IsNotFound(err))
   141  	assert.Assert(t, !IsNotFound(nil))
   142  }
   143  
   144  func TestGetPluginDirs(t *testing.T) {
   145  	cli := test.NewFakeCli(nil)
   146  
   147  	pluginDir, err := config.Path("cli-plugins")
   148  	assert.NilError(t, err)
   149  	expected := append([]string{pluginDir}, defaultSystemPluginDirs...)
   150  
   151  	var pluginDirs []string
   152  	pluginDirs, err = getPluginDirs(cli.ConfigFile())
   153  	assert.Equal(t, strings.Join(expected, ":"), strings.Join(pluginDirs, ":"))
   154  	assert.NilError(t, err)
   155  
   156  	extras := []string{
   157  		"foo", "bar", "baz",
   158  	}
   159  	expected = append(extras, expected...)
   160  	cli.SetConfigFile(&configfile.ConfigFile{
   161  		CLIPluginsExtraDirs: extras,
   162  	})
   163  	pluginDirs, err = getPluginDirs(cli.ConfigFile())
   164  	assert.DeepEqual(t, expected, pluginDirs)
   165  	assert.NilError(t, err)
   166  }
   167  

View as plain text