github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/plugin/list_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/internal/test"
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/api/types/filters"
    11  
    12  	"gotest.tools/v3/assert"
    13  	is "gotest.tools/v3/assert/cmp"
    14  	"gotest.tools/v3/golden"
    15  )
    16  
    17  func TestListErrors(t *testing.T) {
    18  	testCases := []struct {
    19  		description   string
    20  		args          []string
    21  		flags         map[string]string
    22  		expectedError string
    23  		listFunc      func(filter filters.Args) (types.PluginsListResponse, error)
    24  	}{
    25  		{
    26  			description:   "too many arguments",
    27  			args:          []string{"foo"},
    28  			expectedError: "accepts no arguments",
    29  		},
    30  		{
    31  			description:   "error listing plugins",
    32  			args:          []string{},
    33  			expectedError: "error listing plugins",
    34  			listFunc: func(filter filters.Args) (types.PluginsListResponse, error) {
    35  				return types.PluginsListResponse{}, fmt.Errorf("error listing plugins")
    36  			},
    37  		},
    38  		{
    39  			description: "invalid format",
    40  			args:        []string{},
    41  			flags: map[string]string{
    42  				"format": "{{invalid format}}",
    43  			},
    44  			expectedError: "Template parsing error",
    45  		},
    46  	}
    47  
    48  	for _, tc := range testCases {
    49  		cli := test.NewFakeCli(&fakeClient{pluginListFunc: tc.listFunc})
    50  		cmd := newListCommand(cli)
    51  		cmd.SetArgs(tc.args)
    52  		for key, value := range tc.flags {
    53  			cmd.Flags().Set(key, value)
    54  		}
    55  		cmd.SetOut(ioutil.Discard)
    56  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    57  	}
    58  }
    59  
    60  func TestList(t *testing.T) {
    61  	singlePluginListFunc := func(_ filters.Args) (types.PluginsListResponse, error) {
    62  		return types.PluginsListResponse{
    63  			{
    64  				ID:      "id-foo",
    65  				Name:    "name-foo",
    66  				Enabled: true,
    67  				Config: types.PluginConfig{
    68  					Description: "desc-bar",
    69  				},
    70  			},
    71  		}, nil
    72  	}
    73  
    74  	testCases := []struct {
    75  		description string
    76  		args        []string
    77  		flags       map[string]string
    78  		golden      string
    79  		listFunc    func(filter filters.Args) (types.PluginsListResponse, error)
    80  	}{
    81  		{
    82  			description: "list with no additional flags",
    83  			args:        []string{},
    84  			golden:      "plugin-list-without-format.golden",
    85  			listFunc:    singlePluginListFunc,
    86  		},
    87  		{
    88  			description: "list with filters",
    89  			args:        []string{},
    90  			flags: map[string]string{
    91  				"filter": "foo=bar",
    92  			},
    93  			golden: "plugin-list-without-format.golden",
    94  			listFunc: func(filter filters.Args) (types.PluginsListResponse, error) {
    95  				assert.Check(t, is.Equal("bar", filter.Get("foo")[0]))
    96  				return singlePluginListFunc(filter)
    97  			},
    98  		},
    99  		{
   100  			description: "list with quiet option",
   101  			args:        []string{},
   102  			flags: map[string]string{
   103  				"quiet": "true",
   104  			},
   105  			golden:   "plugin-list-with-quiet-option.golden",
   106  			listFunc: singlePluginListFunc,
   107  		},
   108  		{
   109  			description: "list with no-trunc option",
   110  			args:        []string{},
   111  			flags: map[string]string{
   112  				"no-trunc": "true",
   113  				"format":   "{{ .ID }}",
   114  			},
   115  			golden: "plugin-list-with-no-trunc-option.golden",
   116  			listFunc: func(_ filters.Args) (types.PluginsListResponse, error) {
   117  				return types.PluginsListResponse{
   118  					{
   119  						ID:      "xyg4z2hiSLO5yTnBJfg4OYia9gKA6Qjd",
   120  						Name:    "name-foo",
   121  						Enabled: true,
   122  						Config: types.PluginConfig{
   123  							Description: "desc-bar",
   124  						},
   125  					},
   126  				}, nil
   127  			},
   128  		},
   129  		{
   130  			description: "list with format",
   131  			args:        []string{},
   132  			flags: map[string]string{
   133  				"format": "{{ .Name }}",
   134  			},
   135  			golden:   "plugin-list-with-format.golden",
   136  			listFunc: singlePluginListFunc,
   137  		},
   138  		{
   139  			description: "list output is sorted based on plugin name",
   140  			args:        []string{},
   141  			flags: map[string]string{
   142  				"format": "{{ .Name }}",
   143  			},
   144  			golden: "plugin-list-sort.golden",
   145  			listFunc: func(_ filters.Args) (types.PluginsListResponse, error) {
   146  				return types.PluginsListResponse{
   147  					{
   148  						ID:   "id-1",
   149  						Name: "plugin-1-foo",
   150  					},
   151  					{
   152  						ID:   "id-2",
   153  						Name: "plugin-10-foo",
   154  					},
   155  					{
   156  						ID:   "id-3",
   157  						Name: "plugin-2-foo",
   158  					},
   159  				}, nil
   160  			},
   161  		},
   162  	}
   163  
   164  	for _, tc := range testCases {
   165  		cli := test.NewFakeCli(&fakeClient{pluginListFunc: tc.listFunc})
   166  		cmd := newListCommand(cli)
   167  		cmd.SetArgs(tc.args)
   168  		for key, value := range tc.flags {
   169  			cmd.Flags().Set(key, value)
   170  		}
   171  		assert.NilError(t, cmd.Execute())
   172  		golden.Assert(t, cli.OutBuffer().String(), tc.golden)
   173  	}
   174  }