docker_cli_commit_test.go 5.8 KB

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