From fb1f22b06a8785af3ddf4bf8893de75a52295cf8 Mon Sep 17 00:00:00 2001 From: allencloud Date: Thu, 2 Feb 2017 04:03:58 +0800 Subject: [PATCH] do not fail fast when executing inspect command Signed-off-by: allencloud --- cli/command/inspect/inspector.go | 17 +++++---- integration-cli/docker_cli_inspect_test.go | 22 +++++++---- .../docker_cli_network_unix_test.go | 38 ++++++++++++++----- integration-cli/docker_cli_volume_test.go | 8 ++-- 4 files changed, 57 insertions(+), 28 deletions(-) diff --git a/cli/command/inspect/inspector.go b/cli/command/inspect/inspector.go index 1e53671f84..a899da065b 100644 --- a/cli/command/inspect/inspector.go +++ b/cli/command/inspect/inspector.go @@ -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 } diff --git a/integration-cli/docker_cli_inspect_test.go b/integration-cli/docker_cli_inspect_test.go index a4d2dfa7e2..330cfc9a1b 100644 --- a/integration-cli/docker_cli_inspect_test.go +++ b/integration-cli/docker_cli_inspect_test.go @@ -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) { diff --git a/integration-cli/docker_cli_network_unix_test.go b/integration-cli/docker_cli_network_unix_test.go index a7f6a73851..c5e233f6d2 100644 --- a/integration-cli/docker_cli_network_unix_test.go +++ b/integration-cli/docker_cli_network_unix_test.go @@ -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) { diff --git a/integration-cli/docker_cli_volume_test.go b/integration-cli/docker_cli_volume_test.go index a6284aa94f..eadf5bb2d0 100644 --- a/integration-cli/docker_cli_volume_test.go +++ b/integration-cli/docker_cli_volume_test.go @@ -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) {