docker_cli_stats_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "time"
  6. "github.com/docker/docker/pkg/integration/checker"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestStatsNoStream(c *check.C) {
  10. testRequires(c, DaemonIsLinux)
  11. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  12. id := strings.TrimSpace(out)
  13. c.Assert(waitRun(id), checker.IsNil)
  14. statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
  15. type output struct {
  16. out []byte
  17. err error
  18. }
  19. ch := make(chan output)
  20. go func() {
  21. out, err := statsCmd.Output()
  22. ch <- output{out, err}
  23. }()
  24. select {
  25. case outerr := <-ch:
  26. c.Assert(outerr.err, checker.IsNil, check.Commentf("Error running stats: %v", outerr.err))
  27. c.Assert(string(outerr.out), checker.Contains, id) //running container wasn't present in output
  28. case <-time.After(3 * time.Second):
  29. statsCmd.Process.Kill()
  30. c.Fatalf("stats did not return immediately when not streaming")
  31. }
  32. }
  33. func (s *DockerSuite) TestStatsContainerNotFound(c *check.C) {
  34. testRequires(c, DaemonIsLinux)
  35. out, _, err := dockerCmdWithError("stats", "notfound")
  36. c.Assert(err, checker.NotNil)
  37. c.Assert(out, checker.Contains, "no such id: notfound", check.Commentf("Expected to fail on not found container stats, got %q instead", out))
  38. out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
  39. c.Assert(err, checker.NotNil)
  40. c.Assert(out, checker.Contains, "no such id: notfound", check.Commentf("Expected to fail on not found container stats with --no-stream, got %q instead", out))
  41. }