docker_cli_build_unix_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // +build !windows
  2. package main
  3. import (
  4. "encoding/json"
  5. "os/exec"
  6. "strings"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
  10. testRequires(c, CpuCfsQuota)
  11. name := "testbuildresourceconstraints"
  12. ctx, err := fakeContext(`
  13. FROM hello-world:frozen
  14. RUN ["/hello"]
  15. `, map[string]string{})
  16. if err != nil {
  17. c.Fatal(err)
  18. }
  19. cmd := exec.Command(dockerBinary, "build", "--no-cache", "--rm=false", "--memory=64m", "--memory-swap=-1", "--cpuset-cpus=0", "--cpuset-mems=0", "--cpu-shares=100", "--cpu-quota=8000", "-t", name, ".")
  20. cmd.Dir = ctx.Dir
  21. out, _, err := runCommandWithOutput(cmd)
  22. if err != nil {
  23. c.Fatal(err, out)
  24. }
  25. out, _ = dockerCmd(c, "ps", "-lq")
  26. cID := strings.TrimSpace(out)
  27. type hostConfig struct {
  28. Memory int64
  29. MemorySwap int64
  30. CpusetCpus string
  31. CpusetMems string
  32. CpuShares int64
  33. CpuQuota int64
  34. }
  35. cfg, err := inspectFieldJSON(cID, "HostConfig")
  36. if err != nil {
  37. c.Fatal(err)
  38. }
  39. var c1 hostConfig
  40. if err := json.Unmarshal([]byte(cfg), &c1); err != nil {
  41. c.Fatal(err, cfg)
  42. }
  43. if c1.Memory != 67108864 || c1.MemorySwap != -1 || c1.CpusetCpus != "0" || c1.CpusetMems != "0" || c1.CpuShares != 100 || c1.CpuQuota != 8000 {
  44. c.Fatalf("resource constraints not set properly:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d, CpuQuota: %d",
  45. c1.Memory, c1.MemorySwap, c1.CpusetCpus, c1.CpusetMems, c1.CpuShares, c1.CpuQuota)
  46. }
  47. // Make sure constraints aren't saved to image
  48. _, _ = dockerCmd(c, "run", "--name=test", name)
  49. cfg, err = inspectFieldJSON("test", "HostConfig")
  50. if err != nil {
  51. c.Fatal(err)
  52. }
  53. var c2 hostConfig
  54. if err := json.Unmarshal([]byte(cfg), &c2); err != nil {
  55. c.Fatal(err, cfg)
  56. }
  57. if c2.Memory == 67108864 || c2.MemorySwap == -1 || c2.CpusetCpus == "0" || c2.CpusetMems == "0" || c2.CpuShares == 100 || c2.CpuQuota == 8000 {
  58. c.Fatalf("resource constraints leaked from build:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d, CpuQuota: %d",
  59. c2.Memory, c2.MemorySwap, c2.CpusetCpus, c2.CpusetMems, c2.CpuShares, c2.CpuQuota)
  60. }
  61. }