docker_cli_cp_utils_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "testing"
  12. "github.com/docker/docker/pkg/archive"
  13. "gotest.tools/v3/assert"
  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 *testing.T, dir string) {
  82. c.Helper()
  83. for _, fd := range defaultFileData {
  84. path := filepath.Join(dir, filepath.FromSlash(fd.path))
  85. switch fd.filetype {
  86. case ftRegular:
  87. assert.NilError(c, os.WriteFile(path, []byte(fd.contents+"\n"), os.FileMode(fd.mode)))
  88. case ftDir:
  89. assert.NilError(c, os.Mkdir(path, os.FileMode(fd.mode)))
  90. case ftSymlink:
  91. assert.NilError(c, os.Symlink(fd.contents, path))
  92. }
  93. if fd.filetype != ftSymlink && runtime.GOOS != "windows" {
  94. assert.NilError(c, os.Chown(path, fd.uid, fd.gid))
  95. }
  96. }
  97. }
  98. type testContainerOptions struct {
  99. addContent bool
  100. readOnly bool
  101. volumes []string
  102. workDir string
  103. command string
  104. }
  105. func makeTestContainer(c *testing.T, options testContainerOptions) (containerID string) {
  106. c.Helper()
  107. if options.addContent {
  108. mkContentCmd := defaultMkContentCommand()
  109. if options.command == "" {
  110. options.command = mkContentCmd
  111. } else {
  112. options.command = fmt.Sprintf("%s && %s", defaultMkContentCommand(), options.command)
  113. }
  114. }
  115. if options.command == "" {
  116. options.command = "#(nop)"
  117. }
  118. args := []string{"run", "-d"}
  119. for _, volume := range options.volumes {
  120. args = append(args, "-v", volume)
  121. }
  122. if options.workDir != "" {
  123. args = append(args, "-w", options.workDir)
  124. }
  125. if options.readOnly {
  126. args = append(args, "--read-only")
  127. }
  128. args = append(args, "busybox", "/bin/sh", "-c", options.command)
  129. out, _ := dockerCmd(c, args...)
  130. containerID = strings.TrimSpace(out)
  131. out, _ = dockerCmd(c, "wait", containerID)
  132. exitCode := strings.TrimSpace(out)
  133. if exitCode != "0" {
  134. out, _ = dockerCmd(c, "logs", containerID)
  135. }
  136. assert.Equal(c, exitCode, "0", "failed to make test container: %s", out)
  137. return
  138. }
  139. func makeCatFileCommand(path string) string {
  140. return fmt.Sprintf("if [ -f %s ]; then cat %s; fi", path, path)
  141. }
  142. func cpPath(pathElements ...string) string {
  143. localizedPathElements := make([]string, len(pathElements))
  144. for i, path := range pathElements {
  145. localizedPathElements[i] = filepath.FromSlash(path)
  146. }
  147. return strings.Join(localizedPathElements, string(filepath.Separator))
  148. }
  149. func cpPathTrailingSep(pathElements ...string) string {
  150. return fmt.Sprintf("%s%c", cpPath(pathElements...), filepath.Separator)
  151. }
  152. func containerCpPath(containerID string, pathElements ...string) string {
  153. joined := strings.Join(pathElements, "/")
  154. return fmt.Sprintf("%s:%s", containerID, joined)
  155. }
  156. func containerCpPathTrailingSep(containerID string, pathElements ...string) string {
  157. return fmt.Sprintf("%s/", containerCpPath(containerID, pathElements...))
  158. }
  159. func runDockerCp(c *testing.T, src, dst string) error {
  160. c.Helper()
  161. args := []string{"cp", src, dst}
  162. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...)); err != nil {
  163. return fmt.Errorf("error executing `docker cp` command: %s: %s", err, out)
  164. }
  165. return nil
  166. }
  167. func startContainerGetOutput(c *testing.T, containerID string) (out string, err error) {
  168. c.Helper()
  169. args := []string{"start", "-a", containerID}
  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 *testing.T, label string) (tmpDir string) {
  177. c.Helper()
  178. var err error
  179. tmpDir, err = os.MkdirTemp("", label)
  180. // unable to make temporary directory
  181. assert.NilError(c, err)
  182. return
  183. }
  184. func isCpDirNotExist(err error) bool {
  185. return strings.Contains(err.Error(), archive.ErrDirNotExists.Error())
  186. }
  187. func isCpCannotCopyDir(err error) bool {
  188. return strings.Contains(err.Error(), archive.ErrCannotCopyDir.Error())
  189. }
  190. func isCpCannotCopyReadOnly(err error) bool {
  191. return strings.Contains(err.Error(), "marked read-only")
  192. }
  193. func fileContentEquals(c *testing.T, filename, contents string) error {
  194. c.Helper()
  195. fileBytes, err := os.ReadFile(filename)
  196. if err != nil {
  197. return err
  198. }
  199. expectedBytes, err := io.ReadAll(strings.NewReader(contents))
  200. if err != nil {
  201. return err
  202. }
  203. if !bytes.Equal(fileBytes, expectedBytes) {
  204. return fmt.Errorf("file content not equal - expected %q, got %q", string(expectedBytes), string(fileBytes))
  205. }
  206. return nil
  207. }
  208. func symlinkTargetEquals(c *testing.T, symlink, expectedTarget string) error {
  209. c.Helper()
  210. actualTarget, err := os.Readlink(symlink)
  211. if err != nil {
  212. return err
  213. }
  214. if actualTarget != expectedTarget {
  215. return fmt.Errorf("symlink target points to %q not %q", actualTarget, expectedTarget)
  216. }
  217. return nil
  218. }
  219. func containerStartOutputEquals(c *testing.T, containerID, contents string) error {
  220. c.Helper()
  221. out, err := startContainerGetOutput(c, containerID)
  222. if err != nil {
  223. return err
  224. }
  225. if out != contents {
  226. return fmt.Errorf("output contents not equal - expected %q, got %q", contents, out)
  227. }
  228. return nil
  229. }
  230. func defaultVolumes(tmpDir string) []string {
  231. if testEnv.IsLocalDaemon() {
  232. return []string{
  233. "/vol1",
  234. fmt.Sprintf("%s:/vol2", tmpDir),
  235. fmt.Sprintf("%s:/vol3", filepath.Join(tmpDir, "vol3")),
  236. fmt.Sprintf("%s:/vol_ro:ro", filepath.Join(tmpDir, "vol_ro")),
  237. }
  238. }
  239. // Can't bind-mount volumes with separate host daemon.
  240. return []string{"/vol1", "/vol2", "/vol3", "/vol_ro:/vol_ro:ro"}
  241. }