docker_cli_cp_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. "gotest.tools/v3/icmd"
  16. )
  17. const (
  18. cpTestPathParent = "/some"
  19. cpTestPath = "/some/path"
  20. cpTestName = "test"
  21. cpFullPath = "/some/path/test"
  22. cpContainerContents = "holla, i am the container"
  23. cpHostContents = "hello, i am the host"
  24. )
  25. type DockerCLICpSuite struct {
  26. ds *DockerSuite
  27. }
  28. func (s *DockerCLICpSuite) TearDownTest(ctx context.Context, c *testing.T) {
  29. s.ds.TearDownTest(ctx, c)
  30. }
  31. func (s *DockerCLICpSuite) OnTimeout(c *testing.T) {
  32. s.ds.OnTimeout(c)
  33. }
  34. // Ensure that an all-local path case returns an error.
  35. func (s *DockerCLICpSuite) TestCpLocalOnly(c *testing.T) {
  36. err := runDockerCp(c, "foo", "bar")
  37. assert.ErrorContains(c, err, "must specify at least one container source")
  38. }
  39. // Test for #5656
  40. // Check that garbage paths don't escape the container's rootfs
  41. func (s *DockerCLICpSuite) TestCpGarbagePath(c *testing.T) {
  42. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
  43. containerID := strings.TrimSpace(out)
  44. out, _ = dockerCmd(c, "wait", containerID)
  45. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  46. assert.NilError(c, os.MkdirAll(cpTestPath, os.ModeDir))
  47. hostFile, err := os.Create(cpFullPath)
  48. assert.NilError(c, err)
  49. defer hostFile.Close()
  50. defer os.RemoveAll(cpTestPathParent)
  51. fmt.Fprintf(hostFile, "%s", cpHostContents)
  52. tmpdir, err := os.MkdirTemp("", "docker-integration")
  53. assert.NilError(c, err)
  54. tmpname := filepath.Join(tmpdir, cpTestName)
  55. defer os.RemoveAll(tmpdir)
  56. path := path.Join("../../../../../../../../../../../../", cpFullPath)
  57. dockerCmd(c, "cp", containerID+":"+path, tmpdir)
  58. file, _ := os.Open(tmpname)
  59. defer file.Close()
  60. test, err := io.ReadAll(file)
  61. assert.NilError(c, err)
  62. assert.Assert(c, string(test) != cpHostContents, "output matched host file -- garbage path can escape container rootfs")
  63. assert.Assert(c, string(test) == cpContainerContents, "output doesn't match the input for garbage path")
  64. }
  65. // Check that relative paths are relative to the container's rootfs
  66. func (s *DockerCLICpSuite) TestCpRelativePath(c *testing.T) {
  67. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
  68. containerID := strings.TrimSpace(out)
  69. out, _ = dockerCmd(c, "wait", containerID)
  70. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  71. assert.NilError(c, os.MkdirAll(cpTestPath, os.ModeDir))
  72. hostFile, err := os.Create(cpFullPath)
  73. assert.NilError(c, err)
  74. defer hostFile.Close()
  75. defer os.RemoveAll(cpTestPathParent)
  76. fmt.Fprintf(hostFile, "%s", cpHostContents)
  77. tmpdir, err := os.MkdirTemp("", "docker-integration")
  78. assert.NilError(c, err)
  79. tmpname := filepath.Join(tmpdir, cpTestName)
  80. defer os.RemoveAll(tmpdir)
  81. var relPath string
  82. if path.IsAbs(cpFullPath) {
  83. // normally this is `filepath.Rel("/", cpFullPath)` but we cannot
  84. // get this unix-path manipulation on windows with filepath.
  85. relPath = cpFullPath[1:]
  86. }
  87. assert.Assert(c, path.IsAbs(cpFullPath), "path %s was assumed to be an absolute path", cpFullPath)
  88. dockerCmd(c, "cp", containerID+":"+relPath, tmpdir)
  89. file, _ := os.Open(tmpname)
  90. defer file.Close()
  91. test, err := io.ReadAll(file)
  92. assert.NilError(c, err)
  93. assert.Assert(c, string(test) != cpHostContents, "output matched host file -- relative path can escape container rootfs")
  94. assert.Assert(c, string(test) == cpContainerContents, "output doesn't match the input for relative path")
  95. }
  96. // Check that absolute paths are relative to the container's rootfs
  97. func (s *DockerCLICpSuite) TestCpAbsolutePath(c *testing.T) {
  98. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
  99. containerID := strings.TrimSpace(out)
  100. out, _ = dockerCmd(c, "wait", containerID)
  101. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  102. assert.NilError(c, os.MkdirAll(cpTestPath, os.ModeDir))
  103. hostFile, err := os.Create(cpFullPath)
  104. assert.NilError(c, err)
  105. defer hostFile.Close()
  106. defer os.RemoveAll(cpTestPathParent)
  107. fmt.Fprintf(hostFile, "%s", cpHostContents)
  108. tmpdir, err := os.MkdirTemp("", "docker-integration")
  109. assert.NilError(c, err)
  110. tmpname := filepath.Join(tmpdir, cpTestName)
  111. defer os.RemoveAll(tmpdir)
  112. path := cpFullPath
  113. dockerCmd(c, "cp", containerID+":"+path, 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. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path")
  126. containerID := strings.TrimSpace(out)
  127. out, _ = dockerCmd(c, "wait", containerID)
  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. path := path.Join("/", "container_path")
  140. dockerCmd(c, "cp", containerID+":"+path, 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. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPathParent+" /dir_link")
  151. containerID := strings.TrimSpace(out)
  152. out, _ = dockerCmd(c, "wait", containerID)
  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. 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. 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. out, _ := dockerCmd(c, "create", "-v", testVol+":/testVol", "busybox")
  192. containerID := strings.TrimSpace(out)
  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. 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. 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. out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name())
  230. }
  231. assert.ErrorContains(c, err, "", out)
  232. // It *should* have copied the directory using the asked name "dir_link".
  233. stat, err = os.Lstat(expectedPath)
  234. assert.NilError(c, err, "unable to stat resource at %q", expectedPath)
  235. assert.Assert(c, stat.IsDir(), "should have copied a directory but got %q instead", stat.Mode())
  236. // And this directory should contain the file copied from the host at the
  237. // expected location: "/testVol/dir_link/path/test"
  238. expectedFilepath := filepath.Join(testVol, "dir_link/path/test")
  239. fileContents, err := os.ReadFile(expectedFilepath)
  240. assert.NilError(c, err)
  241. assert.Equal(c, string(fileContents), cpHostContents)
  242. }
  243. // Test for #5619
  244. // Check that symlinks which are part of the resource path are still relative to the container's rootfs
  245. func (s *DockerCLICpSuite) TestCpSymlinkComponent(c *testing.T) {
  246. testRequires(c, DaemonIsLinux)
  247. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path")
  248. containerID := strings.TrimSpace(out)
  249. out, _ = dockerCmd(c, "wait", containerID)
  250. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  251. assert.NilError(c, os.MkdirAll(cpTestPath, os.ModeDir))
  252. hostFile, err := os.Create(cpFullPath)
  253. assert.NilError(c, err)
  254. defer hostFile.Close()
  255. defer os.RemoveAll(cpTestPathParent)
  256. fmt.Fprintf(hostFile, "%s", cpHostContents)
  257. tmpdir, err := os.MkdirTemp("", "docker-integration")
  258. assert.NilError(c, err)
  259. tmpname := filepath.Join(tmpdir, cpTestName)
  260. defer os.RemoveAll(tmpdir)
  261. path := path.Join("/", "container_path", cpTestName)
  262. dockerCmd(c, "cp", containerID+":"+path, tmpdir)
  263. file, _ := os.Open(tmpname)
  264. defer file.Close()
  265. test, err := io.ReadAll(file)
  266. assert.NilError(c, err)
  267. assert.Assert(c, string(test) != cpHostContents, "output matched host file -- symlink path component can escape container rootfs")
  268. assert.Equal(c, string(test), cpContainerContents, "output doesn't match the input for symlink path component")
  269. }
  270. // Check that cp with unprivileged user doesn't return any error
  271. func (s *DockerCLICpSuite) TestCpUnprivilegedUser(c *testing.T) {
  272. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  273. testRequires(c, UnixCli) // uses chmod/su: not available on windows
  274. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName)
  275. containerID := strings.TrimSpace(out)
  276. out, _ = dockerCmd(c, "wait", containerID)
  277. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  278. tmpdir, err := os.MkdirTemp("", "docker-integration")
  279. assert.NilError(c, err)
  280. defer os.RemoveAll(tmpdir)
  281. err = os.Chmod(tmpdir, 0o777)
  282. assert.NilError(c, err)
  283. result := icmd.RunCommand("su", "unprivilegeduser", "-c",
  284. fmt.Sprintf("%s cp %s:%s %s", dockerBinary, containerID, cpTestName, tmpdir))
  285. result.Assert(c, icmd.Expected{})
  286. }
  287. func (s *DockerCLICpSuite) TestCpSpecialFiles(c *testing.T) {
  288. testRequires(c, DaemonIsLinux)
  289. testRequires(c, testEnv.IsLocalDaemon)
  290. outDir, err := os.MkdirTemp("", "cp-test-special-files")
  291. assert.NilError(c, err)
  292. defer os.RemoveAll(outDir)
  293. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch /foo")
  294. containerID := strings.TrimSpace(out)
  295. out, _ = dockerCmd(c, "wait", containerID)
  296. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  297. // Copy actual /etc/resolv.conf
  298. dockerCmd(c, "cp", containerID+":/etc/resolv.conf", outDir)
  299. expected := readContainerFile(c, containerID, "resolv.conf")
  300. actual, err := os.ReadFile(outDir + "/resolv.conf")
  301. assert.NilError(c, err)
  302. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container resolvconf")
  303. // Copy actual /etc/hosts
  304. dockerCmd(c, "cp", containerID+":/etc/hosts", outDir)
  305. expected = readContainerFile(c, containerID, "hosts")
  306. actual, err = os.ReadFile(outDir + "/hosts")
  307. assert.NilError(c, err)
  308. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container hosts")
  309. // Copy actual /etc/resolv.conf
  310. dockerCmd(c, "cp", containerID+":/etc/hostname", outDir)
  311. expected = readContainerFile(c, containerID, "hostname")
  312. actual, err = os.ReadFile(outDir + "/hostname")
  313. assert.NilError(c, err)
  314. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container hostname")
  315. }
  316. func (s *DockerCLICpSuite) TestCpVolumePath(c *testing.T) {
  317. // stat /tmp/cp-test-volumepath851508420/test gets permission denied for the user
  318. testRequires(c, NotUserNamespace)
  319. testRequires(c, DaemonIsLinux)
  320. testRequires(c, testEnv.IsLocalDaemon)
  321. tmpDir, err := os.MkdirTemp("", "cp-test-volumepath")
  322. assert.NilError(c, err)
  323. defer os.RemoveAll(tmpDir)
  324. outDir, err := os.MkdirTemp("", "cp-test-volumepath-out")
  325. assert.NilError(c, err)
  326. defer os.RemoveAll(outDir)
  327. _, err = os.Create(tmpDir + "/test")
  328. assert.NilError(c, err)
  329. out, _ := dockerCmd(c, "run", "-d", "-v", "/foo", "-v", tmpDir+"/test:/test", "-v", tmpDir+":/baz", "busybox", "/bin/sh", "-c", "touch /foo/bar")
  330. containerID := strings.TrimSpace(out)
  331. out, _ = dockerCmd(c, "wait", containerID)
  332. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  333. // Copy actual volume path
  334. dockerCmd(c, "cp", containerID+":/foo", outDir)
  335. stat, err := os.Stat(outDir + "/foo")
  336. assert.NilError(c, err)
  337. assert.Assert(c, stat.IsDir(), "Expected copied content to be dir")
  338. stat, err = os.Stat(outDir + "/foo/bar")
  339. assert.NilError(c, err)
  340. assert.Assert(c, !stat.IsDir(), "Expected file `bar` to be a file")
  341. // Copy file nested in volume
  342. dockerCmd(c, "cp", containerID+":/foo/bar", outDir)
  343. stat, err = os.Stat(outDir + "/bar")
  344. assert.NilError(c, err)
  345. assert.Assert(c, !stat.IsDir(), "Expected file `bar` to be a file")
  346. // Copy Bind-mounted dir
  347. dockerCmd(c, "cp", containerID+":/baz", outDir)
  348. stat, err = os.Stat(outDir + "/baz")
  349. assert.NilError(c, err)
  350. assert.Assert(c, stat.IsDir(), "Expected `baz` to be a dir")
  351. // Copy file nested in bind-mounted dir
  352. dockerCmd(c, "cp", containerID+":/baz/test", outDir)
  353. fb, err := os.ReadFile(outDir + "/baz/test")
  354. assert.NilError(c, err)
  355. fb2, err := os.ReadFile(tmpDir + "/test")
  356. assert.NilError(c, err)
  357. assert.Assert(c, bytes.Equal(fb, fb2), "Expected copied file to be duplicate of bind-mounted file")
  358. // Copy bind-mounted file
  359. dockerCmd(c, "cp", containerID+":/test", outDir)
  360. fb, err = os.ReadFile(outDir + "/test")
  361. assert.NilError(c, err)
  362. fb2, err = os.ReadFile(tmpDir + "/test")
  363. assert.NilError(c, err)
  364. assert.Assert(c, bytes.Equal(fb, fb2), "Expected copied file to be duplicate of bind-mounted file")
  365. }
  366. func (s *DockerCLICpSuite) TestCpToDot(c *testing.T) {
  367. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
  368. containerID := strings.TrimSpace(out)
  369. out, _ = dockerCmd(c, "wait", containerID)
  370. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  371. tmpdir, err := os.MkdirTemp("", "docker-integration")
  372. assert.NilError(c, err)
  373. defer os.RemoveAll(tmpdir)
  374. cwd, err := os.Getwd()
  375. assert.NilError(c, err)
  376. defer os.Chdir(cwd)
  377. err = os.Chdir(tmpdir)
  378. assert.NilError(c, err)
  379. dockerCmd(c, "cp", containerID+":/test", ".")
  380. content, err := os.ReadFile("./test")
  381. assert.NilError(c, err)
  382. assert.Equal(c, string(content), "lololol\n")
  383. }
  384. func (s *DockerCLICpSuite) TestCpToStdout(c *testing.T) {
  385. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
  386. containerID := strings.TrimSpace(out)
  387. out, _ = dockerCmd(c, "wait", containerID)
  388. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  389. out, err := RunCommandPipelineWithOutput(
  390. exec.Command(dockerBinary, "cp", containerID+":/test", "-"),
  391. exec.Command("tar", "-vtf", "-"))
  392. assert.NilError(c, err)
  393. assert.Check(c, is.Contains(out, "test"))
  394. assert.Check(c, is.Contains(out, "-rw"))
  395. }
  396. func (s *DockerCLICpSuite) TestCpNameHasColon(c *testing.T) {
  397. testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
  398. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /te:s:t")
  399. containerID := strings.TrimSpace(out)
  400. out, _ = dockerCmd(c, "wait", containerID)
  401. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  402. tmpdir, err := os.MkdirTemp("", "docker-integration")
  403. assert.NilError(c, err)
  404. defer os.RemoveAll(tmpdir)
  405. dockerCmd(c, "cp", containerID+":/te:s:t", tmpdir)
  406. content, err := os.ReadFile(tmpdir + "/te:s:t")
  407. assert.NilError(c, err)
  408. assert.Equal(c, string(content), "lololol\n")
  409. }
  410. func (s *DockerCLICpSuite) TestCopyAndRestart(c *testing.T) {
  411. testRequires(c, DaemonIsLinux)
  412. expectedMsg := "hello"
  413. out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", expectedMsg)
  414. containerID := strings.TrimSpace(out)
  415. out, _ = dockerCmd(c, "wait", containerID)
  416. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  417. tmpDir, err := os.MkdirTemp("", "test-docker-restart-after-copy-")
  418. assert.NilError(c, err)
  419. defer os.RemoveAll(tmpDir)
  420. dockerCmd(c, "cp", fmt.Sprintf("%s:/etc/group", containerID), tmpDir)
  421. out, _ = dockerCmd(c, "start", "-a", containerID)
  422. assert.Equal(c, strings.TrimSpace(out), expectedMsg)
  423. }
  424. func (s *DockerCLICpSuite) TestCopyCreatedContainer(c *testing.T) {
  425. testRequires(c, DaemonIsLinux)
  426. dockerCmd(c, "create", "--name", "test_cp", "-v", "/test", "busybox")
  427. tmpDir, err := os.MkdirTemp("", "test")
  428. assert.NilError(c, err)
  429. defer os.RemoveAll(tmpDir)
  430. dockerCmd(c, "cp", "test_cp:/bin/sh", tmpDir)
  431. }
  432. // test copy with option `-L`: following symbol link
  433. // Check that symlinks to a file behave as expected when copying one from
  434. // a container to host following symbol link
  435. func (s *DockerCLICpSuite) TestCpSymlinkFromConToHostFollowSymlink(c *testing.T) {
  436. testRequires(c, DaemonIsLinux)
  437. out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" /dir_link")
  438. assert.Equal(c, exitCode, 0, "failed to set up container: %s", out)
  439. cleanedContainerID := strings.TrimSpace(out)
  440. out, _ = dockerCmd(c, "wait", cleanedContainerID)
  441. assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
  442. testDir, err := os.MkdirTemp("", "test-cp-symlink-container-to-host-follow-symlink")
  443. assert.NilError(c, err)
  444. defer os.RemoveAll(testDir)
  445. // This copy command should copy the symlink, not the target, into the
  446. // temporary directory.
  447. dockerCmd(c, "cp", "-L", cleanedContainerID+":"+"/dir_link", testDir)
  448. expectedPath := filepath.Join(testDir, "dir_link")
  449. expected := []byte(cpContainerContents)
  450. actual, err := os.ReadFile(expectedPath)
  451. assert.NilError(c, err)
  452. os.Remove(expectedPath)
  453. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container symbol link target")
  454. // now test copy symbol link to a non-existing file in host
  455. expectedPath = filepath.Join(testDir, "somefile_host")
  456. // expectedPath shouldn't exist, if exists, remove it
  457. if _, err := os.Lstat(expectedPath); err == nil {
  458. os.Remove(expectedPath)
  459. }
  460. dockerCmd(c, "cp", "-L", cleanedContainerID+":"+"/dir_link", expectedPath)
  461. actual, err = os.ReadFile(expectedPath)
  462. assert.NilError(c, err)
  463. defer os.Remove(expectedPath)
  464. assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container symbol link target")
  465. }