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