docker_cli_cp_to_container_test.go 22 KB

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