docker_cli_build_unix_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. _, _, err = 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. if err != nil {
  20. c.Fatal(err)
  21. }
  22. out, _ := dockerCmd(c, "ps", "-lq")
  23. cID := strings.TrimSpace(out)
  24. type hostConfig struct {
  25. Memory int64
  26. MemorySwap int64
  27. CpusetCpus string
  28. CpusetMems string
  29. CPUShares int64
  30. CPUQuota int64
  31. Ulimits []*units.Ulimit
  32. }
  33. cfg, err := inspectFieldJSON(cID, "HostConfig")
  34. c.Assert(err, checker.IsNil)
  35. var c1 hostConfig
  36. err = json.Unmarshal([]byte(cfg), &c1)
  37. c.Assert(err, checker.IsNil, check.Commentf(cfg))
  38. c.Assert(c1.Memory, checker.Equals, int64(64*1024*1024), check.Commentf("resource constraints not set properly for Memory"))
  39. c.Assert(c1.MemorySwap, checker.Equals, int64(-1), check.Commentf("resource constraints not set properly for MemorySwap"))
  40. c.Assert(c1.CpusetCpus, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetCpus"))
  41. c.Assert(c1.CpusetMems, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetMems"))
  42. c.Assert(c1.CPUShares, checker.Equals, int64(100), check.Commentf("resource constraints not set properly for CPUShares"))
  43. c.Assert(c1.CPUQuota, checker.Equals, int64(8000), check.Commentf("resource constraints not set properly for CPUQuota"))
  44. c.Assert(c1.Ulimits[0].Name, checker.Equals, "nofile", check.Commentf("resource constraints not set properly for Ulimits"))
  45. c.Assert(c1.Ulimits[0].Hard, checker.Equals, int64(42), check.Commentf("resource constraints not set properly for Ulimits"))
  46. // Make sure constraints aren't saved to image
  47. dockerCmd(c, "run", "--name=test", name)
  48. cfg, err = inspectFieldJSON("test", "HostConfig")
  49. c.Assert(err, checker.IsNil)
  50. var c2 hostConfig
  51. err = json.Unmarshal([]byte(cfg), &c2)
  52. c.Assert(err, checker.IsNil, check.Commentf(cfg))
  53. c.Assert(c2.Memory, check.Not(checker.Equals), int64(64*1024*1024), check.Commentf("resource leaked from build for Memory"))
  54. c.Assert(c2.MemorySwap, check.Not(checker.Equals), int64(-1), check.Commentf("resource leaked from build for MemorySwap"))
  55. c.Assert(c2.CpusetCpus, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetCpus"))
  56. c.Assert(c2.CpusetMems, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetMems"))
  57. c.Assert(c2.CPUShares, check.Not(checker.Equals), int64(100), check.Commentf("resource leaked from build for CPUShares"))
  58. c.Assert(c2.CPUQuota, check.Not(checker.Equals), int64(8000), check.Commentf("resource leaked from build for CPUQuota"))
  59. c.Assert(c2.Ulimits, checker.IsNil, check.Commentf("resource leaked from build for Ulimits"))
  60. }