mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
6052f2b396
I noticed that we're using a homegrown package for assertions. The functions are extremely similar to testify, but with enough slight differences to be confusing (for example, Equal takes its arguments in a different order). We already vendor testify, and it's used in a few places by tests. I also found some problems with pkg/testutil/assert. For example, the NotNil function seems to be broken. It checks the argument against "nil", which only works for an interface. If you pass in a nil map or slice, the equality check will fail. In the interest of avoiding NIH, I'm proposing replacing pkg/testutil/assert with testify. The test code looks almost the same, but we avoid the confusion of having two similar but slightly different assertion packages, and having to maintain our own package instead of using a commonly-used one. In the process, I found a few places where the tests should halt if an assertion fails, so I've made those cases (that I noticed) use "require" instead of "assert", and I've vendored the "require" package from testify alongside the already-present "assert" package. Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
173 lines
5.6 KiB
Go
173 lines
5.6 KiB
Go
package secret
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/cli/config/configfile"
|
|
"github.com/docker/docker/cli/internal/test"
|
|
"github.com/pkg/errors"
|
|
// Import builders to get the builder function as package function
|
|
. "github.com/docker/docker/cli/internal/test/builders"
|
|
"github.com/docker/docker/pkg/testutil"
|
|
"github.com/docker/docker/pkg/testutil/golden"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
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 {
|
|
buf := new(bytes.Buffer)
|
|
cmd := newSecretListCommand(
|
|
test.NewFakeCli(&fakeClient{
|
|
secretListFunc: tc.secretListFunc,
|
|
}, buf),
|
|
)
|
|
cmd.SetArgs(tc.args)
|
|
cmd.SetOutput(ioutil.Discard)
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
}
|
|
}
|
|
|
|
func TestSecretList(t *testing.T) {
|
|
buf := new(bytes.Buffer)
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
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
|
|
},
|
|
}, buf)
|
|
cli.SetConfigfile(&configfile.ConfigFile{})
|
|
cmd := newSecretListCommand(cli)
|
|
cmd.SetOutput(buf)
|
|
assert.NoError(t, cmd.Execute())
|
|
actual := buf.String()
|
|
expected := golden.Get(t, []byte(actual), "secret-list.golden")
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
}
|
|
|
|
func TestSecretListWithQuietOption(t *testing.T) {
|
|
buf := new(bytes.Buffer)
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
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
|
|
},
|
|
}, buf)
|
|
cli.SetConfigfile(&configfile.ConfigFile{})
|
|
cmd := newSecretListCommand(cli)
|
|
cmd.Flags().Set("quiet", "true")
|
|
assert.NoError(t, cmd.Execute())
|
|
actual := buf.String()
|
|
expected := golden.Get(t, []byte(actual), "secret-list-with-quiet-option.golden")
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
}
|
|
|
|
func TestSecretListWithConfigFormat(t *testing.T) {
|
|
buf := new(bytes.Buffer)
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
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
|
|
},
|
|
}, buf)
|
|
cli.SetConfigfile(&configfile.ConfigFile{
|
|
SecretFormat: "{{ .Name }} {{ .Labels }}",
|
|
})
|
|
cmd := newSecretListCommand(cli)
|
|
assert.NoError(t, cmd.Execute())
|
|
actual := buf.String()
|
|
expected := golden.Get(t, []byte(actual), "secret-list-with-config-format.golden")
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
}
|
|
|
|
func TestSecretListWithFormat(t *testing.T) {
|
|
buf := new(bytes.Buffer)
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
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
|
|
},
|
|
}, buf)
|
|
cmd := newSecretListCommand(cli)
|
|
cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}")
|
|
assert.NoError(t, cmd.Execute())
|
|
actual := buf.String()
|
|
expected := golden.Get(t, []byte(actual), "secret-list-with-format.golden")
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
}
|
|
|
|
func TestSecretListWithFilter(t *testing.T) {
|
|
buf := new(bytes.Buffer)
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
assert.Equal(t, "foo", options.Filters.Get("name")[0], "foo")
|
|
assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0])
|
|
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
|
|
},
|
|
}, buf)
|
|
cli.SetConfigfile(&configfile.ConfigFile{})
|
|
cmd := newSecretListCommand(cli)
|
|
cmd.Flags().Set("filter", "name=foo")
|
|
cmd.Flags().Set("filter", "label=lbl1=Label-bar")
|
|
assert.NoError(t, cmd.Execute())
|
|
actual := buf.String()
|
|
expected := golden.Get(t, []byte(actual), "secret-list-with-filter.golden")
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
}
|