docker_cli_commit_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. )
  7. func TestCommitAfterContainerIsDone(t *testing.T) {
  8. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  9. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  10. if err != nil {
  11. t.Fatalf("failed to run container: %s, %v", out, err)
  12. }
  13. cleanedContainerID := strings.TrimSpace(out)
  14. waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
  15. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  16. t.Fatalf("error thrown while waiting for container: %s, %v", out, err)
  17. }
  18. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
  19. out, _, err = runCommandWithOutput(commitCmd)
  20. if err != nil {
  21. t.Fatalf("failed to commit container to image: %s, %v", out, err)
  22. }
  23. cleanedImageID := strings.TrimSpace(out)
  24. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
  25. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  26. t.Fatalf("failed to inspect image: %s, %v", out, err)
  27. }
  28. deleteContainer(cleanedContainerID)
  29. deleteImages(cleanedImageID)
  30. logDone("commit - echo foo and commit the image")
  31. }
  32. func TestCommitWithoutPause(t *testing.T) {
  33. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  34. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  35. if err != nil {
  36. t.Fatalf("failed to run container: %s, %v", out, err)
  37. }
  38. cleanedContainerID := strings.TrimSpace(out)
  39. waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
  40. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  41. t.Fatalf("error thrown while waiting for container: %s, %v", out, err)
  42. }
  43. commitCmd := exec.Command(dockerBinary, "commit", "-p=false", cleanedContainerID)
  44. out, _, err = runCommandWithOutput(commitCmd)
  45. if err != nil {
  46. t.Fatalf("failed to commit container to image: %s, %v", out, err)
  47. }
  48. cleanedImageID := strings.TrimSpace(out)
  49. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
  50. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  51. t.Fatalf("failed to inspect image: %s, %v", out, err)
  52. }
  53. deleteContainer(cleanedContainerID)
  54. deleteImages(cleanedImageID)
  55. logDone("commit - echo foo and commit the image with --pause=false")
  56. }
  57. //test commit a paused container should not unpause it after commit
  58. func TestCommitPausedContainer(t *testing.T) {
  59. defer deleteAllContainers()
  60. defer unpauseAllContainers()
  61. cmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox")
  62. out, _, _, err := runCommandWithStdoutStderr(cmd)
  63. if err != nil {
  64. t.Fatalf("failed to run container: %v, output: %q", err, out)
  65. }
  66. cleanedContainerID := strings.TrimSpace(out)
  67. cmd = exec.Command(dockerBinary, "pause", cleanedContainerID)
  68. out, _, _, err = runCommandWithStdoutStderr(cmd)
  69. if err != nil {
  70. t.Fatalf("failed to pause container: %v, output: %q", err, out)
  71. }
  72. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
  73. out, _, err = runCommandWithOutput(commitCmd)
  74. if err != nil {
  75. t.Fatalf("failed to commit container to image: %s, %v", out, err)
  76. }
  77. cleanedImageID := strings.TrimSpace(out)
  78. defer deleteImages(cleanedImageID)
  79. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Paused}}", cleanedContainerID)
  80. out, _, _, err = runCommandWithStdoutStderr(cmd)
  81. if err != nil {
  82. t.Fatalf("failed to inspect container: %v, output: %q", err, out)
  83. }
  84. if !strings.Contains(out, "true") {
  85. t.Fatalf("commit should not unpause a paused container")
  86. }
  87. logDone("commit - commit a paused container will not unpause it")
  88. }
  89. func TestCommitNewFile(t *testing.T) {
  90. defer deleteAllContainers()
  91. cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  92. if _, err := runCommand(cmd); err != nil {
  93. t.Fatal(err)
  94. }
  95. cmd = exec.Command(dockerBinary, "commit", "commiter")
  96. imageID, _, err := runCommandWithOutput(cmd)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. imageID = strings.Trim(imageID, "\r\n")
  101. defer deleteImages(imageID)
  102. cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
  103. out, _, err := runCommandWithOutput(cmd)
  104. if err != nil {
  105. t.Fatal(err, out)
  106. }
  107. if actual := strings.Trim(out, "\r\n"); actual != "koye" {
  108. t.Fatalf("expected output koye received %q", actual)
  109. }
  110. logDone("commit - commit file and read")
  111. }
  112. func TestCommitHardlink(t *testing.T) {
  113. defer deleteAllContainers()
  114. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
  115. firstOuput, _, err := runCommandWithOutput(cmd)
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
  120. inode := chunks[0]
  121. found := false
  122. for _, chunk := range chunks[1:] {
  123. if chunk == inode {
  124. found = true
  125. break
  126. }
  127. }
  128. if !found {
  129. t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  130. }
  131. cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
  132. imageID, _, err := runCommandWithOutput(cmd)
  133. if err != nil {
  134. t.Fatal(imageID, err)
  135. }
  136. imageID = strings.Trim(imageID, "\r\n")
  137. defer deleteImages(imageID)
  138. cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
  139. secondOuput, _, err := runCommandWithOutput(cmd)
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
  144. inode = chunks[0]
  145. found = false
  146. for _, chunk := range chunks[1:] {
  147. if chunk == inode {
  148. found = true
  149. break
  150. }
  151. }
  152. if !found {
  153. t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  154. }
  155. logDone("commit - commit hardlinks")
  156. }
  157. func TestCommitTTY(t *testing.T) {
  158. defer deleteImages("ttytest")
  159. defer deleteAllContainers()
  160. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  161. if _, err := runCommand(cmd); err != nil {
  162. t.Fatal(err)
  163. }
  164. cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
  165. imageID, _, err := runCommandWithOutput(cmd)
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. imageID = strings.Trim(imageID, "\r\n")
  170. cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
  171. if _, err := runCommand(cmd); err != nil {
  172. t.Fatal(err)
  173. }
  174. logDone("commit - commit tty")
  175. }
  176. func TestCommitWithHostBindMount(t *testing.T) {
  177. defer deleteAllContainers()
  178. cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  179. if _, err := runCommand(cmd); err != nil {
  180. t.Fatal(err)
  181. }
  182. cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
  183. imageID, _, err := runCommandWithOutput(cmd)
  184. if err != nil {
  185. t.Fatal(imageID, err)
  186. }
  187. imageID = strings.Trim(imageID, "\r\n")
  188. defer deleteImages(imageID)
  189. cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
  190. if _, err := runCommand(cmd); err != nil {
  191. t.Fatal(err)
  192. }
  193. logDone("commit - commit bind mounted file")
  194. }
  195. func TestCommitChange(t *testing.T) {
  196. defer deleteAllContainers()
  197. cmd := exec.Command(dockerBinary, "run", "--name", "test", "busybox", "true")
  198. if _, err := runCommand(cmd); err != nil {
  199. t.Fatal(err)
  200. }
  201. cmd = exec.Command(dockerBinary, "commit",
  202. "--change", "EXPOSE 8080",
  203. "--change", "ENV DEBUG true",
  204. "--change", "ENV test 1",
  205. "--change", "ENV PATH /foo",
  206. "test", "test-commit")
  207. imageId, _, err := runCommandWithOutput(cmd)
  208. if err != nil {
  209. t.Fatal(imageId, err)
  210. }
  211. imageId = strings.Trim(imageId, "\r\n")
  212. defer deleteImages(imageId)
  213. expected := map[string]string{
  214. "Config.ExposedPorts": "map[8080/tcp:map[]]",
  215. "Config.Env": "[DEBUG=true test=1 PATH=/foo]",
  216. }
  217. for conf, value := range expected {
  218. res, err := inspectField(imageId, conf)
  219. if err != nil {
  220. t.Errorf("failed to get value %s, error: %s", conf, err)
  221. }
  222. if res != value {
  223. t.Errorf("%s('%s'), expected %s", conf, res, value)
  224. }
  225. }
  226. logDone("commit - commit --change")
  227. }
  228. // TODO: commit --run is deprecated, remove this once --run is removed
  229. func TestCommitMergeConfigRun(t *testing.T) {
  230. defer deleteAllContainers()
  231. name := "commit-test"
  232. out, _, _ := dockerCmd(t, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
  233. id := strings.TrimSpace(out)
  234. dockerCmd(t, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
  235. defer deleteImages("commit-test")
  236. out, _, _ = dockerCmd(t, "run", "--name", name, "commit-test")
  237. if strings.TrimSpace(out) != "testing" {
  238. t.Fatal("run config in commited container was not merged")
  239. }
  240. type cfg struct {
  241. Env []string
  242. Cmd []string
  243. }
  244. config1 := cfg{}
  245. if err := inspectFieldAndMarshall(id, "Config", &config1); err != nil {
  246. t.Fatal(err)
  247. }
  248. config2 := cfg{}
  249. if err := inspectFieldAndMarshall(name, "Config", &config2); err != nil {
  250. t.Fatal(err)
  251. }
  252. // Env has at least PATH loaded as well here, so let's just grab the FOO one
  253. var env1, env2 string
  254. for _, e := range config1.Env {
  255. if strings.HasPrefix(e, "FOO") {
  256. env1 = e
  257. break
  258. }
  259. }
  260. for _, e := range config2.Env {
  261. if strings.HasPrefix(e, "FOO") {
  262. env2 = e
  263. break
  264. }
  265. }
  266. if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
  267. t.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
  268. }
  269. logDone("commit - configs are merged with --run")
  270. }