docker_cli_cp_to_container_test.go 22 KB

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