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>
82 lines
2 KiB
Go
82 lines
2 KiB
Go
package secret
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/cli/internal/test"
|
|
"github.com/docker/docker/pkg/testutil"
|
|
"github.com/pkg/errors"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSecretRemoveErrors(t *testing.T) {
|
|
testCases := []struct {
|
|
args []string
|
|
secretRemoveFunc func(string) error
|
|
expectedError string
|
|
}{
|
|
{
|
|
args: []string{},
|
|
expectedError: "requires at least 1 argument(s).",
|
|
},
|
|
{
|
|
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(
|
|
test.NewFakeCli(&fakeClient{
|
|
secretRemoveFunc: tc.secretRemoveFunc,
|
|
}, buf),
|
|
)
|
|
cmd.SetArgs(tc.args)
|
|
cmd.SetOutput(ioutil.Discard)
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
}
|
|
}
|
|
|
|
func TestSecretRemoveWithName(t *testing.T) {
|
|
names := []string{"foo", "bar"}
|
|
buf := new(bytes.Buffer)
|
|
var removedSecrets []string
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
secretRemoveFunc: func(name string) error {
|
|
removedSecrets = append(removedSecrets, name)
|
|
return nil
|
|
},
|
|
}, buf)
|
|
cmd := newSecretRemoveCommand(cli)
|
|
cmd.SetArgs(names)
|
|
assert.NoError(t, cmd.Execute())
|
|
assert.Equal(t, names, strings.Split(strings.TrimSpace(buf.String()), "\n"))
|
|
assert.Equal(t, names, removedSecrets)
|
|
}
|
|
|
|
func TestSecretRemoveContinueAfterError(t *testing.T) {
|
|
names := []string{"foo", "bar"}
|
|
buf := new(bytes.Buffer)
|
|
var removedSecrets []string
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
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)
|
|
cmd.SetArgs(names)
|
|
assert.EqualError(t, cmd.Execute(), "error removing secret: foo")
|
|
assert.Equal(t, names, removedSecrets)
|
|
}
|