Browse code

do not fail fast when executing inspect command

Signed-off-by: allencloud <allen.sun@daocloud.io>

allencloud authored on 2017/02/02 05:03:58
Showing 4 changed files
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"encoding/json"
6 6
 	"fmt"
7 7
 	"io"
8
+	"strings"
8 9
 	"text/template"
9 10
 
10 11
 	"github.com/Sirupsen/logrus"
... ...
@@ -60,17 +61,16 @@ func Inspect(out io.Writer, references []string, tmplStr string, getRef GetRefFu
60 60
 		return cli.StatusError{StatusCode: 64, Status: err.Error()}
61 61
 	}
62 62
 
63
-	var inspectErr error
63
+	var inspectErrs []string
64 64
 	for _, ref := range references {
65 65
 		element, raw, err := getRef(ref)
66 66
 		if err != nil {
67
-			inspectErr = err
68
-			break
67
+			inspectErrs = append(inspectErrs, err.Error())
68
+			continue
69 69
 		}
70 70
 
71 71
 		if err := inspector.Inspect(element, raw); err != nil {
72
-			inspectErr = err
73
-			break
72
+			inspectErrs = append(inspectErrs, err.Error())
74 73
 		}
75 74
 	}
76 75
 
... ...
@@ -78,8 +78,11 @@ func Inspect(out io.Writer, references []string, tmplStr string, getRef GetRefFu
78 78
 		logrus.Errorf("%s\n", err)
79 79
 	}
80 80
 
81
-	if inspectErr != nil {
82
-		return cli.StatusError{StatusCode: 1, Status: inspectErr.Error()}
81
+	if len(inspectErrs) != 0 {
82
+		return cli.StatusError{
83
+			StatusCode: 1,
84
+			Status:     strings.Join(inspectErrs, "\n"),
85
+		}
83 86
 	}
84 87
 	return nil
85 88
 }
... ...
@@ -353,14 +353,22 @@ func (s *DockerSuite) TestInspectByPrefix(c *check.C) {
353 353
 }
354 354
 
355 355
 func (s *DockerSuite) TestInspectStopWhenNotFound(c *check.C) {
356
-	runSleepingContainer(c, "--name=busybox", "-d")
357
-	runSleepingContainer(c, "--name=not-shown", "-d")
358
-	out, _, err := dockerCmdWithError("inspect", "--type=container", "--format='{{.Name}}'", "busybox", "missing", "not-shown")
359
-
360
-	c.Assert(err, checker.Not(check.IsNil))
361
-	c.Assert(out, checker.Contains, "busybox")
362
-	c.Assert(out, checker.Not(checker.Contains), "not-shown")
363
-	c.Assert(out, checker.Contains, "Error: No such container: missing")
356
+	runSleepingContainer(c, "--name=busybox1", "-d")
357
+	runSleepingContainer(c, "--name=busybox2", "-d")
358
+	result := dockerCmdWithResult("inspect", "--type=container", "--format='{{.Name}}'", "busybox1", "busybox2", "missing")
359
+
360
+	c.Assert(result.Error, checker.Not(check.IsNil))
361
+	c.Assert(result.Stdout(), checker.Contains, "busybox1")
362
+	c.Assert(result.Stdout(), checker.Contains, "busybox2")
363
+	c.Assert(result.Stderr(), checker.Contains, "Error: No such container: missing")
364
+
365
+	// test inspect would not fast fail
366
+	result = dockerCmdWithResult("inspect", "--type=container", "--format='{{.Name}}'", "missing", "busybox1", "busybox2")
367
+
368
+	c.Assert(result.Error, checker.Not(check.IsNil))
369
+	c.Assert(result.Stdout(), checker.Contains, "busybox1")
370
+	c.Assert(result.Stdout(), checker.Contains, "busybox2")
371
+	c.Assert(result.Stderr(), checker.Contains, "Error: No such container: missing")
364 372
 }
365 373
 
366 374
 func (s *DockerSuite) TestInspectHistory(c *check.C) {
... ...
@@ -486,20 +486,24 @@ func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
486 486
 	err := json.Unmarshal([]byte(result.Stdout()), &networkResources)
487 487
 	c.Assert(err, check.IsNil)
488 488
 	c.Assert(networkResources, checker.HasLen, 2)
489
+}
489 490
 
490
-	// Should print an error, return an exitCode 1 *but* should print the host network
491
-	result = dockerCmdWithResult("network", "inspect", "host", "nonexistent")
491
+func (s *DockerSuite) TestDockerInspectMultipleNetworksIncludingNonexistent(c *check.C) {
492
+	// non-existent network was not at the beginning of the inspect list
493
+	// This should print an error, return an exitCode 1 and print the host network
494
+	result := dockerCmdWithResult("network", "inspect", "host", "nonexistent")
492 495
 	c.Assert(result, icmd.Matches, icmd.Expected{
493 496
 		ExitCode: 1,
494 497
 		Err:      "Error: No such network: nonexistent",
495 498
 		Out:      "host",
496 499
 	})
497 500
 
498
-	networkResources = []types.NetworkResource{}
499
-	err = json.Unmarshal([]byte(result.Stdout()), &networkResources)
501
+	networkResources := []types.NetworkResource{}
502
+	err := json.Unmarshal([]byte(result.Stdout()), &networkResources)
500 503
 	c.Assert(err, check.IsNil)
501 504
 	c.Assert(networkResources, checker.HasLen, 1)
502 505
 
506
+	// Only one non-existent network to inspect
503 507
 	// Should print an error and return an exitCode, nothing else
504 508
 	result = dockerCmdWithResult("network", "inspect", "nonexistent")
505 509
 	c.Assert(result, icmd.Matches, icmd.Expected{
... ...
@@ -507,6 +511,20 @@ func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
507 507
 		Err:      "Error: No such network: nonexistent",
508 508
 		Out:      "[]",
509 509
 	})
510
+
511
+	// non-existent network was at the beginning of the inspect list
512
+	// Should not fail fast, and still print host network but print an error
513
+	result = dockerCmdWithResult("network", "inspect", "nonexistent", "host")
514
+	c.Assert(result, icmd.Matches, icmd.Expected{
515
+		ExitCode: 1,
516
+		Err:      "Error: No such network: nonexistent",
517
+		Out:      "host",
518
+	})
519
+
520
+	networkResources = []types.NetworkResource{}
521
+	err = json.Unmarshal([]byte(result.Stdout()), &networkResources)
522
+	c.Assert(err, check.IsNil)
523
+	c.Assert(networkResources, checker.HasLen, 1)
510 524
 }
511 525
 
512 526
 func (s *DockerSuite) TestDockerInspectNetworkWithContainerName(c *check.C) {
... ...
@@ -51,9 +51,9 @@ func (s *DockerSuite) TestVolumeCLIInspect(c *check.C) {
51 51
 func (s *DockerSuite) TestVolumeCLIInspectMulti(c *check.C) {
52 52
 	dockerCmd(c, "volume", "create", "test1")
53 53
 	dockerCmd(c, "volume", "create", "test2")
54
-	dockerCmd(c, "volume", "create", "not-shown")
54
+	dockerCmd(c, "volume", "create", "test3")
55 55
 
56
-	result := dockerCmdWithResult("volume", "inspect", "--format={{ .Name }}", "test1", "test2", "doesntexist", "not-shown")
56
+	result := dockerCmdWithResult("volume", "inspect", "--format={{ .Name }}", "test1", "test2", "doesntexist", "test3")
57 57
 	c.Assert(result, icmd.Matches, icmd.Expected{
58 58
 		ExitCode: 1,
59 59
 		Err:      "No such volume: doesntexist",
... ...
@@ -61,11 +61,11 @@ func (s *DockerSuite) TestVolumeCLIInspectMulti(c *check.C) {
61 61
 
62 62
 	out := result.Stdout()
63 63
 	outArr := strings.Split(strings.TrimSpace(out), "\n")
64
-	c.Assert(len(outArr), check.Equals, 2, check.Commentf("\n%s", out))
64
+	c.Assert(len(outArr), check.Equals, 3, check.Commentf("\n%s", out))
65 65
 
66 66
 	c.Assert(out, checker.Contains, "test1")
67 67
 	c.Assert(out, checker.Contains, "test2")
68
-	c.Assert(out, checker.Not(checker.Contains), "not-shown")
68
+	c.Assert(out, checker.Contains, "test3")
69 69
 }
70 70
 
71 71
 func (s *DockerSuite) TestVolumeCLILs(c *check.C) {