docker_cli_build_unix_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // +build !windows
  2. package main
  3. import (
  4. "encoding/json"
  5. "strings"
  6. "github.com/go-check/check"
  7. )
  8. func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
  9. testRequires(c, CpuCfsQuota)
  10. name := "testbuildresourceconstraints"
  11. ctx, err := fakeContext(`
  12. FROM hello-world:frozen
  13. RUN ["/hello"]
  14. `, map[string]string{})
  15. if err != nil {
  16. c.Fatal(err)
  17. }
  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", "-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. }
  29. cfg, err := inspectFieldJSON(cID, "HostConfig")
  30. if err != nil {
  31. c.Fatal(err)
  32. }
  33. var c1 hostConfig
  34. if err := json.Unmarshal([]byte(cfg), &c1); err != nil {
  35. c.Fatal(err, cfg)
  36. }
  37. if c1.Memory != 67108864 || c1.MemorySwap != -1 || c1.CpusetCpus != "0" || c1.CpusetMems != "0" || c1.CpuShares != 100 || c1.CpuQuota != 8000 {
  38. c.Fatalf("resource constraints not set properly:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d, CpuQuota: %d",
  39. c1.Memory, c1.MemorySwap, c1.CpusetCpus, c1.CpusetMems, c1.CpuShares, c1.CpuQuota)
  40. }
  41. // Make sure constraints aren't saved to image
  42. dockerCmd(c, "run", "--name=test", name)
  43. cfg, err = inspectFieldJSON("test", "HostConfig")
  44. if err != nil {
  45. c.Fatal(err)
  46. }
  47. var c2 hostConfig
  48. if err := json.Unmarshal([]byte(cfg), &c2); err != nil {
  49. c.Fatal(err, cfg)
  50. }
  51. if c2.Memory == 67108864 || c2.MemorySwap == -1 || c2.CpusetCpus == "0" || c2.CpusetMems == "0" || c2.CpuShares == 100 || c2.CpuQuota == 8000 {
  52. c.Fatalf("resource constraints leaked from build:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d, CpuQuota: %d",
  53. c2.Memory, c2.MemorySwap, c2.CpusetCpus, c2.CpusetMems, c2.CpuShares, c2.CpuQuota)
  54. }
  55. }