Browse code

Merge pull request #29702 from WeiZhang555/stats-all-format-name-panic

Send "Name" and "ID" when stating stopped containers
(cherry picked from commit 22472c8be50ca0ddaf8207d46c6a9f5e792bd0af)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Alexander Morozov authored on 2017/02/15 03:48:42
Showing 3 changed files
... ...
@@ -33,7 +33,9 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c
33 33
 
34 34
 	// If the container is either not running or restarting and requires no stream, return an empty stats.
35 35
 	if (!container.IsRunning() || container.IsRestarting()) && !config.Stream {
36
-		return json.NewEncoder(config.OutStream).Encode(&types.Stats{})
36
+		return json.NewEncoder(config.OutStream).Encode(&types.StatsJSON{
37
+			Name: container.Name,
38
+			ID:   container.ID})
37 39
 	}
38 40
 
39 41
 	outStream := config.OutStream
... ...
@@ -120,7 +120,14 @@ func (s *statsCollector) run() {
120 120
 			if err != nil {
121 121
 				if _, ok := err.(errNotRunning); !ok {
122 122
 					logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
123
+					continue
123 124
 				}
125
+
126
+				// publish empty stats containing only name and ID if not running
127
+				pair.publisher.Publish(types.StatsJSON{
128
+					Name: pair.container.Name,
129
+					ID:   pair.container.ID,
130
+				})
124 131
 				continue
125 132
 			}
126 133
 			// FIXME: move to containerd on Linux (not Windows)
... ...
@@ -157,3 +157,22 @@ func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
157 157
 		// ignore, done
158 158
 	}
159 159
 }
160
+
161
+func (s *DockerSuite) TestStatsFormatAll(c *check.C) {
162
+	// Windows does not support stats
163
+	testRequires(c, DaemonIsLinux)
164
+
165
+	dockerCmd(c, "run", "-d", "--name=RunningOne", "busybox", "top")
166
+	c.Assert(waitRun("RunningOne"), check.IsNil)
167
+	dockerCmd(c, "run", "-d", "--name=ExitedOne", "busybox", "top")
168
+	dockerCmd(c, "stop", "ExitedOne")
169
+	c.Assert(waitExited("ExitedOne", 5*time.Second), check.IsNil)
170
+
171
+	out, _ := dockerCmd(c, "stats", "--no-stream", "--format", "{{.Name}}")
172
+	c.Assert(out, checker.Contains, "RunningOne")
173
+	c.Assert(out, checker.Not(checker.Contains), "ExitedOne")
174
+
175
+	out, _ = dockerCmd(c, "stats", "--all", "--no-stream", "--format", "{{.Name}}")
176
+	c.Assert(out, checker.Contains, "RunningOne")
177
+	c.Assert(out, checker.Contains, "ExitedOne")
178
+}