Adds several integration tests for `docker cp` behavior with over a dozen
tests for each of:
container -> local
local -> container
Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,503 @@ |
| 0 |
+package main |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "os" |
|
| 4 |
+ "path/filepath" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/go-check/check" |
|
| 7 |
+) |
|
| 8 |
+ |
|
| 9 |
+// docker cp CONTAINER:PATH LOCALPATH |
|
| 10 |
+ |
|
| 11 |
+// Try all of the test cases from the archive package which implements the |
|
| 12 |
+// internals of `docker cp` and ensure that the behavior matches when actually |
|
| 13 |
+// copying to and from containers. |
|
| 14 |
+ |
|
| 15 |
+// Basic assumptions about SRC and DST: |
|
| 16 |
+// 1. SRC must exist. |
|
| 17 |
+// 2. If SRC ends with a trailing separator, it must be a directory. |
|
| 18 |
+// 3. DST parent directory must exist. |
|
| 19 |
+// 4. If DST exists as a file, it must not end with a trailing separator. |
|
| 20 |
+ |
|
| 21 |
+// First get these easy error cases out of the way. |
|
| 22 |
+ |
|
| 23 |
+// Test for error when SRC does not exist. |
|
| 24 |
+func (s *DockerSuite) TestCpFromErrSrcNotExists(c *check.C) {
|
|
| 25 |
+ cID := makeTestContainer(c, testContainerOptions{})
|
|
| 26 |
+ defer deleteContainer(cID) |
|
| 27 |
+ |
|
| 28 |
+ tmpDir := getTestDir(c, "test-cp-from-err-src-not-exists") |
|
| 29 |
+ defer os.RemoveAll(tmpDir) |
|
| 30 |
+ |
|
| 31 |
+ err := runDockerCp(c, containerCpPath(cID, "file1"), tmpDir) |
|
| 32 |
+ if err == nil {
|
|
| 33 |
+ c.Fatal("expected IsNotExist error, but got nil instead")
|
|
| 34 |
+ } |
|
| 35 |
+ |
|
| 36 |
+ if !isCpNotExist(err) {
|
|
| 37 |
+ c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
|
|
| 38 |
+ } |
|
| 39 |
+} |
|
| 40 |
+ |
|
| 41 |
+// Test for error when SRC ends in a trailing |
|
| 42 |
+// path separator but it exists as a file. |
|
| 43 |
+func (s *DockerSuite) TestCpFromErrSrcNotDir(c *check.C) {
|
|
| 44 |
+ cID := makeTestContainer(c, testContainerOptions{addContent: true})
|
|
| 45 |
+ defer deleteContainer(cID) |
|
| 46 |
+ |
|
| 47 |
+ tmpDir := getTestDir(c, "test-cp-from-err-src-not-dir") |
|
| 48 |
+ defer os.RemoveAll(tmpDir) |
|
| 49 |
+ |
|
| 50 |
+ err := runDockerCp(c, containerCpPathTrailingSep(cID, "file1"), tmpDir) |
|
| 51 |
+ if err == nil {
|
|
| 52 |
+ c.Fatal("expected IsNotDir error, but got nil instead")
|
|
| 53 |
+ } |
|
| 54 |
+ |
|
| 55 |
+ if !isCpNotDir(err) {
|
|
| 56 |
+ c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
|
|
| 57 |
+ } |
|
| 58 |
+} |
|
| 59 |
+ |
|
| 60 |
+// Test for error when SRC is a valid file or directory, |
|
| 61 |
+// bu the DST parent directory does not exist. |
|
| 62 |
+func (s *DockerSuite) TestCpFromErrDstParentNotExists(c *check.C) {
|
|
| 63 |
+ cID := makeTestContainer(c, testContainerOptions{addContent: true})
|
|
| 64 |
+ defer deleteContainer(cID) |
|
| 65 |
+ |
|
| 66 |
+ tmpDir := getTestDir(c, "test-cp-from-err-dst-parent-not-exists") |
|
| 67 |
+ defer os.RemoveAll(tmpDir) |
|
| 68 |
+ |
|
| 69 |
+ makeTestContentInDir(c, tmpDir) |
|
| 70 |
+ |
|
| 71 |
+ // Try with a file source. |
|
| 72 |
+ srcPath := containerCpPath(cID, "/file1") |
|
| 73 |
+ dstPath := cpPath(tmpDir, "notExists", "file1") |
|
| 74 |
+ |
|
| 75 |
+ err := runDockerCp(c, srcPath, dstPath) |
|
| 76 |
+ if err == nil {
|
|
| 77 |
+ c.Fatal("expected IsNotExist error, but got nil instead")
|
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ if !isCpNotExist(err) {
|
|
| 81 |
+ c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
|
|
| 82 |
+ } |
|
| 83 |
+ |
|
| 84 |
+ // Try with a directory source. |
|
| 85 |
+ srcPath = containerCpPath(cID, "/dir1") |
|
| 86 |
+ |
|
| 87 |
+ if err := runDockerCp(c, srcPath, dstPath); err == nil {
|
|
| 88 |
+ c.Fatal("expected IsNotExist error, but got nil instead")
|
|
| 89 |
+ } |
|
| 90 |
+ |
|
| 91 |
+ if !isCpNotExist(err) {
|
|
| 92 |
+ c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
|
|
| 93 |
+ } |
|
| 94 |
+} |
|
| 95 |
+ |
|
| 96 |
+// Test for error when DST ends in a trailing |
|
| 97 |
+// path separator but exists as a file. |
|
| 98 |
+func (s *DockerSuite) TestCpFromErrDstNotDir(c *check.C) {
|
|
| 99 |
+ cID := makeTestContainer(c, testContainerOptions{addContent: true})
|
|
| 100 |
+ defer deleteContainer(cID) |
|
| 101 |
+ |
|
| 102 |
+ tmpDir := getTestDir(c, "test-cp-from-err-dst-not-dir") |
|
| 103 |
+ defer os.RemoveAll(tmpDir) |
|
| 104 |
+ |
|
| 105 |
+ makeTestContentInDir(c, tmpDir) |
|
| 106 |
+ |
|
| 107 |
+ // Try with a file source. |
|
| 108 |
+ srcPath := containerCpPath(cID, "/file1") |
|
| 109 |
+ dstPath := cpPathTrailingSep(tmpDir, "file1") |
|
| 110 |
+ |
|
| 111 |
+ err := runDockerCp(c, srcPath, dstPath) |
|
| 112 |
+ if err == nil {
|
|
| 113 |
+ c.Fatal("expected IsNotDir error, but got nil instead")
|
|
| 114 |
+ } |
|
| 115 |
+ |
|
| 116 |
+ if !isCpNotDir(err) {
|
|
| 117 |
+ c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
|
|
| 118 |
+ } |
|
| 119 |
+ |
|
| 120 |
+ // Try with a directory source. |
|
| 121 |
+ srcPath = containerCpPath(cID, "/dir1") |
|
| 122 |
+ |
|
| 123 |
+ if err := runDockerCp(c, srcPath, dstPath); err == nil {
|
|
| 124 |
+ c.Fatal("expected IsNotDir error, but got nil instead")
|
|
| 125 |
+ } |
|
| 126 |
+ |
|
| 127 |
+ if !isCpNotDir(err) {
|
|
| 128 |
+ c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
|
|
| 129 |
+ } |
|
| 130 |
+} |
|
| 131 |
+ |
|
| 132 |
+// Possibilities are reduced to the remaining 10 cases: |
|
| 133 |
+// |
|
| 134 |
+// case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action |
|
| 135 |
+// =================================================================================================== |
|
| 136 |
+// A | no | - | no | - | no | create file |
|
| 137 |
+// B | no | - | no | - | yes | error |
|
| 138 |
+// C | no | - | yes | no | - | overwrite file |
|
| 139 |
+// D | no | - | yes | yes | - | create file in dst dir |
|
| 140 |
+// E | yes | no | no | - | - | create dir, copy contents |
|
| 141 |
+// F | yes | no | yes | no | - | error |
|
| 142 |
+// G | yes | no | yes | yes | - | copy dir and contents |
|
| 143 |
+// H | yes | yes | no | - | - | create dir, copy contents |
|
| 144 |
+// I | yes | yes | yes | no | - | error |
|
| 145 |
+// J | yes | yes | yes | yes | - | copy dir contents |
|
| 146 |
+// |
|
| 147 |
+ |
|
| 148 |
+// A. SRC specifies a file and DST (no trailing path separator) doesn't |
|
| 149 |
+// exist. This should create a file with the name DST and copy the |
|
| 150 |
+// contents of the source file into it. |
|
| 151 |
+func (s *DockerSuite) TestCpFromCaseA(c *check.C) {
|
|
| 152 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 153 |
+ addContent: true, workDir: "/root", |
|
| 154 |
+ }) |
|
| 155 |
+ defer deleteContainer(cID) |
|
| 156 |
+ |
|
| 157 |
+ tmpDir := getTestDir(c, "test-cp-from-case-a") |
|
| 158 |
+ defer os.RemoveAll(tmpDir) |
|
| 159 |
+ |
|
| 160 |
+ srcPath := containerCpPath(cID, "/root/file1") |
|
| 161 |
+ dstPath := cpPath(tmpDir, "itWorks.txt") |
|
| 162 |
+ |
|
| 163 |
+ if err := runDockerCp(c, srcPath, dstPath); err != nil {
|
|
| 164 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 165 |
+ } |
|
| 166 |
+ |
|
| 167 |
+ if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
|
|
| 168 |
+ c.Fatal(err) |
|
| 169 |
+ } |
|
| 170 |
+} |
|
| 171 |
+ |
|
| 172 |
+// B. SRC specifies a file and DST (with trailing path separator) doesn't |
|
| 173 |
+// exist. This should cause an error because the copy operation cannot |
|
| 174 |
+// create a directory when copying a single file. |
|
| 175 |
+func (s *DockerSuite) TestCpFromCaseB(c *check.C) {
|
|
| 176 |
+ cID := makeTestContainer(c, testContainerOptions{addContent: true})
|
|
| 177 |
+ defer deleteContainer(cID) |
|
| 178 |
+ |
|
| 179 |
+ tmpDir := getTestDir(c, "test-cp-from-case-b") |
|
| 180 |
+ defer os.RemoveAll(tmpDir) |
|
| 181 |
+ |
|
| 182 |
+ srcPath := containerCpPath(cID, "/file1") |
|
| 183 |
+ dstDir := cpPathTrailingSep(tmpDir, "testDir") |
|
| 184 |
+ |
|
| 185 |
+ err := runDockerCp(c, srcPath, dstDir) |
|
| 186 |
+ if err == nil {
|
|
| 187 |
+ c.Fatal("expected DirNotExists error, but got nil instead")
|
|
| 188 |
+ } |
|
| 189 |
+ |
|
| 190 |
+ if !isCpDirNotExist(err) {
|
|
| 191 |
+ c.Fatalf("expected DirNotExists error, but got %T: %s", err, err)
|
|
| 192 |
+ } |
|
| 193 |
+} |
|
| 194 |
+ |
|
| 195 |
+// C. SRC specifies a file and DST exists as a file. This should overwrite |
|
| 196 |
+// the file at DST with the contents of the source file. |
|
| 197 |
+func (s *DockerSuite) TestCpFromCaseC(c *check.C) {
|
|
| 198 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 199 |
+ addContent: true, workDir: "/root", |
|
| 200 |
+ }) |
|
| 201 |
+ defer deleteContainer(cID) |
|
| 202 |
+ |
|
| 203 |
+ tmpDir := getTestDir(c, "test-cp-from-case-c") |
|
| 204 |
+ defer os.RemoveAll(tmpDir) |
|
| 205 |
+ |
|
| 206 |
+ makeTestContentInDir(c, tmpDir) |
|
| 207 |
+ |
|
| 208 |
+ srcPath := containerCpPath(cID, "/root/file1") |
|
| 209 |
+ dstPath := cpPath(tmpDir, "file2") |
|
| 210 |
+ |
|
| 211 |
+ // Ensure the local file starts with different content. |
|
| 212 |
+ if err := fileContentEquals(c, dstPath, "file2\n"); err != nil {
|
|
| 213 |
+ c.Fatal(err) |
|
| 214 |
+ } |
|
| 215 |
+ |
|
| 216 |
+ if err := runDockerCp(c, srcPath, dstPath); err != nil {
|
|
| 217 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 218 |
+ } |
|
| 219 |
+ |
|
| 220 |
+ if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
|
|
| 221 |
+ c.Fatal(err) |
|
| 222 |
+ } |
|
| 223 |
+} |
|
| 224 |
+ |
|
| 225 |
+// D. SRC specifies a file and DST exists as a directory. This should place |
|
| 226 |
+// a copy of the source file inside it using the basename from SRC. Ensure |
|
| 227 |
+// this works whether DST has a trailing path separator or not. |
|
| 228 |
+func (s *DockerSuite) TestCpFromCaseD(c *check.C) {
|
|
| 229 |
+ cID := makeTestContainer(c, testContainerOptions{addContent: true})
|
|
| 230 |
+ defer deleteContainer(cID) |
|
| 231 |
+ |
|
| 232 |
+ tmpDir := getTestDir(c, "test-cp-from-case-d") |
|
| 233 |
+ defer os.RemoveAll(tmpDir) |
|
| 234 |
+ |
|
| 235 |
+ makeTestContentInDir(c, tmpDir) |
|
| 236 |
+ |
|
| 237 |
+ srcPath := containerCpPath(cID, "/file1") |
|
| 238 |
+ dstDir := cpPath(tmpDir, "dir1") |
|
| 239 |
+ dstPath := filepath.Join(dstDir, "file1") |
|
| 240 |
+ |
|
| 241 |
+ // Ensure that dstPath doesn't exist. |
|
| 242 |
+ if _, err := os.Stat(dstPath); !os.IsNotExist(err) {
|
|
| 243 |
+ c.Fatalf("did not expect dstPath %q to exist", dstPath)
|
|
| 244 |
+ } |
|
| 245 |
+ |
|
| 246 |
+ if err := runDockerCp(c, srcPath, dstDir); err != nil {
|
|
| 247 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 248 |
+ } |
|
| 249 |
+ |
|
| 250 |
+ if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
|
|
| 251 |
+ c.Fatal(err) |
|
| 252 |
+ } |
|
| 253 |
+ |
|
| 254 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 255 |
+ |
|
| 256 |
+ if err := os.RemoveAll(dstDir); err != nil {
|
|
| 257 |
+ c.Fatalf("unable to remove dstDir: %s", err)
|
|
| 258 |
+ } |
|
| 259 |
+ |
|
| 260 |
+ if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
|
|
| 261 |
+ c.Fatalf("unable to make dstDir: %s", err)
|
|
| 262 |
+ } |
|
| 263 |
+ |
|
| 264 |
+ dstDir = cpPathTrailingSep(tmpDir, "dir1") |
|
| 265 |
+ |
|
| 266 |
+ if err := runDockerCp(c, srcPath, dstDir); err != nil {
|
|
| 267 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 268 |
+ } |
|
| 269 |
+ |
|
| 270 |
+ if err := fileContentEquals(c, dstPath, "file1\n"); err != nil {
|
|
| 271 |
+ c.Fatal(err) |
|
| 272 |
+ } |
|
| 273 |
+} |
|
| 274 |
+ |
|
| 275 |
+// E. SRC specifies a directory and DST does not exist. This should create a |
|
| 276 |
+// directory at DST and copy the contents of the SRC directory into the DST |
|
| 277 |
+// directory. Ensure this works whether DST has a trailing path separator or |
|
| 278 |
+// not. |
|
| 279 |
+func (s *DockerSuite) TestCpFromCaseE(c *check.C) {
|
|
| 280 |
+ cID := makeTestContainer(c, testContainerOptions{addContent: true})
|
|
| 281 |
+ defer deleteContainer(cID) |
|
| 282 |
+ |
|
| 283 |
+ tmpDir := getTestDir(c, "test-cp-from-case-e") |
|
| 284 |
+ defer os.RemoveAll(tmpDir) |
|
| 285 |
+ |
|
| 286 |
+ srcDir := containerCpPath(cID, "dir1") |
|
| 287 |
+ dstDir := cpPath(tmpDir, "testDir") |
|
| 288 |
+ dstPath := filepath.Join(dstDir, "file1-1") |
|
| 289 |
+ |
|
| 290 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 291 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 292 |
+ } |
|
| 293 |
+ |
|
| 294 |
+ if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
|
|
| 295 |
+ c.Fatal(err) |
|
| 296 |
+ } |
|
| 297 |
+ |
|
| 298 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 299 |
+ |
|
| 300 |
+ if err := os.RemoveAll(dstDir); err != nil {
|
|
| 301 |
+ c.Fatalf("unable to remove dstDir: %s", err)
|
|
| 302 |
+ } |
|
| 303 |
+ |
|
| 304 |
+ dstDir = cpPathTrailingSep(tmpDir, "testDir") |
|
| 305 |
+ |
|
| 306 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 307 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 308 |
+ } |
|
| 309 |
+ |
|
| 310 |
+ if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
|
|
| 311 |
+ c.Fatal(err) |
|
| 312 |
+ } |
|
| 313 |
+} |
|
| 314 |
+ |
|
| 315 |
+// F. SRC specifies a directory and DST exists as a file. This should cause an |
|
| 316 |
+// error as it is not possible to overwrite a file with a directory. |
|
| 317 |
+func (s *DockerSuite) TestCpFromCaseF(c *check.C) {
|
|
| 318 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 319 |
+ addContent: true, workDir: "/root", |
|
| 320 |
+ }) |
|
| 321 |
+ defer deleteContainer(cID) |
|
| 322 |
+ |
|
| 323 |
+ tmpDir := getTestDir(c, "test-cp-from-case-f") |
|
| 324 |
+ defer os.RemoveAll(tmpDir) |
|
| 325 |
+ |
|
| 326 |
+ makeTestContentInDir(c, tmpDir) |
|
| 327 |
+ |
|
| 328 |
+ srcDir := containerCpPath(cID, "/root/dir1") |
|
| 329 |
+ dstFile := cpPath(tmpDir, "file1") |
|
| 330 |
+ |
|
| 331 |
+ err := runDockerCp(c, srcDir, dstFile) |
|
| 332 |
+ if err == nil {
|
|
| 333 |
+ c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
|
|
| 334 |
+ } |
|
| 335 |
+ |
|
| 336 |
+ if !isCpCannotCopyDir(err) {
|
|
| 337 |
+ c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
|
|
| 338 |
+ } |
|
| 339 |
+} |
|
| 340 |
+ |
|
| 341 |
+// G. SRC specifies a directory and DST exists as a directory. This should copy |
|
| 342 |
+// the SRC directory and all its contents to the DST directory. Ensure this |
|
| 343 |
+// works whether DST has a trailing path separator or not. |
|
| 344 |
+func (s *DockerSuite) TestCpFromCaseG(c *check.C) {
|
|
| 345 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 346 |
+ addContent: true, workDir: "/root", |
|
| 347 |
+ }) |
|
| 348 |
+ defer deleteContainer(cID) |
|
| 349 |
+ |
|
| 350 |
+ tmpDir := getTestDir(c, "test-cp-from-case-g") |
|
| 351 |
+ defer os.RemoveAll(tmpDir) |
|
| 352 |
+ |
|
| 353 |
+ makeTestContentInDir(c, tmpDir) |
|
| 354 |
+ |
|
| 355 |
+ srcDir := containerCpPath(cID, "/root/dir1") |
|
| 356 |
+ dstDir := cpPath(tmpDir, "dir2") |
|
| 357 |
+ resultDir := filepath.Join(dstDir, "dir1") |
|
| 358 |
+ dstPath := filepath.Join(resultDir, "file1-1") |
|
| 359 |
+ |
|
| 360 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 361 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 362 |
+ } |
|
| 363 |
+ |
|
| 364 |
+ if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
|
|
| 365 |
+ c.Fatal(err) |
|
| 366 |
+ } |
|
| 367 |
+ |
|
| 368 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 369 |
+ |
|
| 370 |
+ if err := os.RemoveAll(dstDir); err != nil {
|
|
| 371 |
+ c.Fatalf("unable to remove dstDir: %s", err)
|
|
| 372 |
+ } |
|
| 373 |
+ |
|
| 374 |
+ if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
|
|
| 375 |
+ c.Fatalf("unable to make dstDir: %s", err)
|
|
| 376 |
+ } |
|
| 377 |
+ |
|
| 378 |
+ dstDir = cpPathTrailingSep(tmpDir, "dir2") |
|
| 379 |
+ |
|
| 380 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 381 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 382 |
+ } |
|
| 383 |
+ |
|
| 384 |
+ if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
|
|
| 385 |
+ c.Fatal(err) |
|
| 386 |
+ } |
|
| 387 |
+} |
|
| 388 |
+ |
|
| 389 |
+// H. SRC specifies a directory's contents only and DST does not exist. This |
|
| 390 |
+// should create a directory at DST and copy the contents of the SRC |
|
| 391 |
+// directory (but not the directory itself) into the DST directory. Ensure |
|
| 392 |
+// this works whether DST has a trailing path separator or not. |
|
| 393 |
+func (s *DockerSuite) TestCpFromCaseH(c *check.C) {
|
|
| 394 |
+ cID := makeTestContainer(c, testContainerOptions{addContent: true})
|
|
| 395 |
+ defer deleteContainer(cID) |
|
| 396 |
+ |
|
| 397 |
+ tmpDir := getTestDir(c, "test-cp-from-case-h") |
|
| 398 |
+ defer os.RemoveAll(tmpDir) |
|
| 399 |
+ |
|
| 400 |
+ srcDir := containerCpPathTrailingSep(cID, "dir1") + "." |
|
| 401 |
+ dstDir := cpPath(tmpDir, "testDir") |
|
| 402 |
+ dstPath := filepath.Join(dstDir, "file1-1") |
|
| 403 |
+ |
|
| 404 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 405 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 406 |
+ } |
|
| 407 |
+ |
|
| 408 |
+ if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
|
|
| 409 |
+ c.Fatal(err) |
|
| 410 |
+ } |
|
| 411 |
+ |
|
| 412 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 413 |
+ |
|
| 414 |
+ if err := os.RemoveAll(dstDir); err != nil {
|
|
| 415 |
+ c.Fatalf("unable to remove resultDir: %s", err)
|
|
| 416 |
+ } |
|
| 417 |
+ |
|
| 418 |
+ dstDir = cpPathTrailingSep(tmpDir, "testDir") |
|
| 419 |
+ |
|
| 420 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 421 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 422 |
+ } |
|
| 423 |
+ |
|
| 424 |
+ if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
|
|
| 425 |
+ c.Fatal(err) |
|
| 426 |
+ } |
|
| 427 |
+} |
|
| 428 |
+ |
|
| 429 |
+// I. SRC specifies a direcotry's contents only and DST exists as a file. This |
|
| 430 |
+// should cause an error as it is not possible to overwrite a file with a |
|
| 431 |
+// directory. |
|
| 432 |
+func (s *DockerSuite) TestCpFromCaseI(c *check.C) {
|
|
| 433 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 434 |
+ addContent: true, workDir: "/root", |
|
| 435 |
+ }) |
|
| 436 |
+ defer deleteContainer(cID) |
|
| 437 |
+ |
|
| 438 |
+ tmpDir := getTestDir(c, "test-cp-from-case-i") |
|
| 439 |
+ defer os.RemoveAll(tmpDir) |
|
| 440 |
+ |
|
| 441 |
+ makeTestContentInDir(c, tmpDir) |
|
| 442 |
+ |
|
| 443 |
+ srcDir := containerCpPathTrailingSep(cID, "/root/dir1") + "." |
|
| 444 |
+ dstFile := cpPath(tmpDir, "file1") |
|
| 445 |
+ |
|
| 446 |
+ err := runDockerCp(c, srcDir, dstFile) |
|
| 447 |
+ if err == nil {
|
|
| 448 |
+ c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
|
|
| 449 |
+ } |
|
| 450 |
+ |
|
| 451 |
+ if !isCpCannotCopyDir(err) {
|
|
| 452 |
+ c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
|
|
| 453 |
+ } |
|
| 454 |
+} |
|
| 455 |
+ |
|
| 456 |
+// J. SRC specifies a directory's contents only and DST exists as a directory. |
|
| 457 |
+// This should copy the contents of the SRC directory (but not the directory |
|
| 458 |
+// itself) into the DST directory. Ensure this works whether DST has a |
|
| 459 |
+// trailing path separator or not. |
|
| 460 |
+func (s *DockerSuite) TestCpFromCaseJ(c *check.C) {
|
|
| 461 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 462 |
+ addContent: true, workDir: "/root", |
|
| 463 |
+ }) |
|
| 464 |
+ defer deleteContainer(cID) |
|
| 465 |
+ |
|
| 466 |
+ tmpDir := getTestDir(c, "test-cp-from-case-j") |
|
| 467 |
+ defer os.RemoveAll(tmpDir) |
|
| 468 |
+ |
|
| 469 |
+ makeTestContentInDir(c, tmpDir) |
|
| 470 |
+ |
|
| 471 |
+ srcDir := containerCpPathTrailingSep(cID, "/root/dir1") + "." |
|
| 472 |
+ dstDir := cpPath(tmpDir, "dir2") |
|
| 473 |
+ dstPath := filepath.Join(dstDir, "file1-1") |
|
| 474 |
+ |
|
| 475 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 476 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 477 |
+ } |
|
| 478 |
+ |
|
| 479 |
+ if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
|
|
| 480 |
+ c.Fatal(err) |
|
| 481 |
+ } |
|
| 482 |
+ |
|
| 483 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 484 |
+ |
|
| 485 |
+ if err := os.RemoveAll(dstDir); err != nil {
|
|
| 486 |
+ c.Fatalf("unable to remove dstDir: %s", err)
|
|
| 487 |
+ } |
|
| 488 |
+ |
|
| 489 |
+ if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
|
|
| 490 |
+ c.Fatalf("unable to make dstDir: %s", err)
|
|
| 491 |
+ } |
|
| 492 |
+ |
|
| 493 |
+ dstDir = cpPathTrailingSep(tmpDir, "dir2") |
|
| 494 |
+ |
|
| 495 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 496 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 497 |
+ } |
|
| 498 |
+ |
|
| 499 |
+ if err := fileContentEquals(c, dstPath, "file1-1\n"); err != nil {
|
|
| 500 |
+ c.Fatal(err) |
|
| 501 |
+ } |
|
| 502 |
+} |
| ... | ... |
@@ -23,6 +23,18 @@ const ( |
| 23 | 23 |
cpHostContents = "hello, i am the host" |
| 24 | 24 |
) |
| 25 | 25 |
|
| 26 |
+// Ensure that an all-local path case returns an error. |
|
| 27 |
+func (s *DockerSuite) TestCpLocalOnly(c *check.C) {
|
|
| 28 |
+ err := runDockerCp(c, "foo", "bar") |
|
| 29 |
+ if err == nil {
|
|
| 30 |
+ c.Fatal("expected failure, got success")
|
|
| 31 |
+ } |
|
| 32 |
+ |
|
| 33 |
+ if !strings.Contains(err.Error(), "must specify at least one container source") {
|
|
| 34 |
+ c.Fatalf("unexpected output: %s", err.Error())
|
|
| 35 |
+ } |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 26 | 38 |
// Test for #5656 |
| 27 | 39 |
// Check that garbage paths don't escape the container's rootfs |
| 28 | 40 |
func (s *DockerSuite) TestCpGarbagePath(c *check.C) {
|
| 29 | 41 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,634 @@ |
| 0 |
+package main |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "os" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/go-check/check" |
|
| 6 |
+) |
|
| 7 |
+ |
|
| 8 |
+// docker cp LOCALPATH CONTAINER:PATH |
|
| 9 |
+ |
|
| 10 |
+// Try all of the test cases from the archive package which implements the |
|
| 11 |
+// internals of `docker cp` and ensure that the behavior matches when actually |
|
| 12 |
+// copying to and from containers. |
|
| 13 |
+ |
|
| 14 |
+// Basic assumptions about SRC and DST: |
|
| 15 |
+// 1. SRC must exist. |
|
| 16 |
+// 2. If SRC ends with a trailing separator, it must be a directory. |
|
| 17 |
+// 3. DST parent directory must exist. |
|
| 18 |
+// 4. If DST exists as a file, it must not end with a trailing separator. |
|
| 19 |
+ |
|
| 20 |
+// First get these easy error cases out of the way. |
|
| 21 |
+ |
|
| 22 |
+// Test for error when SRC does not exist. |
|
| 23 |
+func (s *DockerSuite) TestCpToErrSrcNotExists(c *check.C) {
|
|
| 24 |
+ cID := makeTestContainer(c, testContainerOptions{})
|
|
| 25 |
+ defer deleteContainer(cID) |
|
| 26 |
+ |
|
| 27 |
+ tmpDir := getTestDir(c, "test-cp-to-err-src-not-exists") |
|
| 28 |
+ defer os.RemoveAll(tmpDir) |
|
| 29 |
+ |
|
| 30 |
+ srcPath := cpPath(tmpDir, "file1") |
|
| 31 |
+ dstPath := containerCpPath(cID, "file1") |
|
| 32 |
+ |
|
| 33 |
+ err := runDockerCp(c, srcPath, dstPath) |
|
| 34 |
+ if err == nil {
|
|
| 35 |
+ c.Fatal("expected IsNotExist error, but got nil instead")
|
|
| 36 |
+ } |
|
| 37 |
+ |
|
| 38 |
+ if !isCpNotExist(err) {
|
|
| 39 |
+ c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
|
|
| 40 |
+ } |
|
| 41 |
+} |
|
| 42 |
+ |
|
| 43 |
+// Test for error when SRC ends in a trailing |
|
| 44 |
+// path separator but it exists as a file. |
|
| 45 |
+func (s *DockerSuite) TestCpToErrSrcNotDir(c *check.C) {
|
|
| 46 |
+ cID := makeTestContainer(c, testContainerOptions{})
|
|
| 47 |
+ defer deleteContainer(cID) |
|
| 48 |
+ |
|
| 49 |
+ tmpDir := getTestDir(c, "test-cp-to-err-src-not-dir") |
|
| 50 |
+ defer os.RemoveAll(tmpDir) |
|
| 51 |
+ |
|
| 52 |
+ makeTestContentInDir(c, tmpDir) |
|
| 53 |
+ |
|
| 54 |
+ srcPath := cpPathTrailingSep(tmpDir, "file1") |
|
| 55 |
+ dstPath := containerCpPath(cID, "testDir") |
|
| 56 |
+ |
|
| 57 |
+ err := runDockerCp(c, srcPath, dstPath) |
|
| 58 |
+ if err == nil {
|
|
| 59 |
+ c.Fatal("expected IsNotDir error, but got nil instead")
|
|
| 60 |
+ } |
|
| 61 |
+ |
|
| 62 |
+ if !isCpNotDir(err) {
|
|
| 63 |
+ c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
|
|
| 64 |
+ } |
|
| 65 |
+} |
|
| 66 |
+ |
|
| 67 |
+// Test for error when SRC is a valid file or directory, |
|
| 68 |
+// bu the DST parent directory does not exist. |
|
| 69 |
+func (s *DockerSuite) TestCpToErrDstParentNotExists(c *check.C) {
|
|
| 70 |
+ cID := makeTestContainer(c, testContainerOptions{addContent: true})
|
|
| 71 |
+ defer deleteContainer(cID) |
|
| 72 |
+ |
|
| 73 |
+ tmpDir := getTestDir(c, "test-cp-to-err-dst-parent-not-exists") |
|
| 74 |
+ defer os.RemoveAll(tmpDir) |
|
| 75 |
+ |
|
| 76 |
+ makeTestContentInDir(c, tmpDir) |
|
| 77 |
+ |
|
| 78 |
+ // Try with a file source. |
|
| 79 |
+ srcPath := cpPath(tmpDir, "file1") |
|
| 80 |
+ dstPath := containerCpPath(cID, "/notExists", "file1") |
|
| 81 |
+ |
|
| 82 |
+ err := runDockerCp(c, srcPath, dstPath) |
|
| 83 |
+ if err == nil {
|
|
| 84 |
+ c.Fatal("expected IsNotExist error, but got nil instead")
|
|
| 85 |
+ } |
|
| 86 |
+ |
|
| 87 |
+ if !isCpNotExist(err) {
|
|
| 88 |
+ c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
|
|
| 89 |
+ } |
|
| 90 |
+ |
|
| 91 |
+ // Try with a directory source. |
|
| 92 |
+ srcPath = cpPath(tmpDir, "dir1") |
|
| 93 |
+ |
|
| 94 |
+ if err := runDockerCp(c, srcPath, dstPath); err == nil {
|
|
| 95 |
+ c.Fatal("expected IsNotExist error, but got nil instead")
|
|
| 96 |
+ } |
|
| 97 |
+ |
|
| 98 |
+ if !isCpNotExist(err) {
|
|
| 99 |
+ c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
|
|
| 100 |
+ } |
|
| 101 |
+} |
|
| 102 |
+ |
|
| 103 |
+// Test for error when DST ends in a trailing path separator but exists as a |
|
| 104 |
+// file. Also test that we cannot overwirite an existing directory with a |
|
| 105 |
+// non-directory and cannot overwrite an existing |
|
| 106 |
+func (s *DockerSuite) TestCpToErrDstNotDir(c *check.C) {
|
|
| 107 |
+ cID := makeTestContainer(c, testContainerOptions{addContent: true})
|
|
| 108 |
+ defer deleteContainer(cID) |
|
| 109 |
+ |
|
| 110 |
+ tmpDir := getTestDir(c, "test-cp-to-err-dst-not-dir") |
|
| 111 |
+ defer os.RemoveAll(tmpDir) |
|
| 112 |
+ |
|
| 113 |
+ makeTestContentInDir(c, tmpDir) |
|
| 114 |
+ |
|
| 115 |
+ // Try with a file source. |
|
| 116 |
+ srcPath := cpPath(tmpDir, "dir1/file1-1") |
|
| 117 |
+ dstPath := containerCpPathTrailingSep(cID, "file1") |
|
| 118 |
+ |
|
| 119 |
+ // The client should encounter an error trying to stat the destination |
|
| 120 |
+ // and then be unable to copy since the destination is asserted to be a |
|
| 121 |
+ // directory but does not exist. |
|
| 122 |
+ err := runDockerCp(c, srcPath, dstPath) |
|
| 123 |
+ if err == nil {
|
|
| 124 |
+ c.Fatal("expected DirNotExist error, but got nil instead")
|
|
| 125 |
+ } |
|
| 126 |
+ |
|
| 127 |
+ if !isCpDirNotExist(err) {
|
|
| 128 |
+ c.Fatalf("expected DirNotExist error, but got %T: %s", err, err)
|
|
| 129 |
+ } |
|
| 130 |
+ |
|
| 131 |
+ // Try with a directory source. |
|
| 132 |
+ srcPath = cpPath(tmpDir, "dir1") |
|
| 133 |
+ |
|
| 134 |
+ // The client should encounter an error trying to stat the destination and |
|
| 135 |
+ // then decide to extract to the parent directory instead with a rebased |
|
| 136 |
+ // name in the source archive, but this directory would overwrite the |
|
| 137 |
+ // existing file with the same name. |
|
| 138 |
+ err = runDockerCp(c, srcPath, dstPath) |
|
| 139 |
+ if err == nil {
|
|
| 140 |
+ c.Fatal("expected CannotOverwriteNonDirWithDir error, but got nil instead")
|
|
| 141 |
+ } |
|
| 142 |
+ |
|
| 143 |
+ if !isCannotOverwriteNonDirWithDir(err) {
|
|
| 144 |
+ c.Fatalf("expected CannotOverwriteNonDirWithDir error, but got %T: %s", err, err)
|
|
| 145 |
+ } |
|
| 146 |
+} |
|
| 147 |
+ |
|
| 148 |
+// Possibilities are reduced to the remaining 10 cases: |
|
| 149 |
+// |
|
| 150 |
+// case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action |
|
| 151 |
+// =================================================================================================== |
|
| 152 |
+// A | no | - | no | - | no | create file |
|
| 153 |
+// B | no | - | no | - | yes | error |
|
| 154 |
+// C | no | - | yes | no | - | overwrite file |
|
| 155 |
+// D | no | - | yes | yes | - | create file in dst dir |
|
| 156 |
+// E | yes | no | no | - | - | create dir, copy contents |
|
| 157 |
+// F | yes | no | yes | no | - | error |
|
| 158 |
+// G | yes | no | yes | yes | - | copy dir and contents |
|
| 159 |
+// H | yes | yes | no | - | - | create dir, copy contents |
|
| 160 |
+// I | yes | yes | yes | no | - | error |
|
| 161 |
+// J | yes | yes | yes | yes | - | copy dir contents |
|
| 162 |
+// |
|
| 163 |
+ |
|
| 164 |
+// A. SRC specifies a file and DST (no trailing path separator) doesn't |
|
| 165 |
+// exist. This should create a file with the name DST and copy the |
|
| 166 |
+// contents of the source file into it. |
|
| 167 |
+func (s *DockerSuite) TestCpToCaseA(c *check.C) {
|
|
| 168 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 169 |
+ workDir: "/root", command: makeCatFileCommand("itWorks.txt"),
|
|
| 170 |
+ }) |
|
| 171 |
+ defer deleteContainer(cID) |
|
| 172 |
+ |
|
| 173 |
+ tmpDir := getTestDir(c, "test-cp-to-case-a") |
|
| 174 |
+ defer os.RemoveAll(tmpDir) |
|
| 175 |
+ |
|
| 176 |
+ makeTestContentInDir(c, tmpDir) |
|
| 177 |
+ |
|
| 178 |
+ srcPath := cpPath(tmpDir, "file1") |
|
| 179 |
+ dstPath := containerCpPath(cID, "/root/itWorks.txt") |
|
| 180 |
+ |
|
| 181 |
+ if err := runDockerCp(c, srcPath, dstPath); err != nil {
|
|
| 182 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 183 |
+ } |
|
| 184 |
+ |
|
| 185 |
+ if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
|
|
| 186 |
+ c.Fatal(err) |
|
| 187 |
+ } |
|
| 188 |
+} |
|
| 189 |
+ |
|
| 190 |
+// B. SRC specifies a file and DST (with trailing path separator) doesn't |
|
| 191 |
+// exist. This should cause an error because the copy operation cannot |
|
| 192 |
+// create a directory when copying a single file. |
|
| 193 |
+func (s *DockerSuite) TestCpToCaseB(c *check.C) {
|
|
| 194 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 195 |
+ command: makeCatFileCommand("testDir/file1"),
|
|
| 196 |
+ }) |
|
| 197 |
+ defer deleteContainer(cID) |
|
| 198 |
+ |
|
| 199 |
+ tmpDir := getTestDir(c, "test-cp-to-case-b") |
|
| 200 |
+ defer os.RemoveAll(tmpDir) |
|
| 201 |
+ |
|
| 202 |
+ makeTestContentInDir(c, tmpDir) |
|
| 203 |
+ |
|
| 204 |
+ srcPath := cpPath(tmpDir, "file1") |
|
| 205 |
+ dstDir := containerCpPathTrailingSep(cID, "testDir") |
|
| 206 |
+ |
|
| 207 |
+ err := runDockerCp(c, srcPath, dstDir) |
|
| 208 |
+ if err == nil {
|
|
| 209 |
+ c.Fatal("expected DirNotExists error, but got nil instead")
|
|
| 210 |
+ } |
|
| 211 |
+ |
|
| 212 |
+ if !isCpDirNotExist(err) {
|
|
| 213 |
+ c.Fatalf("expected DirNotExists error, but got %T: %s", err, err)
|
|
| 214 |
+ } |
|
| 215 |
+} |
|
| 216 |
+ |
|
| 217 |
+// C. SRC specifies a file and DST exists as a file. This should overwrite |
|
| 218 |
+// the file at DST with the contents of the source file. |
|
| 219 |
+func (s *DockerSuite) TestCpToCaseC(c *check.C) {
|
|
| 220 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 221 |
+ addContent: true, workDir: "/root", |
|
| 222 |
+ command: makeCatFileCommand("file2"),
|
|
| 223 |
+ }) |
|
| 224 |
+ defer deleteContainer(cID) |
|
| 225 |
+ |
|
| 226 |
+ tmpDir := getTestDir(c, "test-cp-to-case-c") |
|
| 227 |
+ defer os.RemoveAll(tmpDir) |
|
| 228 |
+ |
|
| 229 |
+ makeTestContentInDir(c, tmpDir) |
|
| 230 |
+ |
|
| 231 |
+ srcPath := cpPath(tmpDir, "file1") |
|
| 232 |
+ dstPath := containerCpPath(cID, "/root/file2") |
|
| 233 |
+ |
|
| 234 |
+ // Ensure the container's file starts with the original content. |
|
| 235 |
+ if err := containerStartOutputEquals(c, cID, "file2\n"); err != nil {
|
|
| 236 |
+ c.Fatal(err) |
|
| 237 |
+ } |
|
| 238 |
+ |
|
| 239 |
+ if err := runDockerCp(c, srcPath, dstPath); err != nil {
|
|
| 240 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 241 |
+ } |
|
| 242 |
+ |
|
| 243 |
+ // Should now contain file1's contents. |
|
| 244 |
+ if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
|
|
| 245 |
+ c.Fatal(err) |
|
| 246 |
+ } |
|
| 247 |
+} |
|
| 248 |
+ |
|
| 249 |
+// D. SRC specifies a file and DST exists as a directory. This should place |
|
| 250 |
+// a copy of the source file inside it using the basename from SRC. Ensure |
|
| 251 |
+// this works whether DST has a trailing path separator or not. |
|
| 252 |
+func (s *DockerSuite) TestCpToCaseD(c *check.C) {
|
|
| 253 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 254 |
+ addContent: true, |
|
| 255 |
+ command: makeCatFileCommand("/dir1/file1"),
|
|
| 256 |
+ }) |
|
| 257 |
+ defer deleteContainer(cID) |
|
| 258 |
+ |
|
| 259 |
+ tmpDir := getTestDir(c, "test-cp-to-case-d") |
|
| 260 |
+ defer os.RemoveAll(tmpDir) |
|
| 261 |
+ |
|
| 262 |
+ makeTestContentInDir(c, tmpDir) |
|
| 263 |
+ |
|
| 264 |
+ srcPath := cpPath(tmpDir, "file1") |
|
| 265 |
+ dstDir := containerCpPath(cID, "dir1") |
|
| 266 |
+ |
|
| 267 |
+ // Ensure that dstPath doesn't exist. |
|
| 268 |
+ if err := containerStartOutputEquals(c, cID, ""); err != nil {
|
|
| 269 |
+ c.Fatal(err) |
|
| 270 |
+ } |
|
| 271 |
+ |
|
| 272 |
+ if err := runDockerCp(c, srcPath, dstDir); err != nil {
|
|
| 273 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 274 |
+ } |
|
| 275 |
+ |
|
| 276 |
+ // Should now contain file1's contents. |
|
| 277 |
+ if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
|
|
| 278 |
+ c.Fatal(err) |
|
| 279 |
+ } |
|
| 280 |
+ |
|
| 281 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 282 |
+ |
|
| 283 |
+ // Make new destination container. |
|
| 284 |
+ cID = makeTestContainer(c, testContainerOptions{
|
|
| 285 |
+ addContent: true, |
|
| 286 |
+ command: makeCatFileCommand("/dir1/file1"),
|
|
| 287 |
+ }) |
|
| 288 |
+ defer deleteContainer(cID) |
|
| 289 |
+ |
|
| 290 |
+ dstDir = containerCpPathTrailingSep(cID, "dir1") |
|
| 291 |
+ |
|
| 292 |
+ // Ensure that dstPath doesn't exist. |
|
| 293 |
+ if err := containerStartOutputEquals(c, cID, ""); err != nil {
|
|
| 294 |
+ c.Fatal(err) |
|
| 295 |
+ } |
|
| 296 |
+ |
|
| 297 |
+ if err := runDockerCp(c, srcPath, dstDir); err != nil {
|
|
| 298 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 299 |
+ } |
|
| 300 |
+ |
|
| 301 |
+ // Should now contain file1's contents. |
|
| 302 |
+ if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
|
|
| 303 |
+ c.Fatal(err) |
|
| 304 |
+ } |
|
| 305 |
+} |
|
| 306 |
+ |
|
| 307 |
+// E. SRC specifies a directory and DST does not exist. This should create a |
|
| 308 |
+// directory at DST and copy the contents of the SRC directory into the DST |
|
| 309 |
+// directory. Ensure this works whether DST has a trailing path separator or |
|
| 310 |
+// not. |
|
| 311 |
+func (s *DockerSuite) TestCpToCaseE(c *check.C) {
|
|
| 312 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 313 |
+ command: makeCatFileCommand("/testDir/file1-1"),
|
|
| 314 |
+ }) |
|
| 315 |
+ defer deleteContainer(cID) |
|
| 316 |
+ |
|
| 317 |
+ tmpDir := getTestDir(c, "test-cp-to-case-e") |
|
| 318 |
+ defer os.RemoveAll(tmpDir) |
|
| 319 |
+ |
|
| 320 |
+ makeTestContentInDir(c, tmpDir) |
|
| 321 |
+ |
|
| 322 |
+ srcDir := cpPath(tmpDir, "dir1") |
|
| 323 |
+ dstDir := containerCpPath(cID, "testDir") |
|
| 324 |
+ |
|
| 325 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 326 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 327 |
+ } |
|
| 328 |
+ |
|
| 329 |
+ // Should now contain file1-1's contents. |
|
| 330 |
+ if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
|
|
| 331 |
+ c.Fatal(err) |
|
| 332 |
+ } |
|
| 333 |
+ |
|
| 334 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 335 |
+ |
|
| 336 |
+ // Make new destination container. |
|
| 337 |
+ cID = makeTestContainer(c, testContainerOptions{
|
|
| 338 |
+ command: makeCatFileCommand("/testDir/file1-1"),
|
|
| 339 |
+ }) |
|
| 340 |
+ defer deleteContainer(cID) |
|
| 341 |
+ |
|
| 342 |
+ dstDir = containerCpPathTrailingSep(cID, "testDir") |
|
| 343 |
+ |
|
| 344 |
+ err := runDockerCp(c, srcDir, dstDir) |
|
| 345 |
+ if err != nil {
|
|
| 346 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 347 |
+ } |
|
| 348 |
+ |
|
| 349 |
+ // Should now contain file1-1's contents. |
|
| 350 |
+ if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
|
|
| 351 |
+ c.Fatal(err) |
|
| 352 |
+ } |
|
| 353 |
+} |
|
| 354 |
+ |
|
| 355 |
+// F. SRC specifies a directory and DST exists as a file. This should cause an |
|
| 356 |
+// error as it is not possible to overwrite a file with a directory. |
|
| 357 |
+func (s *DockerSuite) TestCpToCaseF(c *check.C) {
|
|
| 358 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 359 |
+ addContent: true, workDir: "/root", |
|
| 360 |
+ }) |
|
| 361 |
+ defer deleteContainer(cID) |
|
| 362 |
+ |
|
| 363 |
+ tmpDir := getTestDir(c, "test-cp-to-case-f") |
|
| 364 |
+ defer os.RemoveAll(tmpDir) |
|
| 365 |
+ |
|
| 366 |
+ makeTestContentInDir(c, tmpDir) |
|
| 367 |
+ |
|
| 368 |
+ srcDir := cpPath(tmpDir, "dir1") |
|
| 369 |
+ dstFile := containerCpPath(cID, "/root/file1") |
|
| 370 |
+ |
|
| 371 |
+ err := runDockerCp(c, srcDir, dstFile) |
|
| 372 |
+ if err == nil {
|
|
| 373 |
+ c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
|
|
| 374 |
+ } |
|
| 375 |
+ |
|
| 376 |
+ if !isCpCannotCopyDir(err) {
|
|
| 377 |
+ c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
|
|
| 378 |
+ } |
|
| 379 |
+} |
|
| 380 |
+ |
|
| 381 |
+// G. SRC specifies a directory and DST exists as a directory. This should copy |
|
| 382 |
+// the SRC directory and all its contents to the DST directory. Ensure this |
|
| 383 |
+// works whether DST has a trailing path separator or not. |
|
| 384 |
+func (s *DockerSuite) TestCpToCaseG(c *check.C) {
|
|
| 385 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 386 |
+ addContent: true, workDir: "/root", |
|
| 387 |
+ command: makeCatFileCommand("dir2/dir1/file1-1"),
|
|
| 388 |
+ }) |
|
| 389 |
+ defer deleteContainer(cID) |
|
| 390 |
+ |
|
| 391 |
+ tmpDir := getTestDir(c, "test-cp-to-case-g") |
|
| 392 |
+ defer os.RemoveAll(tmpDir) |
|
| 393 |
+ |
|
| 394 |
+ makeTestContentInDir(c, tmpDir) |
|
| 395 |
+ |
|
| 396 |
+ srcDir := cpPath(tmpDir, "dir1") |
|
| 397 |
+ dstDir := containerCpPath(cID, "/root/dir2") |
|
| 398 |
+ |
|
| 399 |
+ // Ensure that dstPath doesn't exist. |
|
| 400 |
+ if err := containerStartOutputEquals(c, cID, ""); err != nil {
|
|
| 401 |
+ c.Fatal(err) |
|
| 402 |
+ } |
|
| 403 |
+ |
|
| 404 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 405 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 406 |
+ } |
|
| 407 |
+ |
|
| 408 |
+ // Should now contain file1-1's contents. |
|
| 409 |
+ if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
|
|
| 410 |
+ c.Fatal(err) |
|
| 411 |
+ } |
|
| 412 |
+ |
|
| 413 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 414 |
+ |
|
| 415 |
+ // Make new destination container. |
|
| 416 |
+ cID = makeTestContainer(c, testContainerOptions{
|
|
| 417 |
+ addContent: true, |
|
| 418 |
+ command: makeCatFileCommand("/dir2/dir1/file1-1"),
|
|
| 419 |
+ }) |
|
| 420 |
+ defer deleteContainer(cID) |
|
| 421 |
+ |
|
| 422 |
+ dstDir = containerCpPathTrailingSep(cID, "/dir2") |
|
| 423 |
+ |
|
| 424 |
+ // Ensure that dstPath doesn't exist. |
|
| 425 |
+ if err := containerStartOutputEquals(c, cID, ""); err != nil {
|
|
| 426 |
+ c.Fatal(err) |
|
| 427 |
+ } |
|
| 428 |
+ |
|
| 429 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 430 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 431 |
+ } |
|
| 432 |
+ |
|
| 433 |
+ // Should now contain file1-1's contents. |
|
| 434 |
+ if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
|
|
| 435 |
+ c.Fatal(err) |
|
| 436 |
+ } |
|
| 437 |
+} |
|
| 438 |
+ |
|
| 439 |
+// H. SRC specifies a directory's contents only and DST does not exist. This |
|
| 440 |
+// should create a directory at DST and copy the contents of the SRC |
|
| 441 |
+// directory (but not the directory itself) into the DST directory. Ensure |
|
| 442 |
+// this works whether DST has a trailing path separator or not. |
|
| 443 |
+func (s *DockerSuite) TestCpToCaseH(c *check.C) {
|
|
| 444 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 445 |
+ command: makeCatFileCommand("/testDir/file1-1"),
|
|
| 446 |
+ }) |
|
| 447 |
+ defer deleteContainer(cID) |
|
| 448 |
+ |
|
| 449 |
+ tmpDir := getTestDir(c, "test-cp-to-case-h") |
|
| 450 |
+ defer os.RemoveAll(tmpDir) |
|
| 451 |
+ |
|
| 452 |
+ makeTestContentInDir(c, tmpDir) |
|
| 453 |
+ |
|
| 454 |
+ srcDir := cpPathTrailingSep(tmpDir, "dir1") + "." |
|
| 455 |
+ dstDir := containerCpPath(cID, "testDir") |
|
| 456 |
+ |
|
| 457 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 458 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 459 |
+ } |
|
| 460 |
+ |
|
| 461 |
+ // Should now contain file1-1's contents. |
|
| 462 |
+ if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
|
|
| 463 |
+ c.Fatal(err) |
|
| 464 |
+ } |
|
| 465 |
+ |
|
| 466 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 467 |
+ |
|
| 468 |
+ // Make new destination container. |
|
| 469 |
+ cID = makeTestContainer(c, testContainerOptions{
|
|
| 470 |
+ command: makeCatFileCommand("/testDir/file1-1"),
|
|
| 471 |
+ }) |
|
| 472 |
+ defer deleteContainer(cID) |
|
| 473 |
+ |
|
| 474 |
+ dstDir = containerCpPathTrailingSep(cID, "testDir") |
|
| 475 |
+ |
|
| 476 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 477 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 478 |
+ } |
|
| 479 |
+ |
|
| 480 |
+ // Should now contain file1-1's contents. |
|
| 481 |
+ if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
|
|
| 482 |
+ c.Fatal(err) |
|
| 483 |
+ } |
|
| 484 |
+} |
|
| 485 |
+ |
|
| 486 |
+// I. SRC specifies a direcotry's contents only and DST exists as a file. This |
|
| 487 |
+// should cause an error as it is not possible to overwrite a file with a |
|
| 488 |
+// directory. |
|
| 489 |
+func (s *DockerSuite) TestCpToCaseI(c *check.C) {
|
|
| 490 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 491 |
+ addContent: true, workDir: "/root", |
|
| 492 |
+ }) |
|
| 493 |
+ defer deleteContainer(cID) |
|
| 494 |
+ |
|
| 495 |
+ tmpDir := getTestDir(c, "test-cp-to-case-i") |
|
| 496 |
+ defer os.RemoveAll(tmpDir) |
|
| 497 |
+ |
|
| 498 |
+ makeTestContentInDir(c, tmpDir) |
|
| 499 |
+ |
|
| 500 |
+ srcDir := cpPathTrailingSep(tmpDir, "dir1") + "." |
|
| 501 |
+ dstFile := containerCpPath(cID, "/root/file1") |
|
| 502 |
+ |
|
| 503 |
+ err := runDockerCp(c, srcDir, dstFile) |
|
| 504 |
+ if err == nil {
|
|
| 505 |
+ c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
|
|
| 506 |
+ } |
|
| 507 |
+ |
|
| 508 |
+ if !isCpCannotCopyDir(err) {
|
|
| 509 |
+ c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
|
|
| 510 |
+ } |
|
| 511 |
+} |
|
| 512 |
+ |
|
| 513 |
+// J. SRC specifies a directory's contents only and DST exists as a directory. |
|
| 514 |
+// This should copy the contents of the SRC directory (but not the directory |
|
| 515 |
+// itself) into the DST directory. Ensure this works whether DST has a |
|
| 516 |
+// trailing path separator or not. |
|
| 517 |
+func (s *DockerSuite) TestCpToCaseJ(c *check.C) {
|
|
| 518 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 519 |
+ addContent: true, workDir: "/root", |
|
| 520 |
+ command: makeCatFileCommand("/dir2/file1-1"),
|
|
| 521 |
+ }) |
|
| 522 |
+ defer deleteContainer(cID) |
|
| 523 |
+ |
|
| 524 |
+ tmpDir := getTestDir(c, "test-cp-to-case-j") |
|
| 525 |
+ defer os.RemoveAll(tmpDir) |
|
| 526 |
+ |
|
| 527 |
+ makeTestContentInDir(c, tmpDir) |
|
| 528 |
+ |
|
| 529 |
+ srcDir := cpPathTrailingSep(tmpDir, "dir1") + "." |
|
| 530 |
+ dstDir := containerCpPath(cID, "/dir2") |
|
| 531 |
+ |
|
| 532 |
+ // Ensure that dstPath doesn't exist. |
|
| 533 |
+ if err := containerStartOutputEquals(c, cID, ""); err != nil {
|
|
| 534 |
+ c.Fatal(err) |
|
| 535 |
+ } |
|
| 536 |
+ |
|
| 537 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 538 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 539 |
+ } |
|
| 540 |
+ |
|
| 541 |
+ // Should now contain file1-1's contents. |
|
| 542 |
+ if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
|
|
| 543 |
+ c.Fatal(err) |
|
| 544 |
+ } |
|
| 545 |
+ |
|
| 546 |
+ // Now try again but using a trailing path separator for dstDir. |
|
| 547 |
+ |
|
| 548 |
+ // Make new destination container. |
|
| 549 |
+ cID = makeTestContainer(c, testContainerOptions{
|
|
| 550 |
+ command: makeCatFileCommand("/dir2/file1-1"),
|
|
| 551 |
+ }) |
|
| 552 |
+ defer deleteContainer(cID) |
|
| 553 |
+ |
|
| 554 |
+ dstDir = containerCpPathTrailingSep(cID, "/dir2") |
|
| 555 |
+ |
|
| 556 |
+ // Ensure that dstPath doesn't exist. |
|
| 557 |
+ if err := containerStartOutputEquals(c, cID, ""); err != nil {
|
|
| 558 |
+ c.Fatal(err) |
|
| 559 |
+ } |
|
| 560 |
+ |
|
| 561 |
+ if err := runDockerCp(c, srcDir, dstDir); err != nil {
|
|
| 562 |
+ c.Fatalf("unexpected error %T: %s", err, err)
|
|
| 563 |
+ } |
|
| 564 |
+ |
|
| 565 |
+ // Should now contain file1-1's contents. |
|
| 566 |
+ if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
|
|
| 567 |
+ c.Fatal(err) |
|
| 568 |
+ } |
|
| 569 |
+} |
|
| 570 |
+ |
|
| 571 |
+// The `docker cp` command should also ensure that you cannot |
|
| 572 |
+// write to a container rootfs that is marked as read-only. |
|
| 573 |
+func (s *DockerSuite) TestCpToErrReadOnlyRootfs(c *check.C) {
|
|
| 574 |
+ tmpDir := getTestDir(c, "test-cp-to-err-read-only-rootfs") |
|
| 575 |
+ defer os.RemoveAll(tmpDir) |
|
| 576 |
+ |
|
| 577 |
+ makeTestContentInDir(c, tmpDir) |
|
| 578 |
+ |
|
| 579 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 580 |
+ readOnly: true, workDir: "/root", |
|
| 581 |
+ command: makeCatFileCommand("shouldNotExist"),
|
|
| 582 |
+ }) |
|
| 583 |
+ defer deleteContainer(cID) |
|
| 584 |
+ |
|
| 585 |
+ srcPath := cpPath(tmpDir, "file1") |
|
| 586 |
+ dstPath := containerCpPath(cID, "/root/shouldNotExist") |
|
| 587 |
+ |
|
| 588 |
+ err := runDockerCp(c, srcPath, dstPath) |
|
| 589 |
+ if err == nil {
|
|
| 590 |
+ c.Fatal("expected ErrContainerRootfsReadonly error, but got nil instead")
|
|
| 591 |
+ } |
|
| 592 |
+ |
|
| 593 |
+ if !isCpCannotCopyReadOnly(err) {
|
|
| 594 |
+ c.Fatalf("expected ErrContainerRootfsReadonly error, but got %T: %s", err, err)
|
|
| 595 |
+ } |
|
| 596 |
+ |
|
| 597 |
+ // Ensure that dstPath doesn't exist. |
|
| 598 |
+ if err := containerStartOutputEquals(c, cID, ""); err != nil {
|
|
| 599 |
+ c.Fatal(err) |
|
| 600 |
+ } |
|
| 601 |
+} |
|
| 602 |
+ |
|
| 603 |
+// The `docker cp` command should also ensure that you |
|
| 604 |
+// cannot write to a volume that is mounted as read-only. |
|
| 605 |
+func (s *DockerSuite) TestCpToErrReadOnlyVolume(c *check.C) {
|
|
| 606 |
+ tmpDir := getTestDir(c, "test-cp-to-err-read-only-volume") |
|
| 607 |
+ defer os.RemoveAll(tmpDir) |
|
| 608 |
+ |
|
| 609 |
+ makeTestContentInDir(c, tmpDir) |
|
| 610 |
+ |
|
| 611 |
+ cID := makeTestContainer(c, testContainerOptions{
|
|
| 612 |
+ volumes: defaultVolumes(tmpDir), workDir: "/root", |
|
| 613 |
+ command: makeCatFileCommand("/vol_ro/shouldNotExist"),
|
|
| 614 |
+ }) |
|
| 615 |
+ defer deleteContainer(cID) |
|
| 616 |
+ |
|
| 617 |
+ srcPath := cpPath(tmpDir, "file1") |
|
| 618 |
+ dstPath := containerCpPath(cID, "/vol_ro/shouldNotExist") |
|
| 619 |
+ |
|
| 620 |
+ err := runDockerCp(c, srcPath, dstPath) |
|
| 621 |
+ if err == nil {
|
|
| 622 |
+ c.Fatal("expected ErrVolumeReadonly error, but got nil instead")
|
|
| 623 |
+ } |
|
| 624 |
+ |
|
| 625 |
+ if !isCpCannotCopyReadOnly(err) {
|
|
| 626 |
+ c.Fatalf("expected ErrVolumeReadonly error, but got %T: %s", err, err)
|
|
| 627 |
+ } |
|
| 628 |
+ |
|
| 629 |
+ // Ensure that dstPath doesn't exist. |
|
| 630 |
+ if err := containerStartOutputEquals(c, cID, ""); err != nil {
|
|
| 631 |
+ c.Fatal(err) |
|
| 632 |
+ } |
|
| 633 |
+} |
| 0 | 634 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,298 @@ |
| 0 |
+package main |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bytes" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ "io/ioutil" |
|
| 6 |
+ "os" |
|
| 7 |
+ "os/exec" |
|
| 8 |
+ "path/filepath" |
|
| 9 |
+ "strings" |
|
| 10 |
+ |
|
| 11 |
+ "github.com/docker/docker/pkg/archive" |
|
| 12 |
+ "github.com/go-check/check" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+type FileType uint32 |
|
| 16 |
+ |
|
| 17 |
+const ( |
|
| 18 |
+ Regular FileType = iota |
|
| 19 |
+ Dir |
|
| 20 |
+ Symlink |
|
| 21 |
+) |
|
| 22 |
+ |
|
| 23 |
+type FileData struct {
|
|
| 24 |
+ filetype FileType |
|
| 25 |
+ path string |
|
| 26 |
+ contents string |
|
| 27 |
+} |
|
| 28 |
+ |
|
| 29 |
+func (fd FileData) creationCommand() string {
|
|
| 30 |
+ var command string |
|
| 31 |
+ |
|
| 32 |
+ switch fd.filetype {
|
|
| 33 |
+ case Regular: |
|
| 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 Dir: |
|
| 37 |
+ command = fmt.Sprintf("mkdir -p %s", fd.path)
|
|
| 38 |
+ case Symlink: |
|
| 39 |
+ command = fmt.Sprintf("ln -fs %s %s", fd.contents, fd.path)
|
|
| 40 |
+ } |
|
| 41 |
+ |
|
| 42 |
+ return command |
|
| 43 |
+} |
|
| 44 |
+ |
|
| 45 |
+func mkFilesCommand(fds []FileData) string {
|
|
| 46 |
+ commands := make([]string, len(fds)) |
|
| 47 |
+ |
|
| 48 |
+ for i, fd := range fds {
|
|
| 49 |
+ commands[i] = fd.creationCommand() |
|
| 50 |
+ } |
|
| 51 |
+ |
|
| 52 |
+ return strings.Join(commands, " && ") |
|
| 53 |
+} |
|
| 54 |
+ |
|
| 55 |
+var defaultFileData = []FileData{
|
|
| 56 |
+ {Regular, "file1", "file1"},
|
|
| 57 |
+ {Regular, "file2", "file2"},
|
|
| 58 |
+ {Regular, "file3", "file3"},
|
|
| 59 |
+ {Regular, "file4", "file4"},
|
|
| 60 |
+ {Regular, "file5", "file5"},
|
|
| 61 |
+ {Regular, "file6", "file6"},
|
|
| 62 |
+ {Regular, "file7", "file7"},
|
|
| 63 |
+ {Dir, "dir1", ""},
|
|
| 64 |
+ {Regular, "dir1/file1-1", "file1-1"},
|
|
| 65 |
+ {Regular, "dir1/file1-2", "file1-2"},
|
|
| 66 |
+ {Dir, "dir2", ""},
|
|
| 67 |
+ {Regular, "dir2/file2-1", "file2-1"},
|
|
| 68 |
+ {Regular, "dir2/file2-2", "file2-2"},
|
|
| 69 |
+ {Dir, "dir3", ""},
|
|
| 70 |
+ {Regular, "dir3/file3-1", "file3-1"},
|
|
| 71 |
+ {Regular, "dir3/file3-2", "file3-2"},
|
|
| 72 |
+ {Dir, "dir4", ""},
|
|
| 73 |
+ {Regular, "dir4/file3-1", "file4-1"},
|
|
| 74 |
+ {Regular, "dir4/file3-2", "file4-2"},
|
|
| 75 |
+ {Dir, "dir5", ""},
|
|
| 76 |
+ {Symlink, "symlink1", "target1"},
|
|
| 77 |
+ {Symlink, "symlink2", "target2"},
|
|
| 78 |
+} |
|
| 79 |
+ |
|
| 80 |
+func defaultMkContentCommand() string {
|
|
| 81 |
+ return mkFilesCommand(defaultFileData) |
|
| 82 |
+} |
|
| 83 |
+ |
|
| 84 |
+func makeTestContentInDir(c *check.C, dir string) {
|
|
| 85 |
+ for _, fd := range defaultFileData {
|
|
| 86 |
+ path := filepath.Join(dir, filepath.FromSlash(fd.path)) |
|
| 87 |
+ switch fd.filetype {
|
|
| 88 |
+ case Regular: |
|
| 89 |
+ if err := ioutil.WriteFile(path, []byte(fd.contents+"\n"), os.FileMode(0666)); err != nil {
|
|
| 90 |
+ c.Fatal(err) |
|
| 91 |
+ } |
|
| 92 |
+ case Dir: |
|
| 93 |
+ if err := os.Mkdir(path, os.FileMode(0777)); err != nil {
|
|
| 94 |
+ c.Fatal(err) |
|
| 95 |
+ } |
|
| 96 |
+ case Symlink: |
|
| 97 |
+ if err := os.Symlink(fd.contents, path); err != nil {
|
|
| 98 |
+ c.Fatal(err) |
|
| 99 |
+ } |
|
| 100 |
+ } |
|
| 101 |
+ } |
|
| 102 |
+} |
|
| 103 |
+ |
|
| 104 |
+type testContainerOptions struct {
|
|
| 105 |
+ addContent bool |
|
| 106 |
+ readOnly bool |
|
| 107 |
+ volumes []string |
|
| 108 |
+ workDir string |
|
| 109 |
+ command string |
|
| 110 |
+} |
|
| 111 |
+ |
|
| 112 |
+func makeTestContainer(c *check.C, options testContainerOptions) (containerID string) {
|
|
| 113 |
+ if options.addContent {
|
|
| 114 |
+ mkContentCmd := defaultMkContentCommand() |
|
| 115 |
+ if options.command == "" {
|
|
| 116 |
+ options.command = mkContentCmd |
|
| 117 |
+ } else {
|
|
| 118 |
+ options.command = fmt.Sprintf("%s && %s", defaultMkContentCommand(), options.command)
|
|
| 119 |
+ } |
|
| 120 |
+ } |
|
| 121 |
+ |
|
| 122 |
+ if options.command == "" {
|
|
| 123 |
+ options.command = "#(nop)" |
|
| 124 |
+ } |
|
| 125 |
+ |
|
| 126 |
+ args := []string{"run", "-d"}
|
|
| 127 |
+ |
|
| 128 |
+ for _, volume := range options.volumes {
|
|
| 129 |
+ args = append(args, "-v", volume) |
|
| 130 |
+ } |
|
| 131 |
+ |
|
| 132 |
+ if options.workDir != "" {
|
|
| 133 |
+ args = append(args, "-w", options.workDir) |
|
| 134 |
+ } |
|
| 135 |
+ |
|
| 136 |
+ if options.readOnly {
|
|
| 137 |
+ args = append(args, "--read-only") |
|
| 138 |
+ } |
|
| 139 |
+ |
|
| 140 |
+ args = append(args, "busybox", "/bin/sh", "-c", options.command) |
|
| 141 |
+ |
|
| 142 |
+ out, status := dockerCmd(c, args...) |
|
| 143 |
+ if status != 0 {
|
|
| 144 |
+ c.Fatalf("failed to run container, status %d: %s", status, out)
|
|
| 145 |
+ } |
|
| 146 |
+ |
|
| 147 |
+ containerID = strings.TrimSpace(out) |
|
| 148 |
+ |
|
| 149 |
+ out, status = dockerCmd(c, "wait", containerID) |
|
| 150 |
+ if status != 0 {
|
|
| 151 |
+ c.Fatalf("failed to wait for test container container, status %d: %s", status, out)
|
|
| 152 |
+ } |
|
| 153 |
+ |
|
| 154 |
+ if exitCode := strings.TrimSpace(out); exitCode != "0" {
|
|
| 155 |
+ logs, status := dockerCmd(c, "logs", containerID) |
|
| 156 |
+ if status != 0 {
|
|
| 157 |
+ logs = "UNABLE TO GET LOGS" |
|
| 158 |
+ } |
|
| 159 |
+ c.Fatalf("failed to make test container, exit code (%d): %s", exitCode, logs)
|
|
| 160 |
+ } |
|
| 161 |
+ |
|
| 162 |
+ return |
|
| 163 |
+} |
|
| 164 |
+ |
|
| 165 |
+func makeCatFileCommand(path string) string {
|
|
| 166 |
+ return fmt.Sprintf("if [ -f %s ]; then cat %s; fi", path, path)
|
|
| 167 |
+} |
|
| 168 |
+ |
|
| 169 |
+func cpPath(pathElements ...string) string {
|
|
| 170 |
+ localizedPathElements := make([]string, len(pathElements)) |
|
| 171 |
+ for i, path := range pathElements {
|
|
| 172 |
+ localizedPathElements[i] = filepath.FromSlash(path) |
|
| 173 |
+ } |
|
| 174 |
+ return strings.Join(localizedPathElements, string(filepath.Separator)) |
|
| 175 |
+} |
|
| 176 |
+ |
|
| 177 |
+func cpPathTrailingSep(pathElements ...string) string {
|
|
| 178 |
+ return fmt.Sprintf("%s%c", cpPath(pathElements...), filepath.Separator)
|
|
| 179 |
+} |
|
| 180 |
+ |
|
| 181 |
+func containerCpPath(containerID string, pathElements ...string) string {
|
|
| 182 |
+ joined := strings.Join(pathElements, "/") |
|
| 183 |
+ return fmt.Sprintf("%s:%s", containerID, joined)
|
|
| 184 |
+} |
|
| 185 |
+ |
|
| 186 |
+func containerCpPathTrailingSep(containerID string, pathElements ...string) string {
|
|
| 187 |
+ return fmt.Sprintf("%s/", containerCpPath(containerID, pathElements...))
|
|
| 188 |
+} |
|
| 189 |
+ |
|
| 190 |
+func runDockerCp(c *check.C, src, dst string) (err error) {
|
|
| 191 |
+ c.Logf("running `docker cp %s %s`", src, dst)
|
|
| 192 |
+ |
|
| 193 |
+ args := []string{"cp", src, dst}
|
|
| 194 |
+ |
|
| 195 |
+ out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...)) |
|
| 196 |
+ if err != nil {
|
|
| 197 |
+ err = fmt.Errorf("error executing `docker cp` command: %s: %s", err, out)
|
|
| 198 |
+ } |
|
| 199 |
+ |
|
| 200 |
+ return |
|
| 201 |
+} |
|
| 202 |
+ |
|
| 203 |
+func startContainerGetOutput(c *check.C, cID string) (out string, err error) {
|
|
| 204 |
+ c.Logf("running `docker start -a %s`", cID)
|
|
| 205 |
+ |
|
| 206 |
+ args := []string{"start", "-a", cID}
|
|
| 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 *check.C, label string) (tmpDir string) {
|
|
| 217 |
+ var err error |
|
| 218 |
+ |
|
| 219 |
+ if tmpDir, err = ioutil.TempDir("", label); err != nil {
|
|
| 220 |
+ c.Fatalf("unable to make temporary directory: %s", err)
|
|
| 221 |
+ } |
|
| 222 |
+ |
|
| 223 |
+ return |
|
| 224 |
+} |
|
| 225 |
+ |
|
| 226 |
+func isCpNotExist(err error) bool {
|
|
| 227 |
+ return strings.Contains(err.Error(), "no such file or directory") || strings.Contains(err.Error(), "cannot find the file specified") |
|
| 228 |
+} |
|
| 229 |
+ |
|
| 230 |
+func isCpDirNotExist(err error) bool {
|
|
| 231 |
+ return strings.Contains(err.Error(), archive.ErrDirNotExists.Error()) |
|
| 232 |
+} |
|
| 233 |
+ |
|
| 234 |
+func isCpNotDir(err error) bool {
|
|
| 235 |
+ return strings.Contains(err.Error(), archive.ErrNotDirectory.Error()) || strings.Contains(err.Error(), "filename, directory name, or volume label syntax is incorrect") |
|
| 236 |
+} |
|
| 237 |
+ |
|
| 238 |
+func isCpCannotCopyDir(err error) bool {
|
|
| 239 |
+ return strings.Contains(err.Error(), archive.ErrCannotCopyDir.Error()) |
|
| 240 |
+} |
|
| 241 |
+ |
|
| 242 |
+func isCpCannotCopyReadOnly(err error) bool {
|
|
| 243 |
+ return strings.Contains(err.Error(), "marked read-only") |
|
| 244 |
+} |
|
| 245 |
+ |
|
| 246 |
+func isCannotOverwriteNonDirWithDir(err error) bool {
|
|
| 247 |
+ return strings.Contains(err.Error(), "cannot overwrite non-directory") |
|
| 248 |
+} |
|
| 249 |
+ |
|
| 250 |
+func fileContentEquals(c *check.C, filename, contents string) (err error) {
|
|
| 251 |
+ c.Logf("checking that file %q contains %q\n", filename, contents)
|
|
| 252 |
+ |
|
| 253 |
+ fileBytes, err := ioutil.ReadFile(filename) |
|
| 254 |
+ if err != nil {
|
|
| 255 |
+ return |
|
| 256 |
+ } |
|
| 257 |
+ |
|
| 258 |
+ expectedBytes, err := ioutil.ReadAll(strings.NewReader(contents)) |
|
| 259 |
+ if err != nil {
|
|
| 260 |
+ return |
|
| 261 |
+ } |
|
| 262 |
+ |
|
| 263 |
+ if !bytes.Equal(fileBytes, expectedBytes) {
|
|
| 264 |
+ err = fmt.Errorf("file content not equal - expected %q, got %q", string(expectedBytes), string(fileBytes))
|
|
| 265 |
+ } |
|
| 266 |
+ |
|
| 267 |
+ return |
|
| 268 |
+} |
|
| 269 |
+ |
|
| 270 |
+func containerStartOutputEquals(c *check.C, cID, contents string) (err error) {
|
|
| 271 |
+ c.Logf("checking that container %q start output contains %q\n", cID, contents)
|
|
| 272 |
+ |
|
| 273 |
+ out, err := startContainerGetOutput(c, cID) |
|
| 274 |
+ if err != nil {
|
|
| 275 |
+ return err |
|
| 276 |
+ } |
|
| 277 |
+ |
|
| 278 |
+ if out != contents {
|
|
| 279 |
+ err = fmt.Errorf("output contents not equal - expected %q, got %q", contents, out)
|
|
| 280 |
+ } |
|
| 281 |
+ |
|
| 282 |
+ return |
|
| 283 |
+} |
|
| 284 |
+ |
|
| 285 |
+func defaultVolumes(tmpDir string) []string {
|
|
| 286 |
+ if SameHostDaemon.Condition() {
|
|
| 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 |
+} |
| ... | ... |
@@ -3,7 +3,9 @@ package main |
| 3 | 3 |
import ( |
| 4 | 4 |
"bufio" |
| 5 | 5 |
"fmt" |
| 6 |
+ "io/ioutil" |
|
| 6 | 7 |
"net/http" |
| 8 |
+ "os" |
|
| 7 | 9 |
"os/exec" |
| 8 | 10 |
"regexp" |
| 9 | 11 |
"strconv" |
| ... | ... |
@@ -519,6 +521,7 @@ func (s *DockerSuite) TestEventsCommit(c *check.C) {
|
| 519 | 519 |
func (s *DockerSuite) TestEventsCopy(c *check.C) {
|
| 520 | 520 |
since := daemonTime(c).Unix() |
| 521 | 521 |
|
| 522 |
+ // Build a test image. |
|
| 522 | 523 |
id, err := buildImage("cpimg", `
|
| 523 | 524 |
FROM busybox |
| 524 | 525 |
RUN echo HI > /tmp/file`, true) |
| ... | ... |
@@ -526,12 +529,31 @@ func (s *DockerSuite) TestEventsCopy(c *check.C) {
|
| 526 | 526 |
c.Fatalf("Couldn't create image: %q", err)
|
| 527 | 527 |
} |
| 528 | 528 |
|
| 529 |
- dockerCmd(c, "run", "--name=cptest", id, "true") |
|
| 530 |
- dockerCmd(c, "cp", "cptest:/tmp/file", "-") |
|
| 529 |
+ // Create an empty test file. |
|
| 530 |
+ tempFile, err := ioutil.TempFile("", "test-events-copy-")
|
|
| 531 |
+ if err != nil {
|
|
| 532 |
+ c.Fatal(err) |
|
| 533 |
+ } |
|
| 534 |
+ defer os.Remove(tempFile.Name()) |
|
| 535 |
+ |
|
| 536 |
+ if err := tempFile.Close(); err != nil {
|
|
| 537 |
+ c.Fatal(err) |
|
| 538 |
+ } |
|
| 539 |
+ |
|
| 540 |
+ dockerCmd(c, "create", "--name=cptest", id) |
|
| 541 |
+ |
|
| 542 |
+ dockerCmd(c, "cp", "cptest:/tmp/file", tempFile.Name()) |
|
| 531 | 543 |
|
| 532 | 544 |
out, _ := dockerCmd(c, "events", "--since=0", "-f", "container=cptest", "--until="+strconv.Itoa(int(since))) |
| 533 |
- if !strings.Contains(out, " copy\n") {
|
|
| 534 |
- c.Fatalf("Missing 'copy' log event\n%s", out)
|
|
| 545 |
+ if !strings.Contains(out, " archive-path\n") {
|
|
| 546 |
+ c.Fatalf("Missing 'archive-path' log event\n%s", out)
|
|
| 547 |
+ } |
|
| 548 |
+ |
|
| 549 |
+ dockerCmd(c, "cp", tempFile.Name(), "cptest:/tmp/filecopy") |
|
| 550 |
+ |
|
| 551 |
+ out, _ = dockerCmd(c, "events", "--since=0", "-f", "container=cptest", "--until="+strconv.Itoa(int(since))) |
|
| 552 |
+ if !strings.Contains(out, " extract-to-dir\n") {
|
|
| 553 |
+ c.Fatalf("Missing 'extract-to-dir' log event\n%s", out)
|
|
| 535 | 554 |
} |
| 536 | 555 |
} |
| 537 | 556 |
|