docker_cli_cp_from_container_test.go 17 KB

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