docker_cli_commit_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package main
  2. import (
  3. "strings"
  4. "github.com/go-check/check"
  5. )
  6. func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
  7. testRequires(c, DaemonIsLinux)
  8. out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  9. cleanedContainerID := strings.TrimSpace(out)
  10. dockerCmd(c, "wait", cleanedContainerID)
  11. out, _ = dockerCmd(c, "commit", cleanedContainerID)
  12. cleanedImageID := strings.TrimSpace(out)
  13. dockerCmd(c, "inspect", cleanedImageID)
  14. }
  15. func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
  16. testRequires(c, DaemonIsLinux)
  17. out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  18. cleanedContainerID := strings.TrimSpace(out)
  19. dockerCmd(c, "wait", cleanedContainerID)
  20. out, _ = dockerCmd(c, "commit", "-p=false", cleanedContainerID)
  21. cleanedImageID := strings.TrimSpace(out)
  22. dockerCmd(c, "inspect", cleanedImageID)
  23. }
  24. //test commit a paused container should not unpause it after commit
  25. func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
  26. testRequires(c, DaemonIsLinux)
  27. defer unpauseAllContainers()
  28. out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
  29. cleanedContainerID := strings.TrimSpace(out)
  30. dockerCmd(c, "pause", cleanedContainerID)
  31. out, _ = dockerCmd(c, "commit", cleanedContainerID)
  32. out, err := inspectField(cleanedContainerID, "State.Paused")
  33. c.Assert(err, check.IsNil)
  34. if !strings.Contains(out, "true") {
  35. c.Fatalf("commit should not unpause a paused container")
  36. }
  37. }
  38. func (s *DockerSuite) TestCommitNewFile(c *check.C) {
  39. testRequires(c, DaemonIsLinux)
  40. dockerCmd(c, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  41. imageID, _ := dockerCmd(c, "commit", "commiter")
  42. imageID = strings.Trim(imageID, "\r\n")
  43. out, _ := dockerCmd(c, "run", imageID, "cat", "/foo")
  44. if actual := strings.Trim(out, "\r\n"); actual != "koye" {
  45. c.Fatalf("expected output koye received %q", actual)
  46. }
  47. }
  48. func (s *DockerSuite) TestCommitHardlink(c *check.C) {
  49. testRequires(c, DaemonIsLinux)
  50. firstOutput, _ := dockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
  51. chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
  52. inode := chunks[0]
  53. found := false
  54. for _, chunk := range chunks[1:] {
  55. if chunk == inode {
  56. found = true
  57. break
  58. }
  59. }
  60. if !found {
  61. c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  62. }
  63. imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
  64. imageID = strings.Trim(imageID, "\r\n")
  65. secondOutput, _ := dockerCmd(c, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
  66. chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
  67. inode = chunks[0]
  68. found = false
  69. for _, chunk := range chunks[1:] {
  70. if chunk == inode {
  71. found = true
  72. break
  73. }
  74. }
  75. if !found {
  76. c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  77. }
  78. }
  79. func (s *DockerSuite) TestCommitTTY(c *check.C) {
  80. testRequires(c, DaemonIsLinux)
  81. dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  82. imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
  83. imageID = strings.Trim(imageID, "\r\n")
  84. dockerCmd(c, "run", "ttytest", "/bin/ls")
  85. }
  86. func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
  87. testRequires(c, DaemonIsLinux)
  88. dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  89. imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
  90. imageID = strings.Trim(imageID, "\r\n")
  91. dockerCmd(c, "run", "bindtest", "true")
  92. }
  93. func (s *DockerSuite) TestCommitChange(c *check.C) {
  94. testRequires(c, DaemonIsLinux)
  95. dockerCmd(c, "run", "--name", "test", "busybox", "true")
  96. imageID, _ := dockerCmd(c, "commit",
  97. "--change", "EXPOSE 8080",
  98. "--change", "ENV DEBUG true",
  99. "--change", "ENV test 1",
  100. "--change", "ENV PATH /foo",
  101. "--change", "LABEL foo bar",
  102. "--change", "CMD [\"/bin/sh\"]",
  103. "--change", "WORKDIR /opt",
  104. "--change", "ENTRYPOINT [\"/bin/sh\"]",
  105. "--change", "USER testuser",
  106. "--change", "VOLUME /var/lib/docker",
  107. "--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
  108. "test", "test-commit")
  109. imageID = strings.Trim(imageID, "\r\n")
  110. expected := map[string]string{
  111. "Config.ExposedPorts": "map[8080/tcp:{}]",
  112. "Config.Env": "[DEBUG=true test=1 PATH=/foo]",
  113. "Config.Labels": "map[foo:bar]",
  114. "Config.Cmd": "{[/bin/sh]}",
  115. "Config.WorkingDir": "/opt",
  116. "Config.Entrypoint": "{[/bin/sh]}",
  117. "Config.User": "testuser",
  118. "Config.Volumes": "map[/var/lib/docker:{}]",
  119. "Config.OnBuild": "[/usr/local/bin/python-build --dir /app/src]",
  120. }
  121. for conf, value := range expected {
  122. res, err := inspectField(imageID, conf)
  123. c.Assert(err, check.IsNil)
  124. if res != value {
  125. c.Errorf("%s('%s'), expected %s", conf, res, value)
  126. }
  127. }
  128. }
  129. // TODO: commit --run is deprecated, remove this once --run is removed
  130. func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
  131. testRequires(c, DaemonIsLinux)
  132. name := "commit-test"
  133. out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
  134. id := strings.TrimSpace(out)
  135. dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
  136. out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
  137. if strings.TrimSpace(out) != "testing" {
  138. c.Fatal("run config in committed container was not merged")
  139. }
  140. type cfg struct {
  141. Env []string
  142. Cmd []string
  143. }
  144. config1 := cfg{}
  145. if err := inspectFieldAndMarshall(id, "Config", &config1); err != nil {
  146. c.Fatal(err)
  147. }
  148. config2 := cfg{}
  149. if err := inspectFieldAndMarshall(name, "Config", &config2); err != nil {
  150. c.Fatal(err)
  151. }
  152. // Env has at least PATH loaded as well here, so let's just grab the FOO one
  153. var env1, env2 string
  154. for _, e := range config1.Env {
  155. if strings.HasPrefix(e, "FOO") {
  156. env1 = e
  157. break
  158. }
  159. }
  160. for _, e := range config2.Env {
  161. if strings.HasPrefix(e, "FOO") {
  162. env2 = e
  163. break
  164. }
  165. }
  166. if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
  167. c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
  168. }
  169. }