docker_cli_cp_from_container_test.go 17 KB

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