docker_cli_cp_to_container_test.go 18 KB

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