cli/command/volume: TestVolumeCreateWithName: minor fixes and improvements

- assert unhandled error
- use sub-tests
- add test-case for conflicting options (both flag and name)
- reset command-args to prevent test failing when running from pre-compiled test-binary
- use a const and a slightly more unique name for the volume-name
- discard stdout/stderr output

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-02-01 14:00:59 +01:00
parent 2e266001c6
commit 8b5e5539e1
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -57,7 +57,7 @@ func TestVolumeCreateErrors(t *testing.T) {
} }
func TestVolumeCreateWithName(t *testing.T) { func TestVolumeCreateWithName(t *testing.T) {
name := "foo" const name = "my-volume-name"
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
volumeCreateFunc: func(body volume.CreateOptions) (volume.Volume, error) { volumeCreateFunc: func(body volume.CreateOptions) (volume.Volume, error) {
if body.Name != name { if body.Name != name {
@ -70,19 +70,37 @@ func TestVolumeCreateWithName(t *testing.T) {
}) })
buf := cli.OutBuffer() buf := cli.OutBuffer()
t.Run("using-flags", func(t *testing.T) {
cmd := newCreateCommand(cli)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs([]string{})
assert.Check(t, cmd.Flags().Set("name", name))
assert.NilError(t, cmd.Execute())
assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), name))
})
// 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() buf.Reset()
cmd = newCreateCommand(cli) t.Run("using-args", func(t *testing.T) {
cmd.SetArgs([]string{name}) cmd := newCreateCommand(cli)
assert.NilError(t, cmd.Execute()) cmd.SetOut(io.Discard)
assert.Check(t, is.Equal(name, strings.TrimSpace(buf.String()))) cmd.SetErr(io.Discard)
cmd.SetArgs([]string{name})
assert.NilError(t, cmd.Execute())
assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), name))
})
buf.Reset()
t.Run("using-both", func(t *testing.T) {
cmd := newCreateCommand(cli)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs([]string{name})
assert.Check(t, cmd.Flags().Set("name", name))
err := cmd.Execute()
assert.Check(t, is.Error(err, `conflicting options: cannot specify a volume-name through both --name and as a positional arg`))
assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), ""))
})
} }
func TestVolumeCreateWithFlags(t *testing.T) { func TestVolumeCreateWithFlags(t *testing.T) {