docker_cli_stats_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package main
  2. import (
  3. "bytes"
  4. "os/exec"
  5. "strings"
  6. "time"
  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), check.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. if outerr.err != nil {
  27. c.Fatalf("Error running stats: %v", outerr.err)
  28. }
  29. if !bytes.Contains(outerr.out, []byte(id)) {
  30. c.Fatalf("running container wasn't present in output")
  31. }
  32. case <-time.After(3 * time.Second):
  33. statsCmd.Process.Kill()
  34. c.Fatalf("stats did not return immediately when not streaming")
  35. }
  36. }
  37. func (s *DockerSuite) TestStatsContainerNotFound(c *check.C) {
  38. testRequires(c, DaemonIsLinux)
  39. out, _, err := dockerCmdWithError("stats", "notfound")
  40. c.Assert(err, check.NotNil)
  41. if !strings.Contains(out, "no such id: notfound") {
  42. c.Fatalf("Expected to fail on not found container stats, got %q instead", out)
  43. }
  44. out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
  45. c.Assert(err, check.NotNil)
  46. if !strings.Contains(out, "no such id: notfound") {
  47. c.Fatalf("Expected to fail on not found container stats with --no-stream, got %q instead", out)
  48. }
  49. }