docker_cli_cp_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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/pkg/integration"
  12. "github.com/docker/docker/pkg/integration/checker"
  13. icmd "github.com/docker/docker/pkg/integration/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, err := readContainerFile(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, err = readContainerFile(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, err = readContainerFile(containerID, "hostname")
  318. actual, err = ioutil.ReadFile(outDir + "/hostname")
  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(string(content), checker.Equals, "lololol\n")
  395. }
  396. func (s *DockerSuite) TestCpToStdout(c *check.C) {
  397. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
  398. containerID := strings.TrimSpace(out)
  399. out, _ = dockerCmd(c, "wait", containerID)
  400. // failed to set up container
  401. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  402. out, _, err := integration.RunCommandPipelineWithOutput(
  403. exec.Command(dockerBinary, "cp", containerID+":/test", "-"),
  404. exec.Command("tar", "-vtf", "-"))
  405. c.Assert(err, checker.IsNil)
  406. c.Assert(out, checker.Contains, "test")
  407. c.Assert(out, checker.Contains, "-rw")
  408. }
  409. func (s *DockerSuite) TestCpNameHasColon(c *check.C) {
  410. testRequires(c, SameHostDaemon, DaemonIsLinux)
  411. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /te:s:t")
  412. containerID := strings.TrimSpace(out)
  413. out, _ = dockerCmd(c, "wait", containerID)
  414. // failed to set up container
  415. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  416. tmpdir, err := ioutil.TempDir("", "docker-integration")
  417. c.Assert(err, checker.IsNil)
  418. defer os.RemoveAll(tmpdir)
  419. dockerCmd(c, "cp", containerID+":/te:s:t", tmpdir)
  420. content, err := ioutil.ReadFile(tmpdir + "/te:s:t")
  421. c.Assert(string(content), checker.Equals, "lololol\n")
  422. }
  423. func (s *DockerSuite) TestCopyAndRestart(c *check.C) {
  424. testRequires(c, DaemonIsLinux)
  425. expectedMsg := "hello"
  426. out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", expectedMsg)
  427. containerID := strings.TrimSpace(out)
  428. out, _ = dockerCmd(c, "wait", containerID)
  429. // failed to set up container
  430. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  431. tmpDir, err := ioutil.TempDir("", "test-docker-restart-after-copy-")
  432. c.Assert(err, checker.IsNil)
  433. defer os.RemoveAll(tmpDir)
  434. dockerCmd(c, "cp", fmt.Sprintf("%s:/etc/group", containerID), tmpDir)
  435. out, _ = dockerCmd(c, "start", "-a", containerID)
  436. c.Assert(strings.TrimSpace(out), checker.Equals, expectedMsg)
  437. }
  438. func (s *DockerSuite) TestCopyCreatedContainer(c *check.C) {
  439. testRequires(c, DaemonIsLinux)
  440. dockerCmd(c, "create", "--name", "test_cp", "-v", "/test", "busybox")
  441. tmpDir, err := ioutil.TempDir("", "test")
  442. c.Assert(err, checker.IsNil)
  443. defer os.RemoveAll(tmpDir)
  444. dockerCmd(c, "cp", "test_cp:/bin/sh", tmpDir)
  445. }
  446. // test copy with option `-L`: following symbol link
  447. // Check that symlinks to a file behave as expected when copying one from
  448. // a container to host following symbol link
  449. func (s *DockerSuite) TestCpSymlinkFromConToHostFollowSymlink(c *check.C) {
  450. testRequires(c, DaemonIsLinux)
  451. out, exitCode := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" /dir_link")
  452. if exitCode != 0 {
  453. c.Fatal("failed to create a container", out)
  454. }
  455. cleanedContainerID := strings.TrimSpace(out)
  456. out, _ = dockerCmd(c, "wait", cleanedContainerID)
  457. if strings.TrimSpace(out) != "0" {
  458. c.Fatal("failed to set up container", out)
  459. }
  460. testDir, err := ioutil.TempDir("", "test-cp-symlink-container-to-host-follow-symlink")
  461. if err != nil {
  462. c.Fatal(err)
  463. }
  464. defer os.RemoveAll(testDir)
  465. // This copy command should copy the symlink, not the target, into the
  466. // temporary directory.
  467. dockerCmd(c, "cp", "-L", cleanedContainerID+":"+"/dir_link", testDir)
  468. expectedPath := filepath.Join(testDir, "dir_link")
  469. expected := []byte(cpContainerContents)
  470. actual, err := ioutil.ReadFile(expectedPath)
  471. if !bytes.Equal(actual, expected) {
  472. c.Fatalf("Expected copied file to be duplicate of the container symbol link target")
  473. }
  474. os.Remove(expectedPath)
  475. // now test copy symbol link to a non-existing file in host
  476. expectedPath = filepath.Join(testDir, "somefile_host")
  477. // expectedPath shouldn't exist, if exists, remove it
  478. if _, err := os.Lstat(expectedPath); err == nil {
  479. os.Remove(expectedPath)
  480. }
  481. dockerCmd(c, "cp", "-L", cleanedContainerID+":"+"/dir_link", expectedPath)
  482. actual, err = ioutil.ReadFile(expectedPath)
  483. if !bytes.Equal(actual, expected) {
  484. c.Fatalf("Expected copied file to be duplicate of the container symbol link target")
  485. }
  486. defer os.Remove(expectedPath)
  487. }