docker_cli_cp_to_container_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package main
  2. import (
  3. "os"
  4. "github.com/docker/docker/integration-cli/checker"
  5. "github.com/go-check/check"
  6. )
  7. // Try all of the test cases from the archive package which implements the
  8. // internals of `docker cp` and ensure that the behavior matches when actually
  9. // copying to and from containers.
  10. // Basic assumptions about SRC and DST:
  11. // 1. SRC must exist.
  12. // 2. If SRC ends with a trailing separator, it must be a directory.
  13. // 3. DST parent directory must exist.
  14. // 4. If DST exists as a file, it must not end with a trailing separator.
  15. // Check that copying from a local path to a symlink in a container copies to
  16. // the symlink target and does not overwrite the container symlink itself.
  17. func (s *DockerSuite) TestCpToSymlinkDestination(c *check.C) {
  18. // stat /tmp/test-cp-to-symlink-destination-262430901/vol3 gets permission denied for the user
  19. testRequires(c, NotUserNamespace)
  20. testRequires(c, DaemonIsLinux)
  21. testRequires(c, testEnv.IsLocalDaemon) // Requires local volume mount bind.
  22. testVol := getTestDir(c, "test-cp-to-symlink-destination-")
  23. defer os.RemoveAll(testVol)
  24. makeTestContentInDir(c, testVol)
  25. containerID := makeTestContainer(c, testContainerOptions{
  26. volumes: defaultVolumes(testVol), // Our bind mount is at /vol2
  27. })
  28. // First, copy a local file to a symlink to a file in the container. This
  29. // should overwrite the symlink target contents with the source contents.
  30. srcPath := cpPath(testVol, "file2")
  31. dstPath := containerCpPath(containerID, "/vol2/symlinkToFile1")
  32. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  33. // The symlink should not have been modified.
  34. c.Assert(symlinkTargetEquals(c, cpPath(testVol, "symlinkToFile1"), "file1"), checker.IsNil)
  35. // The file should have the contents of "file2" now.
  36. c.Assert(fileContentEquals(c, cpPath(testVol, "file1"), "file2\n"), checker.IsNil)
  37. // Next, copy a local file to a symlink to a directory in the container.
  38. // This should copy the file into the symlink target directory.
  39. dstPath = containerCpPath(containerID, "/vol2/symlinkToDir1")
  40. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  41. // The symlink should not have been modified.
  42. c.Assert(symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"), checker.IsNil)
  43. // The file should have the contents of "file2" now.
  44. c.Assert(fileContentEquals(c, cpPath(testVol, "file2"), "file2\n"), checker.IsNil)
  45. // Next, copy a file to a symlink to a file that does not exist (a broken
  46. // symlink) in the container. This should create the target file with the
  47. // contents of the source file.
  48. dstPath = containerCpPath(containerID, "/vol2/brokenSymlinkToFileX")
  49. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  50. // The symlink should not have been modified.
  51. c.Assert(symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToFileX"), "fileX"), checker.IsNil)
  52. // The file should have the contents of "file2" now.
  53. c.Assert(fileContentEquals(c, cpPath(testVol, "fileX"), "file2\n"), checker.IsNil)
  54. // Next, copy a local directory to a symlink to a directory in the
  55. // container. This should copy the directory into the symlink target
  56. // directory and not modify the symlink.
  57. srcPath = cpPath(testVol, "/dir2")
  58. dstPath = containerCpPath(containerID, "/vol2/symlinkToDir1")
  59. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  60. // The symlink should not have been modified.
  61. c.Assert(symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"), checker.IsNil)
  62. // The directory should now contain a copy of "dir2".
  63. c.Assert(fileContentEquals(c, cpPath(testVol, "dir1/dir2/file2-1"), "file2-1\n"), checker.IsNil)
  64. // Next, copy a local directory to a symlink to a local directory that does
  65. // not exist (a broken symlink) in the container. This should create the
  66. // target as a directory with the contents of the source directory. It
  67. // should not modify the symlink.
  68. dstPath = containerCpPath(containerID, "/vol2/brokenSymlinkToDirX")
  69. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  70. // The symlink should not have been modified.
  71. c.Assert(symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToDirX"), "dirX"), checker.IsNil)
  72. // The "dirX" directory should now be a copy of "dir2".
  73. c.Assert(fileContentEquals(c, cpPath(testVol, "dirX/file2-1"), "file2-1\n"), checker.IsNil)
  74. }
  75. // Possibilities are reduced to the remaining 10 cases:
  76. //
  77. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  78. // ===================================================================================================
  79. // A | no | - | no | - | no | create file
  80. // B | no | - | no | - | yes | error
  81. // C | no | - | yes | no | - | overwrite file
  82. // D | no | - | yes | yes | - | create file in dst dir
  83. // E | yes | no | no | - | - | create dir, copy contents
  84. // F | yes | no | yes | no | - | error
  85. // G | yes | no | yes | yes | - | copy dir and contents
  86. // H | yes | yes | no | - | - | create dir, copy contents
  87. // I | yes | yes | yes | no | - | error
  88. // J | yes | yes | yes | yes | - | copy dir contents
  89. //
  90. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  91. // exist. This should create a file with the name DST and copy the
  92. // contents of the source file into it.
  93. func (s *DockerSuite) TestCpToCaseA(c *check.C) {
  94. containerID := makeTestContainer(c, testContainerOptions{
  95. workDir: "/root", command: makeCatFileCommand("itWorks.txt"),
  96. })
  97. tmpDir := getTestDir(c, "test-cp-to-case-a")
  98. defer os.RemoveAll(tmpDir)
  99. makeTestContentInDir(c, tmpDir)
  100. srcPath := cpPath(tmpDir, "file1")
  101. dstPath := containerCpPath(containerID, "/root/itWorks.txt")
  102. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  103. c.Assert(containerStartOutputEquals(c, containerID, "file1\n"), checker.IsNil)
  104. }
  105. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  106. // exist. This should cause an error because the copy operation cannot
  107. // create a directory when copying a single file.
  108. func (s *DockerSuite) TestCpToCaseB(c *check.C) {
  109. containerID := makeTestContainer(c, testContainerOptions{
  110. command: makeCatFileCommand("testDir/file1"),
  111. })
  112. tmpDir := getTestDir(c, "test-cp-to-case-b")
  113. defer os.RemoveAll(tmpDir)
  114. makeTestContentInDir(c, tmpDir)
  115. srcPath := cpPath(tmpDir, "file1")
  116. dstDir := containerCpPathTrailingSep(containerID, "testDir")
  117. err := runDockerCp(c, srcPath, dstDir, nil)
  118. c.Assert(err, checker.NotNil)
  119. c.Assert(isCpDirNotExist(err), checker.True, check.Commentf("expected DirNotExists error, but got %T: %s", err, err))
  120. }
  121. // C. SRC specifies a file and DST exists as a file. This should overwrite
  122. // the file at DST with the contents of the source file.
  123. func (s *DockerSuite) TestCpToCaseC(c *check.C) {
  124. testRequires(c, DaemonIsLinux)
  125. containerID := makeTestContainer(c, testContainerOptions{
  126. addContent: true, workDir: "/root",
  127. command: makeCatFileCommand("file2"),
  128. })
  129. tmpDir := getTestDir(c, "test-cp-to-case-c")
  130. defer os.RemoveAll(tmpDir)
  131. makeTestContentInDir(c, tmpDir)
  132. srcPath := cpPath(tmpDir, "file1")
  133. dstPath := containerCpPath(containerID, "/root/file2")
  134. // Ensure the container's file starts with the original content.
  135. c.Assert(containerStartOutputEquals(c, containerID, "file2\n"), checker.IsNil)
  136. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  137. // Should now contain file1's contents.
  138. c.Assert(containerStartOutputEquals(c, containerID, "file1\n"), checker.IsNil)
  139. }
  140. // D. SRC specifies a file and DST exists as a directory. This should place
  141. // a copy of the source file inside it using the basename from SRC. Ensure
  142. // this works whether DST has a trailing path separator or not.
  143. func (s *DockerSuite) TestCpToCaseD(c *check.C) {
  144. testRequires(c, DaemonIsLinux)
  145. containerID := makeTestContainer(c, testContainerOptions{
  146. addContent: true,
  147. command: makeCatFileCommand("/dir1/file1"),
  148. })
  149. tmpDir := getTestDir(c, "test-cp-to-case-d")
  150. defer os.RemoveAll(tmpDir)
  151. makeTestContentInDir(c, tmpDir)
  152. srcPath := cpPath(tmpDir, "file1")
  153. dstDir := containerCpPath(containerID, "dir1")
  154. // Ensure that dstPath doesn't exist.
  155. c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
  156. c.Assert(runDockerCp(c, srcPath, dstDir, nil), checker.IsNil)
  157. // Should now contain file1's contents.
  158. c.Assert(containerStartOutputEquals(c, containerID, "file1\n"), checker.IsNil)
  159. // Now try again but using a trailing path separator for dstDir.
  160. // Make new destination container.
  161. containerID = makeTestContainer(c, testContainerOptions{
  162. addContent: true,
  163. command: makeCatFileCommand("/dir1/file1"),
  164. })
  165. dstDir = containerCpPathTrailingSep(containerID, "dir1")
  166. // Ensure that dstPath doesn't exist.
  167. c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
  168. c.Assert(runDockerCp(c, srcPath, dstDir, nil), checker.IsNil)
  169. // Should now contain file1's contents.
  170. c.Assert(containerStartOutputEquals(c, containerID, "file1\n"), checker.IsNil)
  171. }
  172. // E. SRC specifies a directory and DST does not exist. This should create a
  173. // directory at DST and copy the contents of the SRC directory into the DST
  174. // directory. Ensure this works whether DST has a trailing path separator or
  175. // not.
  176. func (s *DockerSuite) TestCpToCaseE(c *check.C) {
  177. containerID := makeTestContainer(c, testContainerOptions{
  178. command: makeCatFileCommand("/testDir/file1-1"),
  179. })
  180. tmpDir := getTestDir(c, "test-cp-to-case-e")
  181. defer os.RemoveAll(tmpDir)
  182. makeTestContentInDir(c, tmpDir)
  183. srcDir := cpPath(tmpDir, "dir1")
  184. dstDir := containerCpPath(containerID, "testDir")
  185. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  186. // Should now contain file1-1's contents.
  187. c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
  188. // Now try again but using a trailing path separator for dstDir.
  189. // Make new destination container.
  190. containerID = makeTestContainer(c, testContainerOptions{
  191. command: makeCatFileCommand("/testDir/file1-1"),
  192. })
  193. dstDir = containerCpPathTrailingSep(containerID, "testDir")
  194. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  195. // Should now contain file1-1's contents.
  196. c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
  197. }
  198. // F. SRC specifies a directory and DST exists as a file. This should cause an
  199. // error as it is not possible to overwrite a file with a directory.
  200. func (s *DockerSuite) TestCpToCaseF(c *check.C) {
  201. testRequires(c, DaemonIsLinux)
  202. containerID := makeTestContainer(c, testContainerOptions{
  203. addContent: true, workDir: "/root",
  204. })
  205. tmpDir := getTestDir(c, "test-cp-to-case-f")
  206. defer os.RemoveAll(tmpDir)
  207. makeTestContentInDir(c, tmpDir)
  208. srcDir := cpPath(tmpDir, "dir1")
  209. dstFile := containerCpPath(containerID, "/root/file1")
  210. err := runDockerCp(c, srcDir, dstFile, nil)
  211. c.Assert(err, checker.NotNil)
  212. c.Assert(isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
  213. }
  214. // G. SRC specifies a directory and DST exists as a directory. This should copy
  215. // the SRC directory and all its contents to the DST directory. Ensure this
  216. // works whether DST has a trailing path separator or not.
  217. func (s *DockerSuite) TestCpToCaseG(c *check.C) {
  218. testRequires(c, DaemonIsLinux)
  219. containerID := makeTestContainer(c, testContainerOptions{
  220. addContent: true, workDir: "/root",
  221. command: makeCatFileCommand("dir2/dir1/file1-1"),
  222. })
  223. tmpDir := getTestDir(c, "test-cp-to-case-g")
  224. defer os.RemoveAll(tmpDir)
  225. makeTestContentInDir(c, tmpDir)
  226. srcDir := cpPath(tmpDir, "dir1")
  227. dstDir := containerCpPath(containerID, "/root/dir2")
  228. // Ensure that dstPath doesn't exist.
  229. c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
  230. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  231. // Should now contain file1-1's contents.
  232. c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
  233. // Now try again but using a trailing path separator for dstDir.
  234. // Make new destination container.
  235. containerID = makeTestContainer(c, testContainerOptions{
  236. addContent: true,
  237. command: makeCatFileCommand("/dir2/dir1/file1-1"),
  238. })
  239. dstDir = containerCpPathTrailingSep(containerID, "/dir2")
  240. // Ensure that dstPath doesn't exist.
  241. c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
  242. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  243. // Should now contain file1-1's contents.
  244. c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
  245. }
  246. // H. SRC specifies a directory's contents only and DST does not exist. This
  247. // should create a directory at DST and copy the contents of the SRC
  248. // directory (but not the directory itself) into the DST directory. Ensure
  249. // this works whether DST has a trailing path separator or not.
  250. func (s *DockerSuite) TestCpToCaseH(c *check.C) {
  251. containerID := makeTestContainer(c, testContainerOptions{
  252. command: makeCatFileCommand("/testDir/file1-1"),
  253. })
  254. tmpDir := getTestDir(c, "test-cp-to-case-h")
  255. defer os.RemoveAll(tmpDir)
  256. makeTestContentInDir(c, tmpDir)
  257. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  258. dstDir := containerCpPath(containerID, "testDir")
  259. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  260. // Should now contain file1-1's contents.
  261. c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
  262. // Now try again but using a trailing path separator for dstDir.
  263. // Make new destination container.
  264. containerID = makeTestContainer(c, testContainerOptions{
  265. command: makeCatFileCommand("/testDir/file1-1"),
  266. })
  267. dstDir = containerCpPathTrailingSep(containerID, "testDir")
  268. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  269. // Should now contain file1-1's contents.
  270. c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
  271. }
  272. // I. SRC specifies a directory's contents only and DST exists as a file. This
  273. // should cause an error as it is not possible to overwrite a file with a
  274. // directory.
  275. func (s *DockerSuite) TestCpToCaseI(c *check.C) {
  276. testRequires(c, DaemonIsLinux)
  277. containerID := makeTestContainer(c, testContainerOptions{
  278. addContent: true, workDir: "/root",
  279. })
  280. tmpDir := getTestDir(c, "test-cp-to-case-i")
  281. defer os.RemoveAll(tmpDir)
  282. makeTestContentInDir(c, tmpDir)
  283. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  284. dstFile := containerCpPath(containerID, "/root/file1")
  285. err := runDockerCp(c, srcDir, dstFile, nil)
  286. c.Assert(err, checker.NotNil)
  287. c.Assert(isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
  288. }
  289. // J. SRC specifies a directory's contents only and DST exists as a directory.
  290. // This should copy the contents of the SRC directory (but not the directory
  291. // itself) into the DST directory. Ensure this works whether DST has a
  292. // trailing path separator or not.
  293. func (s *DockerSuite) TestCpToCaseJ(c *check.C) {
  294. testRequires(c, DaemonIsLinux)
  295. containerID := makeTestContainer(c, testContainerOptions{
  296. addContent: true, workDir: "/root",
  297. command: makeCatFileCommand("/dir2/file1-1"),
  298. })
  299. tmpDir := getTestDir(c, "test-cp-to-case-j")
  300. defer os.RemoveAll(tmpDir)
  301. makeTestContentInDir(c, tmpDir)
  302. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  303. dstDir := containerCpPath(containerID, "/dir2")
  304. // Ensure that dstPath doesn't exist.
  305. c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
  306. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  307. // Should now contain file1-1's contents.
  308. c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
  309. // Now try again but using a trailing path separator for dstDir.
  310. // Make new destination container.
  311. containerID = makeTestContainer(c, testContainerOptions{
  312. command: makeCatFileCommand("/dir2/file1-1"),
  313. })
  314. dstDir = containerCpPathTrailingSep(containerID, "/dir2")
  315. // Ensure that dstPath doesn't exist.
  316. c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
  317. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  318. // Should now contain file1-1's contents.
  319. c.Assert(containerStartOutputEquals(c, containerID, "file1-1\n"), checker.IsNil)
  320. }
  321. // The `docker cp` command should also ensure that you cannot
  322. // write to a container rootfs that is marked as read-only.
  323. func (s *DockerSuite) TestCpToErrReadOnlyRootfs(c *check.C) {
  324. // --read-only + userns has remount issues
  325. testRequires(c, DaemonIsLinux, NotUserNamespace)
  326. tmpDir := getTestDir(c, "test-cp-to-err-read-only-rootfs")
  327. defer os.RemoveAll(tmpDir)
  328. makeTestContentInDir(c, tmpDir)
  329. containerID := makeTestContainer(c, testContainerOptions{
  330. readOnly: true, workDir: "/root",
  331. command: makeCatFileCommand("shouldNotExist"),
  332. })
  333. srcPath := cpPath(tmpDir, "file1")
  334. dstPath := containerCpPath(containerID, "/root/shouldNotExist")
  335. err := runDockerCp(c, srcPath, dstPath, nil)
  336. c.Assert(err, checker.NotNil)
  337. c.Assert(isCpCannotCopyReadOnly(err), checker.True, check.Commentf("expected ErrContainerRootfsReadonly error, but got %T: %s", err, err))
  338. // Ensure that dstPath doesn't exist.
  339. c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
  340. }
  341. // The `docker cp` command should also ensure that you
  342. // cannot write to a volume that is mounted as read-only.
  343. func (s *DockerSuite) TestCpToErrReadOnlyVolume(c *check.C) {
  344. // --read-only + userns has remount issues
  345. testRequires(c, DaemonIsLinux, NotUserNamespace)
  346. tmpDir := getTestDir(c, "test-cp-to-err-read-only-volume")
  347. defer os.RemoveAll(tmpDir)
  348. makeTestContentInDir(c, tmpDir)
  349. containerID := makeTestContainer(c, testContainerOptions{
  350. volumes: defaultVolumes(tmpDir), workDir: "/root",
  351. command: makeCatFileCommand("/vol_ro/shouldNotExist"),
  352. })
  353. srcPath := cpPath(tmpDir, "file1")
  354. dstPath := containerCpPath(containerID, "/vol_ro/shouldNotExist")
  355. err := runDockerCp(c, srcPath, dstPath, nil)
  356. c.Assert(err, checker.NotNil)
  357. c.Assert(isCpCannotCopyReadOnly(err), checker.True, check.Commentf("expected ErrVolumeReadonly error, but got %T: %s", err, err))
  358. // Ensure that dstPath doesn't exist.
  359. c.Assert(containerStartOutputEquals(c, containerID, ""), checker.IsNil)
  360. }