do not fail fast when executing inspect command

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2017-02-02 04:03:58 +08:00
parent 239645c08d
commit fb1f22b06a
4 changed files with 57 additions and 28 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"strings"
"text/template"
"github.com/Sirupsen/logrus"
@ -60,17 +61,16 @@ func Inspect(out io.Writer, references []string, tmplStr string, getRef GetRefFu
return cli.StatusError{StatusCode: 64, Status: err.Error()}
}
var inspectErr error
var inspectErrs []string
for _, ref := range references {
element, raw, err := getRef(ref)
if err != nil {
inspectErr = err
break
inspectErrs = append(inspectErrs, err.Error())
continue
}
if err := inspector.Inspect(element, raw); err != nil {
inspectErr = err
break
inspectErrs = append(inspectErrs, err.Error())
}
}
@ -78,8 +78,11 @@ func Inspect(out io.Writer, references []string, tmplStr string, getRef GetRefFu
logrus.Errorf("%s\n", err)
}
if inspectErr != nil {
return cli.StatusError{StatusCode: 1, Status: inspectErr.Error()}
if len(inspectErrs) != 0 {
return cli.StatusError{
StatusCode: 1,
Status: strings.Join(inspectErrs, "\n"),
}
}
return nil
}

View File

@ -353,14 +353,22 @@ func (s *DockerSuite) TestInspectByPrefix(c *check.C) {
}
func (s *DockerSuite) TestInspectStopWhenNotFound(c *check.C) {
runSleepingContainer(c, "--name=busybox", "-d")
runSleepingContainer(c, "--name=not-shown", "-d")
out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.Name}}'", "busybox", "missing", "not-shown")
runSleepingContainer(c, "--name=busybox1", "-d")
runSleepingContainer(c, "--name=busybox2", "-d")
result := dockerCmdWithResult("inspect", "--type=container", "--format='{{.Name}}'", "busybox1", "busybox2", "missing")
c.Assert(err, checker.Not(check.IsNil))
c.Assert(out, checker.Contains, "busybox")
c.Assert(out, checker.Not(checker.Contains), "not-shown")
c.Assert(out, checker.Contains, "Error: No such container: missing")
c.Assert(result.Error, checker.Not(check.IsNil))
c.Assert(result.Stdout(), checker.Contains, "busybox1")
c.Assert(result.Stdout(), checker.Contains, "busybox2")
c.Assert(result.Stderr(), checker.Contains, "Error: No such container: missing")
// test inspect would not fast fail
result = dockerCmdWithResult("inspect", "--type=container", "--format='{{.Name}}'", "missing", "busybox1", "busybox2")
c.Assert(result.Error, checker.Not(check.IsNil))
c.Assert(result.Stdout(), checker.Contains, "busybox1")
c.Assert(result.Stdout(), checker.Contains, "busybox2")
c.Assert(result.Stderr(), checker.Contains, "Error: No such container: missing")
}
func (s *DockerSuite) TestInspectHistory(c *check.C) {

View File

@ -486,9 +486,35 @@ func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
err := json.Unmarshal([]byte(result.Stdout()), &networkResources)
c.Assert(err, check.IsNil)
c.Assert(networkResources, checker.HasLen, 2)
}
// Should print an error, return an exitCode 1 *but* should print the host network
result = dockerCmdWithResult("network", "inspect", "host", "nonexistent")
func (s *DockerSuite) TestDockerInspectMultipleNetworksIncludingNonexistent(c *check.C) {
// non-existent network was not at the beginning of the inspect list
// This should print an error, return an exitCode 1 and print the host network
result := dockerCmdWithResult("network", "inspect", "host", "nonexistent")
c.Assert(result, icmd.Matches, icmd.Expected{
ExitCode: 1,
Err: "Error: No such network: nonexistent",
Out: "host",
})
networkResources := []types.NetworkResource{}
err := json.Unmarshal([]byte(result.Stdout()), &networkResources)
c.Assert(err, check.IsNil)
c.Assert(networkResources, checker.HasLen, 1)
// Only one non-existent network to inspect
// Should print an error and return an exitCode, nothing else
result = dockerCmdWithResult("network", "inspect", "nonexistent")
c.Assert(result, icmd.Matches, icmd.Expected{
ExitCode: 1,
Err: "Error: No such network: nonexistent",
Out: "[]",
})
// non-existent network was at the beginning of the inspect list
// Should not fail fast, and still print host network but print an error
result = dockerCmdWithResult("network", "inspect", "nonexistent", "host")
c.Assert(result, icmd.Matches, icmd.Expected{
ExitCode: 1,
Err: "Error: No such network: nonexistent",
@ -499,14 +525,6 @@ func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
err = json.Unmarshal([]byte(result.Stdout()), &networkResources)
c.Assert(err, check.IsNil)
c.Assert(networkResources, checker.HasLen, 1)
// Should print an error and return an exitCode, nothing else
result = dockerCmdWithResult("network", "inspect", "nonexistent")
c.Assert(result, icmd.Matches, icmd.Expected{
ExitCode: 1,
Err: "Error: No such network: nonexistent",
Out: "[]",
})
}
func (s *DockerSuite) TestDockerInspectNetworkWithContainerName(c *check.C) {

View File

@ -51,9 +51,9 @@ func (s *DockerSuite) TestVolumeCLIInspect(c *check.C) {
func (s *DockerSuite) TestVolumeCLIInspectMulti(c *check.C) {
dockerCmd(c, "volume", "create", "test1")
dockerCmd(c, "volume", "create", "test2")
dockerCmd(c, "volume", "create", "not-shown")
dockerCmd(c, "volume", "create", "test3")
result := dockerCmdWithResult("volume", "inspect", "--format={{ .Name }}", "test1", "test2", "doesntexist", "not-shown")
result := dockerCmdWithResult("volume", "inspect", "--format={{ .Name }}", "test1", "test2", "doesntexist", "test3")
c.Assert(result, icmd.Matches, icmd.Expected{
ExitCode: 1,
Err: "No such volume: doesntexist",
@ -61,11 +61,11 @@ func (s *DockerSuite) TestVolumeCLIInspectMulti(c *check.C) {
out := result.Stdout()
outArr := strings.Split(strings.TrimSpace(out), "\n")
c.Assert(len(outArr), check.Equals, 2, check.Commentf("\n%s", out))
c.Assert(len(outArr), check.Equals, 3, check.Commentf("\n%s", out))
c.Assert(out, checker.Contains, "test1")
c.Assert(out, checker.Contains, "test2")
c.Assert(out, checker.Not(checker.Contains), "not-shown")
c.Assert(out, checker.Contains, "test3")
}
func (s *DockerSuite) TestVolumeCLILs(c *check.C) {