docker_cli_commit_test.go 6.1 KB

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