docker_cli_cp_to_container_test.go 22 KB

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