The validation functions to test for the number of passed arguments did not
pluralize `argument(s)`, and used `argument(s)` in all cases.
This patch adds a simple `pluralize()` helper to improve this.
Before this change, `argument(s)` was used in all cases:
$ docker container ls foobar
"docker container ls" accepts no argument(s).
$ docker network create one two
"docker network create" requires exactly 1 argument(s).
$ docker network connect
"docker network connect" requires exactly 2 argument(s).
$ docker volume create one two
"docker volume create" requires at most 1 argument(s).
After this change, `argument(s)` is properly singularized or plurarized:
$ docker container ls foobar
"docker container ls" accepts no arguments.
$ docker network create one two
"docker network create" requires exactly 1 argument.
$ docker network connect
"docker network connect" requires exactly 2 arguments.
$ docker volume create one two
"docker volume create" requires at most 1 argument.
Test cases were updated accordingly.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
128 lines
3.4 KiB
Go
128 lines
3.4 KiB
Go
package volume
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/docker/cli/cli/internal/test"
|
|
"github.com/docker/docker/api/types"
|
|
volumetypes "github.com/docker/docker/api/types/volume"
|
|
"github.com/docker/docker/pkg/testutil"
|
|
"github.com/pkg/errors"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
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",
|
|
},
|
|
{
|
|
volumeCreateFunc: func(createBody volumetypes.VolumesCreateBody) (types.Volume, error) {
|
|
return types.Volume{}, errors.Errorf("error creating volume")
|
|
},
|
|
expectedError: "error creating volume",
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
cmd := newCreateCommand(
|
|
test.NewFakeCli(&fakeClient{
|
|
volumeCreateFunc: tc.volumeCreateFunc,
|
|
}),
|
|
)
|
|
cmd.SetArgs(tc.args)
|
|
for key, value := range tc.flags {
|
|
cmd.Flags().Set(key, value)
|
|
}
|
|
cmd.SetOutput(ioutil.Discard)
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
}
|
|
}
|
|
|
|
func TestVolumeCreateWithName(t *testing.T) {
|
|
name := "foo"
|
|
buf := new(bytes.Buffer)
|
|
cli := test.NewFakeCliWithOutput(&fakeClient{
|
|
volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) {
|
|
if body.Name != name {
|
|
return types.Volume{}, errors.Errorf("expected name %q, got %q", name, body.Name)
|
|
}
|
|
return types.Volume{
|
|
Name: body.Name,
|
|
}, nil
|
|
},
|
|
}, buf)
|
|
|
|
// Test by flags
|
|
cmd := newCreateCommand(cli)
|
|
cmd.Flags().Set("name", name)
|
|
assert.NoError(t, cmd.Execute())
|
|
assert.Equal(t, name, strings.TrimSpace(buf.String()))
|
|
|
|
// Then by args
|
|
buf.Reset()
|
|
cmd = newCreateCommand(cli)
|
|
cmd.SetArgs([]string{name})
|
|
assert.NoError(t, cmd.Execute())
|
|
assert.Equal(t, name, strings.TrimSpace(buf.String()))
|
|
}
|
|
|
|
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.NewFakeCliWithOutput(&fakeClient{
|
|
volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) {
|
|
if body.Name != "" {
|
|
return types.Volume{}, errors.Errorf("expected empty name, got %q", body.Name)
|
|
}
|
|
if body.Driver != expectedDriver {
|
|
return types.Volume{}, errors.Errorf("expected driver %q, got %q", expectedDriver, body.Driver)
|
|
}
|
|
if !reflect.DeepEqual(body.DriverOpts, expectedOpts) {
|
|
return types.Volume{}, errors.Errorf("expected drivers opts %v, got %v", expectedOpts, body.DriverOpts)
|
|
}
|
|
if !reflect.DeepEqual(body.Labels, expectedLabels) {
|
|
return types.Volume{}, errors.Errorf("expected labels %v, got %v", expectedLabels, body.Labels)
|
|
}
|
|
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")
|
|
assert.NoError(t, cmd.Execute())
|
|
assert.Equal(t, name, strings.TrimSpace(buf.String()))
|
|
}
|