2017-02-27 12:39:35 -05:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
2022-02-25 08:34:38 -05:00
|
|
|
"io"
|
2017-05-08 13:36:04 -04:00
|
|
|
"reflect"
|
2017-02-27 12:39:35 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2022-04-29 13:26:50 -04:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-02-27 12:39:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVolumeCreateErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeCreateFunc func(volume.CreateOptions) (volume.Volume, error)
|
2017-02-27 12:39:35 -05:00
|
|
|
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",
|
2017-02-27 12:39:35 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"too", "many"},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires at most 1 argument",
|
2017-02-27 12:39:35 -05:00
|
|
|
},
|
|
|
|
{
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeCreateFunc: func(createBody volume.CreateOptions) (volume.Volume, error) {
|
|
|
|
return volume.Volume{}, errors.Errorf("error creating volume")
|
2017-02-27 12:39:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error creating volume",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newCreateCommand(
|
2017-07-05 14:43:39 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2017-02-27 12:39:35 -05:00
|
|
|
volumeCreateFunc: tc.volumeCreateFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
}),
|
2017-02-27 12:39:35 -05:00
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
2022-02-25 08:34:38 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumeCreateWithName(t *testing.T) {
|
|
|
|
name := "foo"
|
2017-08-16 13:50:28 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeCreateFunc: func(body volume.CreateOptions) (volume.Volume, error) {
|
2017-02-27 12:39:35 -05:00
|
|
|
if body.Name != name {
|
2022-04-29 13:26:50 -04:00
|
|
|
return volume.Volume{}, errors.Errorf("expected name %q, got %q", name, body.Name)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
2022-04-29 13:26:50 -04:00
|
|
|
return volume.Volume{
|
2017-02-27 12:39:35 -05:00
|
|
|
Name: body.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 13:50:28 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
buf := cli.OutBuffer()
|
2017-02-27 12:39:35 -05:00
|
|
|
|
|
|
|
// Test by flags
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.Flags().Set("name", name)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(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})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(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"
|
|
|
|
|
2017-08-16 13:50:28 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeCreateFunc: func(body volume.CreateOptions) (volume.Volume, error) {
|
2017-02-27 12:39:35 -05:00
|
|
|
if body.Name != "" {
|
2022-04-29 13:26:50 -04:00
|
|
|
return volume.Volume{}, errors.Errorf("expected empty name, got %q", body.Name)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
if body.Driver != expectedDriver {
|
2022-04-29 13:26:50 -04:00
|
|
|
return volume.Volume{}, errors.Errorf("expected driver %q, got %q", expectedDriver, body.Driver)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
2017-05-08 13:36:04 -04:00
|
|
|
if !reflect.DeepEqual(body.DriverOpts, expectedOpts) {
|
2022-04-29 13:26:50 -04:00
|
|
|
return volume.Volume{}, errors.Errorf("expected drivers opts %v, got %v", expectedOpts, body.DriverOpts)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
2017-05-08 13:36:04 -04:00
|
|
|
if !reflect.DeepEqual(body.Labels, expectedLabels) {
|
2022-04-29 13:26:50 -04:00
|
|
|
return volume.Volume{}, errors.Errorf("expected labels %v, got %v", expectedLabels, body.Labels)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
2022-04-29 13:26:50 -04:00
|
|
|
return volume.Volume{
|
2017-02-27 12:39:35 -05:00
|
|
|
Name: name,
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 13:50:28 -04:00
|
|
|
})
|
2017-02-27 12:39:35 -05:00
|
|
|
|
|
|
|
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")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(name, strings.TrimSpace(cli.OutBuffer().String())))
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
2021-02-23 09:23:53 -05:00
|
|
|
|
|
|
|
func TestVolumeCreateCluster(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
volumeCreateFunc: func(body volume.CreateOptions) (volume.Volume, error) {
|
|
|
|
if body.Driver == "csi" && body.ClusterVolumeSpec == nil {
|
|
|
|
return volume.Volume{}, errors.New("expected ClusterVolumeSpec, but none present")
|
|
|
|
}
|
|
|
|
if body.Driver == "notcsi" && body.ClusterVolumeSpec != nil {
|
|
|
|
return volume.Volume{}, errors.New("expected no ClusterVolumeSpec, but present")
|
|
|
|
}
|
|
|
|
return volume.Volume{}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.Flags().Set("type", "block")
|
|
|
|
cmd.Flags().Set("group", "gronp")
|
|
|
|
cmd.Flags().Set("driver", "csi")
|
|
|
|
cmd.SetArgs([]string{"name"})
|
|
|
|
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
|
|
|
|
cmd = newCreateCommand(cli)
|
|
|
|
cmd.Flags().Set("driver", "notcsi")
|
|
|
|
cmd.SetArgs([]string{"name"})
|
|
|
|
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumeCreateClusterOpts(t *testing.T) {
|
|
|
|
expectedBody := volume.CreateOptions{
|
|
|
|
Name: "name",
|
|
|
|
Driver: "csi",
|
|
|
|
DriverOpts: map[string]string{},
|
|
|
|
Labels: map[string]string{},
|
|
|
|
ClusterVolumeSpec: &volume.ClusterVolumeSpec{
|
|
|
|
Group: "gronp",
|
|
|
|
AccessMode: &volume.AccessMode{
|
|
|
|
Scope: volume.ScopeMultiNode,
|
|
|
|
Sharing: volume.SharingOneWriter,
|
|
|
|
// TODO(dperny): support mount options
|
|
|
|
MountVolume: &volume.TypeMount{},
|
|
|
|
},
|
|
|
|
// TODO(dperny): topology requirements
|
|
|
|
CapacityRange: &volume.CapacityRange{
|
|
|
|
RequiredBytes: 1234,
|
|
|
|
LimitBytes: 567890,
|
|
|
|
},
|
|
|
|
Secrets: []volume.Secret{
|
|
|
|
{Key: "key1", Secret: "secret1"},
|
|
|
|
{Key: "key2", Secret: "secret2"},
|
|
|
|
},
|
|
|
|
Availability: volume.AvailabilityActive,
|
|
|
|
AccessibilityRequirements: &volume.TopologyRequirement{
|
|
|
|
Requisite: []volume.Topology{
|
|
|
|
{Segments: map[string]string{"region": "R1", "zone": "Z1"}},
|
|
|
|
{Segments: map[string]string{"region": "R1", "zone": "Z2"}},
|
|
|
|
{Segments: map[string]string{"region": "R1", "zone": "Z3"}},
|
|
|
|
},
|
|
|
|
Preferred: []volume.Topology{
|
|
|
|
{Segments: map[string]string{"region": "R1", "zone": "Z2"}},
|
|
|
|
{Segments: map[string]string{"region": "R1", "zone": "Z3"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
volumeCreateFunc: func(body volume.CreateOptions) (volume.Volume, error) {
|
|
|
|
assert.DeepEqual(t, body, expectedBody)
|
|
|
|
return volume.Volume{}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"name"})
|
|
|
|
cmd.Flags().Set("driver", "csi")
|
|
|
|
cmd.Flags().Set("group", "gronp")
|
|
|
|
cmd.Flags().Set("scope", "multi")
|
|
|
|
cmd.Flags().Set("sharing", "onewriter")
|
|
|
|
cmd.Flags().Set("type", "mount")
|
|
|
|
cmd.Flags().Set("sharing", "onewriter")
|
|
|
|
cmd.Flags().Set("required-bytes", "1234")
|
|
|
|
cmd.Flags().Set("limit-bytes", "567890")
|
|
|
|
|
|
|
|
cmd.Flags().Set("secret", "key1=secret1")
|
|
|
|
cmd.Flags().Set("secret", "key2=secret2")
|
|
|
|
|
|
|
|
cmd.Flags().Set("topology-required", "region=R1,zone=Z1")
|
|
|
|
cmd.Flags().Set("topology-required", "region=R1,zone=Z2")
|
|
|
|
cmd.Flags().Set("topology-required", "region=R1,zone=Z3")
|
|
|
|
|
|
|
|
cmd.Flags().Set("topology-preferred", "region=R1,zone=Z2")
|
|
|
|
cmd.Flags().Set("topology-preferred", "region=R1,zone=Z3")
|
|
|
|
|
|
|
|
cmd.Execute()
|
|
|
|
}
|