docker_cli_cp_to_container_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. package main
  2. import (
  3. "os"
  4. "github.com/docker/docker/integration-cli/checker"
  5. "github.com/go-check/check"
  6. "gotest.tools/assert"
  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 local path to a symlink in a container copies to
  17. // the symlink target and does not overwrite the container symlink itself.
  18. func (s *DockerSuite) TestCpToSymlinkDestination(c *testing.T) {
  19. // stat /tmp/test-cp-to-symlink-destination-262430901/vol3 gets permission denied for the user
  20. testRequires(c, NotUserNamespace)
  21. testRequires(c, DaemonIsLinux)
  22. testRequires(c, testEnv.IsLocalDaemon) // Requires local volume mount bind.
  23. testVol := getTestDir(c, "test-cp-to-symlink-destination-")
  24. defer os.RemoveAll(testVol)
  25. makeTestContentInDir(c, testVol)
  26. containerID := makeTestContainer(c, testContainerOptions{
  27. volumes: defaultVolumes(testVol), // Our bind mount is at /vol2
  28. })
  29. // First, copy a local file to a symlink to a file in the container. This
  30. // should overwrite the symlink target contents with the source contents.
  31. srcPath := cpPath(testVol, "file2")
  32. dstPath := containerCpPath(containerID, "/vol2/symlinkToFile1")
  33. assert.Assert(c, runDockerCp(c, srcPath, dstPath, nil) == nil)
  34. // The symlink should not have been modified.
  35. assert.Assert(c, symlinkTargetEquals(c, cpPath(testVol, "symlinkToFile1"), "file1") == nil)
  36. // The file should have the contents of "file2" now.
  37. assert.Assert(c, fileContentEquals(c, cpPath(testVol, "file1"), "file2\n") == nil)
  38. // Next, copy a local file to a symlink to a directory in the container.
  39. // This should copy the file into the symlink target directory.
  40. dstPath = containerCpPath(containerID, "/vol2/symlinkToDir1")
  41. assert.Assert(c, runDockerCp(c, srcPath, dstPath, nil) == nil)
  42. // The symlink should not have been modified.
  43. assert.Assert(c, symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1") == nil)
  44. // The file should have the contents of "file2" now.
  45. assert.Assert(c, fileContentEquals(c, cpPath(testVol, "file2"), "file2\n") == nil)
  46. // Next, copy a file to a symlink to a file that does not exist (a broken
  47. // symlink) in the container. This should create the target file with the
  48. // contents of the source file.
  49. dstPath = containerCpPath(containerID, "/vol2/brokenSymlinkToFileX")
  50. assert.Assert(c, runDockerCp(c, srcPath, dstPath, nil) == nil)
  51. // The symlink should not have been modified.
  52. assert.Assert(c, symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToFileX"), "fileX") == nil)
  53. // The file should have the contents of "file2" now.
  54. assert.Assert(c, fileContentEquals(c, cpPath(testVol, "fileX"), "file2\n") == nil)
  55. // Next, copy a local directory to a symlink to a directory in the
  56. // container. This should copy the directory into the symlink target
  57. // directory and not modify the symlink.
  58. srcPath = cpPath(testVol, "/dir2")
  59. dstPath = containerCpPath(containerID, "/vol2/symlinkToDir1")
  60. assert.Assert(c, runDockerCp(c, srcPath, dstPath, nil) == nil)
  61. // The symlink should not have been modified.
  62. assert.Assert(c, symlinkTargetEquals(c, cpPath(testVol, "symlinkToDir1"), "dir1") == nil)
  63. // The directory should now contain a copy of "dir2".
  64. assert.Assert(c, fileContentEquals(c, cpPath(testVol, "dir1/dir2/file2-1"), "file2-1\n") == nil)
  65. // Next, copy a local directory to a symlink to a local directory that does
  66. // not exist (a broken symlink) in the container. This should create the
  67. // target as a directory with the contents of the source directory. It
  68. // should not modify the symlink.
  69. dstPath = containerCpPath(containerID, "/vol2/brokenSymlinkToDirX")
  70. assert.Assert(c, runDockerCp(c, srcPath, dstPath, nil) == nil)
  71. // The symlink should not have been modified.
  72. assert.Assert(c, symlinkTargetEquals(c, cpPath(testVol, "brokenSymlinkToDirX"), "dirX") == nil)
  73. // The "dirX" directory should now be a copy of "dir2".
  74. assert.Assert(c, fileContentEquals(c, cpPath(testVol, "dirX/file2-1"), "file2-1\n") == nil)
  75. }
  76. // Possibilities are reduced to the remaining 10 cases:
  77. //
  78. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  79. // ===================================================================================================
  80. // A | no | - | no | - | no | create file
  81. // B | no | - | no | - | yes | error
  82. // C | no | - | yes | no | - | overwrite file
  83. // D | no | - | yes | yes | - | create file in dst dir
  84. // E | yes | no | no | - | - | create dir, copy contents
  85. // F | yes | no | yes | no | - | error
  86. // G | yes | no | yes | yes | - | copy dir and contents
  87. // H | yes | yes | no | - | - | create dir, copy contents
  88. // I | yes | yes | yes | no | - | error
  89. // J | yes | yes | yes | yes | - | copy dir contents
  90. //
  91. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  92. // exist. This should create a file with the name DST and copy the
  93. // contents of the source file into it.
  94. func (s *DockerSuite) TestCpToCaseA(c *testing.T) {
  95. containerID := makeTestContainer(c, testContainerOptions{
  96. workDir: "/root", command: makeCatFileCommand("itWorks.txt"),
  97. })
  98. tmpDir := getTestDir(c, "test-cp-to-case-a")
  99. defer os.RemoveAll(tmpDir)
  100. makeTestContentInDir(c, tmpDir)
  101. srcPath := cpPath(tmpDir, "file1")
  102. dstPath := containerCpPath(containerID, "/root/itWorks.txt")
  103. assert.Assert(c, runDockerCp(c, srcPath, dstPath, nil) == nil)
  104. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1\n") == nil)
  105. }
  106. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  107. // exist. This should cause an error because the copy operation cannot
  108. // create a directory when copying a single file.
  109. func (s *DockerSuite) TestCpToCaseB(c *testing.T) {
  110. containerID := makeTestContainer(c, testContainerOptions{
  111. command: makeCatFileCommand("testDir/file1"),
  112. })
  113. tmpDir := getTestDir(c, "test-cp-to-case-b")
  114. defer os.RemoveAll(tmpDir)
  115. makeTestContentInDir(c, tmpDir)
  116. srcPath := cpPath(tmpDir, "file1")
  117. dstDir := containerCpPathTrailingSep(containerID, "testDir")
  118. err := runDockerCp(c, srcPath, dstDir, nil)
  119. assert.ErrorContains(c, err, "")
  120. assert.Assert(c, isCpDirNotExist(err), checker.True, check.Commentf("expected DirNotExists error, but got %T: %s", err, err))
  121. }
  122. // C. SRC specifies a file and DST exists as a file. This should overwrite
  123. // the file at DST with the contents of the source file.
  124. func (s *DockerSuite) TestCpToCaseC(c *testing.T) {
  125. testRequires(c, DaemonIsLinux)
  126. containerID := makeTestContainer(c, testContainerOptions{
  127. addContent: true, workDir: "/root",
  128. command: makeCatFileCommand("file2"),
  129. })
  130. tmpDir := getTestDir(c, "test-cp-to-case-c")
  131. defer os.RemoveAll(tmpDir)
  132. makeTestContentInDir(c, tmpDir)
  133. srcPath := cpPath(tmpDir, "file1")
  134. dstPath := containerCpPath(containerID, "/root/file2")
  135. // Ensure the container's file starts with the original content.
  136. assert.Assert(c, containerStartOutputEquals(c, containerID, "file2\n") == nil)
  137. assert.Assert(c, runDockerCp(c, srcPath, dstPath, nil) == nil)
  138. // Should now contain file1's contents.
  139. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1\n") == nil)
  140. }
  141. // D. SRC specifies a file and DST exists as a directory. This should place
  142. // a copy of the source file inside it using the basename from SRC. Ensure
  143. // this works whether DST has a trailing path separator or not.
  144. func (s *DockerSuite) TestCpToCaseD(c *testing.T) {
  145. testRequires(c, DaemonIsLinux)
  146. containerID := makeTestContainer(c, testContainerOptions{
  147. addContent: true,
  148. command: makeCatFileCommand("/dir1/file1"),
  149. })
  150. tmpDir := getTestDir(c, "test-cp-to-case-d")
  151. defer os.RemoveAll(tmpDir)
  152. makeTestContentInDir(c, tmpDir)
  153. srcPath := cpPath(tmpDir, "file1")
  154. dstDir := containerCpPath(containerID, "dir1")
  155. // Ensure that dstPath doesn't exist.
  156. assert.Assert(c, containerStartOutputEquals(c, containerID, "") == nil)
  157. assert.Assert(c, runDockerCp(c, srcPath, dstDir, nil) == nil)
  158. // Should now contain file1's contents.
  159. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1\n") == nil)
  160. // Now try again but using a trailing path separator for dstDir.
  161. // Make new destination container.
  162. containerID = makeTestContainer(c, testContainerOptions{
  163. addContent: true,
  164. command: makeCatFileCommand("/dir1/file1"),
  165. })
  166. dstDir = containerCpPathTrailingSep(containerID, "dir1")
  167. // Ensure that dstPath doesn't exist.
  168. assert.Assert(c, containerStartOutputEquals(c, containerID, "") == nil)
  169. assert.Assert(c, runDockerCp(c, srcPath, dstDir, nil) == nil)
  170. // Should now contain file1's contents.
  171. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1\n") == nil)
  172. }
  173. // E. SRC specifies a directory and DST does not exist. This should create a
  174. // directory at DST and copy the contents of the SRC directory into the DST
  175. // directory. Ensure this works whether DST has a trailing path separator or
  176. // not.
  177. func (s *DockerSuite) TestCpToCaseE(c *testing.T) {
  178. containerID := makeTestContainer(c, testContainerOptions{
  179. command: makeCatFileCommand("/testDir/file1-1"),
  180. })
  181. tmpDir := getTestDir(c, "test-cp-to-case-e")
  182. defer os.RemoveAll(tmpDir)
  183. makeTestContentInDir(c, tmpDir)
  184. srcDir := cpPath(tmpDir, "dir1")
  185. dstDir := containerCpPath(containerID, "testDir")
  186. assert.Assert(c, runDockerCp(c, srcDir, dstDir, nil) == nil)
  187. // Should now contain file1-1's contents.
  188. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1-1\n") == nil)
  189. // Now try again but using a trailing path separator for dstDir.
  190. // Make new destination container.
  191. containerID = makeTestContainer(c, testContainerOptions{
  192. command: makeCatFileCommand("/testDir/file1-1"),
  193. })
  194. dstDir = containerCpPathTrailingSep(containerID, "testDir")
  195. assert.Assert(c, runDockerCp(c, srcDir, dstDir, nil) == nil)
  196. // Should now contain file1-1's contents.
  197. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1-1\n") == nil)
  198. }
  199. // F. SRC specifies a directory and DST exists as a file. This should cause an
  200. // error as it is not possible to overwrite a file with a directory.
  201. func (s *DockerSuite) TestCpToCaseF(c *testing.T) {
  202. testRequires(c, DaemonIsLinux)
  203. containerID := makeTestContainer(c, testContainerOptions{
  204. addContent: true, workDir: "/root",
  205. })
  206. tmpDir := getTestDir(c, "test-cp-to-case-f")
  207. defer os.RemoveAll(tmpDir)
  208. makeTestContentInDir(c, tmpDir)
  209. srcDir := cpPath(tmpDir, "dir1")
  210. dstFile := containerCpPath(containerID, "/root/file1")
  211. err := runDockerCp(c, srcDir, dstFile, nil)
  212. assert.ErrorContains(c, err, "")
  213. assert.Assert(c, isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
  214. }
  215. // G. SRC specifies a directory and DST exists as a directory. This should copy
  216. // the SRC directory and all its contents to the DST directory. Ensure this
  217. // works whether DST has a trailing path separator or not.
  218. func (s *DockerSuite) TestCpToCaseG(c *testing.T) {
  219. testRequires(c, DaemonIsLinux)
  220. containerID := makeTestContainer(c, testContainerOptions{
  221. addContent: true, workDir: "/root",
  222. command: makeCatFileCommand("dir2/dir1/file1-1"),
  223. })
  224. tmpDir := getTestDir(c, "test-cp-to-case-g")
  225. defer os.RemoveAll(tmpDir)
  226. makeTestContentInDir(c, tmpDir)
  227. srcDir := cpPath(tmpDir, "dir1")
  228. dstDir := containerCpPath(containerID, "/root/dir2")
  229. // Ensure that dstPath doesn't exist.
  230. assert.Assert(c, containerStartOutputEquals(c, containerID, "") == nil)
  231. assert.Assert(c, runDockerCp(c, srcDir, dstDir, nil) == nil)
  232. // Should now contain file1-1's contents.
  233. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1-1\n") == nil)
  234. // Now try again but using a trailing path separator for dstDir.
  235. // Make new destination container.
  236. containerID = makeTestContainer(c, testContainerOptions{
  237. addContent: true,
  238. command: makeCatFileCommand("/dir2/dir1/file1-1"),
  239. })
  240. dstDir = containerCpPathTrailingSep(containerID, "/dir2")
  241. // Ensure that dstPath doesn't exist.
  242. assert.Assert(c, containerStartOutputEquals(c, containerID, "") == nil)
  243. assert.Assert(c, runDockerCp(c, srcDir, dstDir, nil) == nil)
  244. // Should now contain file1-1's contents.
  245. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1-1\n") == nil)
  246. }
  247. // H. SRC specifies a directory's contents only and DST does not exist. This
  248. // should create a directory at DST and copy the contents of the SRC
  249. // directory (but not the directory itself) into the DST directory. Ensure
  250. // this works whether DST has a trailing path separator or not.
  251. func (s *DockerSuite) TestCpToCaseH(c *testing.T) {
  252. containerID := makeTestContainer(c, testContainerOptions{
  253. command: makeCatFileCommand("/testDir/file1-1"),
  254. })
  255. tmpDir := getTestDir(c, "test-cp-to-case-h")
  256. defer os.RemoveAll(tmpDir)
  257. makeTestContentInDir(c, tmpDir)
  258. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  259. dstDir := containerCpPath(containerID, "testDir")
  260. assert.Assert(c, runDockerCp(c, srcDir, dstDir, nil) == nil)
  261. // Should now contain file1-1's contents.
  262. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1-1\n") == nil)
  263. // Now try again but using a trailing path separator for dstDir.
  264. // Make new destination container.
  265. containerID = makeTestContainer(c, testContainerOptions{
  266. command: makeCatFileCommand("/testDir/file1-1"),
  267. })
  268. dstDir = containerCpPathTrailingSep(containerID, "testDir")
  269. assert.Assert(c, runDockerCp(c, srcDir, dstDir, nil) == nil)
  270. // Should now contain file1-1's contents.
  271. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1-1\n") == nil)
  272. }
  273. // I. SRC specifies a directory's contents only and DST exists as a file. This
  274. // should cause an error as it is not possible to overwrite a file with a
  275. // directory.
  276. func (s *DockerSuite) TestCpToCaseI(c *testing.T) {
  277. testRequires(c, DaemonIsLinux)
  278. containerID := makeTestContainer(c, testContainerOptions{
  279. addContent: true, workDir: "/root",
  280. })
  281. tmpDir := getTestDir(c, "test-cp-to-case-i")
  282. defer os.RemoveAll(tmpDir)
  283. makeTestContentInDir(c, tmpDir)
  284. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  285. dstFile := containerCpPath(containerID, "/root/file1")
  286. err := runDockerCp(c, srcDir, dstFile, nil)
  287. assert.ErrorContains(c, err, "")
  288. assert.Assert(c, isCpCannotCopyDir(err), checker.True, check.Commentf("expected ErrCannotCopyDir error, but got %T: %s", err, err))
  289. }
  290. // J. SRC specifies a directory's contents only and DST exists as a directory.
  291. // This should copy the contents of the SRC directory (but not the directory
  292. // itself) into the DST directory. Ensure this works whether DST has a
  293. // trailing path separator or not.
  294. func (s *DockerSuite) TestCpToCaseJ(c *testing.T) {
  295. testRequires(c, DaemonIsLinux)
  296. containerID := makeTestContainer(c, testContainerOptions{
  297. addContent: true, workDir: "/root",
  298. command: makeCatFileCommand("/dir2/file1-1"),
  299. })
  300. tmpDir := getTestDir(c, "test-cp-to-case-j")
  301. defer os.RemoveAll(tmpDir)
  302. makeTestContentInDir(c, tmpDir)
  303. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  304. dstDir := containerCpPath(containerID, "/dir2")
  305. // Ensure that dstPath doesn't exist.
  306. assert.Assert(c, containerStartOutputEquals(c, containerID, "") == nil)
  307. assert.Assert(c, runDockerCp(c, srcDir, dstDir, nil) == nil)
  308. // Should now contain file1-1's contents.
  309. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1-1\n") == nil)
  310. // Now try again but using a trailing path separator for dstDir.
  311. // Make new destination container.
  312. containerID = makeTestContainer(c, testContainerOptions{
  313. command: makeCatFileCommand("/dir2/file1-1"),
  314. })
  315. dstDir = containerCpPathTrailingSep(containerID, "/dir2")
  316. // Ensure that dstPath doesn't exist.
  317. assert.Assert(c, containerStartOutputEquals(c, containerID, "") == nil)
  318. assert.Assert(c, runDockerCp(c, srcDir, dstDir, nil) == nil)
  319. // Should now contain file1-1's contents.
  320. assert.Assert(c, containerStartOutputEquals(c, containerID, "file1-1\n") == nil)
  321. }
  322. // The `docker cp` command should also ensure that you cannot
  323. // write to a container rootfs that is marked as read-only.
  324. func (s *DockerSuite) TestCpToErrReadOnlyRootfs(c *testing.T) {
  325. // --read-only + userns has remount issues
  326. testRequires(c, DaemonIsLinux, NotUserNamespace)
  327. tmpDir := getTestDir(c, "test-cp-to-err-read-only-rootfs")
  328. defer os.RemoveAll(tmpDir)
  329. makeTestContentInDir(c, tmpDir)
  330. containerID := makeTestContainer(c, testContainerOptions{
  331. readOnly: true, workDir: "/root",
  332. command: makeCatFileCommand("shouldNotExist"),
  333. })
  334. srcPath := cpPath(tmpDir, "file1")
  335. dstPath := containerCpPath(containerID, "/root/shouldNotExist")
  336. err := runDockerCp(c, srcPath, dstPath, nil)
  337. assert.ErrorContains(c, err, "")
  338. assert.Assert(c, isCpCannotCopyReadOnly(err), checker.True, check.Commentf("expected ErrContainerRootfsReadonly error, but got %T: %s", err, err))
  339. // Ensure that dstPath doesn't exist.
  340. assert.Assert(c, containerStartOutputEquals(c, containerID, "") == nil)
  341. }
  342. // The `docker cp` command should also ensure that you
  343. // cannot write to a volume that is mounted as read-only.
  344. func (s *DockerSuite) TestCpToErrReadOnlyVolume(c *testing.T) {
  345. // --read-only + userns has remount issues
  346. testRequires(c, DaemonIsLinux, NotUserNamespace)
  347. tmpDir := getTestDir(c, "test-cp-to-err-read-only-volume")
  348. defer os.RemoveAll(tmpDir)
  349. makeTestContentInDir(c, tmpDir)
  350. containerID := makeTestContainer(c, testContainerOptions{
  351. volumes: defaultVolumes(tmpDir), workDir: "/root",
  352. command: makeCatFileCommand("/vol_ro/shouldNotExist"),
  353. })
  354. srcPath := cpPath(tmpDir, "file1")
  355. dstPath := containerCpPath(containerID, "/vol_ro/shouldNotExist")
  356. err := runDockerCp(c, srcPath, dstPath, nil)
  357. assert.ErrorContains(c, err, "")
  358. assert.Assert(c, isCpCannotCopyReadOnly(err), checker.True, check.Commentf("expected ErrVolumeReadonly error, but got %T: %s", err, err))
  359. // Ensure that dstPath doesn't exist.
  360. assert.Assert(c, containerStartOutputEquals(c, containerID, "") == nil)
  361. }