docker_cli_cp_to_container_test.go 22 KB

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