docker_cli_build_unix_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // +build !windows
  2. package main
  3. import (
  4. "encoding/json"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "github.com/docker/docker/pkg/integration/checker"
  11. "github.com/docker/go-units"
  12. "github.com/go-check/check"
  13. )
  14. func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
  15. testRequires(c, cpuCfsQuota)
  16. name := "testbuildresourceconstraints"
  17. ctx, err := fakeContext(`
  18. FROM hello-world:frozen
  19. RUN ["/hello"]
  20. `, map[string]string{})
  21. c.Assert(err, checker.IsNil)
  22. _, _, 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, ".")
  23. if err != nil {
  24. c.Fatal(err)
  25. }
  26. out, _ := dockerCmd(c, "ps", "-lq")
  27. cID := strings.TrimSpace(out)
  28. type hostConfig struct {
  29. Memory int64
  30. MemorySwap int64
  31. CpusetCpus string
  32. CpusetMems string
  33. CPUShares int64
  34. CPUQuota int64
  35. Ulimits []*units.Ulimit
  36. }
  37. cfg, err := inspectFieldJSON(cID, "HostConfig")
  38. c.Assert(err, checker.IsNil)
  39. var c1 hostConfig
  40. err = json.Unmarshal([]byte(cfg), &c1)
  41. c.Assert(err, checker.IsNil, check.Commentf(cfg))
  42. c.Assert(c1.Memory, checker.Equals, int64(64*1024*1024), check.Commentf("resource constraints not set properly for Memory"))
  43. c.Assert(c1.MemorySwap, checker.Equals, int64(-1), check.Commentf("resource constraints not set properly for MemorySwap"))
  44. c.Assert(c1.CpusetCpus, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetCpus"))
  45. c.Assert(c1.CpusetMems, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetMems"))
  46. c.Assert(c1.CPUShares, checker.Equals, int64(100), check.Commentf("resource constraints not set properly for CPUShares"))
  47. c.Assert(c1.CPUQuota, checker.Equals, int64(8000), check.Commentf("resource constraints not set properly for CPUQuota"))
  48. c.Assert(c1.Ulimits[0].Name, checker.Equals, "nofile", check.Commentf("resource constraints not set properly for Ulimits"))
  49. c.Assert(c1.Ulimits[0].Hard, checker.Equals, int64(42), check.Commentf("resource constraints not set properly for Ulimits"))
  50. // Make sure constraints aren't saved to image
  51. dockerCmd(c, "run", "--name=test", name)
  52. cfg, err = inspectFieldJSON("test", "HostConfig")
  53. c.Assert(err, checker.IsNil)
  54. var c2 hostConfig
  55. err = json.Unmarshal([]byte(cfg), &c2)
  56. c.Assert(err, checker.IsNil, check.Commentf(cfg))
  57. c.Assert(c2.Memory, check.Not(checker.Equals), int64(64*1024*1024), check.Commentf("resource leaked from build for Memory"))
  58. c.Assert(c2.MemorySwap, check.Not(checker.Equals), int64(-1), check.Commentf("resource leaked from build for MemorySwap"))
  59. c.Assert(c2.CpusetCpus, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetCpus"))
  60. c.Assert(c2.CpusetMems, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetMems"))
  61. c.Assert(c2.CPUShares, check.Not(checker.Equals), int64(100), check.Commentf("resource leaked from build for CPUShares"))
  62. c.Assert(c2.CPUQuota, check.Not(checker.Equals), int64(8000), check.Commentf("resource leaked from build for CPUQuota"))
  63. c.Assert(c2.Ulimits, checker.IsNil, check.Commentf("resource leaked from build for Ulimits"))
  64. }
  65. func (s *DockerSuite) TestBuildAddChangeOwnership(c *check.C) {
  66. testRequires(c, DaemonIsLinux)
  67. name := "testbuildaddown"
  68. ctx := func() *FakeContext {
  69. dockerfile := `
  70. FROM busybox
  71. ADD foo /bar/
  72. RUN [ $(stat -c %U:%G "/bar") = 'root:root' ]
  73. RUN [ $(stat -c %U:%G "/bar/foo") = 'root:root' ]
  74. `
  75. tmpDir, err := ioutil.TempDir("", "fake-context")
  76. c.Assert(err, check.IsNil)
  77. testFile, err := os.Create(filepath.Join(tmpDir, "foo"))
  78. if err != nil {
  79. c.Fatalf("failed to create foo file: %v", err)
  80. }
  81. defer testFile.Close()
  82. chownCmd := exec.Command("chown", "daemon:daemon", "foo")
  83. chownCmd.Dir = tmpDir
  84. out, _, err := runCommandWithOutput(chownCmd)
  85. if err != nil {
  86. c.Fatal(err, out)
  87. }
  88. if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
  89. c.Fatalf("failed to open destination dockerfile: %v", err)
  90. }
  91. return fakeContextFromDir(tmpDir)
  92. }()
  93. defer ctx.Close()
  94. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  95. c.Fatalf("build failed to complete for TestBuildAddChangeOwnership: %v", err)
  96. }
  97. }