docker_cli_cp_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. "github.com/docker/docker/integration-cli/cli"
  14. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. "gotest.tools/v3/icmd"
  17. )
  18. const (
  19. cpTestPathParent = "/some"
  20. cpTestPath = "/some/path"
  21. cpTestName = "test"
  22. cpFullPath = "/some/path/test"
  23. cpContainerContents = "holla, i am the container"
  24. cpHostContents = "hello, i am the host"
  25. )
  26. type DockerCLICpSuite struct {
  27. ds *DockerSuite
  28. }
  29. func (s *DockerCLICpSuite) TearDownTest(ctx context.Context, c *testing.T) {
  30. s.ds.TearDownTest(ctx, c)
  31. }
  32. func (s *DockerCLICpSuite) OnTimeout(c *testing.T) {
  33. s.ds.OnTimeout(c)
  34. }
  35. // Ensure that an all-local path case returns an error.
  36. func (s *DockerCLICpSuite) TestCpLocalOnly(c *testing.T) {
  37. err := runDockerCp(c, "foo", "bar")
  38. assert.ErrorContains(c, err, "must specify at least one container source")
  39. }
  40. // Test for #5656
  41. // Check that garbage paths don't escape the container's rootfs
  42. func (s *DockerCLICpSuite) TestCpGarbagePath(c *testing.T) {
  43. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath).Stdout()
  44. containerID = strings.TrimSpace(containerID)
  45. out := cli.DockerCmd(c, "wait", containerID).Combined()
  46. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  47. assert.NilError(c, os.MkdirAll(cpTestPath, os.ModeDir))
  48. hostFile, err := os.Create(cpFullPath)
  49. assert.NilError(c, err)
  50. defer hostFile.Close()
  51. defer os.RemoveAll(cpTestPathParent)
  52. fmt.Fprintf(hostFile, "%s", cpHostContents)
  53. tmpdir, err := os.MkdirTemp("", "docker-integration")
  54. assert.NilError(c, err)
  55. tmpname := filepath.Join(tmpdir, cpTestName)
  56. defer os.RemoveAll(tmpdir)
  57. containerPath := path.Join("../../../../../../../../../../../../", cpFullPath)
  58. cli.DockerCmd(c, "cp", containerID+":"+containerPath, tmpdir)
  59. file, _ := os.Open(tmpname)
  60. defer file.Close()
  61. test, err := io.ReadAll(file)
  62. assert.NilError(c, err)
  63. assert.Assert(c, string(test) != cpHostContents, "output matched host file -- garbage path can escape container rootfs")
  64. assert.Assert(c, string(test) == cpContainerContents, "output doesn't match the input for garbage path")
  65. }
  66. // Check that relative paths are relative to the container's rootfs
  67. func (s *DockerCLICpSuite) TestCpRelativePath(c *testing.T) {
  68. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath).Stdout()
  69. containerID = strings.TrimSpace(containerID)
  70. out := cli.DockerCmd(c, "wait", containerID).Combined()
  71. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  72. assert.NilError(c, os.MkdirAll(cpTestPath, os.ModeDir))
  73. hostFile, err := os.Create(cpFullPath)
  74. assert.NilError(c, err)
  75. defer hostFile.Close()
  76. defer os.RemoveAll(cpTestPathParent)
  77. fmt.Fprintf(hostFile, "%s", cpHostContents)
  78. tmpdir, err := os.MkdirTemp("", "docker-integration")
  79. assert.NilError(c, err)
  80. tmpname := filepath.Join(tmpdir, cpTestName)
  81. defer os.RemoveAll(tmpdir)
  82. var relPath string
  83. if path.IsAbs(cpFullPath) {
  84. // normally this is `filepath.Rel("/", cpFullPath)` but we cannot
  85. // get this unix-path manipulation on windows with filepath.
  86. relPath = cpFullPath[1:]
  87. }
  88. assert.Assert(c, path.IsAbs(cpFullPath), "path %s was assumed to be an absolute path", cpFullPath)
  89. cli.DockerCmd(c, "cp", containerID+":"+relPath, tmpdir)
  90. file, _ := os.Open(tmpname)
  91. defer file.Close()
  92. test, err := io.ReadAll(file)
  93. assert.NilError(c, err)
  94. assert.Assert(c, string(test) != cpHostContents, "output matched host file -- relative path can escape container rootfs")
  95. assert.Assert(c, string(test) == cpContainerContents, "output doesn't match the input for relative path")
  96. }
  97. // Check that absolute paths are relative to the container's rootfs
  98. func (s *DockerCLICpSuite) TestCpAbsolutePath(c *testing.T) {
  99. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath).Stdout()
  100. containerID = strings.TrimSpace(containerID)
  101. out := cli.DockerCmd(c, "wait", containerID).Combined()
  102. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  103. assert.NilError(c, os.MkdirAll(cpTestPath, os.ModeDir))
  104. hostFile, err := os.Create(cpFullPath)
  105. assert.NilError(c, err)
  106. defer hostFile.Close()
  107. defer os.RemoveAll(cpTestPathParent)
  108. fmt.Fprintf(hostFile, "%s", cpHostContents)
  109. tmpdir, err := os.MkdirTemp("", "docker-integration")
  110. assert.NilError(c, err)
  111. tmpname := filepath.Join(tmpdir, cpTestName)
  112. defer os.RemoveAll(tmpdir)
  113. cli.DockerCmd(c, "cp", containerID+":"+cpFullPath, tmpdir)
  114. file, _ := os.Open(tmpname)
  115. defer file.Close()
  116. test, err := io.ReadAll(file)
  117. assert.NilError(c, err)
  118. assert.Assert(c, string(test) != cpHostContents, "output matched host file -- absolute path can escape container rootfs")
  119. assert.Assert(c, string(test) == cpContainerContents, "output doesn't match the input for absolute path")
  120. }
  121. // Test for #5619
  122. // Check that absolute symlinks are still relative to the container's rootfs
  123. func (s *DockerCLICpSuite) TestCpAbsoluteSymlink(c *testing.T) {
  124. testRequires(c, DaemonIsLinux)
  125. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path").Stdout()
  126. containerID = strings.TrimSpace(containerID)
  127. out := cli.DockerCmd(c, "wait", containerID).Combined()
  128. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  129. assert.NilError(c, os.MkdirAll(cpTestPath, os.ModeDir))
  130. hostFile, err := os.Create(cpFullPath)
  131. assert.NilError(c, err)
  132. defer hostFile.Close()
  133. defer os.RemoveAll(cpTestPathParent)
  134. fmt.Fprintf(hostFile, "%s", cpHostContents)
  135. tmpdir, err := os.MkdirTemp("", "docker-integration")
  136. assert.NilError(c, err)
  137. tmpname := filepath.Join(tmpdir, "container_path")
  138. defer os.RemoveAll(tmpdir)
  139. containerPath := path.Join("/", "container_path")
  140. cli.DockerCmd(c, "cp", containerID+":"+containerPath, tmpdir)
  141. // We should have copied a symlink *NOT* the file itself!
  142. linkTarget, err := os.Readlink(tmpname)
  143. assert.NilError(c, err)
  144. assert.Equal(c, linkTarget, filepath.FromSlash(cpFullPath))
  145. }
  146. // Check that symlinks to a directory behave as expected when copying one from
  147. // a container.
  148. func (s *DockerCLICpSuite) TestCpFromSymlinkToDirectory(c *testing.T) {
  149. testRequires(c, DaemonIsLinux)
  150. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPathParent+" /dir_link").Stdout()
  151. containerID = strings.TrimSpace(containerID)
  152. out := cli.DockerCmd(c, "wait", containerID).Combined()
  153. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  154. testDir, err := os.MkdirTemp("", "test-cp-from-symlink-to-dir-")
  155. assert.NilError(c, err)
  156. defer os.RemoveAll(testDir)
  157. // This copy command should copy the symlink, not the target, into the
  158. // temporary directory.
  159. cli.DockerCmd(c, "cp", containerID+":"+"/dir_link", testDir)
  160. expectedPath := filepath.Join(testDir, "dir_link")
  161. linkTarget, err := os.Readlink(expectedPath)
  162. assert.NilError(c, err)
  163. assert.Equal(c, linkTarget, filepath.FromSlash(cpTestPathParent))
  164. os.Remove(expectedPath)
  165. // This copy command should resolve the symlink (note the trailing
  166. // separator), copying the target into the temporary directory.
  167. cli.DockerCmd(c, "cp", containerID+":"+"/dir_link/", testDir)
  168. // It *should not* have copied the directory using the target's name, but
  169. // used the given name instead.
  170. unexpectedPath := filepath.Join(testDir, cpTestPathParent)
  171. stat, err := os.Lstat(unexpectedPath)
  172. if err == nil {
  173. out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name())
  174. }
  175. assert.ErrorContains(c, err, "", out)
  176. // It *should* have copied the directory using the asked name "dir_link".
  177. stat, err = os.Lstat(expectedPath)
  178. assert.NilError(c, err, "unable to stat resource at %q", expectedPath)
  179. assert.Assert(c, stat.IsDir(), "should have copied a directory but got %q instead", stat.Mode())
  180. }
  181. // Check that symlinks to a directory behave as expected when copying one to a
  182. // container.
  183. func (s *DockerCLICpSuite) TestCpToSymlinkToDirectory(c *testing.T) {
  184. testRequires(c, DaemonIsLinux)
  185. testRequires(c, testEnv.IsLocalDaemon) // Requires local volume mount bind.
  186. testVol, err := os.MkdirTemp("", "test-cp-to-symlink-to-dir-")
  187. assert.NilError(c, err)
  188. defer os.RemoveAll(testVol)
  189. // Create a test container with a local volume. We will test by copying
  190. // to the volume path in the container which we can then verify locally.
  191. containerID := cli.DockerCmd(c, "create", "-v", testVol+":/testVol", "busybox").Stdout()
  192. containerID = strings.TrimSpace(containerID)
  193. // Create a temp directory to hold a test file nested in a directory.
  194. testDir, err := os.MkdirTemp("", "test-cp-to-symlink-to-dir-")
  195. assert.NilError(c, err)
  196. defer os.RemoveAll(testDir)
  197. // This file will be at "/testDir/some/path/test" and will be copied into
  198. // the test volume later.
  199. hostTestFilename := filepath.Join(testDir, cpFullPath)
  200. assert.NilError(c, os.MkdirAll(filepath.Dir(hostTestFilename), os.FileMode(0o700)))
  201. assert.NilError(c, os.WriteFile(hostTestFilename, []byte(cpHostContents), os.FileMode(0o600)))
  202. // Now create another temp directory to hold a symlink to the
  203. // "/testDir/some" directory.
  204. linkDir, err := os.MkdirTemp("", "test-cp-to-symlink-to-dir-")
  205. assert.NilError(c, err)
  206. defer os.RemoveAll(linkDir)
  207. // Then symlink "/linkDir/dir_link" to "/testdir/some".
  208. linkTarget := filepath.Join(testDir, cpTestPathParent)
  209. localLink := filepath.Join(linkDir, "dir_link")
  210. assert.NilError(c, os.Symlink(linkTarget, localLink))
  211. // Now copy that symlink into the test volume in the container.
  212. cli.DockerCmd(c, "cp", localLink, containerID+":/testVol")
  213. // This copy command should have copied the symlink *not* the target.
  214. expectedPath := filepath.Join(testVol, "dir_link")
  215. actualLinkTarget, err := os.Readlink(expectedPath)
  216. assert.NilError(c, err, "unable to read symlink at %q", expectedPath)
  217. assert.Equal(c, actualLinkTarget, linkTarget)
  218. // Good, now remove that copied link for the next test.
  219. os.Remove(expectedPath)
  220. // This copy command should resolve the symlink (note the trailing
  221. // separator), copying the target into the test volume directory in the
  222. // container.
  223. cli.DockerCmd(c, "cp", localLink+"/", containerID+":/testVol")
  224. // It *should not* have copied the directory using the target's name, but
  225. // used the given name instead.
  226. unexpectedPath := filepath.Join(testVol, cpTestPathParent)
  227. stat, err := os.Lstat(unexpectedPath)
  228. if err == nil {
  229. c.Errorf("target name was unexpectedly preserved: %q - %q", stat.Mode(), stat.Name())
  230. }
  231. // It *should* have copied the directory using the asked name "dir_link".
  232. stat, err = os.Lstat(expectedPath)
  233. assert.NilError(c, err, "unable to stat resource at %q", expectedPath)
  234. assert.Assert(c, stat.IsDir(), "should have copied a directory but got %q instead", stat.Mode())
  235. // And this directory should contain the file copied from the host at the
  236. // expected location: "/testVol/dir_link/path/test"
  237. expectedFilepath := filepath.Join(testVol, "dir_link/path/test")
  238. fileContents, err := os.ReadFile(expectedFilepath)
  239. assert.NilError(c, err)
  240. assert.Equal(c, string(fileContents), cpHostContents)
  241. }
  242. // Test for #5619
  243. // Check that symlinks which are part of the resource path are still relative to the container's rootfs
  244. func (s *DockerCLICpSuite) TestCpSymlinkComponent(c *testing.T) {
  245. testRequires(c, DaemonIsLinux)
  246. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path").Stdout()
  247. containerID = strings.TrimSpace(containerID)
  248. out := cli.DockerCmd(c, "wait", containerID).Combined()
  249. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  250. assert.NilError(c, os.MkdirAll(cpTestPath, os.ModeDir))
  251. hostFile, err := os.Create(cpFullPath)
  252. assert.NilError(c, err)
  253. defer hostFile.Close()
  254. defer os.RemoveAll(cpTestPathParent)
  255. fmt.Fprintf(hostFile, "%s", cpHostContents)
  256. tmpdir, err := os.MkdirTemp("", "docker-integration")
  257. assert.NilError(c, err)
  258. tmpname := filepath.Join(tmpdir, cpTestName)
  259. defer os.RemoveAll(tmpdir)
  260. containerPath := path.Join("/", "container_path", cpTestName)
  261. cli.DockerCmd(c, "cp", containerID+":"+containerPath, tmpdir)
  262. file, _ := os.Open(tmpname)
  263. defer file.Close()
  264. test, err := io.ReadAll(file)
  265. assert.NilError(c, err)
  266. assert.Assert(c, string(test) != cpHostContents, "output matched host file -- symlink path component can escape container rootfs")
  267. assert.Equal(c, string(test), cpContainerContents, "output doesn't match the input for symlink path component")
  268. }
  269. // Check that cp with unprivileged user doesn't return any error
  270. func (s *DockerCLICpSuite) TestCpUnprivilegedUser(c *testing.T) {
  271. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  272. testRequires(c, UnixCli) // uses chmod/su: not available on windows
  273. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName).Stdout()
  274. containerID = strings.TrimSpace(containerID)
  275. out := cli.DockerCmd(c, "wait", containerID).Combined()
  276. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  277. tmpdir, err := os.MkdirTemp("", "docker-integration")
  278. assert.NilError(c, err)
  279. defer os.RemoveAll(tmpdir)
  280. err = os.Chmod(tmpdir, 0o777)
  281. assert.NilError(c, err)
  282. result := icmd.RunCommand("su", "unprivilegeduser", "-c", fmt.Sprintf("%s cp %s:%s %s", dockerBinary, containerID, cpTestName, tmpdir))
  283. result.Assert(c, icmd.Expected{})
  284. }
  285. func (s *DockerCLICpSuite) TestCpSpecialFiles(c *testing.T) {
  286. testRequires(c, DaemonIsLinux)
  287. testRequires(c, testEnv.IsLocalDaemon)
  288. outDir, err := os.MkdirTemp("", "cp-test-special-files")
  289. assert.NilError(c, err)
  290. defer os.RemoveAll(outDir)
  291. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch /foo").Stdout()
  292. containerID = strings.TrimSpace(containerID)
  293. out := cli.DockerCmd(c, "wait", containerID).Combined()
  294. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  295. // Copy actual /etc/resolv.conf
  296. cli.DockerCmd(c, "cp", containerID+":/etc/resolv.conf", outDir)
  297. expected := readContainerFile(c, containerID, "resolv.conf")
  298. actual, err := os.ReadFile(outDir + "/resolv.conf")
  299. assert.NilError(c, err)
  300. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container resolvconf")
  301. // Copy actual /etc/hosts
  302. cli.DockerCmd(c, "cp", containerID+":/etc/hosts", outDir)
  303. expected = readContainerFile(c, containerID, "hosts")
  304. actual, err = os.ReadFile(outDir + "/hosts")
  305. assert.NilError(c, err)
  306. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container hosts")
  307. // Copy actual /etc/resolv.conf
  308. cli.DockerCmd(c, "cp", containerID+":/etc/hostname", outDir)
  309. expected = readContainerFile(c, containerID, "hostname")
  310. actual, err = os.ReadFile(outDir + "/hostname")
  311. assert.NilError(c, err)
  312. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container hostname")
  313. }
  314. func (s *DockerCLICpSuite) TestCpVolumePath(c *testing.T) {
  315. // stat /tmp/cp-test-volumepath851508420/test gets permission denied for the user
  316. testRequires(c, NotUserNamespace)
  317. testRequires(c, DaemonIsLinux)
  318. testRequires(c, testEnv.IsLocalDaemon)
  319. tmpDir, err := os.MkdirTemp("", "cp-test-volumepath")
  320. assert.NilError(c, err)
  321. defer os.RemoveAll(tmpDir)
  322. outDir, err := os.MkdirTemp("", "cp-test-volumepath-out")
  323. assert.NilError(c, err)
  324. defer os.RemoveAll(outDir)
  325. _, err = os.Create(tmpDir + "/test")
  326. assert.NilError(c, err)
  327. containerID := cli.DockerCmd(c, "run", "-d", "-v", "/foo", "-v", tmpDir+"/test:/test", "-v", tmpDir+":/baz", "busybox", "/bin/sh", "-c", "touch /foo/bar").Stdout()
  328. containerID = strings.TrimSpace(containerID)
  329. out := cli.DockerCmd(c, "wait", containerID).Combined()
  330. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  331. // Copy actual volume path
  332. cli.DockerCmd(c, "cp", containerID+":/foo", outDir)
  333. stat, err := os.Stat(outDir + "/foo")
  334. assert.NilError(c, err)
  335. assert.Assert(c, stat.IsDir(), "Expected copied content to be dir")
  336. stat, err = os.Stat(outDir + "/foo/bar")
  337. assert.NilError(c, err)
  338. assert.Assert(c, !stat.IsDir(), "Expected file `bar` to be a file")
  339. // Copy file nested in volume
  340. cli.DockerCmd(c, "cp", containerID+":/foo/bar", outDir)
  341. stat, err = os.Stat(outDir + "/bar")
  342. assert.NilError(c, err)
  343. assert.Assert(c, !stat.IsDir(), "Expected file `bar` to be a file")
  344. // Copy Bind-mounted dir
  345. cli.DockerCmd(c, "cp", containerID+":/baz", outDir)
  346. stat, err = os.Stat(outDir + "/baz")
  347. assert.NilError(c, err)
  348. assert.Assert(c, stat.IsDir(), "Expected `baz` to be a dir")
  349. // Copy file nested in bind-mounted dir
  350. cli.DockerCmd(c, "cp", containerID+":/baz/test", outDir)
  351. fb, err := os.ReadFile(outDir + "/baz/test")
  352. assert.NilError(c, err)
  353. fb2, err := os.ReadFile(tmpDir + "/test")
  354. assert.NilError(c, err)
  355. assert.Assert(c, bytes.Equal(fb, fb2), "Expected copied file to be duplicate of bind-mounted file")
  356. // Copy bind-mounted file
  357. cli.DockerCmd(c, "cp", containerID+":/test", outDir)
  358. fb, err = os.ReadFile(outDir + "/test")
  359. assert.NilError(c, err)
  360. fb2, err = os.ReadFile(tmpDir + "/test")
  361. assert.NilError(c, err)
  362. assert.Assert(c, bytes.Equal(fb, fb2), "Expected copied file to be duplicate of bind-mounted file")
  363. }
  364. func (s *DockerCLICpSuite) TestCpToDot(c *testing.T) {
  365. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test").Stdout()
  366. containerID = strings.TrimSpace(containerID)
  367. out := cli.DockerCmd(c, "wait", containerID).Combined()
  368. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  369. tmpdir, err := os.MkdirTemp("", "docker-integration")
  370. assert.NilError(c, err)
  371. defer os.RemoveAll(tmpdir)
  372. cwd, err := os.Getwd()
  373. assert.NilError(c, err)
  374. defer os.Chdir(cwd)
  375. err = os.Chdir(tmpdir)
  376. assert.NilError(c, err)
  377. cli.DockerCmd(c, "cp", containerID+":/test", ".")
  378. content, err := os.ReadFile("./test")
  379. assert.NilError(c, err)
  380. assert.Equal(c, string(content), "lololol\n")
  381. }
  382. func (s *DockerCLICpSuite) TestCpToStdout(c *testing.T) {
  383. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test").Stdout()
  384. containerID = strings.TrimSpace(containerID)
  385. out := cli.DockerCmd(c, "wait", containerID).Combined()
  386. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  387. out, err := RunCommandPipelineWithOutput(
  388. exec.Command(dockerBinary, "cp", containerID+":/test", "-"),
  389. exec.Command("tar", "-vtf", "-"))
  390. assert.NilError(c, err)
  391. assert.Check(c, is.Contains(out, "test"))
  392. assert.Check(c, is.Contains(out, "-rw"))
  393. }
  394. func (s *DockerCLICpSuite) TestCpNameHasColon(c *testing.T) {
  395. testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
  396. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /te:s:t").Stdout()
  397. containerID = strings.TrimSpace(containerID)
  398. out := cli.DockerCmd(c, "wait", containerID).Combined()
  399. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  400. tmpdir, err := os.MkdirTemp("", "docker-integration")
  401. assert.NilError(c, err)
  402. defer os.RemoveAll(tmpdir)
  403. cli.DockerCmd(c, "cp", containerID+":/te:s:t", tmpdir)
  404. content, err := os.ReadFile(tmpdir + "/te:s:t")
  405. assert.NilError(c, err)
  406. assert.Equal(c, string(content), "lololol\n")
  407. }
  408. func (s *DockerCLICpSuite) TestCopyAndRestart(c *testing.T) {
  409. testRequires(c, DaemonIsLinux)
  410. const expectedMsg = "hello"
  411. containerID := cli.DockerCmd(c, "run", "-d", "busybox", "echo", expectedMsg).Stdout()
  412. containerID = strings.TrimSpace(containerID)
  413. out := cli.DockerCmd(c, "wait", containerID).Combined()
  414. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  415. tmpDir, err := os.MkdirTemp("", "test-docker-restart-after-copy-")
  416. assert.NilError(c, err)
  417. defer os.RemoveAll(tmpDir)
  418. cli.DockerCmd(c, "cp", fmt.Sprintf("%s:/etc/group", containerID), tmpDir)
  419. out = cli.DockerCmd(c, "start", "-a", containerID).Combined()
  420. assert.Equal(c, strings.TrimSpace(out), expectedMsg)
  421. }
  422. func (s *DockerCLICpSuite) TestCopyCreatedContainer(c *testing.T) {
  423. testRequires(c, DaemonIsLinux)
  424. cli.DockerCmd(c, "create", "--name", "test_cp", "-v", "/test", "busybox")
  425. tmpDir, err := os.MkdirTemp("", "test")
  426. assert.NilError(c, err)
  427. defer os.RemoveAll(tmpDir)
  428. cli.DockerCmd(c, "cp", "test_cp:/bin/sh", tmpDir)
  429. }
  430. // test copy with option `-L`: following symbol link
  431. // Check that symlinks to a file behave as expected when copying one from
  432. // a container to host following symbol link
  433. func (s *DockerCLICpSuite) TestCpSymlinkFromConToHostFollowSymlink(c *testing.T) {
  434. testRequires(c, DaemonIsLinux)
  435. result := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" /dir_link")
  436. assert.Equal(c, result.ExitCode, 0, "failed to set up container: %s", result.Combined())
  437. containerID := strings.TrimSpace(result.Stdout())
  438. out := cli.DockerCmd(c, "wait", containerID).Combined()
  439. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  440. testDir, err := os.MkdirTemp("", "test-cp-symlink-container-to-host-follow-symlink")
  441. assert.NilError(c, err)
  442. defer os.RemoveAll(testDir)
  443. // This copy command should copy the symlink, not the target, into the
  444. // temporary directory.
  445. cli.DockerCmd(c, "cp", "-L", containerID+":"+"/dir_link", testDir)
  446. expectedPath := filepath.Join(testDir, "dir_link")
  447. expected := []byte(cpContainerContents)
  448. actual, err := os.ReadFile(expectedPath)
  449. assert.NilError(c, err)
  450. os.Remove(expectedPath)
  451. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container symbol link target")
  452. // now test copy symbol link to a non-existing file in host
  453. expectedPath = filepath.Join(testDir, "somefile_host")
  454. // expectedPath shouldn't exist, if exists, remove it
  455. if _, err := os.Lstat(expectedPath); err == nil {
  456. os.Remove(expectedPath)
  457. }
  458. cli.DockerCmd(c, "cp", "-L", containerID+":"+"/dir_link", expectedPath)
  459. actual, err = os.ReadFile(expectedPath)
  460. assert.NilError(c, err)
  461. defer os.Remove(expectedPath)
  462. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container symbol link target")
  463. }