docker_cli_cp_test.go 22 KB

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