docker_cli_cp_from_container_test.go 15 KB

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