docker_cli_cp_utils.go 7.4 KB

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