docker_cli_cp_to_container_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. testRequires(c, DaemonIsLinux)
  121. testRequires(c, SameHostDaemon) // Requires local volume mount bind.
  122. testVol := getTestDir(c, "test-cp-to-symlink-destination-")
  123. defer os.RemoveAll(testVol)
  124. makeTestContentInDir(c, testVol)
  125. cID := makeTestContainer(c, testContainerOptions{
  126. volumes: defaultVolumes(testVol), // Our bind mount is at /vol2
  127. })
  128. defer deleteContainer(cID)
  129. // First, copy a local file to a symlink to a file in the container. This
  130. // should overwrite the symlink target contents with the source contents.
  131. srcPath := cpPath(testVol, "file2")
  132. dstPath := containerCpPath(cID, "/vol2/symlinkToFile1")
  133. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  134. c.Fatalf("unexpected error %T: %s", err, err)
  135. }
  136. // The symlink should not have been modified.
  137. if err := symlinkTargetEquals(c, cpPath(testVol, "symlinkToFile1"), "file1"); err != nil {
  138. c.Fatal(err)
  139. }
  140. // The file should have the contents of "file2" now.
  141. if err := fileContentEquals(c, cpPath(testVol, "file1"), "file2\n"); err != nil {
  142. c.Fatal(err)
  143. }
  144. // Next, copy a local file to a symlink to a directory in the container.
  145. // This should copy the file into the symlink target directory.
  146. dstPath = containerCpPath(cID, "/vol2/symlinkToDir1")
  147. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  148. c.Fatalf("unexpected error %T: %s", err, err)
  149. }
  150. // The symlink should not have been modified.
  151. if err := symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"); err != nil {
  152. c.Fatal(err)
  153. }
  154. // The file should have the contents of "file2" now.
  155. if err := fileContentEquals(c, cpPath(testVol, "file2"), "file2\n"); err != nil {
  156. c.Fatal(err)
  157. }
  158. // Next, copy a file to a symlink to a file that does not exist (a broken
  159. // symlink) in the container. This should create the target file with the
  160. // contents of the source file.
  161. dstPath = containerCpPath(cID, "/vol2/brokenSymlinkToFileX")
  162. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  163. c.Fatalf("unexpected error %T: %s", err, err)
  164. }
  165. // The symlink should not have been modified.
  166. if err := symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToFileX"), "fileX"); err != nil {
  167. c.Fatal(err)
  168. }
  169. // The file should have the contents of "file2" now.
  170. if err := fileContentEquals(c, cpPath(testVol, "fileX"), "file2\n"); err != nil {
  171. c.Fatal(err)
  172. }
  173. // Next, copy a local directory to a symlink to a directory in the
  174. // container. This should copy the directory into the symlink target
  175. // directory and not modify the symlink.
  176. srcPath = cpPath(testVol, "/dir2")
  177. dstPath = containerCpPath(cID, "/vol2/symlinkToDir1")
  178. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  179. c.Fatalf("unexpected error %T: %s", err, err)
  180. }
  181. // The symlink should not have been modified.
  182. if err := symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1"); err != nil {
  183. c.Fatal(err)
  184. }
  185. // The directory should now contain a copy of "dir2".
  186. if err := fileContentEquals(c, cpPath(testVol, "dir1/dir2/file2-1"), "file2-1\n"); err != nil {
  187. c.Fatal(err)
  188. }
  189. // Next, copy a local directory to a symlink to a local directory that does
  190. // not exist (a broken symlink) in the container. This should create the
  191. // target as a directory with the contents of the source directory. It
  192. // should not modify the symlink.
  193. dstPath = containerCpPath(cID, "/vol2/brokenSymlinkToDirX")
  194. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  195. c.Fatalf("unexpected error %T: %s", err, err)
  196. }
  197. // The symlink should not have been modified.
  198. if err := symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToDirX"), "dirX"); err != nil {
  199. c.Fatal(err)
  200. }
  201. // The "dirX" directory should now be a copy of "dir2".
  202. if err := fileContentEquals(c, cpPath(testVol, "dirX/file2-1"), "file2-1\n"); err != nil {
  203. c.Fatal(err)
  204. }
  205. }
  206. // Possibilities are reduced to the remaining 10 cases:
  207. //
  208. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  209. // ===================================================================================================
  210. // A | no | - | no | - | no | create file
  211. // B | no | - | no | - | yes | error
  212. // C | no | - | yes | no | - | overwrite file
  213. // D | no | - | yes | yes | - | create file in dst dir
  214. // E | yes | no | no | - | - | create dir, copy contents
  215. // F | yes | no | yes | no | - | error
  216. // G | yes | no | yes | yes | - | copy dir and contents
  217. // H | yes | yes | no | - | - | create dir, copy contents
  218. // I | yes | yes | yes | no | - | error
  219. // J | yes | yes | yes | yes | - | copy dir contents
  220. //
  221. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  222. // exist. This should create a file with the name DST and copy the
  223. // contents of the source file into it.
  224. func (s *DockerSuite) TestCpToCaseA(c *check.C) {
  225. testRequires(c, DaemonIsLinux)
  226. cID := makeTestContainer(c, testContainerOptions{
  227. workDir: "/root", command: makeCatFileCommand("itWorks.txt"),
  228. })
  229. defer deleteContainer(cID)
  230. tmpDir := getTestDir(c, "test-cp-to-case-a")
  231. defer os.RemoveAll(tmpDir)
  232. makeTestContentInDir(c, tmpDir)
  233. srcPath := cpPath(tmpDir, "file1")
  234. dstPath := containerCpPath(cID, "/root/itWorks.txt")
  235. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  236. c.Fatalf("unexpected error %T: %s", err, err)
  237. }
  238. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  239. c.Fatal(err)
  240. }
  241. }
  242. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  243. // exist. This should cause an error because the copy operation cannot
  244. // create a directory when copying a single file.
  245. func (s *DockerSuite) TestCpToCaseB(c *check.C) {
  246. testRequires(c, DaemonIsLinux)
  247. cID := makeTestContainer(c, testContainerOptions{
  248. command: makeCatFileCommand("testDir/file1"),
  249. })
  250. defer deleteContainer(cID)
  251. tmpDir := getTestDir(c, "test-cp-to-case-b")
  252. defer os.RemoveAll(tmpDir)
  253. makeTestContentInDir(c, tmpDir)
  254. srcPath := cpPath(tmpDir, "file1")
  255. dstDir := containerCpPathTrailingSep(cID, "testDir")
  256. err := runDockerCp(c, srcPath, dstDir)
  257. if err == nil {
  258. c.Fatal("expected DirNotExists error, but got nil instead")
  259. }
  260. if !isCpDirNotExist(err) {
  261. c.Fatalf("expected DirNotExists error, but got %T: %s", err, err)
  262. }
  263. }
  264. // C. SRC specifies a file and DST exists as a file. This should overwrite
  265. // the file at DST with the contents of the source file.
  266. func (s *DockerSuite) TestCpToCaseC(c *check.C) {
  267. testRequires(c, DaemonIsLinux)
  268. cID := makeTestContainer(c, testContainerOptions{
  269. addContent: true, workDir: "/root",
  270. command: makeCatFileCommand("file2"),
  271. })
  272. defer deleteContainer(cID)
  273. tmpDir := getTestDir(c, "test-cp-to-case-c")
  274. defer os.RemoveAll(tmpDir)
  275. makeTestContentInDir(c, tmpDir)
  276. srcPath := cpPath(tmpDir, "file1")
  277. dstPath := containerCpPath(cID, "/root/file2")
  278. // Ensure the container's file starts with the original content.
  279. if err := containerStartOutputEquals(c, cID, "file2\n"); err != nil {
  280. c.Fatal(err)
  281. }
  282. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  283. c.Fatalf("unexpected error %T: %s", err, err)
  284. }
  285. // Should now contain file1's contents.
  286. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  287. c.Fatal(err)
  288. }
  289. }
  290. // D. SRC specifies a file and DST exists as a directory. This should place
  291. // a copy of the source file inside it using the basename from SRC. Ensure
  292. // this works whether DST has a trailing path separator or not.
  293. func (s *DockerSuite) TestCpToCaseD(c *check.C) {
  294. testRequires(c, DaemonIsLinux)
  295. cID := makeTestContainer(c, testContainerOptions{
  296. addContent: true,
  297. command: makeCatFileCommand("/dir1/file1"),
  298. })
  299. defer deleteContainer(cID)
  300. tmpDir := getTestDir(c, "test-cp-to-case-d")
  301. defer os.RemoveAll(tmpDir)
  302. makeTestContentInDir(c, tmpDir)
  303. srcPath := cpPath(tmpDir, "file1")
  304. dstDir := containerCpPath(cID, "dir1")
  305. // Ensure that dstPath doesn't exist.
  306. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  307. c.Fatal(err)
  308. }
  309. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  310. c.Fatalf("unexpected error %T: %s", err, err)
  311. }
  312. // Should now contain file1's contents.
  313. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  314. c.Fatal(err)
  315. }
  316. // Now try again but using a trailing path separator for dstDir.
  317. // Make new destination container.
  318. cID = makeTestContainer(c, testContainerOptions{
  319. addContent: true,
  320. command: makeCatFileCommand("/dir1/file1"),
  321. })
  322. defer deleteContainer(cID)
  323. dstDir = containerCpPathTrailingSep(cID, "dir1")
  324. // Ensure that dstPath doesn't exist.
  325. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  326. c.Fatal(err)
  327. }
  328. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  329. c.Fatalf("unexpected error %T: %s", err, err)
  330. }
  331. // Should now contain file1's contents.
  332. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  333. c.Fatal(err)
  334. }
  335. }
  336. // E. SRC specifies a directory and DST does not exist. This should create a
  337. // directory at DST and copy the contents of the SRC directory into the DST
  338. // directory. Ensure this works whether DST has a trailing path separator or
  339. // not.
  340. func (s *DockerSuite) TestCpToCaseE(c *check.C) {
  341. testRequires(c, DaemonIsLinux)
  342. cID := makeTestContainer(c, testContainerOptions{
  343. command: makeCatFileCommand("/testDir/file1-1"),
  344. })
  345. defer deleteContainer(cID)
  346. tmpDir := getTestDir(c, "test-cp-to-case-e")
  347. defer os.RemoveAll(tmpDir)
  348. makeTestContentInDir(c, tmpDir)
  349. srcDir := cpPath(tmpDir, "dir1")
  350. dstDir := containerCpPath(cID, "testDir")
  351. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  352. c.Fatalf("unexpected error %T: %s", err, err)
  353. }
  354. // Should now contain file1-1's contents.
  355. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  356. c.Fatal(err)
  357. }
  358. // Now try again but using a trailing path separator for dstDir.
  359. // Make new destination container.
  360. cID = makeTestContainer(c, testContainerOptions{
  361. command: makeCatFileCommand("/testDir/file1-1"),
  362. })
  363. defer deleteContainer(cID)
  364. dstDir = containerCpPathTrailingSep(cID, "testDir")
  365. err := runDockerCp(c, srcDir, dstDir)
  366. if err != nil {
  367. c.Fatalf("unexpected error %T: %s", err, err)
  368. }
  369. // Should now contain file1-1's contents.
  370. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  371. c.Fatal(err)
  372. }
  373. }
  374. // F. SRC specifies a directory and DST exists as a file. This should cause an
  375. // error as it is not possible to overwrite a file with a directory.
  376. func (s *DockerSuite) TestCpToCaseF(c *check.C) {
  377. testRequires(c, DaemonIsLinux)
  378. cID := makeTestContainer(c, testContainerOptions{
  379. addContent: true, workDir: "/root",
  380. })
  381. defer deleteContainer(cID)
  382. tmpDir := getTestDir(c, "test-cp-to-case-f")
  383. defer os.RemoveAll(tmpDir)
  384. makeTestContentInDir(c, tmpDir)
  385. srcDir := cpPath(tmpDir, "dir1")
  386. dstFile := containerCpPath(cID, "/root/file1")
  387. err := runDockerCp(c, srcDir, dstFile)
  388. if err == nil {
  389. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  390. }
  391. if !isCpCannotCopyDir(err) {
  392. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  393. }
  394. }
  395. // G. SRC specifies a directory and DST exists as a directory. This should copy
  396. // the SRC directory and all its contents to the DST directory. Ensure this
  397. // works whether DST has a trailing path separator or not.
  398. func (s *DockerSuite) TestCpToCaseG(c *check.C) {
  399. testRequires(c, DaemonIsLinux)
  400. cID := makeTestContainer(c, testContainerOptions{
  401. addContent: true, workDir: "/root",
  402. command: makeCatFileCommand("dir2/dir1/file1-1"),
  403. })
  404. defer deleteContainer(cID)
  405. tmpDir := getTestDir(c, "test-cp-to-case-g")
  406. defer os.RemoveAll(tmpDir)
  407. makeTestContentInDir(c, tmpDir)
  408. srcDir := cpPath(tmpDir, "dir1")
  409. dstDir := containerCpPath(cID, "/root/dir2")
  410. // Ensure that dstPath doesn't exist.
  411. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  412. c.Fatal(err)
  413. }
  414. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  415. c.Fatalf("unexpected error %T: %s", err, err)
  416. }
  417. // Should now contain file1-1's contents.
  418. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  419. c.Fatal(err)
  420. }
  421. // Now try again but using a trailing path separator for dstDir.
  422. // Make new destination container.
  423. cID = makeTestContainer(c, testContainerOptions{
  424. addContent: true,
  425. command: makeCatFileCommand("/dir2/dir1/file1-1"),
  426. })
  427. defer deleteContainer(cID)
  428. dstDir = containerCpPathTrailingSep(cID, "/dir2")
  429. // Ensure that dstPath doesn't exist.
  430. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  431. c.Fatal(err)
  432. }
  433. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  434. c.Fatalf("unexpected error %T: %s", err, err)
  435. }
  436. // Should now contain file1-1's contents.
  437. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  438. c.Fatal(err)
  439. }
  440. }
  441. // H. SRC specifies a directory's contents only and DST does not exist. This
  442. // should create a directory at DST and copy the contents of the SRC
  443. // directory (but not the directory itself) into the DST directory. Ensure
  444. // this works whether DST has a trailing path separator or not.
  445. func (s *DockerSuite) TestCpToCaseH(c *check.C) {
  446. testRequires(c, DaemonIsLinux)
  447. cID := makeTestContainer(c, testContainerOptions{
  448. command: makeCatFileCommand("/testDir/file1-1"),
  449. })
  450. defer deleteContainer(cID)
  451. tmpDir := getTestDir(c, "test-cp-to-case-h")
  452. defer os.RemoveAll(tmpDir)
  453. makeTestContentInDir(c, tmpDir)
  454. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  455. dstDir := containerCpPath(cID, "testDir")
  456. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  457. c.Fatalf("unexpected error %T: %s", err, err)
  458. }
  459. // Should now contain file1-1's contents.
  460. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  461. c.Fatal(err)
  462. }
  463. // Now try again but using a trailing path separator for dstDir.
  464. // Make new destination container.
  465. cID = makeTestContainer(c, testContainerOptions{
  466. command: makeCatFileCommand("/testDir/file1-1"),
  467. })
  468. defer deleteContainer(cID)
  469. dstDir = containerCpPathTrailingSep(cID, "testDir")
  470. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  471. c.Fatalf("unexpected error %T: %s", err, err)
  472. }
  473. // Should now contain file1-1's contents.
  474. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  475. c.Fatal(err)
  476. }
  477. }
  478. // I. SRC specifies a directory's contents only and DST exists as a file. This
  479. // should cause an error as it is not possible to overwrite a file with a
  480. // directory.
  481. func (s *DockerSuite) TestCpToCaseI(c *check.C) {
  482. testRequires(c, DaemonIsLinux)
  483. cID := makeTestContainer(c, testContainerOptions{
  484. addContent: true, workDir: "/root",
  485. })
  486. defer deleteContainer(cID)
  487. tmpDir := getTestDir(c, "test-cp-to-case-i")
  488. defer os.RemoveAll(tmpDir)
  489. makeTestContentInDir(c, tmpDir)
  490. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  491. dstFile := containerCpPath(cID, "/root/file1")
  492. err := runDockerCp(c, srcDir, dstFile)
  493. if err == nil {
  494. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  495. }
  496. if !isCpCannotCopyDir(err) {
  497. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  498. }
  499. }
  500. // J. SRC specifies a directory's contents only and DST exists as a directory.
  501. // This should copy the contents of the SRC directory (but not the directory
  502. // itself) into the DST directory. Ensure this works whether DST has a
  503. // trailing path separator or not.
  504. func (s *DockerSuite) TestCpToCaseJ(c *check.C) {
  505. testRequires(c, DaemonIsLinux)
  506. cID := makeTestContainer(c, testContainerOptions{
  507. addContent: true, workDir: "/root",
  508. command: makeCatFileCommand("/dir2/file1-1"),
  509. })
  510. defer deleteContainer(cID)
  511. tmpDir := getTestDir(c, "test-cp-to-case-j")
  512. defer os.RemoveAll(tmpDir)
  513. makeTestContentInDir(c, tmpDir)
  514. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  515. dstDir := containerCpPath(cID, "/dir2")
  516. // Ensure that dstPath doesn't exist.
  517. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  518. c.Fatal(err)
  519. }
  520. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  521. c.Fatalf("unexpected error %T: %s", err, err)
  522. }
  523. // Should now contain file1-1's contents.
  524. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  525. c.Fatal(err)
  526. }
  527. // Now try again but using a trailing path separator for dstDir.
  528. // Make new destination container.
  529. cID = makeTestContainer(c, testContainerOptions{
  530. command: makeCatFileCommand("/dir2/file1-1"),
  531. })
  532. defer deleteContainer(cID)
  533. dstDir = containerCpPathTrailingSep(cID, "/dir2")
  534. // Ensure that dstPath doesn't exist.
  535. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  536. c.Fatal(err)
  537. }
  538. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  539. c.Fatalf("unexpected error %T: %s", err, err)
  540. }
  541. // Should now contain file1-1's contents.
  542. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  543. c.Fatal(err)
  544. }
  545. }
  546. // The `docker cp` command should also ensure that you cannot
  547. // write to a container rootfs that is marked as read-only.
  548. func (s *DockerSuite) TestCpToErrReadOnlyRootfs(c *check.C) {
  549. testRequires(c, DaemonIsLinux)
  550. tmpDir := getTestDir(c, "test-cp-to-err-read-only-rootfs")
  551. defer os.RemoveAll(tmpDir)
  552. makeTestContentInDir(c, tmpDir)
  553. cID := makeTestContainer(c, testContainerOptions{
  554. readOnly: true, workDir: "/root",
  555. command: makeCatFileCommand("shouldNotExist"),
  556. })
  557. defer deleteContainer(cID)
  558. srcPath := cpPath(tmpDir, "file1")
  559. dstPath := containerCpPath(cID, "/root/shouldNotExist")
  560. err := runDockerCp(c, srcPath, dstPath)
  561. if err == nil {
  562. c.Fatal("expected ErrContainerRootfsReadonly error, but got nil instead")
  563. }
  564. if !isCpCannotCopyReadOnly(err) {
  565. c.Fatalf("expected ErrContainerRootfsReadonly error, but got %T: %s", err, err)
  566. }
  567. // Ensure that dstPath doesn't exist.
  568. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  569. c.Fatal(err)
  570. }
  571. }
  572. // The `docker cp` command should also ensure that you
  573. // cannot write to a volume that is mounted as read-only.
  574. func (s *DockerSuite) TestCpToErrReadOnlyVolume(c *check.C) {
  575. testRequires(c, DaemonIsLinux)
  576. tmpDir := getTestDir(c, "test-cp-to-err-read-only-volume")
  577. defer os.RemoveAll(tmpDir)
  578. makeTestContentInDir(c, tmpDir)
  579. cID := makeTestContainer(c, testContainerOptions{
  580. volumes: defaultVolumes(tmpDir), workDir: "/root",
  581. command: makeCatFileCommand("/vol_ro/shouldNotExist"),
  582. })
  583. defer deleteContainer(cID)
  584. srcPath := cpPath(tmpDir, "file1")
  585. dstPath := containerCpPath(cID, "/vol_ro/shouldNotExist")
  586. err := runDockerCp(c, srcPath, dstPath)
  587. if err == nil {
  588. c.Fatal("expected ErrVolumeReadonly error, but got nil instead")
  589. }
  590. if !isCpCannotCopyReadOnly(err) {
  591. c.Fatalf("expected ErrVolumeReadonly error, but got %T: %s", err, err)
  592. }
  593. // Ensure that dstPath doesn't exist.
  594. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  595. c.Fatal(err)
  596. }
  597. }