docker_cli_cp_from_container_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/docker/docker/integration-cli/checker"
  6. "github.com/go-check/check"
  7. )
  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. // Check that copying from a container to a local symlink copies to the symlink
  17. // target and does not overwrite the local symlink itself.
  18. // TODO: move to docker/cli and/or integration/container/copy_test.go
  19. func (s *DockerSuite) TestCpFromSymlinkDestination(c *check.C) {
  20. testRequires(c, DaemonIsLinux)
  21. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  22. tmpDir := getTestDir(c, "test-cp-from-err-dst-not-dir")
  23. defer os.RemoveAll(tmpDir)
  24. makeTestContentInDir(c, tmpDir)
  25. // First, copy a file from the container to a symlink to a file. This
  26. // should overwrite the symlink target contents with the source contents.
  27. srcPath := containerCpPath(containerID, "/file2")
  28. dstPath := cpPath(tmpDir, "symlinkToFile1")
  29. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  30. // The symlink should not have been modified.
  31. c.Assert(symlinkTargetEquals(c, dstPath, "file1"), checker.IsNil)
  32. // The file should have the contents of "file2" now.
  33. c.Assert(fileContentEquals(c, cpPath(tmpDir, "file1"), "file2\n"), checker.IsNil)
  34. // Next, copy a file from the container to a symlink to a directory. This
  35. // should copy the file into the symlink target directory.
  36. dstPath = cpPath(tmpDir, "symlinkToDir1")
  37. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  38. // The symlink should not have been modified.
  39. c.Assert(symlinkTargetEquals(c, dstPath, "dir1"), checker.IsNil)
  40. // The file should have the contents of "file2" now.
  41. c.Assert(fileContentEquals(c, cpPath(tmpDir, "file2"), "file2\n"), checker.IsNil)
  42. // Next, copy a file from the container to a symlink to a file that does
  43. // not exist (a broken symlink). This should create the target file with
  44. // the contents of the source file.
  45. dstPath = cpPath(tmpDir, "brokenSymlinkToFileX")
  46. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  47. // The symlink should not have been modified.
  48. c.Assert(symlinkTargetEquals(c, dstPath, "fileX"), checker.IsNil)
  49. // The file should have the contents of "file2" now.
  50. c.Assert(fileContentEquals(c, cpPath(tmpDir, "fileX"), "file2\n"), checker.IsNil)
  51. // Next, copy a directory from the container to a symlink to a local
  52. // directory. This should copy the directory into the symlink target
  53. // directory and not modify the symlink.
  54. srcPath = containerCpPath(containerID, "/dir2")
  55. dstPath = cpPath(tmpDir, "symlinkToDir1")
  56. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  57. // The symlink should not have been modified.
  58. c.Assert(symlinkTargetEquals(c, dstPath, "dir1"), checker.IsNil)
  59. // The directory should now contain a copy of "dir2".
  60. c.Assert(fileContentEquals(c, cpPath(tmpDir, "dir1/dir2/file2-1"), "file2-1\n"), checker.IsNil)
  61. // Next, copy a directory from the container to a symlink to a local
  62. // directory that does not exist (a broken symlink). This should create
  63. // the target as a directory with the contents of the source directory. It
  64. // should not modify the symlink.
  65. dstPath = cpPath(tmpDir, "brokenSymlinkToDirX")
  66. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  67. // The symlink should not have been modified.
  68. c.Assert(symlinkTargetEquals(c, dstPath, "dirX"), checker.IsNil)
  69. // The "dirX" directory should now be a copy of "dir2".
  70. c.Assert(fileContentEquals(c, cpPath(tmpDir, "dirX/file2-1"), "file2-1\n"), checker.IsNil)
  71. }
  72. // Possibilities are reduced to the remaining 10 cases:
  73. //
  74. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  75. // ===================================================================================================
  76. // A | no | - | no | - | no | create file
  77. // B | no | - | no | - | yes | error
  78. // C | no | - | yes | no | - | overwrite file
  79. // D | no | - | yes | yes | - | create file in dst dir
  80. // E | yes | no | no | - | - | create dir, copy contents
  81. // F | yes | no | yes | no | - | error
  82. // G | yes | no | yes | yes | - | copy dir and contents
  83. // H | yes | yes | no | - | - | create dir, copy contents
  84. // I | yes | yes | yes | no | - | error
  85. // J | yes | yes | yes | yes | - | copy dir contents
  86. //
  87. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  88. // exist. This should create a file with the name DST and copy the
  89. // contents of the source file into it.
  90. func (s *DockerSuite) TestCpFromCaseA(c *check.C) {
  91. testRequires(c, DaemonIsLinux)
  92. containerID := makeTestContainer(c, testContainerOptions{
  93. addContent: true, workDir: "/root",
  94. })
  95. tmpDir := getTestDir(c, "test-cp-from-case-a")
  96. defer os.RemoveAll(tmpDir)
  97. srcPath := containerCpPath(containerID, "/root/file1")
  98. dstPath := cpPath(tmpDir, "itWorks.txt")
  99. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  100. c.Assert(fileContentEquals(c, dstPath, "file1\n"), checker.IsNil)
  101. }
  102. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  103. // exist. This should cause an error because the copy operation cannot
  104. // create a directory when copying a single file.
  105. func (s *DockerSuite) TestCpFromCaseB(c *check.C) {
  106. testRequires(c, DaemonIsLinux)
  107. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  108. tmpDir := getTestDir(c, "test-cp-from-case-b")
  109. defer os.RemoveAll(tmpDir)
  110. srcPath := containerCpPath(containerID, "/file1")
  111. dstDir := cpPathTrailingSep(tmpDir, "testDir")
  112. err := runDockerCp(c, srcPath, dstDir, nil)
  113. c.Assert(err, checker.NotNil)
  114. c.Assert(isCpDirNotExist(err), checker.True, check.Commentf("expected DirNotExists error, but got %T: %s", err, err))
  115. }
  116. // C. SRC specifies a file and DST exists as a file. This should overwrite
  117. // the file at DST with the contents of the source file.
  118. func (s *DockerSuite) TestCpFromCaseC(c *check.C) {
  119. testRequires(c, DaemonIsLinux)
  120. containerID := makeTestContainer(c, testContainerOptions{
  121. addContent: true, workDir: "/root",
  122. })
  123. tmpDir := getTestDir(c, "test-cp-from-case-c")
  124. defer os.RemoveAll(tmpDir)
  125. makeTestContentInDir(c, tmpDir)
  126. srcPath := containerCpPath(containerID, "/root/file1")
  127. dstPath := cpPath(tmpDir, "file2")
  128. // Ensure the local file starts with different content.
  129. c.Assert(fileContentEquals(c, dstPath, "file2\n"), checker.IsNil)
  130. c.Assert(runDockerCp(c, srcPath, dstPath, nil), checker.IsNil)
  131. c.Assert(fileContentEquals(c, dstPath, "file1\n"), checker.IsNil)
  132. }
  133. // D. SRC specifies a file and DST exists as a directory. This should place
  134. // a copy of the source file inside it using the basename from SRC. Ensure
  135. // this works whether DST has a trailing path separator or not.
  136. func (s *DockerSuite) TestCpFromCaseD(c *check.C) {
  137. testRequires(c, DaemonIsLinux)
  138. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  139. tmpDir := getTestDir(c, "test-cp-from-case-d")
  140. defer os.RemoveAll(tmpDir)
  141. makeTestContentInDir(c, tmpDir)
  142. srcPath := containerCpPath(containerID, "/file1")
  143. dstDir := cpPath(tmpDir, "dir1")
  144. dstPath := filepath.Join(dstDir, "file1")
  145. // Ensure that dstPath doesn't exist.
  146. _, err := os.Stat(dstPath)
  147. c.Assert(os.IsNotExist(err), checker.True, check.Commentf("did not expect dstPath %q to exist", dstPath))
  148. c.Assert(runDockerCp(c, srcPath, dstDir, nil), checker.IsNil)
  149. c.Assert(fileContentEquals(c, dstPath, "file1\n"), checker.IsNil)
  150. // Now try again but using a trailing path separator for dstDir.
  151. // unable to remove dstDir
  152. c.Assert(os.RemoveAll(dstDir), checker.IsNil)
  153. // unable to make dstDir
  154. c.Assert(os.MkdirAll(dstDir, os.FileMode(0755)), checker.IsNil)
  155. dstDir = cpPathTrailingSep(tmpDir, "dir1")
  156. c.Assert(runDockerCp(c, srcPath, dstDir, nil), checker.IsNil)
  157. c.Assert(fileContentEquals(c, dstPath, "file1\n"), checker.IsNil)
  158. }
  159. // E. SRC specifies a directory and DST does not exist. This should create a
  160. // directory at DST and copy the contents of the SRC directory into the DST
  161. // directory. Ensure this works whether DST has a trailing path separator or
  162. // not.
  163. func (s *DockerSuite) TestCpFromCaseE(c *check.C) {
  164. testRequires(c, DaemonIsLinux)
  165. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  166. tmpDir := getTestDir(c, "test-cp-from-case-e")
  167. defer os.RemoveAll(tmpDir)
  168. srcDir := containerCpPath(containerID, "dir1")
  169. dstDir := cpPath(tmpDir, "testDir")
  170. dstPath := filepath.Join(dstDir, "file1-1")
  171. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  172. c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
  173. // Now try again but using a trailing path separator for dstDir.
  174. // unable to remove dstDir
  175. c.Assert(os.RemoveAll(dstDir), checker.IsNil)
  176. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  177. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  178. c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
  179. }
  180. // F. SRC specifies a directory and DST exists as a file. This should cause an
  181. // error as it is not possible to overwrite a file with a directory.
  182. func (s *DockerSuite) TestCpFromCaseF(c *check.C) {
  183. testRequires(c, DaemonIsLinux)
  184. containerID := makeTestContainer(c, testContainerOptions{
  185. addContent: true, workDir: "/root",
  186. })
  187. tmpDir := getTestDir(c, "test-cp-from-case-f")
  188. defer os.RemoveAll(tmpDir)
  189. makeTestContentInDir(c, tmpDir)
  190. srcDir := containerCpPath(containerID, "/root/dir1")
  191. dstFile := cpPath(tmpDir, "file1")
  192. err := runDockerCp(c, srcDir, dstFile, nil)
  193. c.Assert(err, checker.NotNil)
  194. c.Assert(isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
  195. }
  196. // G. SRC specifies a directory and DST exists as a directory. This should copy
  197. // the SRC directory and all its contents to the DST directory. Ensure this
  198. // works whether DST has a trailing path separator or not.
  199. func (s *DockerSuite) TestCpFromCaseG(c *check.C) {
  200. testRequires(c, DaemonIsLinux)
  201. containerID := makeTestContainer(c, testContainerOptions{
  202. addContent: true, workDir: "/root",
  203. })
  204. tmpDir := getTestDir(c, "test-cp-from-case-g")
  205. defer os.RemoveAll(tmpDir)
  206. makeTestContentInDir(c, tmpDir)
  207. srcDir := containerCpPath(containerID, "/root/dir1")
  208. dstDir := cpPath(tmpDir, "dir2")
  209. resultDir := filepath.Join(dstDir, "dir1")
  210. dstPath := filepath.Join(resultDir, "file1-1")
  211. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  212. c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
  213. // Now try again but using a trailing path separator for dstDir.
  214. // unable to remove dstDir
  215. c.Assert(os.RemoveAll(dstDir), checker.IsNil)
  216. // unable to make dstDir
  217. c.Assert(os.MkdirAll(dstDir, os.FileMode(0755)), checker.IsNil)
  218. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  219. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  220. c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
  221. }
  222. // H. SRC specifies a directory's contents only and DST does not exist. This
  223. // should create a directory at DST and copy the contents of the SRC
  224. // directory (but not the directory itself) into the DST directory. Ensure
  225. // this works whether DST has a trailing path separator or not.
  226. func (s *DockerSuite) TestCpFromCaseH(c *check.C) {
  227. testRequires(c, DaemonIsLinux)
  228. containerID := makeTestContainer(c, testContainerOptions{addContent: true})
  229. tmpDir := getTestDir(c, "test-cp-from-case-h")
  230. defer os.RemoveAll(tmpDir)
  231. srcDir := containerCpPathTrailingSep(containerID, "dir1") + "."
  232. dstDir := cpPath(tmpDir, "testDir")
  233. dstPath := filepath.Join(dstDir, "file1-1")
  234. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  235. c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
  236. // Now try again but using a trailing path separator for dstDir.
  237. // unable to remove resultDir
  238. c.Assert(os.RemoveAll(dstDir), checker.IsNil)
  239. dstDir = cpPathTrailingSep(tmpDir, "testDir")
  240. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  241. c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
  242. }
  243. // I. SRC specifies a directory's contents only and DST exists as a file. This
  244. // should cause an error as it is not possible to overwrite a file with a
  245. // directory.
  246. func (s *DockerSuite) TestCpFromCaseI(c *check.C) {
  247. testRequires(c, DaemonIsLinux)
  248. containerID := makeTestContainer(c, testContainerOptions{
  249. addContent: true, workDir: "/root",
  250. })
  251. tmpDir := getTestDir(c, "test-cp-from-case-i")
  252. defer os.RemoveAll(tmpDir)
  253. makeTestContentInDir(c, tmpDir)
  254. srcDir := containerCpPathTrailingSep(containerID, "/root/dir1") + "."
  255. dstFile := cpPath(tmpDir, "file1")
  256. err := runDockerCp(c, srcDir, dstFile, nil)
  257. c.Assert(err, checker.NotNil)
  258. c.Assert(isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
  259. }
  260. // J. SRC specifies a directory's contents only and DST exists as a directory.
  261. // This should copy the contents of the SRC directory (but not the directory
  262. // itself) into the DST directory. Ensure this works whether DST has a
  263. // trailing path separator or not.
  264. func (s *DockerSuite) TestCpFromCaseJ(c *check.C) {
  265. testRequires(c, DaemonIsLinux)
  266. containerID := makeTestContainer(c, testContainerOptions{
  267. addContent: true, workDir: "/root",
  268. })
  269. tmpDir := getTestDir(c, "test-cp-from-case-j")
  270. defer os.RemoveAll(tmpDir)
  271. makeTestContentInDir(c, tmpDir)
  272. srcDir := containerCpPathTrailingSep(containerID, "/root/dir1") + "."
  273. dstDir := cpPath(tmpDir, "dir2")
  274. dstPath := filepath.Join(dstDir, "file1-1")
  275. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  276. c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
  277. // Now try again but using a trailing path separator for dstDir.
  278. // unable to remove dstDir
  279. c.Assert(os.RemoveAll(dstDir), checker.IsNil)
  280. // unable to make dstDir
  281. c.Assert(os.MkdirAll(dstDir, os.FileMode(0755)), checker.IsNil)
  282. dstDir = cpPathTrailingSep(tmpDir, "dir2")
  283. c.Assert(runDockerCp(c, srcDir, dstDir, nil), checker.IsNil)
  284. c.Assert(fileContentEquals(c, dstPath, "file1-1\n"), checker.IsNil)
  285. }