docker_cli_commit_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package main
  2. import (
  3. "strings"
  4. "github.com/docker/docker/integration-cli/checker"
  5. "github.com/docker/docker/integration-cli/cli"
  6. "github.com/go-check/check"
  7. )
  8. func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
  9. out := cli.DockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo").Combined()
  10. cleanedContainerID := strings.TrimSpace(out)
  11. cli.DockerCmd(c, "wait", cleanedContainerID)
  12. out = cli.DockerCmd(c, "commit", cleanedContainerID).Combined()
  13. cleanedImageID := strings.TrimSpace(out)
  14. cli.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. 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 = inspectField(c, cleanedContainerID, "State.Paused")
  33. // commit should not unpause a paused container
  34. c.Assert(out, checker.Contains, "true")
  35. }
  36. func (s *DockerSuite) TestCommitNewFile(c *check.C) {
  37. dockerCmd(c, "run", "--name", "committer", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  38. imageID, _ := dockerCmd(c, "commit", "committer")
  39. imageID = strings.TrimSpace(imageID)
  40. out, _ := dockerCmd(c, "run", imageID, "cat", "/foo")
  41. actual := strings.TrimSpace(out)
  42. c.Assert(actual, checker.Equals, "koye")
  43. }
  44. func (s *DockerSuite) TestCommitHardlink(c *check.C) {
  45. testRequires(c, DaemonIsLinux)
  46. firstOutput, _ := dockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
  47. chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
  48. inode := chunks[0]
  49. chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2)
  50. 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:]))
  51. imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
  52. imageID = strings.TrimSpace(imageID)
  53. secondOutput, _ := dockerCmd(c, "run", "-t", imageID, "ls", "-di", "file1", "file2")
  54. chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
  55. inode = chunks[0]
  56. chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
  57. 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:]))
  58. }
  59. func (s *DockerSuite) TestCommitTTY(c *check.C) {
  60. dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  61. imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
  62. imageID = strings.TrimSpace(imageID)
  63. dockerCmd(c, "run", imageID, "/bin/ls")
  64. }
  65. func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
  66. testRequires(c, DaemonIsLinux)
  67. dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  68. imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
  69. imageID = strings.TrimSpace(imageID)
  70. dockerCmd(c, "run", imageID, "true")
  71. }
  72. func (s *DockerSuite) TestCommitChange(c *check.C) {
  73. dockerCmd(c, "run", "--name", "test", "busybox", "true")
  74. imageID, _ := dockerCmd(c, "commit",
  75. "--change", "EXPOSE 8080",
  76. "--change", "ENV DEBUG true",
  77. "--change", "ENV test 1",
  78. "--change", "ENV PATH /foo",
  79. "--change", "LABEL foo bar",
  80. "--change", "CMD [\"/bin/sh\"]",
  81. "--change", "WORKDIR /opt",
  82. "--change", "ENTRYPOINT [\"/bin/sh\"]",
  83. "--change", "USER testuser",
  84. "--change", "VOLUME /var/lib/docker",
  85. "--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
  86. "test", "test-commit")
  87. imageID = strings.TrimSpace(imageID)
  88. // The ordering here is due to `PATH` being overridden from the container's
  89. // ENV. On windows, the container doesn't have a `PATH` ENV variable so
  90. // the ordering is the same as the cli.
  91. expectedEnv := "[PATH=/foo DEBUG=true test=1]"
  92. if testEnv.DaemonPlatform() == "windows" {
  93. expectedEnv = "[DEBUG=true test=1 PATH=/foo]"
  94. }
  95. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  96. prefix = strings.ToUpper(prefix) // Force C: as that's how WORKDIR is normalized on Windows
  97. expected := map[string]string{
  98. "Config.ExposedPorts": "map[8080/tcp:{}]",
  99. "Config.Env": expectedEnv,
  100. "Config.Labels": "map[foo:bar]",
  101. "Config.Cmd": "[/bin/sh]",
  102. "Config.WorkingDir": prefix + slash + "opt",
  103. "Config.Entrypoint": "[/bin/sh]",
  104. "Config.User": "testuser",
  105. "Config.Volumes": "map[/var/lib/docker:{}]",
  106. "Config.OnBuild": "[/usr/local/bin/python-build --dir /app/src]",
  107. }
  108. for conf, value := range expected {
  109. res := inspectField(c, imageID, conf)
  110. if res != value {
  111. c.Errorf("%s('%s'), expected %s", conf, res, value)
  112. }
  113. }
  114. }
  115. func (s *DockerSuite) TestCommitChangeLabels(c *check.C) {
  116. dockerCmd(c, "run", "--name", "test", "--label", "some=label", "busybox", "true")
  117. imageID, _ := dockerCmd(c, "commit",
  118. "--change", "LABEL some=label2",
  119. "test", "test-commit")
  120. imageID = strings.TrimSpace(imageID)
  121. c.Assert(inspectField(c, imageID, "Config.Labels"), checker.Equals, "map[some:label2]")
  122. // check that container labels didn't change
  123. c.Assert(inspectField(c, "test", "Config.Labels"), checker.Equals, "map[some:label]")
  124. }