docker_cli_commit_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package main
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/docker/docker/api/types/versions"
  6. "github.com/docker/docker/integration-cli/cli"
  7. "gotest.tools/assert"
  8. )
  9. func (s *DockerSuite) TestCommitAfterContainerIsDone(c *testing.T) {
  10. out := cli.DockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo").Combined()
  11. cleanedContainerID := strings.TrimSpace(out)
  12. cli.DockerCmd(c, "wait", cleanedContainerID)
  13. out = cli.DockerCmd(c, "commit", cleanedContainerID).Combined()
  14. cleanedImageID := strings.TrimSpace(out)
  15. cli.DockerCmd(c, "inspect", cleanedImageID)
  16. }
  17. func (s *DockerSuite) TestCommitWithoutPause(c *testing.T) {
  18. testRequires(c, DaemonIsLinux)
  19. out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  20. cleanedContainerID := strings.TrimSpace(out)
  21. dockerCmd(c, "wait", cleanedContainerID)
  22. out, _ = dockerCmd(c, "commit", "-p=false", cleanedContainerID)
  23. cleanedImageID := strings.TrimSpace(out)
  24. dockerCmd(c, "inspect", cleanedImageID)
  25. }
  26. // TestCommitPausedContainer tests that a paused container is not unpaused after being committed
  27. func (s *DockerSuite) TestCommitPausedContainer(c *testing.T) {
  28. testRequires(c, DaemonIsLinux)
  29. out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
  30. cleanedContainerID := strings.TrimSpace(out)
  31. dockerCmd(c, "pause", cleanedContainerID)
  32. dockerCmd(c, "commit", cleanedContainerID)
  33. out = inspectField(c, cleanedContainerID, "State.Paused")
  34. // commit should not unpause a paused container
  35. assert.Assert(c, strings.Contains(out, "true"))
  36. }
  37. func (s *DockerSuite) TestCommitNewFile(c *testing.T) {
  38. dockerCmd(c, "run", "--name", "committer", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  39. imageID, _ := dockerCmd(c, "commit", "committer")
  40. imageID = strings.TrimSpace(imageID)
  41. out, _ := dockerCmd(c, "run", imageID, "cat", "/foo")
  42. actual := strings.TrimSpace(out)
  43. assert.Equal(c, actual, "koye")
  44. }
  45. func (s *DockerSuite) TestCommitHardlink(c *testing.T) {
  46. testRequires(c, DaemonIsLinux)
  47. firstOutput, _ := dockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
  48. chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
  49. inode := chunks[0]
  50. chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2)
  51. assert.Assert(c, strings.Contains(chunks[1], chunks[0]), "Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  52. imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
  53. imageID = strings.TrimSpace(imageID)
  54. secondOutput, _ := dockerCmd(c, "run", "-t", imageID, "ls", "-di", "file1", "file2")
  55. chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
  56. inode = chunks[0]
  57. chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
  58. assert.Assert(c, strings.Contains(chunks[1], chunks[0]), "Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  59. }
  60. func (s *DockerSuite) TestCommitTTY(c *testing.T) {
  61. dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  62. imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
  63. imageID = strings.TrimSpace(imageID)
  64. dockerCmd(c, "run", imageID, "/bin/ls")
  65. }
  66. func (s *DockerSuite) TestCommitWithHostBindMount(c *testing.T) {
  67. testRequires(c, DaemonIsLinux)
  68. dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  69. imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
  70. imageID = strings.TrimSpace(imageID)
  71. dockerCmd(c, "run", imageID, "true")
  72. }
  73. func (s *DockerSuite) TestCommitChange(c *testing.T) {
  74. dockerCmd(c, "run", "--name", "test", "busybox", "true")
  75. imageID, _ := dockerCmd(c, "commit",
  76. "--change", "EXPOSE 8080",
  77. "--change", "ENV DEBUG true",
  78. "--change", "ENV test 1",
  79. "--change", "ENV PATH /foo",
  80. "--change", "LABEL foo bar",
  81. "--change", "CMD [\"/bin/sh\"]",
  82. "--change", "WORKDIR /opt",
  83. "--change", "ENTRYPOINT [\"/bin/sh\"]",
  84. "--change", "USER testuser",
  85. "--change", "VOLUME /var/lib/docker",
  86. "--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
  87. "test", "test-commit")
  88. imageID = strings.TrimSpace(imageID)
  89. expectedEnv := "[DEBUG=true test=1 PATH=/foo]"
  90. // bug fixed in 1.36, add min APi >= 1.36 requirement
  91. // PR record https://github.com/moby/moby/pull/35582
  92. if versions.GreaterThan(testEnv.DaemonAPIVersion(), "1.35") && testEnv.OSType != "windows" {
  93. // The ordering here is due to `PATH` being overridden from the container's
  94. // ENV. On windows, the container doesn't have a `PATH` ENV variable so
  95. // the ordering is the same as the cli.
  96. expectedEnv = "[PATH=/foo DEBUG=true test=1]"
  97. }
  98. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  99. prefix = strings.ToUpper(prefix) // Force C: as that's how WORKDIR is normalized on Windows
  100. expected := map[string]string{
  101. "Config.ExposedPorts": "map[8080/tcp:{}]",
  102. "Config.Env": expectedEnv,
  103. "Config.Labels": "map[foo:bar]",
  104. "Config.Cmd": "[/bin/sh]",
  105. "Config.WorkingDir": prefix + slash + "opt",
  106. "Config.Entrypoint": "[/bin/sh]",
  107. "Config.User": "testuser",
  108. "Config.Volumes": "map[/var/lib/docker:{}]",
  109. "Config.OnBuild": "[/usr/local/bin/python-build --dir /app/src]",
  110. }
  111. for conf, value := range expected {
  112. res := inspectField(c, imageID, conf)
  113. if res != value {
  114. c.Errorf("%s('%s'), expected %s", conf, res, value)
  115. }
  116. }
  117. }
  118. func (s *DockerSuite) TestCommitChangeLabels(c *testing.T) {
  119. dockerCmd(c, "run", "--name", "test", "--label", "some=label", "busybox", "true")
  120. imageID, _ := dockerCmd(c, "commit",
  121. "--change", "LABEL some=label2",
  122. "test", "test-commit")
  123. imageID = strings.TrimSpace(imageID)
  124. assert.Equal(c, inspectField(c, imageID, "Config.Labels"), "map[some:label2]")
  125. // check that container labels didn't change
  126. assert.Equal(c, inspectField(c, "test", "Config.Labels"), "map[some:label]")
  127. }