Merge pull request #30648 from allencloud/do-not-fail-fast-when-inspect
do not fail fast when executing inspect command
This commit is contained in:
commit
562d2dc978
4 changed files with 57 additions and 28 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"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()}
|
return cli.StatusError{StatusCode: 64, Status: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
var inspectErr error
|
var inspectErrs []string
|
||||||
for _, ref := range references {
|
for _, ref := range references {
|
||||||
element, raw, err := getRef(ref)
|
element, raw, err := getRef(ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
inspectErr = err
|
inspectErrs = append(inspectErrs, err.Error())
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := inspector.Inspect(element, raw); err != nil {
|
if err := inspector.Inspect(element, raw); err != nil {
|
||||||
inspectErr = err
|
inspectErrs = append(inspectErrs, err.Error())
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,8 +78,11 @@ func Inspect(out io.Writer, references []string, tmplStr string, getRef GetRefFu
|
||||||
logrus.Errorf("%s\n", err)
|
logrus.Errorf("%s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if inspectErr != nil {
|
if len(inspectErrs) != 0 {
|
||||||
return cli.StatusError{StatusCode: 1, Status: inspectErr.Error()}
|
return cli.StatusError{
|
||||||
|
StatusCode: 1,
|
||||||
|
Status: strings.Join(inspectErrs, "\n"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -353,14 +353,22 @@ func (s *DockerSuite) TestInspectByPrefix(c *check.C) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DockerSuite) TestInspectStopWhenNotFound(c *check.C) {
|
func (s *DockerSuite) TestInspectStopWhenNotFound(c *check.C) {
|
||||||
runSleepingContainer(c, "--name=busybox", "-d")
|
runSleepingContainer(c, "--name=busybox1", "-d")
|
||||||
runSleepingContainer(c, "--name=not-shown", "-d")
|
runSleepingContainer(c, "--name=busybox2", "-d")
|
||||||
out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.Name}}'", "busybox", "missing", "not-shown")
|
result := dockerCmdWithResult("inspect", "--type=container", "--format='{{.Name}}'", "busybox1", "busybox2", "missing")
|
||||||
|
|
||||||
c.Assert(err, checker.Not(check.IsNil))
|
c.Assert(result.Error, checker.Not(check.IsNil))
|
||||||
c.Assert(out, checker.Contains, "busybox")
|
c.Assert(result.Stdout(), checker.Contains, "busybox1")
|
||||||
c.Assert(out, checker.Not(checker.Contains), "not-shown")
|
c.Assert(result.Stdout(), checker.Contains, "busybox2")
|
||||||
c.Assert(out, checker.Contains, "Error: No such container: missing")
|
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) {
|
func (s *DockerSuite) TestInspectHistory(c *check.C) {
|
||||||
|
|
|
@ -486,9 +486,35 @@ func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
|
||||||
err := json.Unmarshal([]byte(result.Stdout()), &networkResources)
|
err := json.Unmarshal([]byte(result.Stdout()), &networkResources)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
c.Assert(networkResources, checker.HasLen, 2)
|
c.Assert(networkResources, checker.HasLen, 2)
|
||||||
|
}
|
||||||
|
|
||||||
// Should print an error, return an exitCode 1 *but* should print the host network
|
func (s *DockerSuite) TestDockerInspectMultipleNetworksIncludingNonexistent(c *check.C) {
|
||||||
result = dockerCmdWithResult("network", "inspect", "host", "nonexistent")
|
// 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{
|
c.Assert(result, icmd.Matches, icmd.Expected{
|
||||||
ExitCode: 1,
|
ExitCode: 1,
|
||||||
Err: "Error: No such network: nonexistent",
|
Err: "Error: No such network: nonexistent",
|
||||||
|
@ -499,14 +525,6 @@ func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
|
||||||
err = json.Unmarshal([]byte(result.Stdout()), &networkResources)
|
err = json.Unmarshal([]byte(result.Stdout()), &networkResources)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
c.Assert(networkResources, checker.HasLen, 1)
|
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) {
|
func (s *DockerSuite) TestDockerInspectNetworkWithContainerName(c *check.C) {
|
||||||
|
|
|
@ -51,9 +51,9 @@ func (s *DockerSuite) TestVolumeCLIInspect(c *check.C) {
|
||||||
func (s *DockerSuite) TestVolumeCLIInspectMulti(c *check.C) {
|
func (s *DockerSuite) TestVolumeCLIInspectMulti(c *check.C) {
|
||||||
dockerCmd(c, "volume", "create", "test1")
|
dockerCmd(c, "volume", "create", "test1")
|
||||||
dockerCmd(c, "volume", "create", "test2")
|
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{
|
c.Assert(result, icmd.Matches, icmd.Expected{
|
||||||
ExitCode: 1,
|
ExitCode: 1,
|
||||||
Err: "No such volume: doesntexist",
|
Err: "No such volume: doesntexist",
|
||||||
|
@ -61,11 +61,11 @@ func (s *DockerSuite) TestVolumeCLIInspectMulti(c *check.C) {
|
||||||
|
|
||||||
out := result.Stdout()
|
out := result.Stdout()
|
||||||
outArr := strings.Split(strings.TrimSpace(out), "\n")
|
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, "test1")
|
||||||
c.Assert(out, checker.Contains, "test2")
|
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) {
|
func (s *DockerSuite) TestVolumeCLILs(c *check.C) {
|
||||||
|
|
Loading…
Reference in a new issue