This error didn't do a great job at formatting. If a StatusError was
produced without a Status message, it would print a very non-informative
error, with information missing.
Let's update the output:
- If a status-message is provided; print just that (after all the
status code is something that can be found from the shell, e.g.
through `echo $?` in Bash).
- If no status-message is provided: print a message more similar to
Go's `exec.ExecError`, which uses `os.rocessState.String()` (see [1]).
Before this patch, an error without custom status would print:
Status: , Code: 2
After this patch:
exit status 2
In situations where a custom error-message is provided, the error-message
is print as-is, whereas before this patch, the message got combined with
the `Status:` and `Code:`, which resulted in some odd output.
Before this patch:
docker volume --no-such-flag
Status: unknown flag: --no-such-flag
See 'docker volume --help'.
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove unused local volumes
rm Remove one or more volumes
update Update a volume (cluster volumes only)
Run 'docker volume COMMAND --help' for more information on a command.
, Code: 125
With this patch, the error is shown as-is;
docker volume --no-such-flag
unknown flag: --no-such-flag
See 'docker volume --help'.
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove unused local volumes
rm Remove one or more volumes
update Update a volume (cluster volumes only)
Run 'docker volume COMMAND --help' for more information on a command.
While the exit-code is no longer printed, it's still properly handled;
echo $?
125
[1]: 82c14346d8/src/os/exec_posix.go (L107-L135)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
121 lines
3.1 KiB
Go
121 lines
3.1 KiB
Go
package network
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
"github.com/docker/docker/api/types/network"
|
|
"github.com/docker/docker/errdefs"
|
|
"github.com/pkg/errors"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestNetworkRemoveForce(t *testing.T) {
|
|
tests := []struct {
|
|
doc string
|
|
args []string
|
|
expectedErr string
|
|
}{
|
|
{
|
|
doc: "existing network",
|
|
args: []string{"existing-network"},
|
|
},
|
|
{
|
|
doc: "existing network (forced)",
|
|
args: []string{"--force", "existing-network"},
|
|
},
|
|
{
|
|
doc: "non-existing network",
|
|
args: []string{"no-such-network"},
|
|
expectedErr: "no such network: no-such-network",
|
|
},
|
|
{
|
|
doc: "non-existing network (forced)",
|
|
args: []string{"--force", "no-such-network"},
|
|
},
|
|
{
|
|
doc: "in-use network",
|
|
args: []string{"in-use-network"},
|
|
expectedErr: "network is in use",
|
|
},
|
|
{
|
|
doc: "in-use network (forced)",
|
|
args: []string{"--force", "in-use-network"},
|
|
expectedErr: "network is in use",
|
|
},
|
|
{
|
|
doc: "multiple networks",
|
|
args: []string{"existing-network", "no-such-network"},
|
|
expectedErr: "no such network: no-such-network",
|
|
},
|
|
{
|
|
doc: "multiple networks (forced)",
|
|
args: []string{"--force", "existing-network", "no-such-network"},
|
|
},
|
|
{
|
|
doc: "multiple networks 2 (forced)",
|
|
args: []string{"--force", "existing-network", "no-such-network", "in-use-network"},
|
|
expectedErr: "network is in use",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
tc := tc
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
fakeCli := test.NewFakeCli(&fakeClient{
|
|
networkRemoveFunc: func(ctx context.Context, networkID string) error {
|
|
switch networkID {
|
|
case "no-such-network":
|
|
return errdefs.NotFound(errors.New("no such network: no-such-network"))
|
|
case "in-use-network":
|
|
return errdefs.Forbidden(errors.New("network is in use"))
|
|
case "existing-network":
|
|
return nil
|
|
default:
|
|
return nil
|
|
}
|
|
},
|
|
})
|
|
|
|
cmd := newRemoveCommand(fakeCli)
|
|
cmd.SetOut(io.Discard)
|
|
cmd.SetErr(fakeCli.ErrBuffer())
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
|
if tc.expectedErr == "" {
|
|
assert.NilError(t, err)
|
|
} else {
|
|
assert.Check(t, is.Contains(fakeCli.ErrBuffer().String(), tc.expectedErr))
|
|
assert.ErrorContains(t, err, "exit status 1")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNetworkRemovePromptTermination(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
t.Cleanup(cancel)
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
networkRemoveFunc: func(ctx context.Context, networkID string) error {
|
|
return errors.New("fakeClient networkRemoveFunc should not be called")
|
|
},
|
|
networkInspectFunc: func(ctx context.Context, networkID string, options network.InspectOptions) (network.Inspect, []byte, error) {
|
|
return network.Inspect{
|
|
ID: "existing-network",
|
|
Name: "existing-network",
|
|
Ingress: true,
|
|
}, nil, nil
|
|
},
|
|
})
|
|
cmd := newRemoveCommand(cli)
|
|
cmd.SetArgs([]string{"existing-network"})
|
|
cmd.SetOut(io.Discard)
|
|
cmd.SetErr(io.Discard)
|
|
test.TerminatePrompt(ctx, t, cmd, cli)
|
|
}
|