docker_cli_build_unix_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // +build !windows
  2. package main
  3. import (
  4. "bufio"
  5. "bytes"
  6. "encoding/json"
  7. "io/ioutil"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "regexp"
  12. "strings"
  13. "time"
  14. "github.com/docker/docker/pkg/integration/checker"
  15. "github.com/docker/go-units"
  16. "github.com/go-check/check"
  17. )
  18. func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
  19. testRequires(c, cpuCfsQuota)
  20. name := "testbuildresourceconstraints"
  21. ctx, err := fakeContext(`
  22. FROM hello-world:frozen
  23. RUN ["/hello"]
  24. `, map[string]string{})
  25. c.Assert(err, checker.IsNil)
  26. _, _, 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, ".")
  27. if err != nil {
  28. c.Fatal(err)
  29. }
  30. out, _ := dockerCmd(c, "ps", "-lq")
  31. cID := strings.TrimSpace(out)
  32. type hostConfig struct {
  33. Memory int64
  34. MemorySwap int64
  35. CpusetCpus string
  36. CpusetMems string
  37. CPUShares int64
  38. CPUQuota int64
  39. Ulimits []*units.Ulimit
  40. }
  41. cfg := inspectFieldJSON(c, cID, "HostConfig")
  42. var c1 hostConfig
  43. err = json.Unmarshal([]byte(cfg), &c1)
  44. c.Assert(err, checker.IsNil, check.Commentf(cfg))
  45. c.Assert(c1.Memory, checker.Equals, int64(64*1024*1024), check.Commentf("resource constraints not set properly for Memory"))
  46. c.Assert(c1.MemorySwap, checker.Equals, int64(-1), check.Commentf("resource constraints not set properly for MemorySwap"))
  47. c.Assert(c1.CpusetCpus, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetCpus"))
  48. c.Assert(c1.CpusetMems, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetMems"))
  49. c.Assert(c1.CPUShares, checker.Equals, int64(100), check.Commentf("resource constraints not set properly for CPUShares"))
  50. c.Assert(c1.CPUQuota, checker.Equals, int64(8000), check.Commentf("resource constraints not set properly for CPUQuota"))
  51. c.Assert(c1.Ulimits[0].Name, checker.Equals, "nofile", check.Commentf("resource constraints not set properly for Ulimits"))
  52. c.Assert(c1.Ulimits[0].Hard, checker.Equals, int64(42), check.Commentf("resource constraints not set properly for Ulimits"))
  53. // Make sure constraints aren't saved to image
  54. dockerCmd(c, "run", "--name=test", name)
  55. cfg = inspectFieldJSON(c, "test", "HostConfig")
  56. var c2 hostConfig
  57. err = json.Unmarshal([]byte(cfg), &c2)
  58. c.Assert(err, checker.IsNil, check.Commentf(cfg))
  59. c.Assert(c2.Memory, check.Not(checker.Equals), int64(64*1024*1024), check.Commentf("resource leaked from build for Memory"))
  60. c.Assert(c2.MemorySwap, check.Not(checker.Equals), int64(-1), check.Commentf("resource leaked from build for MemorySwap"))
  61. c.Assert(c2.CpusetCpus, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetCpus"))
  62. c.Assert(c2.CpusetMems, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetMems"))
  63. c.Assert(c2.CPUShares, check.Not(checker.Equals), int64(100), check.Commentf("resource leaked from build for CPUShares"))
  64. c.Assert(c2.CPUQuota, check.Not(checker.Equals), int64(8000), check.Commentf("resource leaked from build for CPUQuota"))
  65. c.Assert(c2.Ulimits, checker.IsNil, check.Commentf("resource leaked from build for Ulimits"))
  66. }
  67. func (s *DockerSuite) TestBuildAddChangeOwnership(c *check.C) {
  68. testRequires(c, DaemonIsLinux)
  69. name := "testbuildaddown"
  70. ctx := func() *FakeContext {
  71. dockerfile := `
  72. FROM busybox
  73. ADD foo /bar/
  74. RUN [ $(stat -c %U:%G "/bar") = 'root:root' ]
  75. RUN [ $(stat -c %U:%G "/bar/foo") = 'root:root' ]
  76. `
  77. tmpDir, err := ioutil.TempDir("", "fake-context")
  78. c.Assert(err, check.IsNil)
  79. testFile, err := os.Create(filepath.Join(tmpDir, "foo"))
  80. if err != nil {
  81. c.Fatalf("failed to create foo file: %v", err)
  82. }
  83. defer testFile.Close()
  84. chownCmd := exec.Command("chown", "daemon:daemon", "foo")
  85. chownCmd.Dir = tmpDir
  86. out, _, err := runCommandWithOutput(chownCmd)
  87. if err != nil {
  88. c.Fatal(err, out)
  89. }
  90. if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
  91. c.Fatalf("failed to open destination dockerfile: %v", err)
  92. }
  93. return fakeContextFromDir(tmpDir)
  94. }()
  95. defer ctx.Close()
  96. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  97. c.Fatalf("build failed to complete for TestBuildAddChangeOwnership: %v", err)
  98. }
  99. }
  100. // Test that an infinite sleep during a build is killed if the client disconnects.
  101. // This test is fairly hairy because there are lots of ways to race.
  102. // Strategy:
  103. // * Monitor the output of docker events starting from before
  104. // * Run a 1-year-long sleep from a docker build.
  105. // * When docker events sees container start, close the "docker build" command
  106. // * Wait for docker events to emit a dying event.
  107. func (s *DockerSuite) TestBuildCancellationKillsSleep(c *check.C) {
  108. testRequires(c, DaemonIsLinux)
  109. name := "testbuildcancellation"
  110. observer, err := newEventObserver(c)
  111. c.Assert(err, checker.IsNil)
  112. err = observer.Start()
  113. c.Assert(err, checker.IsNil)
  114. defer observer.Stop()
  115. // (Note: one year, will never finish)
  116. ctx, err := fakeContext("FROM busybox\nRUN sleep 31536000", nil)
  117. if err != nil {
  118. c.Fatal(err)
  119. }
  120. defer ctx.Close()
  121. buildCmd := exec.Command(dockerBinary, "build", "-t", name, ".")
  122. buildCmd.Dir = ctx.Dir
  123. stdoutBuild, err := buildCmd.StdoutPipe()
  124. if err := buildCmd.Start(); err != nil {
  125. c.Fatalf("failed to run build: %s", err)
  126. }
  127. matchCID := regexp.MustCompile("Running in (.+)")
  128. scanner := bufio.NewScanner(stdoutBuild)
  129. outputBuffer := new(bytes.Buffer)
  130. var buildID string
  131. for scanner.Scan() {
  132. line := scanner.Text()
  133. outputBuffer.WriteString(line)
  134. outputBuffer.WriteString("\n")
  135. if matches := matchCID.FindStringSubmatch(line); len(matches) > 0 {
  136. buildID = matches[1]
  137. break
  138. }
  139. }
  140. if buildID == "" {
  141. c.Fatalf("Unable to find build container id in build output:\n%s", outputBuffer.String())
  142. }
  143. testActions := map[string]chan bool{
  144. "start": make(chan bool, 1),
  145. "die": make(chan bool, 1),
  146. }
  147. matcher := matchEventLine(buildID, "container", testActions)
  148. processor := processEventMatch(testActions)
  149. go observer.Match(matcher, processor)
  150. select {
  151. case <-time.After(10 * time.Second):
  152. observer.CheckEventError(c, buildID, "start", matcher)
  153. case <-testActions["start"]:
  154. // ignore, done
  155. }
  156. // Send a kill to the `docker build` command.
  157. // Causes the underlying build to be cancelled due to socket close.
  158. if err := buildCmd.Process.Kill(); err != nil {
  159. c.Fatalf("error killing build command: %s", err)
  160. }
  161. // Get the exit status of `docker build`, check it exited because killed.
  162. if err := buildCmd.Wait(); err != nil && !isKilled(err) {
  163. c.Fatalf("wait failed during build run: %T %s", err, err)
  164. }
  165. select {
  166. case <-time.After(10 * time.Second):
  167. observer.CheckEventError(c, buildID, "die", matcher)
  168. case <-testActions["die"]:
  169. // ignore, done
  170. }
  171. }