docker_cli_cp_to_container_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. package main
  2. import (
  3. "os"
  4. "github.com/go-check/check"
  5. )
  6. // docker cp LOCALPATH CONTAINER:PATH
  7. // Try all of the test cases from the archive package which implements the
  8. // internals of `docker cp` and ensure that the behavior matches when actually
  9. // copying to and from containers.
  10. // Basic assumptions about SRC and DST:
  11. // 1. SRC must exist.
  12. // 2. If SRC ends with a trailing separator, it must be a directory.
  13. // 3. DST parent directory must exist.
  14. // 4. If DST exists as a file, it must not end with a trailing separator.
  15. // First get these easy error cases out of the way.
  16. // Test for error when SRC does not exist.
  17. func (s *DockerSuite) TestCpToErrSrcNotExists(c *check.C) {
  18. testRequires(c, DaemonIsLinux)
  19. cID := makeTestContainer(c, testContainerOptions{})
  20. defer deleteContainer(cID)
  21. tmpDir := getTestDir(c, "test-cp-to-err-src-not-exists")
  22. defer os.RemoveAll(tmpDir)
  23. srcPath := cpPath(tmpDir, "file1")
  24. dstPath := containerCpPath(cID, "file1")
  25. err := runDockerCp(c, srcPath, dstPath)
  26. if err == nil {
  27. c.Fatal("expected IsNotExist error, but got nil instead")
  28. }
  29. if !isCpNotExist(err) {
  30. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  31. }
  32. }
  33. // Test for error when SRC ends in a trailing
  34. // path separator but it exists as a file.
  35. func (s *DockerSuite) TestCpToErrSrcNotDir(c *check.C) {
  36. testRequires(c, DaemonIsLinux)
  37. cID := makeTestContainer(c, testContainerOptions{})
  38. defer deleteContainer(cID)
  39. tmpDir := getTestDir(c, "test-cp-to-err-src-not-dir")
  40. defer os.RemoveAll(tmpDir)
  41. makeTestContentInDir(c, tmpDir)
  42. srcPath := cpPathTrailingSep(tmpDir, "file1")
  43. dstPath := containerCpPath(cID, "testDir")
  44. err := runDockerCp(c, srcPath, dstPath)
  45. if err == nil {
  46. c.Fatal("expected IsNotDir error, but got nil instead")
  47. }
  48. if !isCpNotDir(err) {
  49. c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  50. }
  51. }
  52. // Test for error when SRC is a valid file or directory,
  53. // bu the DST parent directory does not exist.
  54. func (s *DockerSuite) TestCpToErrDstParentNotExists(c *check.C) {
  55. testRequires(c, DaemonIsLinux)
  56. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  57. defer deleteContainer(cID)
  58. tmpDir := getTestDir(c, "test-cp-to-err-dst-parent-not-exists")
  59. defer os.RemoveAll(tmpDir)
  60. makeTestContentInDir(c, tmpDir)
  61. // Try with a file source.
  62. srcPath := cpPath(tmpDir, "file1")
  63. dstPath := containerCpPath(cID, "/notExists", "file1")
  64. err := runDockerCp(c, srcPath, dstPath)
  65. if err == nil {
  66. c.Fatal("expected IsNotExist error, but got nil instead")
  67. }
  68. if !isCpNotExist(err) {
  69. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  70. }
  71. // Try with a directory source.
  72. srcPath = cpPath(tmpDir, "dir1")
  73. if err := runDockerCp(c, srcPath, dstPath); err == nil {
  74. c.Fatal("expected IsNotExist error, but got nil instead")
  75. }
  76. if !isCpNotExist(err) {
  77. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  78. }
  79. }
  80. // Test for error when DST ends in a trailing path separator but exists as a
  81. // file. Also test that we cannot overwirite an existing directory with a
  82. // non-directory and cannot overwrite an existing
  83. func (s *DockerSuite) TestCpToErrDstNotDir(c *check.C) {
  84. testRequires(c, DaemonIsLinux)
  85. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  86. defer deleteContainer(cID)
  87. tmpDir := getTestDir(c, "test-cp-to-err-dst-not-dir")
  88. defer os.RemoveAll(tmpDir)
  89. makeTestContentInDir(c, tmpDir)
  90. // Try with a file source.
  91. srcPath := cpPath(tmpDir, "dir1/file1-1")
  92. dstPath := containerCpPathTrailingSep(cID, "file1")
  93. // The client should encounter an error trying to stat the destination
  94. // and then be unable to copy since the destination is asserted to be a
  95. // directory but does not exist.
  96. err := runDockerCp(c, srcPath, dstPath)
  97. if err == nil {
  98. c.Fatal("expected DirNotExist error, but got nil instead")
  99. }
  100. if !isCpDirNotExist(err) {
  101. c.Fatalf("expected DirNotExist error, but got %T: %s", err, err)
  102. }
  103. // Try with a directory source.
  104. srcPath = cpPath(tmpDir, "dir1")
  105. // The client should encounter an error trying to stat the destination and
  106. // then decide to extract to the parent directory instead with a rebased
  107. // name in the source archive, but this directory would overwrite the
  108. // existing file with the same name.
  109. err = runDockerCp(c, srcPath, dstPath)
  110. if err == nil {
  111. c.Fatal("expected CannotOverwriteNonDirWithDir error, but got nil instead")
  112. }
  113. if !isCannotOverwriteNonDirWithDir(err) {
  114. c.Fatalf("expected CannotOverwriteNonDirWithDir error, but got %T: %s", err, err)
  115. }
  116. }
  117. // Check that copying from a local path to a symlink in a container copies to
  118. // the symlink target and does not overwrite the container symlink itself.
  119. func (s *DockerSuite) TestCpToSymlinkDestination(c *check.C) {
  120. // stat /tmp/test-cp-to-symlink-destination-262430901/vol3 gets permission denied for the user
  121. testRequires(c, NotUserNamespace)
  122. testRequires(c, DaemonIsLinux)
  123. testRequires(c, SameHostDaemon) // Requires local volume mount bind.
  124. testVol := getTestDir(c, "test-cp-to-symlink-destination-")
  125. defer os.RemoveAll(testVol)
  126. makeTestContentInDir(c, testVol)
  127. cID := makeTestContainer(c, testContainerOptions{
  128. volumes: defaultVolumes(testVol), // Our bind mount is at /vol2
  129. })
  130. defer deleteContainer(cID)
  131. // First, copy a local file to a symlink to a file in the container. This
  132. // should overwrite the symlink target contents with the source contents.
  133. srcPath := cpPath(testVol, "file2")
  134. dstPath := containerCpPath(cID, "/vol2/symlinkToFile1")
  135. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  136. c.Fatalf("unexpected error %T: %s", err, err)
  137. }
  138. // The symlink should not have been modified.
  139. if err := symlinkTargetEquals(c, cpPath(testVol, "symlinkToFile1"), "file1"); err != nil {
  140. c.Fatal(err)
  141. }
  142. // The file should have the contents of "file2" now.
  143. if err := fileContentEquals(c, cpPath(testVol, "file1"), "file2\n"); err != nil {
  144. c.Fatal(err)
  145. }
  146. // Next, copy a local file to a symlink to a directory in the container.
  147. // This should copy the file into the symlink target directory.
  148. dstPath = containerCpPath(cID, "/vol2/symlinkToDir1")
  149. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  150. c.Fatalf("unexpected error %T: %s", err, err)
  151. }
  152. // The symlink should not have been modified.
  153. if err := symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"); err != nil {
  154. c.Fatal(err)
  155. }
  156. // The file should have the contents of "file2" now.
  157. if err := fileContentEquals(c, cpPath(testVol, "file2"), "file2\n"); err != nil {
  158. c.Fatal(err)
  159. }
  160. // Next, copy a file to a symlink to a file that does not exist (a broken
  161. // symlink) in the container. This should create the target file with the
  162. // contents of the source file.
  163. dstPath = containerCpPath(cID, "/vol2/brokenSymlinkToFileX")
  164. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  165. c.Fatalf("unexpected error %T: %s", err, err)
  166. }
  167. // The symlink should not have been modified.
  168. if err := symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToFileX"), "fileX"); err != nil {
  169. c.Fatal(err)
  170. }
  171. // The file should have the contents of "file2" now.
  172. if err := fileContentEquals(c, cpPath(testVol, "fileX"), "file2\n"); err != nil {
  173. c.Fatal(err)
  174. }
  175. // Next, copy a local directory to a symlink to a directory in the
  176. // container. This should copy the directory into the symlink target
  177. // directory and not modify the symlink.
  178. srcPath = cpPath(testVol, "/dir2")
  179. dstPath = containerCpPath(cID, "/vol2/symlinkToDir1")
  180. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  181. c.Fatalf("unexpected error %T: %s", err, err)
  182. }
  183. // The symlink should not have been modified.
  184. if err := symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"); err != nil {
  185. c.Fatal(err)
  186. }
  187. // The directory should now contain a copy of "dir2".
  188. if err := fileContentEquals(c, cpPath(testVol, "dir1/dir2/file2-1"), "file2-1\n"); err != nil {
  189. c.Fatal(err)
  190. }
  191. // Next, copy a local directory to a symlink to a local directory that does
  192. // not exist (a broken symlink) in the container. This should create the
  193. // target as a directory with the contents of the source directory. It
  194. // should not modify the symlink.
  195. dstPath = containerCpPath(cID, "/vol2/brokenSymlinkToDirX")
  196. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  197. c.Fatalf("unexpected error %T: %s", err, err)
  198. }
  199. // The symlink should not have been modified.
  200. if err := symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToDirX"), "dirX"); err != nil {
  201. c.Fatal(err)
  202. }
  203. // The "dirX" directory should now be a copy of "dir2".
  204. if err := fileContentEquals(c, cpPath(testVol, "dirX/file2-1"), "file2-1\n"); err != nil {
  205. c.Fatal(err)
  206. }
  207. }
  208. // Possibilities are reduced to the remaining 10 cases:
  209. //
  210. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  211. // ===================================================================================================
  212. // A | no | - | no | - | no | create file
  213. // B | no | - | no | - | yes | error
  214. // C | no | - | yes | no | - | overwrite file
  215. // D | no | - | yes | yes | - | create file in dst dir
  216. // E | yes | no | no | - | - | create dir, copy contents
  217. // F | yes | no | yes | no | - | error
  218. // G | yes | no | yes | yes | - | copy dir and contents
  219. // H | yes | yes | no | - | - | create dir, copy contents
  220. // I | yes | yes | yes | no | - | error
  221. // J | yes | yes | yes | yes | - | copy dir contents
  222. //
  223. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  224. // exist. This should create a file with the name DST and copy the
  225. // contents of the source file into it.
  226. func (s *DockerSuite) TestCpToCaseA(c *check.C) {
  227. testRequires(c, DaemonIsLinux)
  228. cID := makeTestContainer(c, testContainerOptions{
  229. workDir: "/root", command: makeCatFileCommand("itWorks.txt"),
  230. })
  231. defer deleteContainer(cID)
  232. tmpDir := getTestDir(c, "test-cp-to-case-a")
  233. defer os.RemoveAll(tmpDir)
  234. makeTestContentInDir(c, tmpDir)
  235. srcPath := cpPath(tmpDir, "file1")
  236. dstPath := containerCpPath(cID, "/root/itWorks.txt")
  237. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  238. c.Fatalf("unexpected error %T: %s", err, err)
  239. }
  240. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  241. c.Fatal(err)
  242. }
  243. }
  244. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  245. // exist. This should cause an error because the copy operation cannot
  246. // create a directory when copying a single file.
  247. func (s *DockerSuite) TestCpToCaseB(c *check.C) {
  248. testRequires(c, DaemonIsLinux)
  249. cID := makeTestContainer(c, testContainerOptions{
  250. command: makeCatFileCommand("testDir/file1"),
  251. })
  252. defer deleteContainer(cID)
  253. tmpDir := getTestDir(c, "test-cp-to-case-b")
  254. defer os.RemoveAll(tmpDir)
  255. makeTestContentInDir(c, tmpDir)
  256. srcPath := cpPath(tmpDir, "file1")
  257. dstDir := containerCpPathTrailingSep(cID, "testDir")
  258. err := runDockerCp(c, srcPath, dstDir)
  259. if err == nil {
  260. c.Fatal("expected DirNotExists error, but got nil instead")
  261. }
  262. if !isCpDirNotExist(err) {
  263. c.Fatalf("expected DirNotExists error, but got %T: %s", err, err)
  264. }
  265. }
  266. // C. SRC specifies a file and DST exists as a file. This should overwrite
  267. // the file at DST with the contents of the source file.
  268. func (s *DockerSuite) TestCpToCaseC(c *check.C) {
  269. testRequires(c, DaemonIsLinux)
  270. cID := makeTestContainer(c, testContainerOptions{
  271. addContent: true, workDir: "/root",
  272. command: makeCatFileCommand("file2"),
  273. })
  274. defer deleteContainer(cID)
  275. tmpDir := getTestDir(c, "test-cp-to-case-c")
  276. defer os.RemoveAll(tmpDir)
  277. makeTestContentInDir(c, tmpDir)
  278. srcPath := cpPath(tmpDir, "file1")
  279. dstPath := containerCpPath(cID, "/root/file2")
  280. // Ensure the container's file starts with the original content.
  281. if err := containerStartOutputEquals(c, cID, "file2\n"); err != nil {
  282. c.Fatal(err)
  283. }
  284. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  285. c.Fatalf("unexpected error %T: %s", err, err)
  286. }
  287. // Should now contain file1's contents.
  288. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  289. c.Fatal(err)
  290. }
  291. }
  292. // D. SRC specifies a file and DST exists as a directory. This should place
  293. // a copy of the source file inside it using the basename from SRC. Ensure
  294. // this works whether DST has a trailing path separator or not.
  295. func (s *DockerSuite) TestCpToCaseD(c *check.C) {
  296. testRequires(c, DaemonIsLinux)
  297. cID := makeTestContainer(c, testContainerOptions{
  298. addContent: true,
  299. command: makeCatFileCommand("/dir1/file1"),
  300. })
  301. defer deleteContainer(cID)
  302. tmpDir := getTestDir(c, "test-cp-to-case-d")
  303. defer os.RemoveAll(tmpDir)
  304. makeTestContentInDir(c, tmpDir)
  305. srcPath := cpPath(tmpDir, "file1")
  306. dstDir := containerCpPath(cID, "dir1")
  307. // Ensure that dstPath doesn't exist.
  308. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  309. c.Fatal(err)
  310. }
  311. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  312. c.Fatalf("unexpected error %T: %s", err, err)
  313. }
  314. // Should now contain file1's contents.
  315. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  316. c.Fatal(err)
  317. }
  318. // Now try again but using a trailing path separator for dstDir.
  319. // Make new destination container.
  320. cID = makeTestContainer(c, testContainerOptions{
  321. addContent: true,
  322. command: makeCatFileCommand("/dir1/file1"),
  323. })
  324. defer deleteContainer(cID)
  325. dstDir = containerCpPathTrailingSep(cID, "dir1")
  326. // Ensure that dstPath doesn't exist.
  327. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  328. c.Fatal(err)
  329. }
  330. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  331. c.Fatalf("unexpected error %T: %s", err, err)
  332. }
  333. // Should now contain file1's contents.
  334. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  335. c.Fatal(err)
  336. }
  337. }
  338. // E. SRC specifies a directory and DST does not exist. This should create a
  339. // directory at DST and copy the contents of the SRC directory into the DST
  340. // directory. Ensure this works whether DST has a trailing path separator or
  341. // not.
  342. func (s *DockerSuite) TestCpToCaseE(c *check.C) {
  343. testRequires(c, DaemonIsLinux)
  344. cID := makeTestContainer(c, testContainerOptions{
  345. command: makeCatFileCommand("/testDir/file1-1"),
  346. })
  347. defer deleteContainer(cID)
  348. tmpDir := getTestDir(c, "test-cp-to-case-e")
  349. defer os.RemoveAll(tmpDir)
  350. makeTestContentInDir(c, tmpDir)
  351. srcDir := cpPath(tmpDir, "dir1")
  352. dstDir := containerCpPath(cID, "testDir")
  353. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  354. c.Fatalf("unexpected error %T: %s", err, err)
  355. }
  356. // Should now contain file1-1's contents.
  357. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  358. c.Fatal(err)
  359. }
  360. // Now try again but using a trailing path separator for dstDir.
  361. // Make new destination container.
  362. cID = makeTestContainer(c, testContainerOptions{
  363. command: makeCatFileCommand("/testDir/file1-1"),
  364. })
  365. defer deleteContainer(cID)
  366. dstDir = containerCpPathTrailingSep(cID, "testDir")
  367. err := runDockerCp(c, srcDir, dstDir)
  368. if err != nil {
  369. c.Fatalf("unexpected error %T: %s", err, err)
  370. }
  371. // Should now contain file1-1's contents.
  372. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  373. c.Fatal(err)
  374. }
  375. }
  376. // F. SRC specifies a directory and DST exists as a file. This should cause an
  377. // error as it is not possible to overwrite a file with a directory.
  378. func (s *DockerSuite) TestCpToCaseF(c *check.C) {
  379. testRequires(c, DaemonIsLinux)
  380. cID := makeTestContainer(c, testContainerOptions{
  381. addContent: true, workDir: "/root",
  382. })
  383. defer deleteContainer(cID)
  384. tmpDir := getTestDir(c, "test-cp-to-case-f")
  385. defer os.RemoveAll(tmpDir)
  386. makeTestContentInDir(c, tmpDir)
  387. srcDir := cpPath(tmpDir, "dir1")
  388. dstFile := containerCpPath(cID, "/root/file1")
  389. err := runDockerCp(c, srcDir, dstFile)
  390. if err == nil {
  391. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  392. }
  393. if !isCpCannotCopyDir(err) {
  394. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  395. }
  396. }
  397. // G. SRC specifies a directory and DST exists as a directory. This should copy
  398. // the SRC directory and all its contents to the DST directory. Ensure this
  399. // works whether DST has a trailing path separator or not.
  400. func (s *DockerSuite) TestCpToCaseG(c *check.C) {
  401. testRequires(c, DaemonIsLinux)
  402. cID := makeTestContainer(c, testContainerOptions{
  403. addContent: true, workDir: "/root",
  404. command: makeCatFileCommand("dir2/dir1/file1-1"),
  405. })
  406. defer deleteContainer(cID)
  407. tmpDir := getTestDir(c, "test-cp-to-case-g")
  408. defer os.RemoveAll(tmpDir)
  409. makeTestContentInDir(c, tmpDir)
  410. srcDir := cpPath(tmpDir, "dir1")
  411. dstDir := containerCpPath(cID, "/root/dir2")
  412. // Ensure that dstPath doesn't exist.
  413. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  414. c.Fatal(err)
  415. }
  416. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  417. c.Fatalf("unexpected error %T: %s", err, err)
  418. }
  419. // Should now contain file1-1's contents.
  420. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  421. c.Fatal(err)
  422. }
  423. // Now try again but using a trailing path separator for dstDir.
  424. // Make new destination container.
  425. cID = makeTestContainer(c, testContainerOptions{
  426. addContent: true,
  427. command: makeCatFileCommand("/dir2/dir1/file1-1"),
  428. })
  429. defer deleteContainer(cID)
  430. dstDir = containerCpPathTrailingSep(cID, "/dir2")
  431. // Ensure that dstPath doesn't exist.
  432. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  433. c.Fatal(err)
  434. }
  435. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  436. c.Fatalf("unexpected error %T: %s", err, err)
  437. }
  438. // Should now contain file1-1's contents.
  439. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  440. c.Fatal(err)
  441. }
  442. }
  443. // H. SRC specifies a directory's contents only and DST does not exist. This
  444. // should create a directory at DST and copy the contents of the SRC
  445. // directory (but not the directory itself) into the DST directory. Ensure
  446. // this works whether DST has a trailing path separator or not.
  447. func (s *DockerSuite) TestCpToCaseH(c *check.C) {
  448. testRequires(c, DaemonIsLinux)
  449. cID := makeTestContainer(c, testContainerOptions{
  450. command: makeCatFileCommand("/testDir/file1-1"),
  451. })
  452. defer deleteContainer(cID)
  453. tmpDir := getTestDir(c, "test-cp-to-case-h")
  454. defer os.RemoveAll(tmpDir)
  455. makeTestContentInDir(c, tmpDir)
  456. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  457. dstDir := containerCpPath(cID, "testDir")
  458. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  459. c.Fatalf("unexpected error %T: %s", err, err)
  460. }
  461. // Should now contain file1-1's contents.
  462. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  463. c.Fatal(err)
  464. }
  465. // Now try again but using a trailing path separator for dstDir.
  466. // Make new destination container.
  467. cID = makeTestContainer(c, testContainerOptions{
  468. command: makeCatFileCommand("/testDir/file1-1"),
  469. })
  470. defer deleteContainer(cID)
  471. dstDir = containerCpPathTrailingSep(cID, "testDir")
  472. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  473. c.Fatalf("unexpected error %T: %s", err, err)
  474. }
  475. // Should now contain file1-1's contents.
  476. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  477. c.Fatal(err)
  478. }
  479. }
  480. // I. SRC specifies a directory's contents only and DST exists as a file. This
  481. // should cause an error as it is not possible to overwrite a file with a
  482. // directory.
  483. func (s *DockerSuite) TestCpToCaseI(c *check.C) {
  484. testRequires(c, DaemonIsLinux)
  485. cID := makeTestContainer(c, testContainerOptions{
  486. addContent: true, workDir: "/root",
  487. })
  488. defer deleteContainer(cID)
  489. tmpDir := getTestDir(c, "test-cp-to-case-i")
  490. defer os.RemoveAll(tmpDir)
  491. makeTestContentInDir(c, tmpDir)
  492. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  493. dstFile := containerCpPath(cID, "/root/file1")
  494. err := runDockerCp(c, srcDir, dstFile)
  495. if err == nil {
  496. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  497. }
  498. if !isCpCannotCopyDir(err) {
  499. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  500. }
  501. }
  502. // J. SRC specifies a directory's contents only and DST exists as a directory.
  503. // This should copy the contents of the SRC directory (but not the directory
  504. // itself) into the DST directory. Ensure this works whether DST has a
  505. // trailing path separator or not.
  506. func (s *DockerSuite) TestCpToCaseJ(c *check.C) {
  507. testRequires(c, DaemonIsLinux)
  508. cID := makeTestContainer(c, testContainerOptions{
  509. addContent: true, workDir: "/root",
  510. command: makeCatFileCommand("/dir2/file1-1"),
  511. })
  512. defer deleteContainer(cID)
  513. tmpDir := getTestDir(c, "test-cp-to-case-j")
  514. defer os.RemoveAll(tmpDir)
  515. makeTestContentInDir(c, tmpDir)
  516. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  517. dstDir := containerCpPath(cID, "/dir2")
  518. // Ensure that dstPath doesn't exist.
  519. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  520. c.Fatal(err)
  521. }
  522. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  523. c.Fatalf("unexpected error %T: %s", err, err)
  524. }
  525. // Should now contain file1-1's contents.
  526. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  527. c.Fatal(err)
  528. }
  529. // Now try again but using a trailing path separator for dstDir.
  530. // Make new destination container.
  531. cID = makeTestContainer(c, testContainerOptions{
  532. command: makeCatFileCommand("/dir2/file1-1"),
  533. })
  534. defer deleteContainer(cID)
  535. dstDir = containerCpPathTrailingSep(cID, "/dir2")
  536. // Ensure that dstPath doesn't exist.
  537. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  538. c.Fatal(err)
  539. }
  540. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  541. c.Fatalf("unexpected error %T: %s", err, err)
  542. }
  543. // Should now contain file1-1's contents.
  544. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  545. c.Fatal(err)
  546. }
  547. }
  548. // The `docker cp` command should also ensure that you cannot
  549. // write to a container rootfs that is marked as read-only.
  550. func (s *DockerSuite) TestCpToErrReadOnlyRootfs(c *check.C) {
  551. // --read-only + userns has remount issues
  552. testRequires(c, DaemonIsLinux, NotUserNamespace)
  553. tmpDir := getTestDir(c, "test-cp-to-err-read-only-rootfs")
  554. defer os.RemoveAll(tmpDir)
  555. makeTestContentInDir(c, tmpDir)
  556. cID := makeTestContainer(c, testContainerOptions{
  557. readOnly: true, workDir: "/root",
  558. command: makeCatFileCommand("shouldNotExist"),
  559. })
  560. defer deleteContainer(cID)
  561. srcPath := cpPath(tmpDir, "file1")
  562. dstPath := containerCpPath(cID, "/root/shouldNotExist")
  563. err := runDockerCp(c, srcPath, dstPath)
  564. if err == nil {
  565. c.Fatal("expected ErrContainerRootfsReadonly error, but got nil instead")
  566. }
  567. if !isCpCannotCopyReadOnly(err) {
  568. c.Fatalf("expected ErrContainerRootfsReadonly error, but got %T: %s", err, err)
  569. }
  570. // Ensure that dstPath doesn't exist.
  571. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  572. c.Fatal(err)
  573. }
  574. }
  575. // The `docker cp` command should also ensure that you
  576. // cannot write to a volume that is mounted as read-only.
  577. func (s *DockerSuite) TestCpToErrReadOnlyVolume(c *check.C) {
  578. // --read-only + userns has remount issues
  579. testRequires(c, DaemonIsLinux, NotUserNamespace)
  580. tmpDir := getTestDir(c, "test-cp-to-err-read-only-volume")
  581. defer os.RemoveAll(tmpDir)
  582. makeTestContentInDir(c, tmpDir)
  583. cID := makeTestContainer(c, testContainerOptions{
  584. volumes: defaultVolumes(tmpDir), workDir: "/root",
  585. command: makeCatFileCommand("/vol_ro/shouldNotExist"),
  586. })
  587. defer deleteContainer(cID)
  588. srcPath := cpPath(tmpDir, "file1")
  589. dstPath := containerCpPath(cID, "/vol_ro/shouldNotExist")
  590. err := runDockerCp(c, srcPath, dstPath)
  591. if err == nil {
  592. c.Fatal("expected ErrVolumeReadonly error, but got nil instead")
  593. }
  594. if !isCpCannotCopyReadOnly(err) {
  595. c.Fatalf("expected ErrVolumeReadonly error, but got %T: %s", err, err)
  596. }
  597. // Ensure that dstPath doesn't exist.
  598. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  599. c.Fatal(err)
  600. }
  601. }