2017-04-01 03:07:22 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2019-04-02 11:20:21 -04:00
|
|
|
. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
|
2017-04-01 03:07:22 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-04-01 03:07:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecretListErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
secretListFunc func(types.SecretListOptions) ([]swarm.Secret, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
expectedError: "accepts no argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
|
|
return []swarm.Secret{}, errors.Errorf("error listing secrets")
|
|
|
|
},
|
|
|
|
expectedError: "error listing secrets",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newSecretListCommand(
|
2017-08-16 12:53:41 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretListFunc: tc.secretListFunc,
|
2017-08-16 12:53:41 -04:00
|
|
|
}),
|
2017-04-01 03:07:22 -04:00
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
2020-05-07 08:25:59 -04:00
|
|
|
cmd.SetOut(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretList(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
|
|
return []swarm.Secret{
|
2017-07-07 04:22:52 -04:00
|
|
|
*Secret(SecretID("ID-1-foo"),
|
|
|
|
SecretName("1-foo"),
|
2017-04-01 03:07:22 -04:00
|
|
|
SecretVersion(swarm.Version{Index: 10}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
2017-07-07 04:22:52 -04:00
|
|
|
*Secret(SecretID("ID-10-foo"),
|
|
|
|
SecretName("10-foo"),
|
|
|
|
SecretVersion(swarm.Version{Index: 11}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
SecretDriver("driver"),
|
|
|
|
),
|
|
|
|
*Secret(SecretID("ID-2-foo"),
|
|
|
|
SecretName("2-foo"),
|
2017-04-01 03:07:22 -04:00
|
|
|
SecretVersion(swarm.Version{Index: 11}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
2017-07-30 07:01:39 -04:00
|
|
|
SecretDriver("driver"),
|
2017-04-01 03:07:22 -04:00
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
cmd := newSecretListCommand(cli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-07-07 04:22:52 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-sort.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretListWithQuietOption(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
|
|
return []swarm.Secret{
|
|
|
|
*Secret(SecretID("ID-foo"), SecretName("foo")),
|
|
|
|
*Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{
|
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
cmd := newSecretListCommand(cli)
|
|
|
|
cmd.Flags().Set("quiet", "true")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 12:53:41 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-quiet-option.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretListWithConfigFormat(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
|
|
return []swarm.Secret{
|
|
|
|
*Secret(SecretID("ID-foo"), SecretName("foo")),
|
|
|
|
*Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{
|
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-07-05 14:34:16 -04:00
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
2017-04-01 03:07:22 -04:00
|
|
|
SecretFormat: "{{ .Name }} {{ .Labels }}",
|
|
|
|
})
|
|
|
|
cmd := newSecretListCommand(cli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 12:53:41 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-config-format.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretListWithFormat(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
|
|
return []swarm.Secret{
|
|
|
|
*Secret(SecretID("ID-foo"), SecretName("foo")),
|
|
|
|
*Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{
|
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
cmd := newSecretListCommand(cli)
|
|
|
|
cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 12:53:41 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-format.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretListWithFilter(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("foo", options.Filters.Get("name")[0]), "foo")
|
|
|
|
assert.Check(t, is.Equal("lbl1=Label-bar", options.Filters.Get("label")[0]))
|
2017-04-01 03:07:22 -04:00
|
|
|
return []swarm.Secret{
|
|
|
|
*Secret(SecretID("ID-foo"),
|
|
|
|
SecretName("foo"),
|
|
|
|
SecretVersion(swarm.Version{Index: 10}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
|
|
|
*Secret(SecretID("ID-bar"),
|
|
|
|
SecretName("bar"),
|
|
|
|
SecretVersion(swarm.Version{Index: 11}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
cmd := newSecretListCommand(cli)
|
|
|
|
cmd.Flags().Set("filter", "name=foo")
|
|
|
|
cmd.Flags().Set("filter", "label=lbl1=Label-bar")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 12:53:41 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-filter.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|