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