docker_cli_cp_to_container_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 *DockerCLICpSuite) 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 exist.
  81. //
  82. // This should create a file with the name DST and copy the contents of the
  83. // source file into it.
  84. func (s *DockerCLICpSuite) TestCpToCaseA(c *testing.T) {
  85. containerID := makeTestContainer(c, testContainerOptions{
  86. workDir: "/root", command: makeCatFileCommand("itWorks.txt"),
  87. })
  88. tmpDir := getTestDir(c, "test-cp-to-case-a")
  89. defer os.RemoveAll(tmpDir)
  90. makeTestContentInDir(c, tmpDir)
  91. srcPath := cpPath(tmpDir, "file1")
  92. dstPath := containerCpPath(containerID, "/root/itWorks.txt")
  93. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  94. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1\n"))
  95. }
  96. // B. SRC specifies a file and DST (with trailing path separator) doesn't exist.
  97. //
  98. // This should cause an error because the copy operation cannot create a
  99. // directory when copying a single file.
  100. func (s *DockerCLICpSuite) TestCpToCaseB(c *testing.T) {
  101. containerID := makeTestContainer(c, testContainerOptions{
  102. command: makeCatFileCommand("testDir/file1"),
  103. })
  104. tmpDir := getTestDir(c, "test-cp-to-case-b")
  105. defer os.RemoveAll(tmpDir)
  106. makeTestContentInDir(c, tmpDir)
  107. srcPath := cpPath(tmpDir, "file1")
  108. dstDir := containerCpPathTrailingSep(containerID, "testDir")
  109. err := runDockerCp(c, srcPath, dstDir)
  110. assert.ErrorContains(c, err, "")
  111. assert.Assert(c, isCpDirNotExist(err), "expected DirNotExists error, but got %T: %s", err, err)
  112. }
  113. // C. SRC specifies a file and DST exists as a file.
  114. //
  115. // This should overwrite the file at DST with the contents of the source file.
  116. func (s *DockerCLICpSuite) TestCpToCaseC(c *testing.T) {
  117. testRequires(c, DaemonIsLinux)
  118. containerID := makeTestContainer(c, testContainerOptions{
  119. addContent: true, workDir: "/root",
  120. command: makeCatFileCommand("file2"),
  121. })
  122. tmpDir := getTestDir(c, "test-cp-to-case-c")
  123. defer os.RemoveAll(tmpDir)
  124. makeTestContentInDir(c, tmpDir)
  125. srcPath := cpPath(tmpDir, "file1")
  126. dstPath := containerCpPath(containerID, "/root/file2")
  127. // Ensure the container's file starts with the original content.
  128. assert.NilError(c, containerStartOutputEquals(c, containerID, "file2\n"))
  129. assert.NilError(c, runDockerCp(c, srcPath, dstPath))
  130. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1\n"), "Should now contain file1's contents")
  131. }
  132. // D. SRC specifies a file and DST exists as a directory.
  133. //
  134. // This should place a copy of the source file inside it using the basename from
  135. // SRC. Ensure this works whether DST has a trailing path separator or not.
  136. func (s *DockerCLICpSuite) TestCpToCaseD(c *testing.T) {
  137. testRequires(c, DaemonIsLinux)
  138. containerID := makeTestContainer(c, testContainerOptions{
  139. addContent: true,
  140. command: makeCatFileCommand("/dir1/file1"),
  141. })
  142. tmpDir := getTestDir(c, "test-cp-to-case-d")
  143. defer os.RemoveAll(tmpDir)
  144. makeTestContentInDir(c, tmpDir)
  145. srcPath := cpPath(tmpDir, "file1")
  146. dstDir := containerCpPath(containerID, "dir1")
  147. assert.NilError(c, containerStartOutputEquals(c, containerID, ""), "dstPath should not have existed")
  148. assert.NilError(c, runDockerCp(c, srcPath, dstDir))
  149. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1\n"), "Should now contain file1's contents")
  150. // Now try again but using a trailing path separator for dstDir.
  151. // Make new destination container.
  152. containerID = makeTestContainer(c, testContainerOptions{
  153. addContent: true,
  154. command: makeCatFileCommand("/dir1/file1"),
  155. })
  156. dstDir = containerCpPathTrailingSep(containerID, "dir1")
  157. assert.NilError(c, containerStartOutputEquals(c, containerID, ""), "dstPath should not have existed")
  158. assert.NilError(c, runDockerCp(c, srcPath, dstDir))
  159. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1\n"), "Should now contain file1's contents")
  160. }
  161. // E. SRC specifies a directory and DST does not exist.
  162. //
  163. // This should create a directory at DST and copy the contents of the SRC
  164. // directory into the DST directory. Ensure this works whether DST has a
  165. // trailing path separator or not.
  166. func (s *DockerCLICpSuite) TestCpToCaseE(c *testing.T) {
  167. containerID := makeTestContainer(c, testContainerOptions{
  168. command: makeCatFileCommand("/testDir/file1-1"),
  169. })
  170. tmpDir := getTestDir(c, "test-cp-to-case-e")
  171. defer os.RemoveAll(tmpDir)
  172. makeTestContentInDir(c, tmpDir)
  173. srcDir := cpPath(tmpDir, "dir1")
  174. dstDir := containerCpPath(containerID, "testDir")
  175. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  176. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1-1\n"), "Should now contain file1-1's contents")
  177. // Now try again but using a trailing path separator for dstDir.
  178. // Make new destination container.
  179. containerID = makeTestContainer(c, testContainerOptions{
  180. command: makeCatFileCommand("/testDir/file1-1"),
  181. })
  182. dstDir = containerCpPathTrailingSep(containerID, "testDir")
  183. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  184. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1-1\n"), "Should now contain file1-1's contents")
  185. }
  186. // F. SRC specifies a directory and DST exists as a file.
  187. //
  188. // This should cause an error as it is not possible to overwrite a file with a
  189. // directory.
  190. func (s *DockerCLICpSuite) TestCpToCaseF(c *testing.T) {
  191. testRequires(c, DaemonIsLinux)
  192. containerID := makeTestContainer(c, testContainerOptions{
  193. addContent: true, workDir: "/root",
  194. })
  195. tmpDir := getTestDir(c, "test-cp-to-case-f")
  196. defer os.RemoveAll(tmpDir)
  197. makeTestContentInDir(c, tmpDir)
  198. srcDir := cpPath(tmpDir, "dir1")
  199. dstFile := containerCpPath(containerID, "/root/file1")
  200. err := runDockerCp(c, srcDir, dstFile)
  201. assert.ErrorContains(c, err, "")
  202. assert.Assert(c, isCpCannotCopyDir(err), "expected ErrCannotCopyDir error, but got %T: %s", err, err)
  203. }
  204. // G. SRC specifies a directory and DST exists as a directory.
  205. //
  206. // This should copy the SRC directory and all its contents to the DST directory.
  207. // Ensure this works whether DST has a trailing path separator or not.
  208. func (s *DockerCLICpSuite) TestCpToCaseG(c *testing.T) {
  209. testRequires(c, DaemonIsLinux)
  210. containerID := makeTestContainer(c, testContainerOptions{
  211. addContent: true, workDir: "/root",
  212. command: makeCatFileCommand("dir2/dir1/file1-1"),
  213. })
  214. tmpDir := getTestDir(c, "test-cp-to-case-g")
  215. defer os.RemoveAll(tmpDir)
  216. makeTestContentInDir(c, tmpDir)
  217. srcDir := cpPath(tmpDir, "dir1")
  218. dstDir := containerCpPath(containerID, "/root/dir2")
  219. assert.NilError(c, containerStartOutputEquals(c, containerID, ""), "dstPath should not have existed")
  220. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  221. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1-1\n"), "Should now contain file1-1's contents")
  222. // Now try again but using a trailing path separator for dstDir.
  223. // Make new destination container.
  224. containerID = makeTestContainer(c, testContainerOptions{
  225. addContent: true,
  226. command: makeCatFileCommand("/dir2/dir1/file1-1"),
  227. })
  228. dstDir = containerCpPathTrailingSep(containerID, "/dir2")
  229. assert.NilError(c, containerStartOutputEquals(c, containerID, ""), "dstPath should not have existed")
  230. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  231. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1-1\n"), "Should now contain file1-1's contents")
  232. }
  233. // H. SRC specifies a directory's contents only and DST does not exist.
  234. //
  235. // This should create a directory at DST and copy the contents of the SRC
  236. // directory (but not the directory itself) into the DST directory. Ensure
  237. // this works whether DST has a trailing path separator or not.
  238. func (s *DockerCLICpSuite) TestCpToCaseH(c *testing.T) {
  239. containerID := makeTestContainer(c, testContainerOptions{
  240. command: makeCatFileCommand("/testDir/file1-1"),
  241. })
  242. tmpDir := getTestDir(c, "test-cp-to-case-h")
  243. defer os.RemoveAll(tmpDir)
  244. makeTestContentInDir(c, tmpDir)
  245. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  246. dstDir := containerCpPath(containerID, "testDir")
  247. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  248. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1-1\n"), "Should now contain file1-1's contents")
  249. // Now try again but using a trailing path separator for dstDir.
  250. // Make new destination container.
  251. containerID = makeTestContainer(c, testContainerOptions{
  252. command: makeCatFileCommand("/testDir/file1-1"),
  253. })
  254. dstDir = containerCpPathTrailingSep(containerID, "testDir")
  255. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  256. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1-1\n"), "Should now contain file1-1's contents")
  257. }
  258. // I. SRC specifies a directory's contents only and DST exists as a file.
  259. //
  260. // This should cause an error as it is not possible to overwrite a file with a
  261. // directory.
  262. func (s *DockerCLICpSuite) TestCpToCaseI(c *testing.T) {
  263. testRequires(c, DaemonIsLinux)
  264. containerID := makeTestContainer(c, testContainerOptions{
  265. addContent: true, workDir: "/root",
  266. })
  267. tmpDir := getTestDir(c, "test-cp-to-case-i")
  268. defer os.RemoveAll(tmpDir)
  269. makeTestContentInDir(c, tmpDir)
  270. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  271. dstFile := containerCpPath(containerID, "/root/file1")
  272. err := runDockerCp(c, srcDir, dstFile)
  273. assert.ErrorContains(c, err, "")
  274. assert.Assert(c, isCpCannotCopyDir(err), "expected ErrCannotCopyDir error, but got %T: %s", err, err)
  275. }
  276. // J. SRC specifies a directory's contents only and DST exists as a directory.
  277. //
  278. // This should copy the contents of the SRC directory (but not the directory
  279. // itself) into the DST directory. Ensure this works whether DST has a
  280. // trailing path separator or not.
  281. func (s *DockerCLICpSuite) TestCpToCaseJ(c *testing.T) {
  282. testRequires(c, DaemonIsLinux)
  283. containerID := makeTestContainer(c, testContainerOptions{
  284. addContent: true, workDir: "/root",
  285. command: makeCatFileCommand("/dir2/file1-1"),
  286. })
  287. tmpDir := getTestDir(c, "test-cp-to-case-j")
  288. defer os.RemoveAll(tmpDir)
  289. makeTestContentInDir(c, tmpDir)
  290. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  291. dstDir := containerCpPath(containerID, "/dir2")
  292. assert.NilError(c, containerStartOutputEquals(c, containerID, ""), "dstPath should not have existed")
  293. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  294. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1-1\n"), "Should've contained file1-1's contents")
  295. // Now try again but using a trailing path separator for dstDir.
  296. // Make new destination container.
  297. containerID = makeTestContainer(c, testContainerOptions{
  298. command: makeCatFileCommand("/dir2/file1-1"),
  299. })
  300. dstDir = containerCpPathTrailingSep(containerID, "/dir2")
  301. assert.NilError(c, containerStartOutputEquals(c, containerID, ""), "dstPath should not have existed")
  302. assert.NilError(c, runDockerCp(c, srcDir, dstDir))
  303. assert.NilError(c, containerStartOutputEquals(c, containerID, "file1-1\n"), "Should've contained file1-1's contents")
  304. }
  305. // The `docker cp` command should also ensure that you cannot
  306. // write to a container rootfs that is marked as read-only.
  307. func (s *DockerCLICpSuite) TestCpToErrReadOnlyRootfs(c *testing.T) {
  308. // --read-only + userns has remount issues
  309. testRequires(c, DaemonIsLinux, NotUserNamespace)
  310. tmpDir := getTestDir(c, "test-cp-to-err-read-only-rootfs")
  311. defer os.RemoveAll(tmpDir)
  312. makeTestContentInDir(c, tmpDir)
  313. containerID := makeTestContainer(c, testContainerOptions{
  314. readOnly: true, workDir: "/root",
  315. command: makeCatFileCommand("shouldNotExist"),
  316. })
  317. srcPath := cpPath(tmpDir, "file1")
  318. dstPath := containerCpPath(containerID, "/root/shouldNotExist")
  319. err := runDockerCp(c, srcPath, dstPath)
  320. assert.ErrorContains(c, err, "")
  321. assert.Assert(c, isCpCannotCopyReadOnly(err), "expected ErrContainerRootfsReadonly error, but got %T: %s", err, err)
  322. assert.NilError(c, containerStartOutputEquals(c, containerID, ""), "dstPath should not have existed")
  323. }
  324. // The `docker cp` command should also ensure that you
  325. // cannot write to a volume that is mounted as read-only.
  326. func (s *DockerCLICpSuite) TestCpToErrReadOnlyVolume(c *testing.T) {
  327. // --read-only + userns has remount issues
  328. testRequires(c, DaemonIsLinux, NotUserNamespace)
  329. tmpDir := getTestDir(c, "test-cp-to-err-read-only-volume")
  330. defer os.RemoveAll(tmpDir)
  331. makeTestContentInDir(c, tmpDir)
  332. containerID := makeTestContainer(c, testContainerOptions{
  333. volumes: defaultVolumes(tmpDir), workDir: "/root",
  334. command: makeCatFileCommand("/vol_ro/shouldNotExist"),
  335. })
  336. srcPath := cpPath(tmpDir, "file1")
  337. dstPath := containerCpPath(containerID, "/vol_ro/shouldNotExist")
  338. err := runDockerCp(c, srcPath, dstPath)
  339. assert.ErrorContains(c, err, "")
  340. assert.Assert(c, isCpCannotCopyReadOnly(err), "expected ErrVolumeReadonly error, but got %T: %s", err, err)
  341. assert.NilError(c, containerStartOutputEquals(c, containerID, ""), "dstPath should not have existed")
  342. }