docker_cli_cp_from_container_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. cID := makeTestContainer(c, testContainerOptions{})
  20. defer deleteContainer(cID)
  21. tmpDir := getTestDir(c, "test-cp-from-err-src-not-exists")
  22. defer os.RemoveAll(tmpDir)
  23. err := runDockerCp(c, containerCpPath(cID, "file1"), tmpDir)
  24. if err == nil {
  25. c.Fatal("expected IsNotExist error, but got nil instead")
  26. }
  27. if !isCpNotExist(err) {
  28. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  29. }
  30. }
  31. // Test for error when SRC ends in a trailing
  32. // path separator but it exists as a file.
  33. func (s *DockerSuite) TestCpFromErrSrcNotDir(c *check.C) {
  34. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  35. defer deleteContainer(cID)
  36. tmpDir := getTestDir(c, "test-cp-from-err-src-not-dir")
  37. defer os.RemoveAll(tmpDir)
  38. err := runDockerCp(c, containerCpPathTrailingSep(cID, "file1"), tmpDir)
  39. if err == nil {
  40. c.Fatal("expected IsNotDir error, but got nil instead")
  41. }
  42. if !isCpNotDir(err) {
  43. c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  44. }
  45. }
  46. // Test for error when SRC is a valid file or directory,
  47. // bu the DST parent directory does not exist.
  48. func (s *DockerSuite) TestCpFromErrDstParentNotExists(c *check.C) {
  49. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  50. defer deleteContainer(cID)
  51. tmpDir := getTestDir(c, "test-cp-from-err-dst-parent-not-exists")
  52. defer os.RemoveAll(tmpDir)
  53. makeTestContentInDir(c, tmpDir)
  54. // Try with a file source.
  55. srcPath := containerCpPath(cID, "/file1")
  56. dstPath := cpPath(tmpDir, "notExists", "file1")
  57. err := runDockerCp(c, srcPath, dstPath)
  58. if err == nil {
  59. c.Fatal("expected IsNotExist error, but got nil instead")
  60. }
  61. if !isCpNotExist(err) {
  62. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  63. }
  64. // Try with a directory source.
  65. srcPath = containerCpPath(cID, "/dir1")
  66. if err := runDockerCp(c, srcPath, dstPath); err == nil {
  67. c.Fatal("expected IsNotExist error, but got nil instead")
  68. }
  69. if !isCpNotExist(err) {
  70. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  71. }
  72. }
  73. // Test for error when DST ends in a trailing
  74. // path separator but exists as a file.
  75. func (s *DockerSuite) TestCpFromErrDstNotDir(c *check.C) {
  76. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  77. defer deleteContainer(cID)
  78. tmpDir := getTestDir(c, "test-cp-from-err-dst-not-dir")
  79. defer os.RemoveAll(tmpDir)
  80. makeTestContentInDir(c, tmpDir)
  81. // Try with a file source.
  82. srcPath := containerCpPath(cID, "/file1")
  83. dstPath := cpPathTrailingSep(tmpDir, "file1")
  84. err := runDockerCp(c, srcPath, dstPath)
  85. if err == nil {
  86. c.Fatal("expected IsNotDir error, but got nil instead")
  87. }
  88. if !isCpNotDir(err) {
  89. c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  90. }
  91. // Try with a directory source.
  92. srcPath = containerCpPath(cID, "/dir1")
  93. if err := runDockerCp(c, srcPath, dstPath); err == nil {
  94. c.Fatal("expected IsNotDir error, but got nil instead")
  95. }
  96. if !isCpNotDir(err) {
  97. c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  98. }
  99. }
  100. // Possibilities are reduced to the remaining 10 cases:
  101. //
  102. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  103. // ===================================================================================================
  104. // A | no | - | no | - | no | create file
  105. // B | no | - | no | - | yes | error
  106. // C | no | - | yes | no | - | overwrite file
  107. // D | no | - | yes | yes | - | create file in dst dir
  108. // E | yes | no | no | - | - | create dir, copy contents
  109. // F | yes | no | yes | no | - | error
  110. // G | yes | no | yes | yes | - | copy dir and contents
  111. // H | yes | yes | no | - | - | create dir, copy contents
  112. // I | yes | yes | yes | no | - | error
  113. // J | yes | yes | yes | yes | - | copy dir contents
  114. //
  115. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  116. // exist. This should create a file with the name DST and copy the
  117. // contents of the source file into it.
  118. func (s *DockerSuite) TestCpFromCaseA(c *check.C) {
  119. cID := makeTestContainer(c, testContainerOptions{
  120. addContent: true, workDir: "/root",
  121. })
  122. defer deleteContainer(cID)
  123. tmpDir := getTestDir(c, "test-cp-from-case-a")
  124. defer os.RemoveAll(tmpDir)
  125. srcPath := containerCpPath(cID, "/root/file1")
  126. dstPath := cpPath(tmpDir, "itWorks.txt")
  127. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  128. c.Fatalf("unexpected error %T: %s", err, err)
  129. }
  130. if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
  131. c.Fatal(err)
  132. }
  133. }
  134. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  135. // exist. This should cause an error because the copy operation cannot
  136. // create a directory when copying a single file.
  137. func (s *DockerSuite) TestCpFromCaseB(c *check.C) {
  138. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  139. defer deleteContainer(cID)
  140. tmpDir := getTestDir(c, "test-cp-from-case-b")
  141. defer os.RemoveAll(tmpDir)
  142. srcPath := containerCpPath(cID, "/file1")
  143. dstDir := cpPathTrailingSep(tmpDir, "testDir")
  144. err := runDockerCp(c, srcPath, dstDir)
  145. if err == nil {
  146. c.Fatal("expected DirNotExists error, but got nil instead")
  147. }
  148. if !isCpDirNotExist(err) {
  149. c.Fatalf("expected DirNotExists error, but got %T: %s", err, err)
  150. }
  151. }
  152. // C. SRC specifies a file and DST exists as a file. This should overwrite
  153. // the file at DST with the contents of the source file.
  154. func (s *DockerSuite) TestCpFromCaseC(c *check.C) {
  155. cID := makeTestContainer(c, testContainerOptions{
  156. addContent: true, workDir: "/root",
  157. })
  158. defer deleteContainer(cID)
  159. tmpDir := getTestDir(c, "test-cp-from-case-c")
  160. defer os.RemoveAll(tmpDir)
  161. makeTestContentInDir(c, tmpDir)
  162. srcPath := containerCpPath(cID, "/root/file1")
  163. dstPath := cpPath(tmpDir, "file2")
  164. // Ensure the local file starts with different content.
  165. if err := fileContentEquals(c, dstPath, "file2\n"); err != nil {
  166. c.Fatal(err)
  167. }
  168. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  169. c.Fatalf("unexpected error %T: %s", err, err)
  170. }
  171. if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
  172. c.Fatal(err)
  173. }
  174. }
  175. // D. SRC specifies a file and DST exists as a directory. This should place
  176. // a copy of the source file inside it using the basename from SRC. Ensure
  177. // this works whether DST has a trailing path separator or not.
  178. func (s *DockerSuite) TestCpFromCaseD(c *check.C) {
  179. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  180. defer deleteContainer(cID)
  181. tmpDir := getTestDir(c, "test-cp-from-case-d")
  182. defer os.RemoveAll(tmpDir)
  183. makeTestContentInDir(c, tmpDir)
  184. srcPath := containerCpPath(cID, "/file1")
  185. dstDir := cpPath(tmpDir, "dir1")
  186. dstPath := filepath.Join(dstDir, "file1")
  187. // Ensure that dstPath doesn't exist.
  188. if _, err := os.Stat(dstPath); !os.IsNotExist(err) {
  189. c.Fatalf("did not expect dstPath %q to exist", dstPath)
  190. }
  191. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  192. c.Fatalf("unexpected error %T: %s", err, err)
  193. }
  194. if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
  195. c.Fatal(err)
  196. }
  197. // Now try again but using a trailing path separator for dstDir.
  198. if err := os.RemoveAll(dstDir); err != nil {
  199. c.Fatalf("unable to remove dstDir: %s", err)
  200. }
  201. if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  202. c.Fatalf("unable to make dstDir: %s", err)
  203. }
  204. dstDir = cpPathTrailingSep(tmpDir, "dir1")
  205. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  206. c.Fatalf("unexpected error %T: %s", err, err)
  207. }
  208. if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
  209. c.Fatal(err)
  210. }
  211. }
  212. // E. SRC specifies a directory and DST does not exist. This should create a
  213. // directory at DST and copy the contents of the SRC directory into the DST
  214. // directory. Ensure this works whether DST has a trailing path separator or
  215. // not.
  216. func (s *DockerSuite) TestCpFromCaseE(c *check.C) {
  217. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  218. defer deleteContainer(cID)
  219. tmpDir := getTestDir(c, "test-cp-from-case-e")
  220. defer os.RemoveAll(tmpDir)
  221. srcDir := containerCpPath(cID, "dir1")
  222. dstDir := cpPath(tmpDir, "testDir")
  223. dstPath := filepath.Join(dstDir, "file1-1")
  224. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  225. c.Fatalf("unexpected error %T: %s", err, err)
  226. }
  227. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  228. c.Fatal(err)
  229. }
  230. // Now try again but using a trailing path separator for dstDir.
  231. if err := os.RemoveAll(dstDir); err != nil {
  232. c.Fatalf("unable to remove dstDir: %s", err)
  233. }
  234. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  235. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  236. c.Fatalf("unexpected error %T: %s", err, err)
  237. }
  238. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  239. c.Fatal(err)
  240. }
  241. }
  242. // F. SRC specifies a directory and DST exists as a file. This should cause an
  243. // error as it is not possible to overwrite a file with a directory.
  244. func (s *DockerSuite) TestCpFromCaseF(c *check.C) {
  245. cID := makeTestContainer(c, testContainerOptions{
  246. addContent: true, workDir: "/root",
  247. })
  248. defer deleteContainer(cID)
  249. tmpDir := getTestDir(c, "test-cp-from-case-f")
  250. defer os.RemoveAll(tmpDir)
  251. makeTestContentInDir(c, tmpDir)
  252. srcDir := containerCpPath(cID, "/root/dir1")
  253. dstFile := cpPath(tmpDir, "file1")
  254. err := runDockerCp(c, srcDir, dstFile)
  255. if err == nil {
  256. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  257. }
  258. if !isCpCannotCopyDir(err) {
  259. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  260. }
  261. }
  262. // G. SRC specifies a directory and DST exists as a directory. This should copy
  263. // the SRC directory and all its contents to the DST directory. Ensure this
  264. // works whether DST has a trailing path separator or not.
  265. func (s *DockerSuite) TestCpFromCaseG(c *check.C) {
  266. cID := makeTestContainer(c, testContainerOptions{
  267. addContent: true, workDir: "/root",
  268. })
  269. defer deleteContainer(cID)
  270. tmpDir := getTestDir(c, "test-cp-from-case-g")
  271. defer os.RemoveAll(tmpDir)
  272. makeTestContentInDir(c, tmpDir)
  273. srcDir := containerCpPath(cID, "/root/dir1")
  274. dstDir := cpPath(tmpDir, "dir2")
  275. resultDir := filepath.Join(dstDir, "dir1")
  276. dstPath := filepath.Join(resultDir, "file1-1")
  277. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  278. c.Fatalf("unexpected error %T: %s", err, err)
  279. }
  280. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  281. c.Fatal(err)
  282. }
  283. // Now try again but using a trailing path separator for dstDir.
  284. if err := os.RemoveAll(dstDir); err != nil {
  285. c.Fatalf("unable to remove dstDir: %s", err)
  286. }
  287. if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  288. c.Fatalf("unable to make dstDir: %s", err)
  289. }
  290. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  291. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  292. c.Fatalf("unexpected error %T: %s", err, err)
  293. }
  294. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  295. c.Fatal(err)
  296. }
  297. }
  298. // H. SRC specifies a directory's contents only and DST does not exist. This
  299. // should create a directory at DST and copy the contents of the SRC
  300. // directory (but not the directory itself) into the DST directory. Ensure
  301. // this works whether DST has a trailing path separator or not.
  302. func (s *DockerSuite) TestCpFromCaseH(c *check.C) {
  303. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  304. defer deleteContainer(cID)
  305. tmpDir := getTestDir(c, "test-cp-from-case-h")
  306. defer os.RemoveAll(tmpDir)
  307. srcDir := containerCpPathTrailingSep(cID, "dir1") + "."
  308. dstDir := cpPath(tmpDir, "testDir")
  309. dstPath := filepath.Join(dstDir, "file1-1")
  310. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  311. c.Fatalf("unexpected error %T: %s", err, err)
  312. }
  313. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  314. c.Fatal(err)
  315. }
  316. // Now try again but using a trailing path separator for dstDir.
  317. if err := os.RemoveAll(dstDir); err != nil {
  318. c.Fatalf("unable to remove resultDir: %s", err)
  319. }
  320. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  321. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  322. c.Fatalf("unexpected error %T: %s", err, err)
  323. }
  324. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  325. c.Fatal(err)
  326. }
  327. }
  328. // I. SRC specifies a direcotry's contents only and DST exists as a file. This
  329. // should cause an error as it is not possible to overwrite a file with a
  330. // directory.
  331. func (s *DockerSuite) TestCpFromCaseI(c *check.C) {
  332. cID := makeTestContainer(c, testContainerOptions{
  333. addContent: true, workDir: "/root",
  334. })
  335. defer deleteContainer(cID)
  336. tmpDir := getTestDir(c, "test-cp-from-case-i")
  337. defer os.RemoveAll(tmpDir)
  338. makeTestContentInDir(c, tmpDir)
  339. srcDir := containerCpPathTrailingSep(cID, "/root/dir1") + "."
  340. dstFile := cpPath(tmpDir, "file1")
  341. err := runDockerCp(c, srcDir, dstFile)
  342. if err == nil {
  343. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  344. }
  345. if !isCpCannotCopyDir(err) {
  346. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  347. }
  348. }
  349. // J. SRC specifies a directory's contents only and DST exists as a directory.
  350. // This should copy the contents of the SRC directory (but not the directory
  351. // itself) into the DST directory. Ensure this works whether DST has a
  352. // trailing path separator or not.
  353. func (s *DockerSuite) TestCpFromCaseJ(c *check.C) {
  354. cID := makeTestContainer(c, testContainerOptions{
  355. addContent: true, workDir: "/root",
  356. })
  357. defer deleteContainer(cID)
  358. tmpDir := getTestDir(c, "test-cp-from-case-j")
  359. defer os.RemoveAll(tmpDir)
  360. makeTestContentInDir(c, tmpDir)
  361. srcDir := containerCpPathTrailingSep(cID, "/root/dir1") + "."
  362. dstDir := cpPath(tmpDir, "dir2")
  363. dstPath := filepath.Join(dstDir, "file1-1")
  364. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  365. c.Fatalf("unexpected error %T: %s", err, err)
  366. }
  367. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  368. c.Fatal(err)
  369. }
  370. // Now try again but using a trailing path separator for dstDir.
  371. if err := os.RemoveAll(dstDir); err != nil {
  372. c.Fatalf("unable to remove dstDir: %s", err)
  373. }
  374. if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  375. c.Fatalf("unable to make dstDir: %s", err)
  376. }
  377. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  378. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  379. c.Fatalf("unexpected error %T: %s", err, err)
  380. }
  381. if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
  382. c.Fatal(err)
  383. }
  384. }