github.com/rumpl/bof@v23.0.0-rc.2+incompatible/pkg/archive/changes_test.go (about) 1 package archive // import "github.com/docker/docker/pkg/archive" 2 3 import ( 4 "os" 5 "os/exec" 6 "path" 7 "path/filepath" 8 "runtime" 9 "sort" 10 "strconv" 11 "strings" 12 "syscall" 13 "testing" 14 "time" 15 16 "github.com/Microsoft/hcsshim/osversion" 17 "github.com/docker/docker/pkg/idtools" 18 "github.com/docker/docker/pkg/parsers/kernel" 19 "github.com/docker/docker/pkg/system" 20 "gotest.tools/v3/assert" 21 "gotest.tools/v3/skip" 22 ) 23 24 func max(x, y int) int { 25 if x >= y { 26 return x 27 } 28 return y 29 } 30 31 func copyDir(src, dst string) error { 32 if runtime.GOOS != "windows" { 33 return exec.Command("cp", "-a", src, dst).Run() 34 } 35 36 // Could have used xcopy src dst /E /I /H /Y /B. However, xcopy has the 37 // unfortunate side effect of not preserving timestamps of newly created 38 // directories in the target directory, so we don't get accurate changes. 39 // Use robocopy instead. Note this isn't available in microsoft/nanoserver. 40 // But it has gotchas. See https://weblogs.sqlteam.com/robv/archive/2010/02/17/61106.aspx 41 err := exec.Command("robocopy", filepath.FromSlash(src), filepath.FromSlash(dst), "/SL", "/COPYALL", "/MIR").Run() 42 if exiterr, ok := err.(*exec.ExitError); ok { 43 if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { 44 if status.ExitStatus()&24 == 0 { 45 return nil 46 } 47 } 48 } 49 return err 50 } 51 52 type FileType uint32 53 54 const ( 55 Regular FileType = iota 56 Dir 57 Symlink 58 ) 59 60 type FileData struct { 61 filetype FileType 62 path string 63 contents string 64 permissions os.FileMode 65 } 66 67 func createSampleDir(t *testing.T, root string) { 68 files := []FileData{ 69 {filetype: Regular, path: "file1", contents: "file1\n", permissions: 0600}, 70 {filetype: Regular, path: "file2", contents: "file2\n", permissions: 0666}, 71 {filetype: Regular, path: "file3", contents: "file3\n", permissions: 0404}, 72 {filetype: Regular, path: "file4", contents: "file4\n", permissions: 0600}, 73 {filetype: Regular, path: "file5", contents: "file5\n", permissions: 0600}, 74 {filetype: Regular, path: "file6", contents: "file6\n", permissions: 0600}, 75 {filetype: Regular, path: "file7", contents: "file7\n", permissions: 0600}, 76 {filetype: Dir, path: "dir1", contents: "", permissions: 0740}, 77 {filetype: Regular, path: "dir1/file1-1", contents: "file1-1\n", permissions: 01444}, 78 {filetype: Regular, path: "dir1/file1-2", contents: "file1-2\n", permissions: 0666}, 79 {filetype: Dir, path: "dir2", contents: "", permissions: 0700}, 80 {filetype: Regular, path: "dir2/file2-1", contents: "file2-1\n", permissions: 0666}, 81 {filetype: Regular, path: "dir2/file2-2", contents: "file2-2\n", permissions: 0666}, 82 {filetype: Dir, path: "dir3", contents: "", permissions: 0700}, 83 {filetype: Regular, path: "dir3/file3-1", contents: "file3-1\n", permissions: 0666}, 84 {filetype: Regular, path: "dir3/file3-2", contents: "file3-2\n", permissions: 0666}, 85 {filetype: Dir, path: "dir4", contents: "", permissions: 0700}, 86 {filetype: Regular, path: "dir4/file3-1", contents: "file4-1\n", permissions: 0666}, 87 {filetype: Regular, path: "dir4/file3-2", contents: "file4-2\n", permissions: 0666}, 88 {filetype: Symlink, path: "symlink1", contents: "target1", permissions: 0666}, 89 {filetype: Symlink, path: "symlink2", contents: "target2", permissions: 0666}, 90 {filetype: Symlink, path: "symlink3", contents: root + "/file1", permissions: 0666}, 91 {filetype: Symlink, path: "symlink4", contents: root + "/symlink3", permissions: 0666}, 92 {filetype: Symlink, path: "dirSymlink", contents: root + "/dir1", permissions: 0740}, 93 } 94 provisionSampleDir(t, root, files) 95 } 96 97 func provisionSampleDir(t *testing.T, root string, files []FileData) { 98 now := time.Now() 99 for _, info := range files { 100 p := path.Join(root, info.path) 101 if info.filetype == Dir { 102 err := os.MkdirAll(p, info.permissions) 103 assert.NilError(t, err) 104 } else if info.filetype == Regular { 105 err := os.WriteFile(p, []byte(info.contents), info.permissions) 106 assert.NilError(t, err) 107 } else if info.filetype == Symlink { 108 err := os.Symlink(info.contents, p) 109 assert.NilError(t, err) 110 } 111 112 if info.filetype != Symlink { 113 // Set a consistent ctime, atime for all files and dirs 114 err := system.Chtimes(p, now, now) 115 assert.NilError(t, err) 116 } 117 } 118 } 119 120 func TestChangeString(t *testing.T) { 121 modifyChange := Change{"change", ChangeModify} 122 toString := modifyChange.String() 123 if toString != "C change" { 124 t.Fatalf("String() of a change with ChangeModify Kind should have been %s but was %s", "C change", toString) 125 } 126 addChange := Change{"change", ChangeAdd} 127 toString = addChange.String() 128 if toString != "A change" { 129 t.Fatalf("String() of a change with ChangeAdd Kind should have been %s but was %s", "A change", toString) 130 } 131 deleteChange := Change{"change", ChangeDelete} 132 toString = deleteChange.String() 133 if toString != "D change" { 134 t.Fatalf("String() of a change with ChangeDelete Kind should have been %s but was %s", "D change", toString) 135 } 136 } 137 138 func TestChangesWithNoChanges(t *testing.T) { 139 rwLayer, err := os.MkdirTemp("", "docker-changes-test") 140 assert.NilError(t, err) 141 defer os.RemoveAll(rwLayer) 142 layer, err := os.MkdirTemp("", "docker-changes-test-layer") 143 assert.NilError(t, err) 144 defer os.RemoveAll(layer) 145 createSampleDir(t, layer) 146 changes, err := Changes([]string{layer}, rwLayer) 147 assert.NilError(t, err) 148 if len(changes) != 0 { 149 t.Fatalf("Changes with no difference should have detect no changes, but detected %d", len(changes)) 150 } 151 } 152 153 func TestChangesWithChanges(t *testing.T) { 154 // Mock the readonly layer 155 layer, err := os.MkdirTemp("", "docker-changes-test-layer") 156 assert.NilError(t, err) 157 defer os.RemoveAll(layer) 158 createSampleDir(t, layer) 159 os.MkdirAll(path.Join(layer, "dir1/subfolder"), 0740) 160 161 // Mock the RW layer 162 rwLayer, err := os.MkdirTemp("", "docker-changes-test") 163 assert.NilError(t, err) 164 defer os.RemoveAll(rwLayer) 165 166 // Create a folder in RW layer 167 dir1 := path.Join(rwLayer, "dir1") 168 os.MkdirAll(dir1, 0740) 169 deletedFile := path.Join(dir1, ".wh.file1-2") 170 os.WriteFile(deletedFile, []byte{}, 0600) 171 modifiedFile := path.Join(dir1, "file1-1") 172 os.WriteFile(modifiedFile, []byte{0x00}, 01444) 173 // Let's add a subfolder for a newFile 174 subfolder := path.Join(dir1, "subfolder") 175 os.MkdirAll(subfolder, 0740) 176 newFile := path.Join(subfolder, "newFile") 177 os.WriteFile(newFile, []byte{}, 0740) 178 179 changes, err := Changes([]string{layer}, rwLayer) 180 assert.NilError(t, err) 181 182 expectedChanges := []Change{ 183 {filepath.FromSlash("/dir1"), ChangeModify}, 184 {filepath.FromSlash("/dir1/file1-1"), ChangeModify}, 185 {filepath.FromSlash("/dir1/file1-2"), ChangeDelete}, 186 {filepath.FromSlash("/dir1/subfolder"), ChangeModify}, 187 {filepath.FromSlash("/dir1/subfolder/newFile"), ChangeAdd}, 188 } 189 checkChanges(expectedChanges, changes, t) 190 } 191 192 // See https://github.com/docker/docker/pull/13590 193 func TestChangesWithChangesGH13590(t *testing.T) { 194 // TODO Windows. Needs further investigation to identify the failure 195 if runtime.GOOS == "windows" { 196 t.Skip("needs more investigation") 197 } 198 baseLayer, err := os.MkdirTemp("", "docker-changes-test.") 199 assert.NilError(t, err) 200 defer os.RemoveAll(baseLayer) 201 202 dir3 := path.Join(baseLayer, "dir1/dir2/dir3") 203 os.MkdirAll(dir3, 07400) 204 205 file := path.Join(dir3, "file.txt") 206 os.WriteFile(file, []byte("hello"), 0666) 207 208 layer, err := os.MkdirTemp("", "docker-changes-test2.") 209 assert.NilError(t, err) 210 defer os.RemoveAll(layer) 211 212 // Test creating a new file 213 if err := copyDir(baseLayer+"/dir1", layer+"/"); err != nil { 214 t.Fatalf("Cmd failed: %q", err) 215 } 216 217 os.Remove(path.Join(layer, "dir1/dir2/dir3/file.txt")) 218 file = path.Join(layer, "dir1/dir2/dir3/file1.txt") 219 os.WriteFile(file, []byte("bye"), 0666) 220 221 changes, err := Changes([]string{baseLayer}, layer) 222 assert.NilError(t, err) 223 224 expectedChanges := []Change{ 225 {"/dir1/dir2/dir3", ChangeModify}, 226 {"/dir1/dir2/dir3/file1.txt", ChangeAdd}, 227 } 228 checkChanges(expectedChanges, changes, t) 229 230 // Now test changing a file 231 layer, err = os.MkdirTemp("", "docker-changes-test3.") 232 assert.NilError(t, err) 233 defer os.RemoveAll(layer) 234 235 if err := copyDir(baseLayer+"/dir1", layer+"/"); err != nil { 236 t.Fatalf("Cmd failed: %q", err) 237 } 238 239 file = path.Join(layer, "dir1/dir2/dir3/file.txt") 240 os.WriteFile(file, []byte("bye"), 0666) 241 242 changes, err = Changes([]string{baseLayer}, layer) 243 assert.NilError(t, err) 244 245 expectedChanges = []Change{ 246 {"/dir1/dir2/dir3/file.txt", ChangeModify}, 247 } 248 checkChanges(expectedChanges, changes, t) 249 } 250 251 // Create a directory, copy it, make sure we report no changes between the two 252 func TestChangesDirsEmpty(t *testing.T) { 253 // Note we parse kernel.GetKernelVersion rather than system.GetOSVersion 254 // as test binaries aren't manifested, so would otherwise report the wrong 255 // build number. 256 if runtime.GOOS == "windows" { 257 v, err := kernel.GetKernelVersion() 258 assert.NilError(t, err) 259 build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0]) 260 if build >= osversion.V19H1 { 261 t.Skip("FIXME: broken on Windows 1903 and up; see #39846") 262 } 263 } 264 265 src, err := os.MkdirTemp("", "docker-changes-test") 266 assert.NilError(t, err) 267 defer os.RemoveAll(src) 268 createSampleDir(t, src) 269 dst := src + "-copy" 270 err = copyDir(src, dst) 271 assert.NilError(t, err) 272 defer os.RemoveAll(dst) 273 changes, err := ChangesDirs(dst, src) 274 assert.NilError(t, err) 275 276 if len(changes) != 0 { 277 t.Fatalf("Reported changes for identical dirs: %v", changes) 278 } 279 os.RemoveAll(src) 280 os.RemoveAll(dst) 281 } 282 283 func mutateSampleDir(t *testing.T, root string) { 284 // Remove a regular file 285 err := os.RemoveAll(path.Join(root, "file1")) 286 assert.NilError(t, err) 287 288 // Remove a directory 289 err = os.RemoveAll(path.Join(root, "dir1")) 290 assert.NilError(t, err) 291 292 // Remove a symlink 293 err = os.RemoveAll(path.Join(root, "symlink1")) 294 assert.NilError(t, err) 295 296 // Rewrite a file 297 err = os.WriteFile(path.Join(root, "file2"), []byte("fileNN\n"), 0777) 298 assert.NilError(t, err) 299 300 // Replace a file 301 err = os.RemoveAll(path.Join(root, "file3")) 302 assert.NilError(t, err) 303 err = os.WriteFile(path.Join(root, "file3"), []byte("fileMM\n"), 0404) 304 assert.NilError(t, err) 305 306 // Touch file 307 err = system.Chtimes(path.Join(root, "file4"), time.Now().Add(time.Second), time.Now().Add(time.Second)) 308 assert.NilError(t, err) 309 310 // Replace file with dir 311 err = os.RemoveAll(path.Join(root, "file5")) 312 assert.NilError(t, err) 313 err = os.MkdirAll(path.Join(root, "file5"), 0666) 314 assert.NilError(t, err) 315 316 // Create new file 317 err = os.WriteFile(path.Join(root, "filenew"), []byte("filenew\n"), 0777) 318 assert.NilError(t, err) 319 320 // Create new dir 321 err = os.MkdirAll(path.Join(root, "dirnew"), 0766) 322 assert.NilError(t, err) 323 324 // Create a new symlink 325 err = os.Symlink("targetnew", path.Join(root, "symlinknew")) 326 assert.NilError(t, err) 327 328 // Change a symlink 329 err = os.RemoveAll(path.Join(root, "symlink2")) 330 assert.NilError(t, err) 331 332 err = os.Symlink("target2change", path.Join(root, "symlink2")) 333 assert.NilError(t, err) 334 335 // Replace dir with file 336 err = os.RemoveAll(path.Join(root, "dir2")) 337 assert.NilError(t, err) 338 err = os.WriteFile(path.Join(root, "dir2"), []byte("dir2\n"), 0777) 339 assert.NilError(t, err) 340 341 // Touch dir 342 err = system.Chtimes(path.Join(root, "dir3"), time.Now().Add(time.Second), time.Now().Add(time.Second)) 343 assert.NilError(t, err) 344 } 345 346 func TestChangesDirsMutated(t *testing.T) { 347 // Note we parse kernel.GetKernelVersion rather than system.GetOSVersion 348 // as test binaries aren't manifested, so would otherwise report the wrong 349 // build number. 350 if runtime.GOOS == "windows" { 351 v, err := kernel.GetKernelVersion() 352 assert.NilError(t, err) 353 build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0]) 354 if build >= osversion.V19H1 { 355 t.Skip("FIXME: broken on Windows 1903 and up; see #39846") 356 } 357 } 358 359 src, err := os.MkdirTemp("", "docker-changes-test") 360 assert.NilError(t, err) 361 createSampleDir(t, src) 362 dst := src + "-copy" 363 err = copyDir(src, dst) 364 assert.NilError(t, err) 365 defer os.RemoveAll(src) 366 defer os.RemoveAll(dst) 367 368 mutateSampleDir(t, dst) 369 370 changes, err := ChangesDirs(dst, src) 371 assert.NilError(t, err) 372 373 sort.Sort(changesByPath(changes)) 374 375 expectedChanges := []Change{ 376 {filepath.FromSlash("/dir1"), ChangeDelete}, 377 {filepath.FromSlash("/dir2"), ChangeModify}, 378 } 379 380 // Note there is slight difference between the Linux and Windows 381 // implementations here. Due to https://github.com/moby/moby/issues/9874, 382 // and the fix at https://github.com/moby/moby/pull/11422, Linux does not 383 // consider a change to the directory time as a change. Windows on NTFS 384 // does. See https://github.com/moby/moby/pull/37982 for more information. 385 // 386 // Note also: https://github.com/moby/moby/pull/37982#discussion_r223523114 387 // that differences are ordered in the way the test is currently written, hence 388 // this is in the middle of the list of changes rather than at the start or 389 // end. Potentially can be addressed later. 390 if runtime.GOOS == "windows" { 391 expectedChanges = append(expectedChanges, Change{filepath.FromSlash("/dir3"), ChangeModify}) 392 } 393 394 expectedChanges = append(expectedChanges, []Change{ 395 {filepath.FromSlash("/dirnew"), ChangeAdd}, 396 {filepath.FromSlash("/file1"), ChangeDelete}, 397 {filepath.FromSlash("/file2"), ChangeModify}, 398 {filepath.FromSlash("/file3"), ChangeModify}, 399 {filepath.FromSlash("/file4"), ChangeModify}, 400 {filepath.FromSlash("/file5"), ChangeModify}, 401 {filepath.FromSlash("/filenew"), ChangeAdd}, 402 {filepath.FromSlash("/symlink1"), ChangeDelete}, 403 {filepath.FromSlash("/symlink2"), ChangeModify}, 404 {filepath.FromSlash("/symlinknew"), ChangeAdd}, 405 }...) 406 407 for i := 0; i < max(len(changes), len(expectedChanges)); i++ { 408 if i >= len(expectedChanges) { 409 t.Fatalf("unexpected change %s\n", changes[i].String()) 410 } 411 if i >= len(changes) { 412 t.Fatalf("no change for expected change %s\n", expectedChanges[i].String()) 413 } 414 if changes[i].Path == expectedChanges[i].Path { 415 if changes[i] != expectedChanges[i] { 416 t.Fatalf("Wrong change for %s, expected %s, got %s\n", changes[i].Path, changes[i].String(), expectedChanges[i].String()) 417 } 418 } else if changes[i].Path < expectedChanges[i].Path { 419 t.Fatalf("unexpected change %q %q\n", changes[i].String(), expectedChanges[i].Path) 420 } else { 421 t.Fatalf("no change for expected change %s != %s\n", expectedChanges[i].String(), changes[i].String()) 422 } 423 } 424 } 425 426 func TestApplyLayer(t *testing.T) { 427 // TODO Windows. This is very close to working, but it fails with changes 428 // to \symlinknew and \symlink2. The destination has an updated 429 // Access/Modify/Change/Birth date to the source (~3/100th sec different). 430 // Needs further investigation as to why, but I currently believe this is 431 // just the way NTFS works. I don't think it's a bug in this test or archive. 432 if runtime.GOOS == "windows" { 433 t.Skip("needs further investigation") 434 } 435 src, err := os.MkdirTemp("", "docker-changes-test") 436 assert.NilError(t, err) 437 createSampleDir(t, src) 438 defer os.RemoveAll(src) 439 dst := src + "-copy" 440 err = copyDir(src, dst) 441 assert.NilError(t, err) 442 mutateSampleDir(t, dst) 443 defer os.RemoveAll(dst) 444 445 changes, err := ChangesDirs(dst, src) 446 assert.NilError(t, err) 447 448 layer, err := ExportChanges(dst, changes, idtools.IdentityMapping{}) 449 assert.NilError(t, err) 450 451 layerCopy, err := NewTempArchive(layer, "") 452 assert.NilError(t, err) 453 454 _, err = ApplyLayer(src, layerCopy) 455 assert.NilError(t, err) 456 457 changes2, err := ChangesDirs(src, dst) 458 assert.NilError(t, err) 459 460 if len(changes2) != 0 { 461 t.Fatalf("Unexpected differences after reapplying mutation: %v", changes2) 462 } 463 } 464 465 func TestChangesSizeWithHardlinks(t *testing.T) { 466 // TODO Windows. Needs further investigation. Likely in ChangeSizes not 467 // coping correctly with hardlinks on Windows. 468 if runtime.GOOS == "windows" { 469 t.Skip("needs further investigation") 470 } 471 srcDir, err := os.MkdirTemp("", "docker-test-srcDir") 472 assert.NilError(t, err) 473 defer os.RemoveAll(srcDir) 474 475 destDir, err := os.MkdirTemp("", "docker-test-destDir") 476 assert.NilError(t, err) 477 defer os.RemoveAll(destDir) 478 479 creationSize, err := prepareUntarSourceDirectory(100, destDir, true) 480 assert.NilError(t, err) 481 482 changes, err := ChangesDirs(destDir, srcDir) 483 assert.NilError(t, err) 484 485 got := ChangesSize(destDir, changes) 486 if got != int64(creationSize) { 487 t.Errorf("Expected %d bytes of changes, got %d", creationSize, got) 488 } 489 } 490 491 func TestChangesSizeWithNoChanges(t *testing.T) { 492 size := ChangesSize("/tmp", nil) 493 if size != 0 { 494 t.Fatalf("ChangesSizes with no changes should be 0, was %d", size) 495 } 496 } 497 498 func TestChangesSizeWithOnlyDeleteChanges(t *testing.T) { 499 changes := []Change{ 500 {Path: "deletedPath", Kind: ChangeDelete}, 501 } 502 size := ChangesSize("/tmp", changes) 503 if size != 0 { 504 t.Fatalf("ChangesSizes with only delete changes should be 0, was %d", size) 505 } 506 } 507 508 func TestChangesSize(t *testing.T) { 509 parentPath, err := os.MkdirTemp("", "docker-changes-test") 510 assert.NilError(t, err) 511 defer os.RemoveAll(parentPath) 512 addition := path.Join(parentPath, "addition") 513 err = os.WriteFile(addition, []byte{0x01, 0x01, 0x01}, 0744) 514 assert.NilError(t, err) 515 modification := path.Join(parentPath, "modification") 516 err = os.WriteFile(modification, []byte{0x01, 0x01, 0x01}, 0744) 517 assert.NilError(t, err) 518 519 changes := []Change{ 520 {Path: "addition", Kind: ChangeAdd}, 521 {Path: "modification", Kind: ChangeModify}, 522 } 523 size := ChangesSize(parentPath, changes) 524 if size != 6 { 525 t.Fatalf("Expected 6 bytes of changes, got %d", size) 526 } 527 } 528 529 func checkChanges(expectedChanges, changes []Change, t *testing.T) { 530 skip.If(t, runtime.GOOS != "windows" && os.Getuid() != 0, "skipping test that requires root") 531 sort.Sort(changesByPath(expectedChanges)) 532 sort.Sort(changesByPath(changes)) 533 for i := 0; i < max(len(changes), len(expectedChanges)); i++ { 534 if i >= len(expectedChanges) { 535 t.Fatalf("unexpected change %s\n", changes[i].String()) 536 } 537 if i >= len(changes) { 538 t.Fatalf("no change for expected change %s\n", expectedChanges[i].String()) 539 } 540 if changes[i].Path == expectedChanges[i].Path { 541 if changes[i] != expectedChanges[i] { 542 t.Fatalf("Wrong change for %s, expected %s, got %s\n", changes[i].Path, changes[i].String(), expectedChanges[i].String()) 543 } 544 } else if changes[i].Path < expectedChanges[i].Path { 545 t.Fatalf("unexpected change %s\n", changes[i].String()) 546 } else { 547 t.Fatalf("no change for expected change %s != %s\n", expectedChanges[i].String(), changes[i].String()) 548 } 549 } 550 }