2017-04-01 03:07:22 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2017-05-08 13:36:04 -04:00
|
|
|
"reflect"
|
2017-04-01 03:07:22 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-08-21 16:39:35 -04:00
|
|
|
"github.com/docker/cli/internal/test/testutil"
|
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"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
const secretDataFile = "secret-create-with-name.golden"
|
|
|
|
|
|
|
|
func TestSecretCreateErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
secretCreateFunc func(swarm.SecretSpec) (types.SecretCreateResponse, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{args: []string{"too", "many", "arguments"},
|
2017-07-30 07:01:39 -04:00
|
|
|
expectedError: "requires at least 1 and at most 2 arguments",
|
|
|
|
},
|
|
|
|
{args: []string{"create", "--driver", "driver", "-"},
|
|
|
|
expectedError: "secret data must be empty",
|
2017-04-01 03:07:22 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"name", filepath.Join("testdata", secretDataFile)},
|
|
|
|
secretCreateFunc: func(secretSpec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("error creating secret")
|
|
|
|
},
|
|
|
|
expectedError: "error creating secret",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newSecretCreateCommand(
|
2017-07-05 14:43:39 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretCreateFunc: tc.secretCreateFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
}),
|
2017-04-01 03:07:22 -04:00
|
|
|
)
|
|
|
|
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 TestSecretCreateWithName(t *testing.T) {
|
|
|
|
name := "foo"
|
2018-02-21 13:45:34 -05:00
|
|
|
data, err := ioutil.ReadFile(filepath.Join("testdata", secretDataFile))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
expected := swarm.SecretSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: name,
|
|
|
|
Labels: make(map[string]string),
|
|
|
|
},
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
2018-02-21 13:45:34 -05:00
|
|
|
if !reflect.DeepEqual(spec, expected) {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
return types.SecretCreateResponse{
|
|
|
|
ID: "ID-" + spec.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
|
|
|
|
cmd := newSecretCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
|
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())
|
2017-08-16 12:53:41 -04:00
|
|
|
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
|
2017-07-30 07:01:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretCreateWithDriver(t *testing.T) {
|
|
|
|
expectedDriver := &swarm.Driver{
|
|
|
|
Name: "secret-driver",
|
|
|
|
}
|
|
|
|
name := "foo"
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
|
|
|
if spec.Name != name {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
|
|
|
|
}
|
|
|
|
|
2018-02-21 13:45:34 -05:00
|
|
|
if spec.Driver.Name != expectedDriver.Name {
|
2017-07-30 07:01:39 -04:00
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.SecretCreateResponse{
|
|
|
|
ID: "ID-" + spec.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newSecretCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{name})
|
|
|
|
cmd.Flags().Set("driver", expectedDriver.Name)
|
|
|
|
assert.NoError(t, cmd.Execute())
|
|
|
|
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
|
2018-02-21 13:45:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretCreateWithTemplatingDriver(t *testing.T) {
|
|
|
|
expectedDriver := &swarm.Driver{
|
|
|
|
Name: "template-driver",
|
|
|
|
}
|
|
|
|
name := "foo"
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
|
|
|
if spec.Name != name {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if spec.Templating.Name != expectedDriver.Name {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.SecretCreateResponse{
|
|
|
|
ID: "ID-" + spec.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newSecretCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{name})
|
|
|
|
cmd.Flags().Set("template-driver", expectedDriver.Name)
|
|
|
|
assert.NoError(t, cmd.Execute())
|
|
|
|
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretCreateWithLabels(t *testing.T) {
|
|
|
|
expectedLabels := map[string]string{
|
|
|
|
"lbl1": "Label-foo",
|
|
|
|
"lbl2": "Label-bar",
|
|
|
|
}
|
|
|
|
name := "foo"
|
|
|
|
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
|
|
|
if spec.Name != name {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:36:04 -04:00
|
|
|
if !reflect.DeepEqual(spec.Labels, expectedLabels) {
|
2017-04-01 03:07:22 -04:00
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected labels %v, got %v", expectedLabels, spec.Labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.SecretCreateResponse{
|
|
|
|
ID: "ID-" + spec.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
|
|
|
|
cmd := newSecretCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
|
|
|
|
cmd.Flags().Set("label", "lbl1=Label-foo")
|
|
|
|
cmd.Flags().Set("label", "lbl2=Label-bar")
|
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())
|
2017-08-16 12:53:41 -04:00
|
|
|
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|