docker_cli_cp_from_container_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "gotest.tools/v3/assert"
  7. )
  8. // Try all of the test cases from the archive package which implements the
  9. // internals of `docker cp` and ensure that the behavior matches when actually
  10. // copying to and from containers.
  11. // Basic assumptions about SRC and DST:
  12. // 1. SRC must exist.
  13. // 2. If SRC ends with a trailing separator, it must be a directory.
  14. // 3. DST parent directory must exist.
  15. // 4. If DST exists as a file, it must not end with a trailing separator.
  16. // Check that copying from a container to a local symlink copies to the symlink
  17. // target and does not overwrite the local symlink itself.
  18. // TODO: move to docker/cli and/or integration/container/copy_test.go
  19. func (s *DockerCLICpSuite) TestCpFromSymlinkDestination(c *testing.T) {
  20. testRequires(c, DaemonIsLinux)
  21. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  22. tmpDir := getTestDir(c, "test-cp-from-err-dst-not-dir")
  23. defer os.RemoveAll(tmpDir)
  24. makeTestContentInDir(c, tmpDir)
  25. // First, copy a file from the container to a symlink to a file. This
  26. // should overwrite the symlink target contents with the source contents.
  27. srcPath := containerCpPath(containerID, "/file2")
  28. dstPath := cpPath(tmpDir, "symlinkToFile1")
  29. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  30. assert.NilError(c, symlinkTargetEquals(c, dstPath, "file1"), "The symlink should not have been modified")
  31. assert.NilError(c, fileContentEquals(c, cpPath(tmpDir, "file1"), "file2\n"), `The file should have the contents of "file2" now`)
  32. // Next, copy a file from the container to a symlink to a directory. This
  33. // should copy the file into the symlink target directory.
  34. dstPath = cpPath(tmpDir, "symlinkToDir1")
  35. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  36. assert.NilError(c, symlinkTargetEquals(c, dstPath, "dir1"), "The symlink should not have been modified")
  37. assert.NilError(c, fileContentEquals(c, cpPath(tmpDir, "file2"), "file2\n"), `The file should have the contents of "file2" now`)
  38. // Next, copy a file from the container to a symlink to a file that does
  39. // not exist (a broken symlink). This should create the target file with
  40. // the contents of the source file.
  41. dstPath = cpPath(tmpDir, "brokenSymlinkToFileX")
  42. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  43. assert.NilError(c, symlinkTargetEquals(c, dstPath, "fileX"), "The symlink should not have been modified")
  44. assert.NilError(c, fileContentEquals(c, cpPath(tmpDir, "fileX"), "file2\n"), `The file should have the contents of "file2" now`)
  45. // Next, copy a directory from the container to a symlink to a local
  46. // directory. This should copy the directory into the symlink target
  47. // directory and not modify the symlink.
  48. srcPath = containerCpPath(containerID, "/dir2")
  49. dstPath = cpPath(tmpDir, "symlinkToDir1")
  50. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  51. assert.NilError(c, symlinkTargetEquals(c, dstPath, "dir1"), "The symlink should not have been modified")
  52. assert.NilError(c, fileContentEquals(c, cpPath(tmpDir, "dir1/dir2/file2-1"), "file2-1\n"), `The directory should now contain a copy of "dir2"`)
  53. // Next, copy a directory from the container to a symlink to a local
  54. // directory that does not exist (a broken symlink). This should create
  55. // the target as a directory with the contents of the source directory. It
  56. // should not modify the symlink.
  57. dstPath = cpPath(tmpDir, "brokenSymlinkToDirX")
  58. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  59. assert.NilError(c, symlinkTargetEquals(c, dstPath, "dirX"), "The symlink should not have been modified")
  60. assert.NilError(c, fileContentEquals(c, cpPath(tmpDir, "dirX/file2-1"), "file2-1\n"), `The "dirX" directory should now be a copy of "dir2"`)
  61. }
  62. // Possibilities are reduced to the remaining 10 cases:
  63. //
  64. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  65. // ===================================================================================================
  66. // A | no | - | no | - | no | create file
  67. // B | no | - | no | - | yes | error
  68. // C | no | - | yes | no | - | overwrite file
  69. // D | no | - | yes | yes | - | create file in dst dir
  70. // E | yes | no | no | - | - | create dir, copy contents
  71. // F | yes | no | yes | no | - | error
  72. // G | yes | no | yes | yes | - | copy dir and contents
  73. // H | yes | yes | no | - | - | create dir, copy contents
  74. // I | yes | yes | yes | no | - | error
  75. // J | yes | yes | yes | yes | - | copy dir contents
  76. //
  77. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  78. // exist. This should create a file with the name DST and copy the
  79. // contents of the source file into it.
  80. func (s *DockerCLICpSuite) TestCpFromCaseA(c *testing.T) {
  81. testRequires(c, DaemonIsLinux)
  82. containerID := makeTestContainer(c, testContainerOptions{
  83. addContent: true, workDir: "/root",
  84. })
  85. tmpDir := getTestDir(c, "test-cp-from-case-a")
  86. defer os.RemoveAll(tmpDir)
  87. srcPath := containerCpPath(containerID, "/root/file1")
  88. dstPath := cpPath(tmpDir, "itWorks.txt")
  89. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  90. assert.NilError(c, fileContentEquals(c, dstPath, "file1\n"))
  91. }
  92. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  93. // exist. This should cause an error because the copy operation cannot
  94. // create a directory when copying a single file.
  95. func (s *DockerCLICpSuite) TestCpFromCaseB(c *testing.T) {
  96. testRequires(c, DaemonIsLinux)
  97. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  98. tmpDir := getTestDir(c, "test-cp-from-case-b")
  99. defer os.RemoveAll(tmpDir)
  100. srcPath := containerCpPath(containerID, "/file1")
  101. dstDir := cpPathTrailingSep(tmpDir, "testDir")
  102. err := runDockerCp(c, srcPath, dstDir)
  103. assert.ErrorContains(c, err, "")
  104. assert.Assert(c, isCpDirNotExist(err), "expected DirNotExists error, but got %T: %s", err, err)
  105. }
  106. // C. SRC specifies a file and DST exists as a file. This should overwrite
  107. // the file at DST with the contents of the source file.
  108. func (s *DockerCLICpSuite) TestCpFromCaseC(c *testing.T) {
  109. testRequires(c, DaemonIsLinux)
  110. containerID := makeTestContainer(c, testContainerOptions{
  111. addContent: true, workDir: "/root",
  112. })
  113. tmpDir := getTestDir(c, "test-cp-from-case-c")
  114. defer os.RemoveAll(tmpDir)
  115. makeTestContentInDir(c, tmpDir)
  116. srcPath := containerCpPath(containerID, "/root/file1")
  117. dstPath := cpPath(tmpDir, "file2")
  118. // Ensure the local file starts with different content.
  119. assert.NilError(c, fileContentEquals(c, dstPath, "file2\n"))
  120. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  121. assert.NilError(c, fileContentEquals(c, dstPath, "file1\n"))
  122. }
  123. // D. SRC specifies a file and DST exists as a directory. This should place
  124. // a copy of the source file inside it using the basename from SRC. Ensure
  125. // this works whether DST has a trailing path separator or not.
  126. func (s *DockerCLICpSuite) TestCpFromCaseD(c *testing.T) {
  127. testRequires(c, DaemonIsLinux)
  128. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  129. tmpDir := getTestDir(c, "test-cp-from-case-d")
  130. defer os.RemoveAll(tmpDir)
  131. makeTestContentInDir(c, tmpDir)
  132. srcPath := containerCpPath(containerID, "/file1")
  133. dstDir := cpPath(tmpDir, "dir1")
  134. dstPath := filepath.Join(dstDir, "file1")
  135. // Ensure that dstPath doesn't exist.
  136. _, err := os.Stat(dstPath)
  137. assert.Assert(c, os.IsNotExist(err), "did not expect dstPath %q to exist", dstPath)
  138. assert.NilError(c, runDockerCp(c, srcPath, dstDir))
  139. assert.NilError(c, fileContentEquals(c, dstPath, "file1\n"))
  140. // Now try again but using a trailing path separator for dstDir.
  141. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove dstDir")
  142. assert.NilError(c, os.MkdirAll(dstDir, os.FileMode(0755)), "unable to make dstDir")
  143. dstDir = cpPathTrailingSep(tmpDir, "dir1")
  144. assert.NilError(c, runDockerCp(c, srcPath, dstDir))
  145. assert.NilError(c, fileContentEquals(c, dstPath, "file1\n"))
  146. }
  147. // E. SRC specifies a directory and DST does not exist. This should create a
  148. // directory at DST and copy the contents of the SRC directory into the DST
  149. // directory. Ensure this works whether DST has a trailing path separator or
  150. // not.
  151. func (s *DockerCLICpSuite) TestCpFromCaseE(c *testing.T) {
  152. testRequires(c, DaemonIsLinux)
  153. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  154. tmpDir := getTestDir(c, "test-cp-from-case-e")
  155. defer os.RemoveAll(tmpDir)
  156. srcDir := containerCpPath(containerID, "dir1")
  157. dstDir := cpPath(tmpDir, "testDir")
  158. dstPath := filepath.Join(dstDir, "file1-1")
  159. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  160. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  161. // Now try again but using a trailing path separator for dstDir.
  162. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove dstDir")
  163. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  164. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  165. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  166. }
  167. // F. SRC specifies a directory and DST exists as a file. This should cause an
  168. // error as it is not possible to overwrite a file with a directory.
  169. func (s *DockerCLICpSuite) TestCpFromCaseF(c *testing.T) {
  170. testRequires(c, DaemonIsLinux)
  171. containerID := makeTestContainer(c, testContainerOptions{
  172. addContent: true, workDir: "/root",
  173. })
  174. tmpDir := getTestDir(c, "test-cp-from-case-f")
  175. defer os.RemoveAll(tmpDir)
  176. makeTestContentInDir(c, tmpDir)
  177. srcDir := containerCpPath(containerID, "/root/dir1")
  178. dstFile := cpPath(tmpDir, "file1")
  179. err := runDockerCp(c, srcDir, dstFile)
  180. assert.ErrorContains(c, err, "")
  181. assert.Assert(c, isCpCannotCopyDir(err), "expected ErrCannotCopyDir error, but got %T: %s", err, err)
  182. }
  183. // G. SRC specifies a directory and DST exists as a directory. This should copy
  184. // the SRC directory and all its contents to the DST directory. Ensure this
  185. // works whether DST has a trailing path separator or not.
  186. func (s *DockerCLICpSuite) TestCpFromCaseG(c *testing.T) {
  187. testRequires(c, DaemonIsLinux)
  188. containerID := makeTestContainer(c, testContainerOptions{
  189. addContent: true, workDir: "/root",
  190. })
  191. tmpDir := getTestDir(c, "test-cp-from-case-g")
  192. defer os.RemoveAll(tmpDir)
  193. makeTestContentInDir(c, tmpDir)
  194. srcDir := containerCpPath(containerID, "/root/dir1")
  195. dstDir := cpPath(tmpDir, "dir2")
  196. resultDir := filepath.Join(dstDir, "dir1")
  197. dstPath := filepath.Join(resultDir, "file1-1")
  198. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  199. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  200. // Now try again but using a trailing path separator for dstDir.
  201. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove dstDir")
  202. assert.NilError(c, os.MkdirAll(dstDir, os.FileMode(0755)), "unable to make dstDir")
  203. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  204. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  205. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  206. }
  207. // H. SRC specifies a directory's contents only and DST does not exist. This
  208. // should create a directory at DST and copy the contents of the SRC
  209. // directory (but not the directory itself) into the DST directory. Ensure
  210. // this works whether DST has a trailing path separator or not.
  211. func (s *DockerCLICpSuite) TestCpFromCaseH(c *testing.T) {
  212. testRequires(c, DaemonIsLinux)
  213. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  214. tmpDir := getTestDir(c, "test-cp-from-case-h")
  215. defer os.RemoveAll(tmpDir)
  216. srcDir := containerCpPathTrailingSep(containerID, "dir1") + "."
  217. dstDir := cpPath(tmpDir, "testDir")
  218. dstPath := filepath.Join(dstDir, "file1-1")
  219. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  220. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  221. // Now try again but using a trailing path separator for dstDir.
  222. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove resultDir")
  223. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  224. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  225. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  226. }
  227. // I. SRC specifies a directory's contents only and DST exists as a file. This
  228. // should cause an error as it is not possible to overwrite a file with a
  229. // directory.
  230. func (s *DockerCLICpSuite) TestCpFromCaseI(c *testing.T) {
  231. testRequires(c, DaemonIsLinux)
  232. containerID := makeTestContainer(c, testContainerOptions{
  233. addContent: true, workDir: "/root",
  234. })
  235. tmpDir := getTestDir(c, "test-cp-from-case-i")
  236. defer os.RemoveAll(tmpDir)
  237. makeTestContentInDir(c, tmpDir)
  238. srcDir := containerCpPathTrailingSep(containerID, "/root/dir1") + "."
  239. dstFile := cpPath(tmpDir, "file1")
  240. err := runDockerCp(c, srcDir, dstFile)
  241. assert.ErrorContains(c, err, "")
  242. assert.Assert(c, isCpCannotCopyDir(err), "expected ErrCannotCopyDir error, but got %T: %s", err, err)
  243. }
  244. // J. SRC specifies a directory's contents only and DST exists as a directory.
  245. // This should copy the contents of the SRC directory (but not the directory
  246. // itself) into the DST directory. Ensure this works whether DST has a
  247. // trailing path separator or not.
  248. func (s *DockerCLICpSuite) TestCpFromCaseJ(c *testing.T) {
  249. testRequires(c, DaemonIsLinux)
  250. containerID := makeTestContainer(c, testContainerOptions{
  251. addContent: true, workDir: "/root",
  252. })
  253. tmpDir := getTestDir(c, "test-cp-from-case-j")
  254. defer os.RemoveAll(tmpDir)
  255. makeTestContentInDir(c, tmpDir)
  256. srcDir := containerCpPathTrailingSep(containerID, "/root/dir1") + "."
  257. dstDir := cpPath(tmpDir, "dir2")
  258. dstPath := filepath.Join(dstDir, "file1-1")
  259. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  260. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  261. // Now try again but using a trailing path separator for dstDir.
  262. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove dstDir")
  263. assert.NilError(c, os.MkdirAll(dstDir, os.FileMode(0755)), "unable to make dstDir")
  264. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  265. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  266. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  267. }