2017-02-27 12:39:35 -05:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
volumetypes "github.com/docker/docker/api/types/volume"
|
|
|
|
"github.com/docker/docker/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-03-09 13:23:45 -05: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-02-27 12:39:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVolumeCreateErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
volumeCreateFunc func(volumetypes.VolumesCreateBody) (types.Volume, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"volumeName"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"name": "volumeName",
|
|
|
|
},
|
|
|
|
expectedError: "Conflicting options: either specify --name or provide positional arg, not both",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"too", "many"},
|
|
|
|
expectedError: "requires at most 1 argument(s)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
volumeCreateFunc: func(createBody volumetypes.VolumesCreateBody) (types.Volume, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.Volume{}, errors.Errorf("error creating volume")
|
2017-02-27 12:39:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error creating volume",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd := newCreateCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{
|
|
|
|
volumeCreateFunc: tc.volumeCreateFunc,
|
|
|
|
}, buf),
|
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
|
|
|
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-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumeCreateWithName(t *testing.T) {
|
|
|
|
name := "foo"
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) {
|
|
|
|
if body.Name != name {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.Volume{}, errors.Errorf("expected name %q, got %q", name, body.Name)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
return types.Volume{
|
|
|
|
Name: body.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}, buf)
|
|
|
|
|
|
|
|
// Test by flags
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.Flags().Set("name", name)
|
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, name, strings.TrimSpace(buf.String()))
|
2017-02-27 12:39:35 -05:00
|
|
|
|
|
|
|
// Then by args
|
|
|
|
buf.Reset()
|
|
|
|
cmd = newCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{name})
|
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, name, strings.TrimSpace(buf.String()))
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumeCreateWithFlags(t *testing.T) {
|
|
|
|
expectedDriver := "foo"
|
|
|
|
expectedOpts := map[string]string{
|
|
|
|
"bar": "1",
|
|
|
|
"baz": "baz",
|
|
|
|
}
|
|
|
|
expectedLabels := map[string]string{
|
|
|
|
"lbl1": "v1",
|
|
|
|
"lbl2": "v2",
|
|
|
|
}
|
|
|
|
name := "banana"
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) {
|
|
|
|
if body.Name != "" {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.Volume{}, errors.Errorf("expected empty name, got %q", body.Name)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
if body.Driver != expectedDriver {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.Volume{}, errors.Errorf("expected driver %q, got %q", expectedDriver, body.Driver)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
if !compareMap(body.DriverOpts, expectedOpts) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.Volume{}, errors.Errorf("expected drivers opts %v, got %v", expectedOpts, body.DriverOpts)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
if !compareMap(body.Labels, expectedLabels) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.Volume{}, errors.Errorf("expected labels %v, got %v", expectedLabels, body.Labels)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
return types.Volume{
|
|
|
|
Name: name,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}, buf)
|
|
|
|
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.Flags().Set("driver", "foo")
|
|
|
|
cmd.Flags().Set("opt", "bar=1")
|
|
|
|
cmd.Flags().Set("opt", "baz=baz")
|
|
|
|
cmd.Flags().Set("label", "lbl1=v1")
|
|
|
|
cmd.Flags().Set("label", "lbl2=v2")
|
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, name, strings.TrimSpace(buf.String()))
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func compareMap(actual map[string]string, expected map[string]string) bool {
|
|
|
|
if len(actual) != len(expected) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for key, value := range actual {
|
|
|
|
if expectedValue, ok := expected[key]; ok {
|
|
|
|
if expectedValue != value {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|