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. "github.com/go-check/check"
  13. )
  14. const (
  15. cpTestPathParent = "/some"
  16. cpTestPath = "/some/path"
  17. cpTestName = "test"
  18. cpFullPath = "/some/path/test"
  19. cpContainerContents = "holla, i am the container"
  20. cpHostContents = "hello, i am the host"
  21. )
  22. // Ensure that an all-local path case returns an error.
  23. func (s *DockerSuite) TestCpLocalOnly(c *check.C) {
  24. err := runDockerCp(c, "foo", "bar")
  25. c.Assert(err, checker.NotNil)
  26. c.Assert(err.Error(), checker.Contains, "must specify at least one container source")
  27. }
  28. // Test for #5656
  29. // Check that garbage paths don't escape the container's rootfs
  30. func (s *DockerSuite) TestCpGarbagePath(c *check.C) {
  31. testRequires(c, DaemonIsLinux)
  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. testRequires(c, DaemonIsLinux)
  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. testRequires(c, DaemonIsLinux)
  96. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath)
  97. containerID := strings.TrimSpace(out)
  98. out, _ = dockerCmd(c, "wait", containerID)
  99. // failed to set up container
  100. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  101. c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
  102. hostFile, err := os.Create(cpFullPath)
  103. c.Assert(err, checker.IsNil)
  104. defer hostFile.Close()
  105. defer os.RemoveAll(cpTestPathParent)
  106. fmt.Fprintf(hostFile, "%s", cpHostContents)
  107. tmpdir, err := ioutil.TempDir("", "docker-integration")
  108. c.Assert(err, checker.IsNil)
  109. tmpname := filepath.Join(tmpdir, cpTestName)
  110. defer os.RemoveAll(tmpdir)
  111. path := cpFullPath
  112. dockerCmd(c, "cp", containerID+":"+path, tmpdir)
  113. file, _ := os.Open(tmpname)
  114. defer file.Close()
  115. test, err := ioutil.ReadAll(file)
  116. c.Assert(err, checker.IsNil)
  117. // output matched host file -- absolute path can escape container rootfs
  118. c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
  119. // output doesn't match the input for absolute path
  120. c.Assert(string(test), checker.Equals, cpContainerContents)
  121. }
  122. // Test for #5619
  123. // Check that absolute symlinks are still relative to the container's rootfs
  124. func (s *DockerSuite) TestCpAbsoluteSymlink(c *check.C) {
  125. testRequires(c, DaemonIsLinux)
  126. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpFullPath+" container_path")
  127. containerID := strings.TrimSpace(out)
  128. out, _ = dockerCmd(c, "wait", containerID)
  129. // failed to set up container
  130. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  131. c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
  132. hostFile, err := os.Create(cpFullPath)
  133. c.Assert(err, checker.IsNil)
  134. defer hostFile.Close()
  135. defer os.RemoveAll(cpTestPathParent)
  136. fmt.Fprintf(hostFile, "%s", cpHostContents)
  137. tmpdir, err := ioutil.TempDir("", "docker-integration")
  138. c.Assert(err, checker.IsNil)
  139. tmpname := filepath.Join(tmpdir, "container_path")
  140. defer os.RemoveAll(tmpdir)
  141. path := path.Join("/", "container_path")
  142. dockerCmd(c, "cp", containerID+":"+path, tmpdir)
  143. // We should have copied a symlink *NOT* the file itself!
  144. linkTarget, err := os.Readlink(tmpname)
  145. c.Assert(err, checker.IsNil)
  146. c.Assert(linkTarget, checker.Equals, filepath.FromSlash(cpFullPath))
  147. }
  148. // Check that symlinks to a directory behave as expected when copying one from
  149. // a container.
  150. func (s *DockerSuite) TestCpFromSymlinkToDirectory(c *check.C) {
  151. testRequires(c, DaemonIsLinux)
  152. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPathParent+" /dir_link")
  153. containerID := strings.TrimSpace(out)
  154. out, _ = dockerCmd(c, "wait", containerID)
  155. // failed to set up container
  156. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  157. testDir, err := ioutil.TempDir("", "test-cp-from-symlink-to-dir-")
  158. c.Assert(err, checker.IsNil)
  159. defer os.RemoveAll(testDir)
  160. // This copy command should copy the symlink, not the target, into the
  161. // temporary directory.
  162. dockerCmd(c, "cp", containerID+":"+"/dir_link", testDir)
  163. expectedPath := filepath.Join(testDir, "dir_link")
  164. linkTarget, err := os.Readlink(expectedPath)
  165. c.Assert(err, checker.IsNil)
  166. c.Assert(linkTarget, checker.Equals, filepath.FromSlash(cpTestPathParent))
  167. os.Remove(expectedPath)
  168. // This copy command should resolve the symlink (note the trailing
  169. // separator), copying the target into the temporary directory.
  170. dockerCmd(c, "cp", containerID+":"+"/dir_link/", testDir)
  171. // It *should not* have copied the directory using the target's name, but
  172. // used the given name instead.
  173. unexpectedPath := filepath.Join(testDir, cpTestPathParent)
  174. stat, err := os.Lstat(unexpectedPath)
  175. if err == nil {
  176. out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name())
  177. }
  178. c.Assert(err, checker.NotNil, check.Commentf(out))
  179. // It *should* have copied the directory using the asked name "dir_link".
  180. stat, err = os.Lstat(expectedPath)
  181. c.Assert(err, checker.IsNil, check.Commentf("unable to stat resource at %q", expectedPath))
  182. c.Assert(stat.IsDir(), checker.True, check.Commentf("should have copied a directory but got %q instead", stat.Mode()))
  183. }
  184. // Check that symlinks to a directory behave as expected when copying one to a
  185. // container.
  186. func (s *DockerSuite) TestCpToSymlinkToDirectory(c *check.C) {
  187. testRequires(c, DaemonIsLinux)
  188. testRequires(c, SameHostDaemon) // Requires local volume mount bind.
  189. testVol, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
  190. c.Assert(err, checker.IsNil)
  191. defer os.RemoveAll(testVol)
  192. // Create a test container with a local volume. We will test by copying
  193. // to the volume path in the container which we can then verify locally.
  194. out, _ := dockerCmd(c, "create", "-v", testVol+":/testVol", "busybox")
  195. containerID := strings.TrimSpace(out)
  196. // Create a temp directory to hold a test file nested in a direcotry.
  197. testDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
  198. c.Assert(err, checker.IsNil)
  199. defer os.RemoveAll(testDir)
  200. // This file will be at "/testDir/some/path/test" and will be copied into
  201. // the test volume later.
  202. hostTestFilename := filepath.Join(testDir, cpFullPath)
  203. c.Assert(os.MkdirAll(filepath.Dir(hostTestFilename), os.FileMode(0700)), checker.IsNil)
  204. c.Assert(ioutil.WriteFile(hostTestFilename, []byte(cpHostContents), os.FileMode(0600)), checker.IsNil)
  205. // Now create another temp directory to hold a symlink to the
  206. // "/testDir/some" directory.
  207. linkDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
  208. c.Assert(err, checker.IsNil)
  209. defer os.RemoveAll(linkDir)
  210. // Then symlink "/linkDir/dir_link" to "/testdir/some".
  211. linkTarget := filepath.Join(testDir, cpTestPathParent)
  212. localLink := filepath.Join(linkDir, "dir_link")
  213. c.Assert(os.Symlink(linkTarget, localLink), checker.IsNil)
  214. // Now copy that symlink into the test volume in the container.
  215. dockerCmd(c, "cp", localLink, containerID+":/testVol")
  216. // This copy command should have copied the symlink *not* the target.
  217. expectedPath := filepath.Join(testVol, "dir_link")
  218. actualLinkTarget, err := os.Readlink(expectedPath)
  219. c.Assert(err, checker.IsNil, check.Commentf("unable to read symlink at %q", expectedPath))
  220. c.Assert(actualLinkTarget, checker.Equals, linkTarget)
  221. // Good, now remove that copied link for the next test.
  222. os.Remove(expectedPath)
  223. // This copy command should resolve the symlink (note the trailing
  224. // separator), copying the target into the test volume directory in the
  225. // container.
  226. dockerCmd(c, "cp", localLink+"/", containerID+":/testVol")
  227. // It *should not* have copied the directory using the target's name, but
  228. // used the given name instead.
  229. unexpectedPath := filepath.Join(testVol, cpTestPathParent)
  230. stat, err := os.Lstat(unexpectedPath)
  231. if err == nil {
  232. out = fmt.Sprintf("target name was copied: %q - %q", stat.Mode(), stat.Name())
  233. }
  234. c.Assert(err, checker.NotNil, check.Commentf(out))
  235. // It *should* have copied the directory using the asked name "dir_link".
  236. stat, err = os.Lstat(expectedPath)
  237. c.Assert(err, checker.IsNil, check.Commentf("unable to stat resource at %q", expectedPath))
  238. c.Assert(stat.IsDir(), checker.True, check.Commentf("should have copied a directory but got %q instead", stat.Mode()))
  239. // And this directory should contain the file copied from the host at the
  240. // expected location: "/testVol/dir_link/path/test"
  241. expectedFilepath := filepath.Join(testVol, "dir_link/path/test")
  242. fileContents, err := ioutil.ReadFile(expectedFilepath)
  243. c.Assert(err, checker.IsNil)
  244. c.Assert(string(fileContents), checker.Equals, cpHostContents)
  245. }
  246. // Test for #5619
  247. // Check that symlinks which are part of the resource path are still relative to the container's rootfs
  248. func (s *DockerSuite) TestCpSymlinkComponent(c *check.C) {
  249. testRequires(c, DaemonIsLinux)
  250. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir -p '"+cpTestPath+"' && echo -n '"+cpContainerContents+"' > "+cpFullPath+" && ln -s "+cpTestPath+" container_path")
  251. containerID := strings.TrimSpace(out)
  252. out, _ = dockerCmd(c, "wait", containerID)
  253. // failed to set up container
  254. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  255. c.Assert(os.MkdirAll(cpTestPath, os.ModeDir), checker.IsNil)
  256. hostFile, err := os.Create(cpFullPath)
  257. c.Assert(err, checker.IsNil)
  258. defer hostFile.Close()
  259. defer os.RemoveAll(cpTestPathParent)
  260. fmt.Fprintf(hostFile, "%s", cpHostContents)
  261. tmpdir, err := ioutil.TempDir("", "docker-integration")
  262. c.Assert(err, checker.IsNil)
  263. tmpname := filepath.Join(tmpdir, cpTestName)
  264. defer os.RemoveAll(tmpdir)
  265. path := path.Join("/", "container_path", cpTestName)
  266. dockerCmd(c, "cp", containerID+":"+path, tmpdir)
  267. file, _ := os.Open(tmpname)
  268. defer file.Close()
  269. test, err := ioutil.ReadAll(file)
  270. c.Assert(err, checker.IsNil)
  271. // output matched host file -- symlink path component can escape container rootfs
  272. c.Assert(string(test), checker.Not(checker.Equals), cpHostContents)
  273. // output doesn't match the input for symlink path component
  274. c.Assert(string(test), checker.Equals, cpContainerContents)
  275. }
  276. // Check that cp with unprivileged user doesn't return any error
  277. func (s *DockerSuite) TestCpUnprivilegedUser(c *check.C) {
  278. testRequires(c, DaemonIsLinux)
  279. testRequires(c, UnixCli) // uses chmod/su: not available on windows
  280. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch "+cpTestName)
  281. containerID := strings.TrimSpace(out)
  282. out, _ = dockerCmd(c, "wait", containerID)
  283. // failed to set up container
  284. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  285. tmpdir, err := ioutil.TempDir("", "docker-integration")
  286. c.Assert(err, checker.IsNil)
  287. defer os.RemoveAll(tmpdir)
  288. c.Assert(os.Chmod(tmpdir, 0777), checker.IsNil)
  289. path := cpTestName
  290. _, _, err = runCommandWithOutput(exec.Command("su", "unprivilegeduser", "-c", dockerBinary+" cp "+containerID+":"+path+" "+tmpdir))
  291. c.Assert(err, checker.IsNil, check.Commentf("couldn't copy with unprivileged user: %s:%s", containerID, path))
  292. }
  293. func (s *DockerSuite) TestCpSpecialFiles(c *check.C) {
  294. testRequires(c, DaemonIsLinux)
  295. testRequires(c, SameHostDaemon)
  296. outDir, err := ioutil.TempDir("", "cp-test-special-files")
  297. c.Assert(err, checker.IsNil)
  298. defer os.RemoveAll(outDir)
  299. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "touch /foo")
  300. containerID := strings.TrimSpace(out)
  301. out, _ = dockerCmd(c, "wait", containerID)
  302. // failed to set up container
  303. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  304. // Copy actual /etc/resolv.conf
  305. dockerCmd(c, "cp", containerID+":/etc/resolv.conf", outDir)
  306. expected, err := readContainerFile(containerID, "resolv.conf")
  307. actual, err := ioutil.ReadFile(outDir + "/resolv.conf")
  308. // Expected copied file to be duplicate of the container resolvconf
  309. c.Assert(bytes.Equal(actual, expected), checker.True)
  310. // Copy actual /etc/hosts
  311. dockerCmd(c, "cp", containerID+":/etc/hosts", outDir)
  312. expected, err = readContainerFile(containerID, "hosts")
  313. actual, err = ioutil.ReadFile(outDir + "/hosts")
  314. // Expected copied file to be duplicate of the container hosts
  315. c.Assert(bytes.Equal(actual, expected), checker.True)
  316. // Copy actual /etc/resolv.conf
  317. dockerCmd(c, "cp", containerID+":/etc/hostname", outDir)
  318. expected, err = readContainerFile(containerID, "hostname")
  319. actual, err = ioutil.ReadFile(outDir + "/hostname")
  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. testRequires(c, DaemonIsLinux)
  382. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
  383. containerID := strings.TrimSpace(out)
  384. out, _ = dockerCmd(c, "wait", containerID)
  385. // failed to set up container
  386. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  387. tmpdir, err := ioutil.TempDir("", "docker-integration")
  388. c.Assert(err, checker.IsNil)
  389. defer os.RemoveAll(tmpdir)
  390. cwd, err := os.Getwd()
  391. c.Assert(err, checker.IsNil)
  392. defer os.Chdir(cwd)
  393. c.Assert(os.Chdir(tmpdir), checker.IsNil)
  394. dockerCmd(c, "cp", containerID+":/test", ".")
  395. content, err := ioutil.ReadFile("./test")
  396. c.Assert(string(content), checker.Equals, "lololol\n")
  397. }
  398. func (s *DockerSuite) TestCpToStdout(c *check.C) {
  399. testRequires(c, DaemonIsLinux)
  400. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
  401. containerID := strings.TrimSpace(out)
  402. out, _ = dockerCmd(c, "wait", containerID)
  403. // failed to set up container
  404. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  405. out, _, err := runCommandPipelineWithOutput(
  406. exec.Command(dockerBinary, "cp", containerID+":/test", "-"),
  407. exec.Command("tar", "-vtf", "-"))
  408. c.Assert(err, checker.IsNil)
  409. c.Assert(out, checker.Contains, "test")
  410. c.Assert(out, checker.Contains, "-rw")
  411. }
  412. func (s *DockerSuite) TestCpNameHasColon(c *check.C) {
  413. testRequires(c, SameHostDaemon, DaemonIsLinux)
  414. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /te:s:t")
  415. containerID := strings.TrimSpace(out)
  416. out, _ = dockerCmd(c, "wait", containerID)
  417. // failed to set up container
  418. c.Assert(strings.TrimSpace(out), checker.Equals, "0")
  419. tmpdir, err := ioutil.TempDir("", "docker-integration")
  420. c.Assert(err, checker.IsNil)
  421. defer os.RemoveAll(tmpdir)
  422. dockerCmd(c, "cp", containerID+":/te:s:t", tmpdir)
  423. content, err := ioutil.ReadFile(tmpdir + "/te:s:t")
  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. 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. }