docker_cli_cp_utils.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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, "symlinkToFile1", "file1"},
  66. {ftSymlink, "symlinkToDir1", "dir1"},
  67. {ftSymlink, "brokenSymlinkToFileX", "fileX"},
  68. {ftSymlink, "brokenSymlinkToDirX", "dirX"},
  69. {ftSymlink, "symlinkToAbsDir", "/root"},
  70. }
  71. func defaultMkContentCommand() string {
  72. return mkFilesCommand(defaultFileData)
  73. }
  74. func makeTestContentInDir(c *check.C, dir string) {
  75. for _, fd := range defaultFileData {
  76. path := filepath.Join(dir, filepath.FromSlash(fd.path))
  77. switch fd.filetype {
  78. case ftRegular:
  79. if err := ioutil.WriteFile(path, []byte(fd.contents+"\n"), os.FileMode(0666)); err != nil {
  80. c.Fatal(err)
  81. }
  82. case ftDir:
  83. if err := os.Mkdir(path, os.FileMode(0777)); err != nil {
  84. c.Fatal(err)
  85. }
  86. case ftSymlink:
  87. if err := os.Symlink(fd.contents, path); err != nil {
  88. c.Fatal(err)
  89. }
  90. }
  91. }
  92. }
  93. type testContainerOptions struct {
  94. addContent bool
  95. readOnly bool
  96. volumes []string
  97. workDir string
  98. command string
  99. }
  100. func makeTestContainer(c *check.C, options testContainerOptions) (containerID string) {
  101. if options.addContent {
  102. mkContentCmd := defaultMkContentCommand()
  103. if options.command == "" {
  104. options.command = mkContentCmd
  105. } else {
  106. options.command = fmt.Sprintf("%s && %s", defaultMkContentCommand(), options.command)
  107. }
  108. }
  109. if options.command == "" {
  110. options.command = "#(nop)"
  111. }
  112. args := []string{"run", "-d"}
  113. for _, volume := range options.volumes {
  114. args = append(args, "-v", volume)
  115. }
  116. if options.workDir != "" {
  117. args = append(args, "-w", options.workDir)
  118. }
  119. if options.readOnly {
  120. args = append(args, "--read-only")
  121. }
  122. args = append(args, "busybox", "/bin/sh", "-c", options.command)
  123. out, status := dockerCmd(c, args...)
  124. if status != 0 {
  125. c.Fatalf("failed to run container, status %d: %s", status, out)
  126. }
  127. containerID = strings.TrimSpace(out)
  128. out, status = dockerCmd(c, "wait", containerID)
  129. if status != 0 {
  130. c.Fatalf("failed to wait for test container container, status %d: %s", status, out)
  131. }
  132. if exitCode := strings.TrimSpace(out); exitCode != "0" {
  133. logs, status := dockerCmd(c, "logs", containerID)
  134. if status != 0 {
  135. logs = "UNABLE TO GET LOGS"
  136. }
  137. c.Fatalf("failed to make test container, exit code (%d): %s", exitCode, logs)
  138. }
  139. return
  140. }
  141. func makeCatFileCommand(path string) string {
  142. return fmt.Sprintf("if [ -f %s ]; then cat %s; fi", path, path)
  143. }
  144. func cpPath(pathElements ...string) string {
  145. localizedPathElements := make([]string, len(pathElements))
  146. for i, path := range pathElements {
  147. localizedPathElements[i] = filepath.FromSlash(path)
  148. }
  149. return strings.Join(localizedPathElements, string(filepath.Separator))
  150. }
  151. func cpPathTrailingSep(pathElements ...string) string {
  152. return fmt.Sprintf("%s%c", cpPath(pathElements...), filepath.Separator)
  153. }
  154. func containerCpPath(containerID string, pathElements ...string) string {
  155. joined := strings.Join(pathElements, "/")
  156. return fmt.Sprintf("%s:%s", containerID, joined)
  157. }
  158. func containerCpPathTrailingSep(containerID string, pathElements ...string) string {
  159. return fmt.Sprintf("%s/", containerCpPath(containerID, pathElements...))
  160. }
  161. func runDockerCp(c *check.C, src, dst string) (err error) {
  162. c.Logf("running `docker cp %s %s`", src, dst)
  163. args := []string{"cp", src, dst}
  164. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
  165. if err != nil {
  166. err = fmt.Errorf("error executing `docker cp` command: %s: %s", err, out)
  167. }
  168. return
  169. }
  170. func startContainerGetOutput(c *check.C, cID string) (out string, err error) {
  171. c.Logf("running `docker start -a %s`", cID)
  172. args := []string{"start", "-a", cID}
  173. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, args...))
  174. if err != nil {
  175. err = fmt.Errorf("error executing `docker start` command: %s: %s", err, out)
  176. }
  177. return
  178. }
  179. func getTestDir(c *check.C, label string) (tmpDir string) {
  180. var err error
  181. if tmpDir, err = ioutil.TempDir("", label); err != nil {
  182. c.Fatalf("unable to make temporary directory: %s", err)
  183. }
  184. return
  185. }
  186. func isCpNotExist(err error) bool {
  187. return strings.Contains(err.Error(), "no such file or directory") || strings.Contains(err.Error(), "cannot find the file specified")
  188. }
  189. func isCpDirNotExist(err error) bool {
  190. return strings.Contains(err.Error(), archive.ErrDirNotExists.Error())
  191. }
  192. func isCpNotDir(err error) bool {
  193. return strings.Contains(err.Error(), archive.ErrNotDirectory.Error()) || strings.Contains(err.Error(), "filename, directory name, or volume label syntax is incorrect")
  194. }
  195. func isCpCannotCopyDir(err error) bool {
  196. return strings.Contains(err.Error(), archive.ErrCannotCopyDir.Error())
  197. }
  198. func isCpCannotCopyReadOnly(err error) bool {
  199. return strings.Contains(err.Error(), "marked read-only")
  200. }
  201. func isCannotOverwriteNonDirWithDir(err error) bool {
  202. return strings.Contains(err.Error(), "cannot overwrite non-directory")
  203. }
  204. func fileContentEquals(c *check.C, filename, contents string) (err error) {
  205. c.Logf("checking that file %q contains %q\n", filename, contents)
  206. fileBytes, err := ioutil.ReadFile(filename)
  207. if err != nil {
  208. return
  209. }
  210. expectedBytes, err := ioutil.ReadAll(strings.NewReader(contents))
  211. if err != nil {
  212. return
  213. }
  214. if !bytes.Equal(fileBytes, expectedBytes) {
  215. err = fmt.Errorf("file content not equal - expected %q, got %q", string(expectedBytes), string(fileBytes))
  216. }
  217. return
  218. }
  219. func symlinkTargetEquals(c *check.C, symlink, expectedTarget string) (err error) {
  220. c.Logf("checking that the symlink %q points to %q\n", symlink, expectedTarget)
  221. actualTarget, err := os.Readlink(symlink)
  222. if err != nil {
  223. return err
  224. }
  225. if actualTarget != expectedTarget {
  226. return fmt.Errorf("symlink target points to %q not %q", actualTarget, expectedTarget)
  227. }
  228. return nil
  229. }
  230. func containerStartOutputEquals(c *check.C, cID, contents string) (err error) {
  231. c.Logf("checking that container %q start output contains %q\n", cID, contents)
  232. out, err := startContainerGetOutput(c, cID)
  233. if err != nil {
  234. return err
  235. }
  236. if out != contents {
  237. err = fmt.Errorf("output contents not equal - expected %q, got %q", contents, out)
  238. }
  239. return
  240. }
  241. func defaultVolumes(tmpDir string) []string {
  242. if SameHostDaemon.Condition() {
  243. return []string{
  244. "/vol1",
  245. fmt.Sprintf("%s:/vol2", tmpDir),
  246. fmt.Sprintf("%s:/vol3", filepath.Join(tmpDir, "vol3")),
  247. fmt.Sprintf("%s:/vol_ro:ro", filepath.Join(tmpDir, "vol_ro")),
  248. }
  249. }
  250. // Can't bind-mount volumes with separate host daemon.
  251. return []string{"/vol1", "/vol2", "/vol3", "/vol_ro:/vol_ro:ro"}
  252. }