DockerCLI/cli/command/volume/create_test.go

127 lines
3.4 KiB
Go
Raw Normal View History

package volume
import (
"io"
"reflect"
"strings"
"testing"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
volumetypes "github.com/docker/docker/api/types/volume"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestVolumeCreateErrors(t *testing.T) {
testCases := []struct {
args []string
flags map[string]string
volumeCreateFunc func(volumetypes.VolumeCreateBody) (types.Volume, error)
expectedError string
}{
{
args: []string{"volumeName"},
flags: map[string]string{
"name": "volumeName",
},
linting: fix incorrectly formatted errors (revive) cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) "invalid interpolation format for %s: %#v. You may need to escape any $ with another $.", ^ cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n", ^ cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return tmpl, errors.Errorf("Template parsing error: %v\n", err) ^ cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return errors.Errorf("Template parsing error: %v\n", err) ^ cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return errors.Errorf("error checking context: '%s'.", err) ^ cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n") ^ cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err) ^ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
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.VolumeCreateBody) (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.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
func TestVolumeCreateWithName(t *testing.T) {
name := "foo"
cli := test.NewFakeCli(&fakeClient{
volumeCreateFunc: func(body volumetypes.VolumeCreateBody) (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 := cli.OutBuffer()
// Test by flags
cmd := newCreateCommand(cli)
cmd.Flags().Set("name", name)
assert.NilError(t, cmd.Execute())
assert.Check(t, is.Equal(name, strings.TrimSpace(buf.String())))
// Then by args
buf.Reset()
cmd = newCreateCommand(cli)
cmd.SetArgs([]string{name})
assert.NilError(t, cmd.Execute())
assert.Check(t, is.Equal(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"
cli := test.NewFakeCli(&fakeClient{
volumeCreateFunc: func(body volumetypes.VolumeCreateBody) (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
},
})
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.NilError(t, cmd.Execute())
assert.Check(t, is.Equal(name, strings.TrimSpace(cli.OutBuffer().String())))
}