docker_cli_cp_utils.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "github.com/docker/docker/pkg/archive"
  11. "github.com/docker/docker/pkg/integration/checker"
  12. "github.com/go-check/check"
  13. )
  14. type fileType uint32
  15. const (
  16. ftRegular fileType = iota
  17. ftDir
  18. ftSymlink
  19. )
  20. type fileData struct {
  21. filetype fileType
  22. path string
  23. contents string
  24. }
  25. func (fd fileData) creationCommand() string {
  26. var command string
  27. switch fd.filetype {
  28. case ftRegular:
  29. // Don't overwrite the file if it already exists!
  30. command = fmt.Sprintf("if [ ! -f %s ]; then echo %q > %s; fi", fd.path, fd.contents, fd.path)
  31. case ftDir:
  32. command = fmt.Sprintf("mkdir -p %s", fd.path)
  33. case ftSymlink:
  34. command = fmt.Sprintf("ln -fs %s %s", fd.contents, fd.path)
  35. }
  36. return command
  37. }
  38. func mkFilesCommand(fds []fileData) string {
  39. commands := make([]string, len(fds))
  40. for i, fd := range fds {
  41. commands[i] = fd.creationCommand()
  42. }
  43. return strings.Join(commands, " && ")
  44. }
  45. var defaultFileData = []fileData{
  46. {ftRegular, "file1", "file1"},
  47. {ftRegular, "file2", "file2"},
  48. {ftRegular, "file3", "file3"},
  49. {ftRegular, "file4", "file4"},
  50. {ftRegular, "file5", "file5"},
  51. {ftRegular, "file6", "file6"},
  52. {ftRegular, "file7", "file7"},
  53. {ftDir, "dir1", ""},
  54. {ftRegular, "dir1/file1-1", "file1-1"},
  55. {ftRegular, "dir1/file1-2", "file1-2"},
  56. {ftDir, "dir2", ""},
  57. {ftRegular, "dir2/file2-1", "file2-1"},
  58. {ftRegular, "dir2/file2-2", "file2-2"},
  59. {ftDir, "dir3", ""},
  60. {ftRegular, "dir3/file3-1", "file3-1"},
  61. {ftRegular, "dir3/file3-2", "file3-2"},
  62. {ftDir, "dir4", ""},
  63. {ftRegular, "dir4/file3-1", "file4-1"},
  64. {ftRegular, "dir4/file3-2", "file4-2"},
  65. {ftDir, "dir5", ""},
  66. {ftSymlink, "symlinkToFile1", "file1"},
  67. {ftSymlink, "symlinkToDir1", "dir1"},
  68. {ftSymlink, "brokenSymlinkToFileX", "fileX"},
  69. {ftSymlink, "brokenSymlinkToDirX", "dirX"},
  70. {ftSymlink, "symlinkToAbsDir", "/root"},
  71. }
  72. func defaultMkContentCommand() string {
  73. return mkFilesCommand(defaultFileData)
  74. }
  75. func makeTestContentInDir(c *check.C, dir string) {
  76. for _, fd := range defaultFileData {
  77. path := filepath.Join(dir, filepath.FromSlash(fd.path))
  78. switch fd.filetype {
  79. case ftRegular:
  80. c.Assert(ioutil.WriteFile(path, []byte(fd.contents+"\n"), os.FileMode(0666)), checker.IsNil)
  81. case ftDir:
  82. c.Assert(os.Mkdir(path, os.FileMode(0777)), checker.IsNil)
  83. case ftSymlink:
  84. c.Assert(os.Symlink(fd.contents, path), checker.IsNil)
  85. }
  86. }
  87. }
  88. type testContainerOptions struct {
  89. addContent bool
  90. readOnly bool
  91. volumes []string
  92. workDir string
  93. command string
  94. }
  95. func makeTestContainer(c *check.C, options testContainerOptions) (containerID string) {
  96. if options.addContent {
  97. mkContentCmd := defaultMkContentCommand()
  98. if options.command == "" {
  99. options.command = mkContentCmd
  100. } else {
  101. options.command = fmt.Sprintf("%s && %s", defaultMkContentCommand(), options.command)
  102. }
  103. }
  104. if options.command == "" {
  105. options.command = "#(nop)"
  106. }
  107. args := []string{"run", "-d"}
  108. for _, volume := range options.volumes {
  109. args = append(args, "-v", volume)
  110. }
  111. if options.workDir != "" {
  112. args = append(args, "-w", options.workDir)
  113. }
  114. if options.readOnly {
  115. args = append(args, "--read-only")
  116. }
  117. args = append(args, "busybox", "/bin/sh", "-c", options.command)
  118. out, _ := dockerCmd(c, args...)
  119. containerID = strings.TrimSpace(out)
  120. out, _ = dockerCmd(c, "wait", containerID)
  121. exitCode := strings.TrimSpace(out)
  122. if exitCode != "0" {
  123. out, _ = dockerCmd(c, "logs", containerID)
  124. }
  125. c.Assert(exitCode, checker.Equals, "0", check.Commentf("failed to make test container: %s", out))
  126. return
  127. }
  128. func makeCatFileCommand(path string) string {
  129. return fmt.Sprintf("if [ -f %s ]; then cat %s; fi", path, path)
  130. }
  131. func cpPath(pathElements ...string) string {
  132. localizedPathElements := make([]string, len(pathElements))
  133. for i, path := range pathElements {
  134. localizedPathElements[i] = filepath.FromSlash(path)
  135. }
  136. return strings.Join(localizedPathElements, string(filepath.Separator))
  137. }
  138. func cpPathTrailingSep(pathElements ...string) string {
  139. return fmt.Sprintf("%s%c", cpPath(pathElements...), filepath.Separator)
  140. }
  141. func containerCpPath(containerID string, pathElements ...string) string {
  142. joined := strings.Join(pathElements, "/")
  143. return fmt.Sprintf("%s:%s", containerID, joined)
  144. }
  145. func containerCpPathTrailingSep(containerID string, pathElements ...string) string {
  146. return fmt.Sprintf("%s/", containerCpPath(containerID, pathElements...))
  147. }
  148. func runDockerCp(c *check.C, src, dst string) (err error) {
  149. c.Logf("running `docker cp %s %s`", src, dst)
  150. args := []string{"cp", src, dst}
  151. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
  152. if err != nil {
  153. err = fmt.Errorf("error executing `docker cp` command: %s: %s", err, out)
  154. }
  155. return
  156. }
  157. func startContainerGetOutput(c *check.C, containerID string) (out string, err error) {
  158. c.Logf("running `docker start -a %s`", containerID)
  159. args := []string{"start", "-a", containerID}
  160. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, args...))
  161. if err != nil {
  162. err = fmt.Errorf("error executing `docker start` command: %s: %s", err, out)
  163. }
  164. return
  165. }
  166. func getTestDir(c *check.C, label string) (tmpDir string) {
  167. var err error
  168. tmpDir, err = ioutil.TempDir("", label)
  169. // unable to make temporary directory
  170. c.Assert(err, checker.IsNil)
  171. return
  172. }
  173. func isCpNotExist(err error) bool {
  174. return strings.Contains(err.Error(), "no such file or directory") || strings.Contains(err.Error(), "cannot find the file specified")
  175. }
  176. func isCpDirNotExist(err error) bool {
  177. return strings.Contains(err.Error(), archive.ErrDirNotExists.Error())
  178. }
  179. func isCpNotDir(err error) bool {
  180. return strings.Contains(err.Error(), archive.ErrNotDirectory.Error()) || strings.Contains(err.Error(), "filename, directory name, or volume label syntax is incorrect")
  181. }
  182. func isCpCannotCopyDir(err error) bool {
  183. return strings.Contains(err.Error(), archive.ErrCannotCopyDir.Error())
  184. }
  185. func isCpCannotCopyReadOnly(err error) bool {
  186. return strings.Contains(err.Error(), "marked read-only")
  187. }
  188. func isCannotOverwriteNonDirWithDir(err error) bool {
  189. return strings.Contains(err.Error(), "cannot overwrite non-directory")
  190. }
  191. func fileContentEquals(c *check.C, filename, contents string) (err error) {
  192. c.Logf("checking that file %q contains %q\n", filename, contents)
  193. fileBytes, err := ioutil.ReadFile(filename)
  194. if err != nil {
  195. return
  196. }
  197. expectedBytes, err := ioutil.ReadAll(strings.NewReader(contents))
  198. if err != nil {
  199. return
  200. }
  201. if !bytes.Equal(fileBytes, expectedBytes) {
  202. err = fmt.Errorf("file content not equal - expected %q, got %q", string(expectedBytes), string(fileBytes))
  203. }
  204. return
  205. }
  206. func symlinkTargetEquals(c *check.C, symlink, expectedTarget string) (err error) {
  207. c.Logf("checking that the symlink %q points to %q\n", symlink, expectedTarget)
  208. actualTarget, err := os.Readlink(symlink)
  209. if err != nil {
  210. return
  211. }
  212. if actualTarget != expectedTarget {
  213. err = fmt.Errorf("symlink target points to %q not %q", actualTarget, expectedTarget)
  214. }
  215. return
  216. }
  217. func containerStartOutputEquals(c *check.C, containerID, contents string) (err error) {
  218. c.Logf("checking that container %q start output contains %q\n", containerID, contents)
  219. out, err := startContainerGetOutput(c, containerID)
  220. if err != nil {
  221. return
  222. }
  223. if out != contents {
  224. err = fmt.Errorf("output contents not equal - expected %q, got %q", contents, out)
  225. }
  226. return
  227. }
  228. func defaultVolumes(tmpDir string) []string {
  229. if SameHostDaemon.Condition() {
  230. return []string{
  231. "/vol1",
  232. fmt.Sprintf("%s:/vol2", tmpDir),
  233. fmt.Sprintf("%s:/vol3", filepath.Join(tmpDir, "vol3")),
  234. fmt.Sprintf("%s:/vol_ro:ro", filepath.Join(tmpDir, "vol_ro")),
  235. }
  236. }
  237. // Can't bind-mount volumes with separate host daemon.
  238. return []string{"/vol1", "/vol2", "/vol3", "/vol_ro:/vol_ro:ro"}
  239. }