2017-04-01 03:07:22 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/internal/test"
|
Remove pkg/testutil/assert in favor of testify
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>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/docker/docker/pkg/testutil"
|
2017-04-01 03:07:22 -04:00
|
|
|
"github.com/pkg/errors"
|
Remove pkg/testutil/assert in favor of testify
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>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-04-01 03:07:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecretRemoveErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
secretRemoveFunc func(string) error
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires at least 1 argument.",
|
2017-04-01 03:07:22 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
secretRemoveFunc: func(name string) error {
|
|
|
|
return errors.Errorf("error removing secret")
|
|
|
|
},
|
|
|
|
expectedError: "error removing secret",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd := newSecretRemoveCommand(
|
2017-07-05 14:19:52 -04:00
|
|
|
test.NewFakeCliWithOutput(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretRemoveFunc: tc.secretRemoveFunc,
|
|
|
|
}, buf),
|
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
Remove pkg/testutil/assert in favor of testify
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>
2017-04-13 18:45:37 -04:00
|
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretRemoveWithName(t *testing.T) {
|
|
|
|
names := []string{"foo", "bar"}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
var removedSecrets []string
|
2017-07-05 14:19:52 -04:00
|
|
|
cli := test.NewFakeCliWithOutput(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretRemoveFunc: func(name string) error {
|
|
|
|
removedSecrets = append(removedSecrets, name)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, buf)
|
|
|
|
cmd := newSecretRemoveCommand(cli)
|
|
|
|
cmd.SetArgs(names)
|
Remove pkg/testutil/assert in favor of testify
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>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, cmd.Execute())
|
|
|
|
assert.Equal(t, names, strings.Split(strings.TrimSpace(buf.String()), "\n"))
|
|
|
|
assert.Equal(t, names, removedSecrets)
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretRemoveContinueAfterError(t *testing.T) {
|
|
|
|
names := []string{"foo", "bar"}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
var removedSecrets []string
|
|
|
|
|
2017-07-05 14:19:52 -04:00
|
|
|
cli := test.NewFakeCliWithOutput(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretRemoveFunc: func(name string) error {
|
|
|
|
removedSecrets = append(removedSecrets, name)
|
|
|
|
if name == "foo" {
|
|
|
|
return errors.Errorf("error removing secret: %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, buf)
|
|
|
|
|
|
|
|
cmd := newSecretRemoveCommand(cli)
|
2017-06-09 13:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2017-04-01 03:07:22 -04:00
|
|
|
cmd.SetArgs(names)
|
Remove pkg/testutil/assert in favor of testify
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>
2017-04-13 18:45:37 -04:00
|
|
|
assert.EqualError(t, cmd.Execute(), "error removing secret: foo")
|
|
|
|
assert.Equal(t, names, removedSecrets)
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|