docker_cli_cp_from_container_test.go 17 KB

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