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

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

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Alexander Morozov 2017-02-14 10:48:42 -08:00 committed by Sebastiaan van Stijn
parent b0e4cf1ad2
commit ecfddcf227
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 29 additions and 1 deletions

View File

@ -33,7 +33,9 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c
// If the container is either not running or restarting and requires no stream, return an empty stats.
if (!container.IsRunning() || container.IsRestarting()) && !config.Stream {
return json.NewEncoder(config.OutStream).Encode(&types.Stats{})
return json.NewEncoder(config.OutStream).Encode(&types.StatsJSON{
Name: container.Name,
ID: container.ID})
}
outStream := config.OutStream

View File

@ -120,7 +120,14 @@ func (s *statsCollector) run() {
if err != nil {
if _, ok := err.(errNotRunning); !ok {
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
continue
}
// publish empty stats containing only name and ID if not running
pair.publisher.Publish(types.StatsJSON{
Name: pair.container.Name,
ID: pair.container.ID,
})
continue
}
// FIXME: move to containerd on Linux (not Windows)

View File

@ -157,3 +157,22 @@ func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
// ignore, done
}
}
func (s *DockerSuite) TestStatsFormatAll(c *check.C) {
// Windows does not support stats
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "-d", "--name=RunningOne", "busybox", "top")
c.Assert(waitRun("RunningOne"), check.IsNil)
dockerCmd(c, "run", "-d", "--name=ExitedOne", "busybox", "top")
dockerCmd(c, "stop", "ExitedOne")
c.Assert(waitExited("ExitedOne", 5*time.Second), check.IsNil)
out, _ := dockerCmd(c, "stats", "--no-stream", "--format", "{{.Name}}")
c.Assert(out, checker.Contains, "RunningOne")
c.Assert(out, checker.Not(checker.Contains), "ExitedOne")
out, _ = dockerCmd(c, "stats", "--all", "--no-stream", "--format", "{{.Name}}")
c.Assert(out, checker.Contains, "RunningOne")
c.Assert(out, checker.Contains, "ExitedOne")
}