docker_cli_commit_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 = inspectField(c, cleanedContainerID, "State.Paused")
  34. // commit should not unpause a paused container
  35. c.Assert(out, checker.Contains, "true")
  36. }
  37. func (s *DockerSuite) TestCommitNewFile(c *check.C) {
  38. testRequires(c, DaemonIsLinux)
  39. dockerCmd(c, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  40. imageID, _ := dockerCmd(c, "commit", "commiter")
  41. imageID = strings.TrimSpace(imageID)
  42. out, _ := dockerCmd(c, "run", imageID, "cat", "/foo")
  43. actual := strings.TrimSpace(out)
  44. c.Assert(actual, checker.Equals, "koye")
  45. }
  46. func (s *DockerSuite) TestCommitHardlink(c *check.C) {
  47. testRequires(c, DaemonIsLinux)
  48. firstOutput, _ := dockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
  49. chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
  50. inode := chunks[0]
  51. chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2)
  52. 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:]))
  53. imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
  54. imageID = strings.TrimSpace(imageID)
  55. secondOutput, _ := dockerCmd(c, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
  56. chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
  57. inode = chunks[0]
  58. chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
  59. 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:]))
  60. }
  61. func (s *DockerSuite) TestCommitTTY(c *check.C) {
  62. testRequires(c, DaemonIsLinux)
  63. dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  64. imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
  65. imageID = strings.TrimSpace(imageID)
  66. dockerCmd(c, "run", "ttytest", "/bin/ls")
  67. }
  68. func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
  69. testRequires(c, DaemonIsLinux)
  70. dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  71. imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
  72. imageID = strings.TrimSpace(imageID)
  73. dockerCmd(c, "run", "bindtest", "true")
  74. }
  75. func (s *DockerSuite) TestCommitChange(c *check.C) {
  76. testRequires(c, DaemonIsLinux)
  77. dockerCmd(c, "run", "--name", "test", "busybox", "true")
  78. imageID, _ := dockerCmd(c, "commit",
  79. "--change", "EXPOSE 8080",
  80. "--change", "ENV DEBUG true",
  81. "--change", "ENV test 1",
  82. "--change", "ENV PATH /foo",
  83. "--change", "LABEL foo bar",
  84. "--change", "CMD [\"/bin/sh\"]",
  85. "--change", "WORKDIR /opt",
  86. "--change", "ENTRYPOINT [\"/bin/sh\"]",
  87. "--change", "USER testuser",
  88. "--change", "VOLUME /var/lib/docker",
  89. "--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
  90. "test", "test-commit")
  91. imageID = strings.TrimSpace(imageID)
  92. expected := map[string]string{
  93. "Config.ExposedPorts": "map[8080/tcp:{}]",
  94. "Config.Env": "[DEBUG=true test=1 PATH=/foo]",
  95. "Config.Labels": "map[foo:bar]",
  96. "Config.Cmd": "[/bin/sh]",
  97. "Config.WorkingDir": "/opt",
  98. "Config.Entrypoint": "[/bin/sh]",
  99. "Config.User": "testuser",
  100. "Config.Volumes": "map[/var/lib/docker:{}]",
  101. "Config.OnBuild": "[/usr/local/bin/python-build --dir /app/src]",
  102. }
  103. for conf, value := range expected {
  104. res := inspectField(c, imageID, conf)
  105. if res != value {
  106. c.Errorf("%s('%s'), expected %s", conf, res, value)
  107. }
  108. }
  109. }
  110. // TODO: commit --run is deprecated, remove this once --run is removed
  111. func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
  112. testRequires(c, DaemonIsLinux)
  113. name := "commit-test"
  114. out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
  115. id := strings.TrimSpace(out)
  116. dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
  117. out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
  118. //run config in committed container was not merged
  119. c.Assert(strings.TrimSpace(out), checker.Equals, "testing")
  120. type cfg struct {
  121. Env []string
  122. Cmd []string
  123. }
  124. config1 := cfg{}
  125. inspectFieldAndMarshall(c, id, "Config", &config1)
  126. config2 := cfg{}
  127. inspectFieldAndMarshall(c, name, "Config", &config2)
  128. // Env has at least PATH loaded as well here, so let's just grab the FOO one
  129. var env1, env2 string
  130. for _, e := range config1.Env {
  131. if strings.HasPrefix(e, "FOO") {
  132. env1 = e
  133. break
  134. }
  135. }
  136. for _, e := range config2.Env {
  137. if strings.HasPrefix(e, "FOO") {
  138. env2 = e
  139. break
  140. }
  141. }
  142. if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
  143. c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
  144. }
  145. }