docker_cli_build_unix_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // +build !windows
  2. package main
  3. import (
  4. "encoding/json"
  5. "strings"
  6. "github.com/docker/docker/pkg/integration/checker"
  7. "github.com/docker/go-units"
  8. "github.com/go-check/check"
  9. )
  10. func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
  11. testRequires(c, cpuCfsQuota)
  12. name := "testbuildresourceconstraints"
  13. ctx, err := fakeContext(`
  14. FROM hello-world:frozen
  15. RUN ["/hello"]
  16. `, map[string]string{})
  17. c.Assert(err, checker.IsNil)
  18. dockerCmdInDir(c, ctx.Dir, "build", "--no-cache", "--rm=false", "--memory=64m", "--memory-swap=-1", "--cpuset-cpus=0", "--cpuset-mems=0", "--cpu-shares=100", "--cpu-quota=8000", "--ulimit", "nofile=42", "-t", name, ".")
  19. out, _ := dockerCmd(c, "ps", "-lq")
  20. cID := strings.TrimSpace(out)
  21. type hostConfig struct {
  22. Memory int64
  23. MemorySwap int64
  24. CpusetCpus string
  25. CpusetMems string
  26. CPUShares int64
  27. CPUQuota int64
  28. Ulimits []*units.Ulimit
  29. }
  30. cfg, err := inspectFieldJSON(cID, "HostConfig")
  31. c.Assert(err, checker.IsNil)
  32. var c1 hostConfig
  33. err = json.Unmarshal([]byte(cfg), &c1)
  34. c.Assert(err, checker.IsNil, check.Commentf(cfg))
  35. c.Assert(c1.Memory, checker.Equals, int64(64*1024*1024), check.Commentf("resource constraints not set properly for Memory"))
  36. c.Assert(c1.MemorySwap, checker.Equals, int64(-1), check.Commentf("resource constraints not set properly for MemorySwap"))
  37. c.Assert(c1.CpusetCpus, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetCpus"))
  38. c.Assert(c1.CpusetMems, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetMems"))
  39. c.Assert(c1.CPUShares, checker.Equals, int64(100), check.Commentf("resource constraints not set properly for CPUShares"))
  40. c.Assert(c1.CPUQuota, checker.Equals, int64(8000), check.Commentf("resource constraints not set properly for CPUQuota"))
  41. c.Assert(c1.Ulimits[0].Name, checker.Equals, "nofile", check.Commentf("resource constraints not set properly for Ulimits"))
  42. c.Assert(c1.Ulimits[0].Hard, checker.Equals, int64(42), check.Commentf("resource constraints not set properly for Ulimits"))
  43. // Make sure constraints aren't saved to image
  44. dockerCmd(c, "run", "--name=test", name)
  45. cfg, err = inspectFieldJSON("test", "HostConfig")
  46. c.Assert(err, checker.IsNil)
  47. var c2 hostConfig
  48. err = json.Unmarshal([]byte(cfg), &c2)
  49. c.Assert(err, checker.IsNil, check.Commentf(cfg))
  50. c.Assert(c2.Memory, check.Not(checker.Equals), int64(64*1024*1024), check.Commentf("resource leaked from build for Memory"))
  51. c.Assert(c2.MemorySwap, check.Not(checker.Equals), int64(-1), check.Commentf("resource leaked from build for MemorySwap"))
  52. c.Assert(c2.CpusetCpus, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetCpus"))
  53. c.Assert(c2.CpusetMems, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetMems"))
  54. c.Assert(c2.CPUShares, check.Not(checker.Equals), int64(100), check.Commentf("resource leaked from build for CPUShares"))
  55. c.Assert(c2.CPUQuota, check.Not(checker.Equals), int64(8000), check.Commentf("resource leaked from build for CPUQuota"))
  56. c.Assert(c2.Ulimits, checker.IsNil, check.Commentf("resource leaked from build for Ulimits"))
  57. }