docker_cli_cp_from_container_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/go-check/check"
  6. )
  7. // docker cp CONTAINER:PATH LOCALPATH
  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) TestCpFromErrSrcNotExists(c *check.C) {
  19. testRequires(c, DaemonIsLinux)
  20. cID := makeTestContainer(c, testContainerOptions{})
  21. defer deleteContainer(cID)
  22. tmpDir := getTestDir(c, "test-cp-from-err-src-not-exists")
  23. defer os.RemoveAll(tmpDir)
  24. err := runDockerCp(c, containerCpPath(cID, "file1"), tmpDir)
  25. if err == nil {
  26. c.Fatal("expected IsNotExist error, but got nil instead")
  27. }
  28. if !isCpNotExist(err) {
  29. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  30. }
  31. }
  32. // Test for error when SRC ends in a trailing
  33. // path separator but it exists as a file.
  34. func (s *DockerSuite) TestCpFromErrSrcNotDir(c *check.C) {
  35. testRequires(c, DaemonIsLinux)
  36. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  37. defer deleteContainer(cID)
  38. tmpDir := getTestDir(c, "test-cp-from-err-src-not-dir")
  39. defer os.RemoveAll(tmpDir)
  40. err := runDockerCp(c, containerCpPathTrailingSep(cID, "file1"), tmpDir)
  41. if err == nil {
  42. c.Fatal("expected IsNotDir error, but got nil instead")
  43. }
  44. if !isCpNotDir(err) {
  45. c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  46. }
  47. }
  48. // Test for error when SRC is a valid file or directory,
  49. // bu the DST parent directory does not exist.
  50. func (s *DockerSuite) TestCpFromErrDstParentNotExists(c *check.C) {
  51. testRequires(c, DaemonIsLinux)
  52. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  53. defer deleteContainer(cID)
  54. tmpDir := getTestDir(c, "test-cp-from-err-dst-parent-not-exists")
  55. defer os.RemoveAll(tmpDir)
  56. makeTestContentInDir(c, tmpDir)
  57. // Try with a file source.
  58. srcPath := containerCpPath(cID, "/file1")
  59. dstPath := cpPath(tmpDir, "notExists", "file1")
  60. err := runDockerCp(c, srcPath, dstPath)
  61. if err == nil {
  62. c.Fatal("expected IsNotExist error, but got nil instead")
  63. }
  64. if !isCpNotExist(err) {
  65. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  66. }
  67. // Try with a directory source.
  68. srcPath = containerCpPath(cID, "/dir1")
  69. if err := runDockerCp(c, srcPath, dstPath); err == nil {
  70. c.Fatal("expected IsNotExist error, but got nil instead")
  71. }
  72. if !isCpNotExist(err) {
  73. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  74. }
  75. }
  76. // Test for error when DST ends in a trailing
  77. // path separator but exists as a file.
  78. func (s *DockerSuite) TestCpFromErrDstNotDir(c *check.C) {
  79. testRequires(c, DaemonIsLinux)
  80. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  81. defer deleteContainer(cID)
  82. tmpDir := getTestDir(c, "test-cp-from-err-dst-not-dir")
  83. defer os.RemoveAll(tmpDir)
  84. makeTestContentInDir(c, tmpDir)
  85. // Try with a file source.
  86. srcPath := containerCpPath(cID, "/file1")
  87. dstPath := cpPathTrailingSep(tmpDir, "file1")
  88. err := runDockerCp(c, srcPath, dstPath)
  89. if err == nil {
  90. c.Fatal("expected IsNotDir error, but got nil instead")
  91. }
  92. if !isCpNotDir(err) {
  93. c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  94. }
  95. // Try with a directory source.
  96. srcPath = containerCpPath(cID, "/dir1")
  97. if err := runDockerCp(c, srcPath, dstPath); err == nil {
  98. c.Fatal("expected IsNotDir error, but got nil instead")
  99. }
  100. if !isCpNotDir(err) {
  101. c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  102. }
  103. }
  104. // Check that copying from a container to a local symlink copies to the symlink
  105. // target and does not overwrite the local symlink itself.
  106. func (s *DockerSuite) TestCpFromSymlinkDestination(c *check.C) {
  107. testRequires(c, DaemonIsLinux)
  108. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  109. defer deleteContainer(cID)
  110. tmpDir := getTestDir(c, "test-cp-from-err-dst-not-dir")
  111. defer os.RemoveAll(tmpDir)
  112. makeTestContentInDir(c, tmpDir)
  113. // First, copy a file from the container to a symlink to a file. This
  114. // should overwrite the symlink target contents with the source contents.
  115. srcPath := containerCpPath(cID, "/file2")
  116. dstPath := cpPath(tmpDir, "symlinkToFile1")
  117. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  118. c.Fatalf("unexpected error %T: %s", err, err)
  119. }
  120. // The symlink should not have been modified.
  121. if err := symlinkTargetEquals(c, dstPath, "file1"); err != nil {
  122. c.Fatal(err)
  123. }
  124. // The file should have the contents of "file2" now.
  125. if err := fileContentEquals(c, cpPath(tmpDir, "file1"), "file2\n"); err != nil {
  126. c.Fatal(err)
  127. }
  128. // Next, copy a file from the container to a symlink to a directory. This
  129. // should copy the file into the symlink target directory.
  130. dstPath = cpPath(tmpDir, "symlinkToDir1")
  131. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  132. c.Fatalf("unexpected error %T: %s", err, err)
  133. }
  134. // The symlink should not have been modified.
  135. if err := symlinkTargetEquals(c, dstPath, "dir1"); err != nil {
  136. c.Fatal(err)
  137. }
  138. // The file should have the contents of "file2" now.
  139. if err := fileContentEquals(c, cpPath(tmpDir, "file2"), "file2\n"); err != nil {
  140. c.Fatal(err)
  141. }
  142. // Next, copy a file from the container to a symlink to a file that does
  143. // not exist (a broken symlink). This should create the target file with
  144. // the contents of the source file.
  145. dstPath = cpPath(tmpDir, "brokenSymlinkToFileX")
  146. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  147. c.Fatalf("unexpected error %T: %s", err, err)
  148. }
  149. // The symlink should not have been modified.
  150. if err := symlinkTargetEquals(c, dstPath, "fileX"); err != nil {
  151. c.Fatal(err)
  152. }
  153. // The file should have the contents of "file2" now.
  154. if err := fileContentEquals(c, cpPath(tmpDir, "fileX"), "file2\n"); err != nil {
  155. c.Fatal(err)
  156. }
  157. // Next, copy a directory from the container to a symlink to a local
  158. // directory. This should copy the directory into the symlink target
  159. // directory and not modify the symlink.
  160. srcPath = containerCpPath(cID, "/dir2")
  161. dstPath = cpPath(tmpDir, "symlinkToDir1")
  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, dstPath, "dir1"); err != nil {
  167. c.Fatal(err)
  168. }
  169. // The directory should now contain a copy of "dir2".
  170. if err := fileContentEquals(c, cpPath(tmpDir, "dir1/dir2/file2-1"), "file2-1\n"); err != nil {
  171. c.Fatal(err)
  172. }
  173. // Next, copy a directory from the container to a symlink to a local
  174. // directory that does not exist (a broken symlink). This should create
  175. // the target as a directory with the contents of the source directory. It
  176. // should not modify the symlink.
  177. dstPath = cpPath(tmpDir, "brokenSymlinkToDirX")
  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, dstPath, "dirX"); err != nil {
  183. c.Fatal(err)
  184. }
  185. // The "dirX" directory should now be a copy of "dir2".
  186. if err := fileContentEquals(c, cpPath(tmpDir, "dirX/file2-1"), "file2-1\n"); err != nil {
  187. c.Fatal(err)
  188. }
  189. }
  190. // Possibilities are reduced to the remaining 10 cases:
  191. //
  192. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  193. // ===================================================================================================
  194. // A | no | - | no | - | no | create file
  195. // B | no | - | no | - | yes | error
  196. // C | no | - | yes | no | - | overwrite file
  197. // D | no | - | yes | yes | - | create file in dst dir
  198. // E | yes | no | no | - | - | create dir, copy contents
  199. // F | yes | no | yes | no | - | error
  200. // G | yes | no | yes | yes | - | copy dir and contents
  201. // H | yes | yes | no | - | - | create dir, copy contents
  202. // I | yes | yes | yes | no | - | error
  203. // J | yes | yes | yes | yes | - | copy dir contents
  204. //
  205. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  206. // exist. This should create a file with the name DST and copy the
  207. // contents of the source file into it.
  208. func (s *DockerSuite) TestCpFromCaseA(c *check.C) {
  209. testRequires(c, DaemonIsLinux)
  210. cID := makeTestContainer(c, testContainerOptions{
  211. addContent: true, workDir: "/root",
  212. })
  213. defer deleteContainer(cID)
  214. tmpDir := getTestDir(c, "test-cp-from-case-a")
  215. defer os.RemoveAll(tmpDir)
  216. srcPath := containerCpPath(cID, "/root/file1")
  217. dstPath := cpPath(tmpDir, "itWorks.txt")
  218. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  219. c.Fatalf("unexpected error %T: %s", err, err)
  220. }
  221. if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
  222. c.Fatal(err)
  223. }
  224. }
  225. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  226. // exist. This should cause an error because the copy operation cannot
  227. // create a directory when copying a single file.
  228. func (s *DockerSuite) TestCpFromCaseB(c *check.C) {
  229. testRequires(c, DaemonIsLinux)
  230. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  231. defer deleteContainer(cID)
  232. tmpDir := getTestDir(c, "test-cp-from-case-b")
  233. defer os.RemoveAll(tmpDir)
  234. srcPath := containerCpPath(cID, "/file1")
  235. dstDir := cpPathTrailingSep(tmpDir, "testDir")
  236. err := runDockerCp(c, srcPath, dstDir)
  237. if err == nil {
  238. c.Fatal("expected DirNotExists error, but got nil instead")
  239. }
  240. if !isCpDirNotExist(err) {
  241. c.Fatalf("expected DirNotExists error, but got %T: %s", err, err)
  242. }
  243. }
  244. // C. SRC specifies a file and DST exists as a file. This should overwrite
  245. // the file at DST with the contents of the source file.
  246. func (s *DockerSuite) TestCpFromCaseC(c *check.C) {
  247. testRequires(c, DaemonIsLinux)
  248. cID := makeTestContainer(c, testContainerOptions{
  249. addContent: true, workDir: "/root",
  250. })
  251. defer deleteContainer(cID)
  252. tmpDir := getTestDir(c, "test-cp-from-case-c")
  253. defer os.RemoveAll(tmpDir)
  254. makeTestContentInDir(c, tmpDir)
  255. srcPath := containerCpPath(cID, "/root/file1")
  256. dstPath := cpPath(tmpDir, "file2")
  257. // Ensure the local file starts with different content.
  258. if err := fileContentEquals(c, dstPath, "file2\n"); err != nil {
  259. c.Fatal(err)
  260. }
  261. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  262. c.Fatalf("unexpected error %T: %s", err, err)
  263. }
  264. if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
  265. c.Fatal(err)
  266. }
  267. }
  268. // D. SRC specifies a file and DST exists as a directory. This should place
  269. // a copy of the source file inside it using the basename from SRC. Ensure
  270. // this works whether DST has a trailing path separator or not.
  271. func (s *DockerSuite) TestCpFromCaseD(c *check.C) {
  272. testRequires(c, DaemonIsLinux)
  273. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  274. defer deleteContainer(cID)
  275. tmpDir := getTestDir(c, "test-cp-from-case-d")
  276. defer os.RemoveAll(tmpDir)
  277. makeTestContentInDir(c, tmpDir)
  278. srcPath := containerCpPath(cID, "/file1")
  279. dstDir := cpPath(tmpDir, "dir1")
  280. dstPath := filepath.Join(dstDir, "file1")
  281. // Ensure that dstPath doesn't exist.
  282. if _, err := os.Stat(dstPath); !os.IsNotExist(err) {
  283. c.Fatalf("did not expect dstPath %q to exist", dstPath)
  284. }
  285. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  286. c.Fatalf("unexpected error %T: %s", err, err)
  287. }
  288. if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
  289. c.Fatal(err)
  290. }
  291. // Now try again but using a trailing path separator for dstDir.
  292. if err := os.RemoveAll(dstDir); err != nil {
  293. c.Fatalf("unable to remove dstDir: %s", err)
  294. }
  295. if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  296. c.Fatalf("unable to make dstDir: %s", err)
  297. }
  298. dstDir = cpPathTrailingSep(tmpDir, "dir1")
  299. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  300. c.Fatalf("unexpected error %T: %s", err, err)
  301. }
  302. if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
  303. c.Fatal(err)
  304. }
  305. }
  306. // E. SRC specifies a directory and DST does not exist. This should create a
  307. // directory at DST and copy the contents of the SRC directory into the DST
  308. // directory. Ensure this works whether DST has a trailing path separator or
  309. // not.
  310. func (s *DockerSuite) TestCpFromCaseE(c *check.C) {
  311. testRequires(c, DaemonIsLinux)
  312. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  313. defer deleteContainer(cID)
  314. tmpDir := getTestDir(c, "test-cp-from-case-e")
  315. defer os.RemoveAll(tmpDir)
  316. srcDir := containerCpPath(cID, "dir1")
  317. dstDir := cpPath(tmpDir, "testDir")
  318. dstPath := filepath.Join(dstDir, "file1-1")
  319. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  320. c.Fatalf("unexpected error %T: %s", err, err)
  321. }
  322. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  323. c.Fatal(err)
  324. }
  325. // Now try again but using a trailing path separator for dstDir.
  326. if err := os.RemoveAll(dstDir); err != nil {
  327. c.Fatalf("unable to remove dstDir: %s", err)
  328. }
  329. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  330. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  331. c.Fatalf("unexpected error %T: %s", err, err)
  332. }
  333. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  334. c.Fatal(err)
  335. }
  336. }
  337. // F. SRC specifies a directory and DST exists as a file. This should cause an
  338. // error as it is not possible to overwrite a file with a directory.
  339. func (s *DockerSuite) TestCpFromCaseF(c *check.C) {
  340. testRequires(c, DaemonIsLinux)
  341. cID := makeTestContainer(c, testContainerOptions{
  342. addContent: true, workDir: "/root",
  343. })
  344. defer deleteContainer(cID)
  345. tmpDir := getTestDir(c, "test-cp-from-case-f")
  346. defer os.RemoveAll(tmpDir)
  347. makeTestContentInDir(c, tmpDir)
  348. srcDir := containerCpPath(cID, "/root/dir1")
  349. dstFile := cpPath(tmpDir, "file1")
  350. err := runDockerCp(c, srcDir, dstFile)
  351. if err == nil {
  352. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  353. }
  354. if !isCpCannotCopyDir(err) {
  355. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  356. }
  357. }
  358. // G. SRC specifies a directory and DST exists as a directory. This should copy
  359. // the SRC directory and all its contents to the DST directory. Ensure this
  360. // works whether DST has a trailing path separator or not.
  361. func (s *DockerSuite) TestCpFromCaseG(c *check.C) {
  362. testRequires(c, DaemonIsLinux)
  363. cID := makeTestContainer(c, testContainerOptions{
  364. addContent: true, workDir: "/root",
  365. })
  366. defer deleteContainer(cID)
  367. tmpDir := getTestDir(c, "test-cp-from-case-g")
  368. defer os.RemoveAll(tmpDir)
  369. makeTestContentInDir(c, tmpDir)
  370. srcDir := containerCpPath(cID, "/root/dir1")
  371. dstDir := cpPath(tmpDir, "dir2")
  372. resultDir := filepath.Join(dstDir, "dir1")
  373. dstPath := filepath.Join(resultDir, "file1-1")
  374. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  375. c.Fatalf("unexpected error %T: %s", err, err)
  376. }
  377. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  378. c.Fatal(err)
  379. }
  380. // Now try again but using a trailing path separator for dstDir.
  381. if err := os.RemoveAll(dstDir); err != nil {
  382. c.Fatalf("unable to remove dstDir: %s", err)
  383. }
  384. if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  385. c.Fatalf("unable to make dstDir: %s", err)
  386. }
  387. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  388. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  389. c.Fatalf("unexpected error %T: %s", err, err)
  390. }
  391. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  392. c.Fatal(err)
  393. }
  394. }
  395. // H. SRC specifies a directory's contents only and DST does not exist. This
  396. // should create a directory at DST and copy the contents of the SRC
  397. // directory (but not the directory itself) into the DST directory. Ensure
  398. // this works whether DST has a trailing path separator or not.
  399. func (s *DockerSuite) TestCpFromCaseH(c *check.C) {
  400. testRequires(c, DaemonIsLinux)
  401. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  402. defer deleteContainer(cID)
  403. tmpDir := getTestDir(c, "test-cp-from-case-h")
  404. defer os.RemoveAll(tmpDir)
  405. srcDir := containerCpPathTrailingSep(cID, "dir1") + "."
  406. dstDir := cpPath(tmpDir, "testDir")
  407. dstPath := filepath.Join(dstDir, "file1-1")
  408. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  409. c.Fatalf("unexpected error %T: %s", err, err)
  410. }
  411. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  412. c.Fatal(err)
  413. }
  414. // Now try again but using a trailing path separator for dstDir.
  415. if err := os.RemoveAll(dstDir); err != nil {
  416. c.Fatalf("unable to remove resultDir: %s", err)
  417. }
  418. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  419. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  420. c.Fatalf("unexpected error %T: %s", err, err)
  421. }
  422. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  423. c.Fatal(err)
  424. }
  425. }
  426. // I. SRC specifies a directory's contents only and DST exists as a file. This
  427. // should cause an error as it is not possible to overwrite a file with a
  428. // directory.
  429. func (s *DockerSuite) TestCpFromCaseI(c *check.C) {
  430. testRequires(c, DaemonIsLinux)
  431. cID := makeTestContainer(c, testContainerOptions{
  432. addContent: true, workDir: "/root",
  433. })
  434. defer deleteContainer(cID)
  435. tmpDir := getTestDir(c, "test-cp-from-case-i")
  436. defer os.RemoveAll(tmpDir)
  437. makeTestContentInDir(c, tmpDir)
  438. srcDir := containerCpPathTrailingSep(cID, "/root/dir1") + "."
  439. dstFile := cpPath(tmpDir, "file1")
  440. err := runDockerCp(c, srcDir, dstFile)
  441. if err == nil {
  442. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  443. }
  444. if !isCpCannotCopyDir(err) {
  445. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  446. }
  447. }
  448. // J. SRC specifies a directory's contents only and DST exists as a directory.
  449. // This should copy the contents of the SRC directory (but not the directory
  450. // itself) into the DST directory. Ensure this works whether DST has a
  451. // trailing path separator or not.
  452. func (s *DockerSuite) TestCpFromCaseJ(c *check.C) {
  453. testRequires(c, DaemonIsLinux)
  454. cID := makeTestContainer(c, testContainerOptions{
  455. addContent: true, workDir: "/root",
  456. })
  457. defer deleteContainer(cID)
  458. tmpDir := getTestDir(c, "test-cp-from-case-j")
  459. defer os.RemoveAll(tmpDir)
  460. makeTestContentInDir(c, tmpDir)
  461. srcDir := containerCpPathTrailingSep(cID, "/root/dir1") + "."
  462. dstDir := cpPath(tmpDir, "dir2")
  463. dstPath := filepath.Join(dstDir, "file1-1")
  464. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  465. c.Fatalf("unexpected error %T: %s", err, err)
  466. }
  467. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  468. c.Fatal(err)
  469. }
  470. // Now try again but using a trailing path separator for dstDir.
  471. if err := os.RemoveAll(dstDir); err != nil {
  472. c.Fatalf("unable to remove dstDir: %s", err)
  473. }
  474. if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  475. c.Fatalf("unable to make dstDir: %s", err)
  476. }
  477. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  478. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  479. c.Fatalf("unexpected error %T: %s", err, err)
  480. }
  481. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  482. c.Fatal(err)
  483. }
  484. }