docker_cli_cp_test.go 22 KB

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