docker_cli_cp_utils_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/integration-cli/cli"
  13. "github.com/docker/docker/pkg/archive"
  14. "gotest.tools/v3/assert"
  15. )
  16. type fileType uint32
  17. const (
  18. ftRegular fileType = iota
  19. ftDir
  20. ftSymlink
  21. )
  22. type fileData struct {
  23. filetype fileType
  24. path string
  25. contents string
  26. uid int
  27. gid int
  28. mode int
  29. }
  30. func (fd fileData) creationCommand() string {
  31. var command string
  32. switch fd.filetype {
  33. case ftRegular:
  34. // Don't overwrite the file if it already exists!
  35. command = fmt.Sprintf("if [ ! -f %s ]; then echo %q > %s; fi", fd.path, fd.contents, fd.path)
  36. case ftDir:
  37. command = fmt.Sprintf("mkdir -p %s", fd.path)
  38. case ftSymlink:
  39. command = fmt.Sprintf("ln -fs %s %s", fd.contents, fd.path)
  40. }
  41. return command
  42. }
  43. func mkFilesCommand(fds []fileData) string {
  44. commands := make([]string, len(fds))
  45. for i, fd := range fds {
  46. commands[i] = fd.creationCommand()
  47. }
  48. return strings.Join(commands, " && ")
  49. }
  50. var defaultFileData = []fileData{
  51. {ftRegular, "file1", "file1", 0, 0, 0o666},
  52. {ftRegular, "file2", "file2", 0, 0, 0o666},
  53. {ftRegular, "file3", "file3", 0, 0, 0o666},
  54. {ftRegular, "file4", "file4", 0, 0, 0o666},
  55. {ftRegular, "file5", "file5", 0, 0, 0o666},
  56. {ftRegular, "file6", "file6", 0, 0, 0o666},
  57. {ftRegular, "file7", "file7", 0, 0, 0o666},
  58. {ftDir, "dir1", "", 0, 0, 0o777},
  59. {ftRegular, "dir1/file1-1", "file1-1", 0, 0, 0o666},
  60. {ftRegular, "dir1/file1-2", "file1-2", 0, 0, 0o666},
  61. {ftDir, "dir2", "", 0, 0, 0o666},
  62. {ftRegular, "dir2/file2-1", "file2-1", 0, 0, 0o666},
  63. {ftRegular, "dir2/file2-2", "file2-2", 0, 0, 0o666},
  64. {ftDir, "dir3", "", 0, 0, 0o666},
  65. {ftRegular, "dir3/file3-1", "file3-1", 0, 0, 0o666},
  66. {ftRegular, "dir3/file3-2", "file3-2", 0, 0, 0o666},
  67. {ftDir, "dir4", "", 0, 0, 0o666},
  68. {ftRegular, "dir4/file3-1", "file4-1", 0, 0, 0o666},
  69. {ftRegular, "dir4/file3-2", "file4-2", 0, 0, 0o666},
  70. {ftDir, "dir5", "", 0, 0, 0o666},
  71. {ftSymlink, "symlinkToFile1", "file1", 0, 0, 0o666},
  72. {ftSymlink, "symlinkToDir1", "dir1", 0, 0, 0o666},
  73. {ftSymlink, "brokenSymlinkToFileX", "fileX", 0, 0, 0o666},
  74. {ftSymlink, "brokenSymlinkToDirX", "dirX", 0, 0, 0o666},
  75. {ftSymlink, "symlinkToAbsDir", "/root", 0, 0, 0o666},
  76. {ftDir, "permdirtest", "", 2, 2, 0o700},
  77. {ftRegular, "permdirtest/permtest", "perm_test", 65534, 65534, 0o400},
  78. }
  79. func defaultMkContentCommand() string {
  80. return mkFilesCommand(defaultFileData)
  81. }
  82. func makeTestContentInDir(c *testing.T, dir string) {
  83. c.Helper()
  84. for _, fd := range defaultFileData {
  85. path := filepath.Join(dir, filepath.FromSlash(fd.path))
  86. switch fd.filetype {
  87. case ftRegular:
  88. assert.NilError(c, os.WriteFile(path, []byte(fd.contents+"\n"), os.FileMode(fd.mode)))
  89. case ftDir:
  90. assert.NilError(c, os.Mkdir(path, os.FileMode(fd.mode)))
  91. case ftSymlink:
  92. assert.NilError(c, os.Symlink(fd.contents, path))
  93. }
  94. if fd.filetype != ftSymlink && runtime.GOOS != "windows" {
  95. assert.NilError(c, os.Chown(path, fd.uid, fd.gid))
  96. }
  97. }
  98. }
  99. type testContainerOptions struct {
  100. addContent bool
  101. readOnly bool
  102. volumes []string
  103. workDir string
  104. command string
  105. }
  106. func makeTestContainer(c *testing.T, options testContainerOptions) (containerID string) {
  107. c.Helper()
  108. if options.addContent {
  109. mkContentCmd := defaultMkContentCommand()
  110. if options.command == "" {
  111. options.command = mkContentCmd
  112. } else {
  113. options.command = fmt.Sprintf("%s && %s", defaultMkContentCommand(), options.command)
  114. }
  115. }
  116. if options.command == "" {
  117. options.command = "#(nop)"
  118. }
  119. args := []string{"run", "-d"}
  120. for _, volume := range options.volumes {
  121. args = append(args, "-v", volume)
  122. }
  123. if options.workDir != "" {
  124. args = append(args, "-w", options.workDir)
  125. }
  126. if options.readOnly {
  127. args = append(args, "--read-only")
  128. }
  129. args = append(args, "busybox", "/bin/sh", "-c", options.command)
  130. out := cli.DockerCmd(c, args...).Combined()
  131. containerID = strings.TrimSpace(out)
  132. out = cli.DockerCmd(c, "wait", containerID).Combined()
  133. exitCode := strings.TrimSpace(out)
  134. if exitCode != "0" {
  135. out = cli.DockerCmd(c, "logs", containerID).Combined()
  136. }
  137. assert.Equal(c, exitCode, "0", "failed to make test container: %s", out)
  138. return
  139. }
  140. func makeCatFileCommand(path string) string {
  141. return fmt.Sprintf("if [ -f %s ]; then cat %s; fi", path, path)
  142. }
  143. func cpPath(pathElements ...string) string {
  144. localizedPathElements := make([]string, len(pathElements))
  145. for i, path := range pathElements {
  146. localizedPathElements[i] = filepath.FromSlash(path)
  147. }
  148. return strings.Join(localizedPathElements, string(filepath.Separator))
  149. }
  150. func cpPathTrailingSep(pathElements ...string) string {
  151. return fmt.Sprintf("%s%c", cpPath(pathElements...), filepath.Separator)
  152. }
  153. func containerCpPath(containerID string, pathElements ...string) string {
  154. joined := strings.Join(pathElements, "/")
  155. return fmt.Sprintf("%s:%s", containerID, joined)
  156. }
  157. func containerCpPathTrailingSep(containerID string, pathElements ...string) string {
  158. return fmt.Sprintf("%s/", containerCpPath(containerID, pathElements...))
  159. }
  160. func runDockerCp(c *testing.T, src, dst string) error {
  161. c.Helper()
  162. args := []string{"cp", src, dst}
  163. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...)); err != nil {
  164. return fmt.Errorf("error executing `docker cp` command: %s: %s", err, out)
  165. }
  166. return nil
  167. }
  168. func startContainerGetOutput(c *testing.T, containerID string) (out string, err error) {
  169. c.Helper()
  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 *testing.T, label string) (tmpDir string) {
  178. c.Helper()
  179. var err error
  180. tmpDir, err = os.MkdirTemp("", label)
  181. // unable to make temporary directory
  182. assert.NilError(c, err)
  183. return
  184. }
  185. func isCpDirNotExist(err error) bool {
  186. return strings.Contains(err.Error(), archive.ErrDirNotExists.Error())
  187. }
  188. func isCpCannotCopyDir(err error) bool {
  189. return strings.Contains(err.Error(), archive.ErrCannotCopyDir.Error())
  190. }
  191. func fileContentEquals(c *testing.T, filename, contents string) error {
  192. c.Helper()
  193. fileBytes, err := os.ReadFile(filename)
  194. if err != nil {
  195. return err
  196. }
  197. expectedBytes, err := io.ReadAll(strings.NewReader(contents))
  198. if err != nil {
  199. return err
  200. }
  201. if !bytes.Equal(fileBytes, expectedBytes) {
  202. return fmt.Errorf("file content not equal - expected %q, got %q", string(expectedBytes), string(fileBytes))
  203. }
  204. return nil
  205. }
  206. func symlinkTargetEquals(c *testing.T, symlink, expectedTarget string) error {
  207. c.Helper()
  208. actualTarget, err := os.Readlink(symlink)
  209. if err != nil {
  210. return err
  211. }
  212. if actualTarget != expectedTarget {
  213. return fmt.Errorf("symlink target points to %q not %q", actualTarget, expectedTarget)
  214. }
  215. return nil
  216. }
  217. func containerStartOutputEquals(c *testing.T, containerID, contents string) error {
  218. c.Helper()
  219. out, err := startContainerGetOutput(c, containerID)
  220. if err != nil {
  221. return err
  222. }
  223. if out != contents {
  224. return fmt.Errorf("output contents not equal - expected %q, got %q", contents, out)
  225. }
  226. return nil
  227. }
  228. func defaultVolumes(tmpDir string) []string {
  229. if testEnv.IsLocalDaemon() {
  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. }