docker_cli_cp_utils_test.go 8.3 KB

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