github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/config/ls_test.go (about)

     1  package config
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/docker/cli/cli/config/configfile"
     9  	"github.com/docker/cli/internal/test"
    10  	. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/api/types/swarm"
    13  	"github.com/pkg/errors"
    14  	"gotest.tools/v3/assert"
    15  	is "gotest.tools/v3/assert/cmp"
    16  	"gotest.tools/v3/golden"
    17  )
    18  
    19  func TestConfigListErrors(t *testing.T) {
    20  	testCases := []struct {
    21  		args           []string
    22  		configListFunc func(types.ConfigListOptions) ([]swarm.Config, error)
    23  		expectedError  string
    24  	}{
    25  		{
    26  			args:          []string{"foo"},
    27  			expectedError: "accepts no argument",
    28  		},
    29  		{
    30  			configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
    31  				return []swarm.Config{}, errors.Errorf("error listing configs")
    32  			},
    33  			expectedError: "error listing configs",
    34  		},
    35  	}
    36  	for _, tc := range testCases {
    37  		cmd := newConfigListCommand(
    38  			test.NewFakeCli(&fakeClient{
    39  				configListFunc: tc.configListFunc,
    40  			}),
    41  		)
    42  		cmd.SetArgs(tc.args)
    43  		cmd.SetOut(ioutil.Discard)
    44  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    45  	}
    46  }
    47  
    48  func TestConfigList(t *testing.T) {
    49  	cli := test.NewFakeCli(&fakeClient{
    50  		configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
    51  			return []swarm.Config{
    52  				*Config(ConfigID("ID-1-foo"),
    53  					ConfigName("1-foo"),
    54  					ConfigVersion(swarm.Version{Index: 10}),
    55  					ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
    56  					ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
    57  				),
    58  				*Config(ConfigID("ID-10-foo"),
    59  					ConfigName("10-foo"),
    60  					ConfigVersion(swarm.Version{Index: 11}),
    61  					ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
    62  					ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
    63  				),
    64  				*Config(ConfigID("ID-2-foo"),
    65  					ConfigName("2-foo"),
    66  					ConfigVersion(swarm.Version{Index: 11}),
    67  					ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
    68  					ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
    69  				),
    70  			}, nil
    71  		},
    72  	})
    73  	cmd := newConfigListCommand(cli)
    74  	assert.NilError(t, cmd.Execute())
    75  	golden.Assert(t, cli.OutBuffer().String(), "config-list-sort.golden")
    76  }
    77  
    78  func TestConfigListWithQuietOption(t *testing.T) {
    79  	cli := test.NewFakeCli(&fakeClient{
    80  		configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
    81  			return []swarm.Config{
    82  				*Config(ConfigID("ID-foo"), ConfigName("foo")),
    83  				*Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{
    84  					"label": "label-bar",
    85  				})),
    86  			}, nil
    87  		},
    88  	})
    89  	cmd := newConfigListCommand(cli)
    90  	cmd.Flags().Set("quiet", "true")
    91  	assert.NilError(t, cmd.Execute())
    92  	golden.Assert(t, cli.OutBuffer().String(), "config-list-with-quiet-option.golden")
    93  }
    94  
    95  func TestConfigListWithConfigFormat(t *testing.T) {
    96  	cli := test.NewFakeCli(&fakeClient{
    97  		configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
    98  			return []swarm.Config{
    99  				*Config(ConfigID("ID-foo"), ConfigName("foo")),
   100  				*Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{
   101  					"label": "label-bar",
   102  				})),
   103  			}, nil
   104  		},
   105  	})
   106  	cli.SetConfigFile(&configfile.ConfigFile{
   107  		ConfigFormat: "{{ .Name }} {{ .Labels }}",
   108  	})
   109  	cmd := newConfigListCommand(cli)
   110  	assert.NilError(t, cmd.Execute())
   111  	golden.Assert(t, cli.OutBuffer().String(), "config-list-with-config-format.golden")
   112  }
   113  
   114  func TestConfigListWithFormat(t *testing.T) {
   115  	cli := test.NewFakeCli(&fakeClient{
   116  		configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
   117  			return []swarm.Config{
   118  				*Config(ConfigID("ID-foo"), ConfigName("foo")),
   119  				*Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{
   120  					"label": "label-bar",
   121  				})),
   122  			}, nil
   123  		},
   124  	})
   125  	cmd := newConfigListCommand(cli)
   126  	cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}")
   127  	assert.NilError(t, cmd.Execute())
   128  	golden.Assert(t, cli.OutBuffer().String(), "config-list-with-format.golden")
   129  }
   130  
   131  func TestConfigListWithFilter(t *testing.T) {
   132  	cli := test.NewFakeCli(&fakeClient{
   133  		configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
   134  			assert.Check(t, is.Equal("foo", options.Filters.Get("name")[0]))
   135  			assert.Check(t, is.Equal("lbl1=Label-bar", options.Filters.Get("label")[0]))
   136  			return []swarm.Config{
   137  				*Config(ConfigID("ID-foo"),
   138  					ConfigName("foo"),
   139  					ConfigVersion(swarm.Version{Index: 10}),
   140  					ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
   141  					ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
   142  				),
   143  				*Config(ConfigID("ID-bar"),
   144  					ConfigName("bar"),
   145  					ConfigVersion(swarm.Version{Index: 11}),
   146  					ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
   147  					ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
   148  				),
   149  			}, nil
   150  		},
   151  	})
   152  	cmd := newConfigListCommand(cli)
   153  	cmd.Flags().Set("filter", "name=foo")
   154  	cmd.Flags().Set("filter", "label=lbl1=Label-bar")
   155  	assert.NilError(t, cmd.Execute())
   156  	golden.Assert(t, cli.OutBuffer().String(), "config-list-with-filter.golden")
   157  }