docker_cli_cp_from_container_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 exist.
  78. //
  79. // This should create a file with the name DST and copy the contents of the
  80. // source file into it.
  81. func (s *DockerCLICpSuite) TestCpFromCaseA(c *testing.T) {
  82. testRequires(c, DaemonIsLinux)
  83. containerID := makeTestContainer(c, testContainerOptions{
  84. addContent: true, workDir: "/root",
  85. })
  86. tmpDir := getTestDir(c, "test-cp-from-case-a")
  87. defer os.RemoveAll(tmpDir)
  88. srcPath := containerCpPath(containerID, "/root/file1")
  89. dstPath := cpPath(tmpDir, "itWorks.txt")
  90. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  91. assert.NilError(c, fileContentEquals(c, dstPath, "file1\n"))
  92. }
  93. // B. SRC specifies a file and DST (with trailing path separator) doesn't exist.
  94. //
  95. // This should cause an error because the copy operation cannot create a directory
  96. // when copying a single file.
  97. func (s *DockerCLICpSuite) TestCpFromCaseB(c *testing.T) {
  98. testRequires(c, DaemonIsLinux)
  99. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  100. tmpDir := getTestDir(c, "test-cp-from-case-b")
  101. defer os.RemoveAll(tmpDir)
  102. srcPath := containerCpPath(containerID, "/file1")
  103. dstDir := cpPathTrailingSep(tmpDir, "testDir")
  104. err := runDockerCp(c, srcPath, dstDir)
  105. assert.ErrorContains(c, err, "")
  106. assert.Assert(c, isCpDirNotExist(err), "expected DirNotExists error, but got %T: %s", err, err)
  107. }
  108. // C. SRC specifies a file and DST exists as a file.
  109. //
  110. // This should overwrite the file at DST with the contents of the source file.
  111. func (s *DockerCLICpSuite) TestCpFromCaseC(c *testing.T) {
  112. testRequires(c, DaemonIsLinux)
  113. containerID := makeTestContainer(c, testContainerOptions{
  114. addContent: true, workDir: "/root",
  115. })
  116. tmpDir := getTestDir(c, "test-cp-from-case-c")
  117. defer os.RemoveAll(tmpDir)
  118. makeTestContentInDir(c, tmpDir)
  119. srcPath := containerCpPath(containerID, "/root/file1")
  120. dstPath := cpPath(tmpDir, "file2")
  121. // Ensure the local file starts with different content.
  122. assert.NilError(c, fileContentEquals(c, dstPath, "file2\n"))
  123. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  124. assert.NilError(c, fileContentEquals(c, dstPath, "file1\n"))
  125. }
  126. // D. SRC specifies a file and DST exists as a directory.
  127. //
  128. // This should place a copy of the source file inside it using the basename from
  129. // SRC. Ensure this works whether DST has a trailing path separator or not.
  130. func (s *DockerCLICpSuite) TestCpFromCaseD(c *testing.T) {
  131. testRequires(c, DaemonIsLinux)
  132. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  133. tmpDir := getTestDir(c, "test-cp-from-case-d")
  134. defer os.RemoveAll(tmpDir)
  135. makeTestContentInDir(c, tmpDir)
  136. srcPath := containerCpPath(containerID, "/file1")
  137. dstDir := cpPath(tmpDir, "dir1")
  138. dstPath := filepath.Join(dstDir, "file1")
  139. // Ensure that dstPath doesn't exist.
  140. _, err := os.Stat(dstPath)
  141. assert.Assert(c, os.IsNotExist(err), "did not expect dstPath %q to exist", dstPath)
  142. assert.NilError(c, runDockerCp(c, srcPath, dstDir))
  143. assert.NilError(c, fileContentEquals(c, dstPath, "file1\n"))
  144. // Now try again but using a trailing path separator for dstDir.
  145. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove dstDir")
  146. assert.NilError(c, os.MkdirAll(dstDir, os.FileMode(0755)), "unable to make dstDir")
  147. dstDir = cpPathTrailingSep(tmpDir, "dir1")
  148. assert.NilError(c, runDockerCp(c, srcPath, dstDir))
  149. assert.NilError(c, fileContentEquals(c, dstPath, "file1\n"))
  150. }
  151. // E. SRC specifies a directory and DST does not exist.
  152. //
  153. // This should create a directory at DST and copy the contents of the SRC directory
  154. // into the DST directory. Ensure this works whether DST has a trailing path
  155. // separator or not.
  156. func (s *DockerCLICpSuite) TestCpFromCaseE(c *testing.T) {
  157. testRequires(c, DaemonIsLinux)
  158. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  159. tmpDir := getTestDir(c, "test-cp-from-case-e")
  160. defer os.RemoveAll(tmpDir)
  161. srcDir := containerCpPath(containerID, "dir1")
  162. dstDir := cpPath(tmpDir, "testDir")
  163. dstPath := filepath.Join(dstDir, "file1-1")
  164. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  165. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  166. // Now try again but using a trailing path separator for dstDir.
  167. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove dstDir")
  168. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  169. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  170. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  171. }
  172. // F. SRC specifies a directory and DST exists as a file.
  173. //
  174. // This should cause an error as it is not possible to overwrite a file with a
  175. // directory.
  176. func (s *DockerCLICpSuite) TestCpFromCaseF(c *testing.T) {
  177. testRequires(c, DaemonIsLinux)
  178. containerID := makeTestContainer(c, testContainerOptions{
  179. addContent: true, workDir: "/root",
  180. })
  181. tmpDir := getTestDir(c, "test-cp-from-case-f")
  182. defer os.RemoveAll(tmpDir)
  183. makeTestContentInDir(c, tmpDir)
  184. srcDir := containerCpPath(containerID, "/root/dir1")
  185. dstFile := cpPath(tmpDir, "file1")
  186. err := runDockerCp(c, srcDir, dstFile)
  187. assert.ErrorContains(c, err, "")
  188. assert.Assert(c, isCpCannotCopyDir(err), "expected ErrCannotCopyDir error, but got %T: %s", err, err)
  189. }
  190. // G. SRC specifies a directory and DST exists as a directory.
  191. //
  192. // This should copy the SRC directory and all its contents to the DST directory.
  193. // Ensure this works whether DST has a trailing path separator or not.
  194. func (s *DockerCLICpSuite) TestCpFromCaseG(c *testing.T) {
  195. testRequires(c, DaemonIsLinux)
  196. containerID := makeTestContainer(c, testContainerOptions{
  197. addContent: true, workDir: "/root",
  198. })
  199. tmpDir := getTestDir(c, "test-cp-from-case-g")
  200. defer os.RemoveAll(tmpDir)
  201. makeTestContentInDir(c, tmpDir)
  202. srcDir := containerCpPath(containerID, "/root/dir1")
  203. dstDir := cpPath(tmpDir, "dir2")
  204. resultDir := filepath.Join(dstDir, "dir1")
  205. dstPath := filepath.Join(resultDir, "file1-1")
  206. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  207. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  208. // Now try again but using a trailing path separator for dstDir.
  209. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove dstDir")
  210. assert.NilError(c, os.MkdirAll(dstDir, os.FileMode(0755)), "unable to make dstDir")
  211. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  212. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  213. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  214. }
  215. // H. SRC specifies a directory's contents only and DST does not exist.
  216. //
  217. // This should create a directory at DST and copy the contents of the SRC
  218. // directory (but not the directory itself) into the DST directory. Ensure
  219. // this works whether DST has a trailing path separator or not.
  220. func (s *DockerCLICpSuite) TestCpFromCaseH(c *testing.T) {
  221. testRequires(c, DaemonIsLinux)
  222. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  223. tmpDir := getTestDir(c, "test-cp-from-case-h")
  224. defer os.RemoveAll(tmpDir)
  225. srcDir := containerCpPathTrailingSep(containerID, "dir1") + "."
  226. dstDir := cpPath(tmpDir, "testDir")
  227. dstPath := filepath.Join(dstDir, "file1-1")
  228. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  229. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  230. // Now try again but using a trailing path separator for dstDir.
  231. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove resultDir")
  232. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  233. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  234. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  235. }
  236. // I. SRC specifies a directory's contents only and DST exists as a file.
  237. //
  238. // This should cause an error as it is not possible to overwrite a file with a
  239. // directory.
  240. func (s *DockerCLICpSuite) TestCpFromCaseI(c *testing.T) {
  241. testRequires(c, DaemonIsLinux)
  242. containerID := makeTestContainer(c, testContainerOptions{
  243. addContent: true, workDir: "/root",
  244. })
  245. tmpDir := getTestDir(c, "test-cp-from-case-i")
  246. defer os.RemoveAll(tmpDir)
  247. makeTestContentInDir(c, tmpDir)
  248. srcDir := containerCpPathTrailingSep(containerID, "/root/dir1") + "."
  249. dstFile := cpPath(tmpDir, "file1")
  250. err := runDockerCp(c, srcDir, dstFile)
  251. assert.ErrorContains(c, err, "")
  252. assert.Assert(c, isCpCannotCopyDir(err), "expected ErrCannotCopyDir error, but got %T: %s", err, err)
  253. }
  254. // J. SRC specifies a directory's contents only and DST exists as a directory.
  255. //
  256. // This should copy the contents of the SRC directory (but not the directory
  257. // itself) into the DST directory. Ensure this works whether DST has a
  258. // trailing path separator or not.
  259. func (s *DockerCLICpSuite) TestCpFromCaseJ(c *testing.T) {
  260. testRequires(c, DaemonIsLinux)
  261. containerID := makeTestContainer(c, testContainerOptions{
  262. addContent: true, workDir: "/root",
  263. })
  264. tmpDir := getTestDir(c, "test-cp-from-case-j")
  265. defer os.RemoveAll(tmpDir)
  266. makeTestContentInDir(c, tmpDir)
  267. srcDir := containerCpPathTrailingSep(containerID, "/root/dir1") + "."
  268. dstDir := cpPath(tmpDir, "dir2")
  269. dstPath := filepath.Join(dstDir, "file1-1")
  270. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  271. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  272. // Now try again but using a trailing path separator for dstDir.
  273. assert.NilError(c, os.RemoveAll(dstDir), "unable to remove dstDir")
  274. assert.NilError(c, os.MkdirAll(dstDir, os.FileMode(0755)), "unable to make dstDir")
  275. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  276. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  277. assert.NilError(c, fileContentEquals(c, dstPath, "file1-1\n"))
  278. }