| 1 | package fs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | _ "crypto/sha256" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "sort" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/containerd/continuity/fs/fstest" |
| 16 | "github.com/pkg/errors" |
| 17 | "github.com/stretchr/testify/require" |
| 18 | "github.com/tonistiigi/fsutil" |
| 19 | ) |
| 20 | |
| 21 | // TODO: Create copy directory which requires privilege |
| 22 | // chown |
| 23 | // mknod |
| 24 | // setxattr fstest.SetXAttr("/home", "trusted.overlay.opaque", "y"), |
| 25 | |
| 26 | func TestCopyDirectory(t *testing.T) { |
| 27 | apply := fstest.Apply( |
| 28 | fstest.CreateDir("/etc/", 0755), |
| 29 | fstest.CreateFile("/etc/hosts", []byte("localhost 127.0.0.1"), 0644), |
| 30 | fstest.Link("/etc/hosts", "/etc/hosts.allow"), |
| 31 | fstest.CreateDir("/usr/local/lib", 0755), |
| 32 | fstest.CreateFile("/usr/local/lib/libnothing.so", []byte{0x00, 0x00}, 0755), |
| 33 | fstest.Symlink("libnothing.so", "/usr/local/lib/libnothing.so.2"), |
| 34 | fstest.CreateDir("/home", 0755), |
| 35 | ) |
| 36 | |
| 37 | exp := filepath.FromSlash("add:/etc,add:/etc/hosts,add:/etc/hosts.allow,add:/home,add:/usr,add:/usr/local,add:/usr/local/lib,add:/usr/local/lib/libnothing.so,add:/usr/local/lib/libnothing.so.2") |
| 38 | |
| 39 | if err := testCopy(t, apply, exp); err != nil { |
| 40 | t.Fatalf("Copy test failed: %+v", err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // This test used to fail because link-no-nothing.txt would be copied first, |
| 45 | // then file operations in dst during the CopyDir would follow the symlink and |
| 46 | // fail. |
| 47 | func TestCopyDirectoryWithLocalSymlink(t *testing.T) { |
| 48 | apply := fstest.Apply( |
| 49 | fstest.CreateFile("nothing.txt", []byte{0x00, 0x00}, 0755), |
| 50 | fstest.Symlink("nothing.txt", "link-no-nothing.txt"), |
| 51 | ) |
| 52 | |
| 53 | exp := filepath.FromSlash("add:/link-no-nothing.txt,add:/nothing.txt") |
| 54 | |
| 55 | if err := testCopy(t, apply, exp); err != nil { |
| 56 | t.Fatalf("Copy test failed: %+v", err) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestCopyToWorkDir(t *testing.T) { |
| 61 | t1 := t.TempDir() |
| 62 | |
| 63 | apply := fstest.Apply( |
| 64 | fstest.CreateFile("foo.txt", []byte("contents"), 0755), |
| 65 | ) |
| 66 | |
| 67 | require.NoError(t, apply.Apply(t1)) |
| 68 | |
| 69 | t2 := t.TempDir() |
| 70 | err := Copy(context.TODO(), t1, "foo.txt", t2, "foo.txt") |
| 71 | require.NoError(t, err) |
| 72 | |
| 73 | err = fstest.CheckDirectoryEqual(t1, t2) |
| 74 | require.NoError(t, err) |
| 75 | } |
| 76 | |
| 77 | func TestCopySingleFile(t *testing.T) { |
| 78 | t1 := t.TempDir() |
| 79 | |
| 80 | apply := fstest.Apply( |
| 81 | fstest.CreateFile("foo.txt", []byte("contents"), 0755), |
| 82 | ) |
| 83 | |
| 84 | require.NoError(t, apply.Apply(t1)) |
| 85 | |
| 86 | t2 := t.TempDir() |
| 87 | |
| 88 | err := Copy(context.TODO(), t1, "foo.txt", t2, "/") |
| 89 | require.NoError(t, err) |
| 90 | |
| 91 | err = fstest.CheckDirectoryEqual(t1, t2) |
| 92 | require.NoError(t, err) |
| 93 | |
| 94 | t3 := t.TempDir() |
| 95 | |
| 96 | err = Copy(context.TODO(), t1, "foo.txt", t3, "foo.txt") |
| 97 | require.NoError(t, err) |
| 98 | |
| 99 | err = fstest.CheckDirectoryEqual(t1, t2) |
| 100 | require.NoError(t, err) |
| 101 | |
| 102 | t4 := t.TempDir() |
| 103 | |
| 104 | err = Copy(context.TODO(), t1, "foo.txt", t4, "foo2.txt") |
| 105 | require.NoError(t, err) |
| 106 | |
| 107 | _, err = os.Stat(filepath.Join(t4, "foo2.txt")) |
| 108 | require.NoError(t, err) |
| 109 | |
| 110 | ch := &changeCollector{} |
| 111 | |
| 112 | err = Copy(context.TODO(), t1, "foo.txt", t4, "a/b/c/foo2.txt", WithChangeNotifier(ch.onChange)) |
| 113 | require.NoError(t, err) |
| 114 | |
| 115 | _, err = os.Stat(filepath.Join(t4, "a/b/c/foo2.txt")) |
| 116 | require.NoError(t, err) |
| 117 | |
| 118 | require.Equal(t, filepath.FromSlash("add:/a/b/c/foo2.txt"), ch.String()) |
| 119 | } |
| 120 | |
| 121 | func TestCopyOverrideFile(t *testing.T) { |
| 122 | t1 := t.TempDir() |
| 123 | |
| 124 | apply := fstest.Apply( |
| 125 | fstest.CreateFile("foo.txt", []byte("contents"), 0755), |
| 126 | ) |
| 127 | |
| 128 | require.NoError(t, apply.Apply(t1)) |
| 129 | |
| 130 | t2 := t.TempDir() |
| 131 | |
| 132 | err := Copy(context.TODO(), t1, "foo.txt", t2, "foo.txt") |
| 133 | require.NoError(t, err) |
| 134 | |
| 135 | err = fstest.CheckDirectoryEqual(t1, t2) |
| 136 | require.NoError(t, err) |
| 137 | |
| 138 | err = Copy(context.TODO(), t1, "foo.txt", t2, "foo.txt") |
| 139 | require.NoError(t, err) |
| 140 | |
| 141 | err = fstest.CheckDirectoryEqual(t1, t2) |
| 142 | require.NoError(t, err) |
| 143 | |
| 144 | err = Copy(context.TODO(), t1, "/.", t2, "/") |
| 145 | require.NoError(t, err) |
| 146 | |
| 147 | err = fstest.CheckDirectoryEqual(t1, t2) |
| 148 | require.NoError(t, err) |
| 149 | } |
| 150 | |
| 151 | func TestCopyDirectoryBasename(t *testing.T) { |
| 152 | t1 := t.TempDir() |
| 153 | |
| 154 | apply := fstest.Apply( |
| 155 | fstest.CreateDir("foo", 0755), |
| 156 | fstest.CreateDir("foo/bar", 0755), |
| 157 | fstest.CreateFile("foo/bar/baz.txt", []byte("contents"), 0755), |
| 158 | ) |
| 159 | require.NoError(t, apply.Apply(t1)) |
| 160 | |
| 161 | t2 := t.TempDir() |
| 162 | |
| 163 | ch := &changeCollector{} |
| 164 | |
| 165 | err := Copy(context.TODO(), t1, "foo", t2, "foo", WithChangeNotifier(ch.onChange)) |
| 166 | require.NoError(t, err) |
| 167 | |
| 168 | err = fstest.CheckDirectoryEqual(t1, t2) |
| 169 | require.NoError(t, err) |
| 170 | |
| 171 | require.Equal(t, filepath.FromSlash("add:/foo,add:/foo/bar,add:/foo/bar/baz.txt"), ch.String()) |
| 172 | |
| 173 | ch = &changeCollector{} |
| 174 | err = Copy(context.TODO(), t1, "foo", t2, "foo", WithCopyInfo(CopyInfo{ |
| 175 | CopyDirContents: true, |
| 176 | ChangeFunc: ch.onChange, |
| 177 | })) |
| 178 | require.NoError(t, err) |
| 179 | |
| 180 | require.Equal(t, filepath.FromSlash("add:/foo/bar,add:/foo/bar/baz.txt"), ch.String()) |
| 181 | |
| 182 | err = fstest.CheckDirectoryEqual(t1, t2) |
| 183 | require.NoError(t, err) |
| 184 | } |
| 185 | |
| 186 | func TestCopyWildcards(t *testing.T) { |
| 187 | t1 := t.TempDir() |
| 188 | |
| 189 | apply := fstest.Apply( |
| 190 | fstest.CreateFile("foo.txt", []byte("foo-contents"), 0755), |
| 191 | fstest.CreateFile("foo.go", []byte("go-contents"), 0755), |
| 192 | fstest.CreateFile("bar.txt", []byte("bar-contents"), 0755), |
| 193 | ) |
| 194 | |
| 195 | require.NoError(t, apply.Apply(t1)) |
| 196 | |
| 197 | t2 := t.TempDir() |
| 198 | |
| 199 | err := Copy(context.TODO(), t1, "foo*", t2, "/") |
| 200 | require.Error(t, err) |
| 201 | |
| 202 | err = Copy(context.TODO(), t1, "foo*", t2, "/", AllowWildcards) |
| 203 | require.NoError(t, err) |
| 204 | |
| 205 | _, err = os.Stat(filepath.Join(t2, "foo.txt")) |
| 206 | require.NoError(t, err) |
| 207 | _, err = os.Stat(filepath.Join(t2, "foo.go")) |
| 208 | require.NoError(t, err) |
| 209 | _, err = os.Stat(filepath.Join(t2, "bar.txt")) |
| 210 | require.Error(t, err) |
| 211 | require.True(t, os.IsNotExist(err)) |
| 212 | |
| 213 | t3 := t.TempDir() |
| 214 | |
| 215 | err = Copy(context.TODO(), t1, "bar*", t3, "foo.txt", AllowWildcards) |
| 216 | require.NoError(t, err) |
| 217 | dt, err := os.ReadFile(filepath.Join(t3, "foo.txt")) |
| 218 | require.NoError(t, err) |
| 219 | require.Equal(t, "bar-contents", string(dt)) |
| 220 | } |
| 221 | |
| 222 | func TestCopyExistingDirDest(t *testing.T) { |
| 223 | if os.Getuid() != 0 { |
| 224 | t.Skip() |
| 225 | } |
| 226 | |
| 227 | t1 := t.TempDir() |
| 228 | |
| 229 | apply := fstest.Apply( |
| 230 | fstest.CreateDir("dir", 0755), |
| 231 | fstest.CreateFile("dir/foo.txt", []byte("foo-contents"), 0644), |
| 232 | fstest.CreateFile("dir/bar.txt", []byte("bar-contents"), 0644), |
| 233 | ) |
| 234 | require.NoError(t, apply.Apply(t1)) |
| 235 | |
| 236 | t2 := t.TempDir() |
| 237 | |
| 238 | apply = fstest.Apply( |
| 239 | // notice how perms for destination and source are different |
| 240 | fstest.CreateDir("dir", 0700), |
| 241 | // dir/foo.txt does not exist, but dir/bar.txt does |
| 242 | // notice how both perms and contents for destination and source are different |
| 243 | fstest.CreateFile("dir/bar.txt", []byte("old-bar-contents"), 0600), |
| 244 | ) |
| 245 | require.NoError(t, apply.Apply(t2)) |
| 246 | |
| 247 | for _, x := range []string{"dir", "dir/bar.txt"} { |
| 248 | err := os.Chown(filepath.Join(t2, x), 1, 1) |
| 249 | require.NoErrorf(t, err, "x=%s", x) |
| 250 | } |
| 251 | |
| 252 | err := Copy(context.TODO(), t1, "dir", t2, "dir", WithCopyInfo(CopyInfo{ |
| 253 | CopyDirContents: true, |
| 254 | })) |
| 255 | require.NoError(t, err) |
| 256 | |
| 257 | // verify that existing destination dir's metadata was not overwritten |
| 258 | st, err := os.Lstat(filepath.Join(t2, "dir")) |
| 259 | require.NoError(t, err) |
| 260 | require.Equal(t, st.Mode()&os.ModePerm, os.FileMode(0700)) |
| 261 | var uid, gid int |
| 262 | if runtime.GOOS != "windows" { |
| 263 | uid, gid = getUIDGID(st) |
| 264 | require.Equal(t, 1, uid) |
| 265 | require.Equal(t, 1, gid) |
| 266 | } |
| 267 | |
| 268 | // verify that non-existing file was created |
| 269 | _, err = os.Lstat(filepath.Join(t2, "dir/foo.txt")) |
| 270 | require.NoError(t, err) |
| 271 | |
| 272 | // verify that existing file's content and metadata was overwritten |
| 273 | st, err = os.Lstat(filepath.Join(t2, "dir/bar.txt")) |
| 274 | require.NoError(t, err) |
| 275 | require.Equal(t, os.FileMode(0644), st.Mode()&os.ModePerm) |
| 276 | if runtime.GOOS != "windows" { |
| 277 | uid, gid = getUIDGID(st) |
| 278 | require.Equal(t, 0, uid) |
| 279 | require.Equal(t, 0, gid) |
| 280 | } |
| 281 | dt, err := os.ReadFile(filepath.Join(t2, "dir/bar.txt")) |
| 282 | require.NoError(t, err) |
| 283 | require.Equal(t, "bar-contents", string(dt)) |
| 284 | } |
| 285 | |
| 286 | func TestCopyDirectoryChmodChown(t *testing.T) { |
| 287 | if os.Getuid() != 0 { |
| 288 | t.Skip() |
| 289 | } |
| 290 | |
| 291 | t1 := t.TempDir() |
| 292 | apply := fstest.Apply( |
| 293 | fstest.CreateDir("dir", 0755), |
| 294 | fstest.CreateFile("dir/foo.txt", []byte("foo-contents"), 0644), |
| 295 | fstest.CreateDir("dir/sub2", 0755), |
| 296 | ) |
| 297 | require.NoError(t, apply.Apply(t1)) |
| 298 | |
| 299 | t2 := t.TempDir() |
| 300 | |
| 301 | mod := int(0700) |
| 302 | err := Copy(context.TODO(), t1, "dir", t2, "dest/sub", WithCopyInfo(CopyInfo{ |
| 303 | CopyDirContents: true, |
| 304 | Mode: &mod, |
| 305 | }), WithChown(100, 200)) |
| 306 | require.NoError(t, err) |
| 307 | |
| 308 | st, err := os.Lstat(filepath.Join(t2, "dest/sub")) |
| 309 | require.NoError(t, err) |
| 310 | uid, gid, ok := readUidGid(st) |
| 311 | if ok { |
| 312 | require.Equal(t, 100, uid) |
| 313 | require.Equal(t, 200, gid) |
| 314 | } |
| 315 | require.Equal(t, os.FileMode(0700), st.Mode()&os.ModePerm) |
| 316 | |
| 317 | st, err = os.Lstat(filepath.Join(t2, "dest/sub/foo.txt")) |
| 318 | require.NoError(t, err) |
| 319 | uid, gid, ok = readUidGid(st) |
| 320 | if ok { |
| 321 | require.Equal(t, 100, uid) |
| 322 | require.Equal(t, 200, gid) |
| 323 | } |
| 324 | require.Equal(t, os.FileMode(0700), st.Mode()&os.ModePerm) |
| 325 | |
| 326 | st, err = os.Lstat(filepath.Join(t2, "dest/sub/sub2")) |
| 327 | require.NoError(t, err) |
| 328 | uid, gid, ok = readUidGid(st) |
| 329 | if ok { |
| 330 | require.Equal(t, 100, uid) |
| 331 | require.Equal(t, 200, gid) |
| 332 | } |
| 333 | require.Equal(t, os.FileMode(0700), st.Mode()&os.ModePerm) |
| 334 | } |
| 335 | |
| 336 | func TestCopyDirectoryContentsTimestamp(t *testing.T) { |
| 337 | timestamp := time.Unix(0, 0) |
| 338 | apply := fstest.Apply( |
| 339 | fstest.CreateDir("/foo/", 0755), |
| 340 | fstest.CreateDir("/foo/bar", 0755), |
| 341 | fstest.CreateFile("/foo/bar/baz", []byte{}, 0644), |
| 342 | ) |
| 343 | |
| 344 | t1 := t.TempDir() |
| 345 | t2 := t.TempDir() |
| 346 | |
| 347 | require.NoError(t, apply.Apply(t1)) |
| 348 | require.NoError(t, Copy(context.TODO(), t1, "/foo", t2, "/foo", WithCopyInfo(CopyInfo{ |
| 349 | CopyDirContents: true, |
| 350 | Utime: ×tamp, |
| 351 | }))) |
| 352 | require.NoError(t, fstest.CheckDirectoryEqual(t1, t2)) |
| 353 | |
| 354 | for _, s := range []string{"/foo/bar/baz", "/foo/bar", "/foo"} { |
| 355 | stat, _ := os.Stat(filepath.Join(t2, s)) |
| 356 | require.Equal(t, timestamp, stat.ModTime()) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | func TestCopySymlinks(t *testing.T) { |
| 361 | t1 := t.TempDir() |
| 362 | |
| 363 | apply := fstest.Apply( |
| 364 | fstest.CreateDir("testdir", 0755), |
| 365 | fstest.CreateFile("testdir/foo.txt", []byte("foo-contents"), 0644), |
| 366 | fstest.Symlink("foo.txt", "testdir/link2"), |
| 367 | fstest.Symlink("/testdir", "link"), |
| 368 | ) |
| 369 | require.NoError(t, apply.Apply(t1)) |
| 370 | |
| 371 | t2 := t.TempDir() |
| 372 | |
| 373 | err := Copy(context.TODO(), t1, "link/link2", t2, "foo", WithCopyInfo(CopyInfo{ |
| 374 | FollowLinks: true, |
| 375 | })) |
| 376 | require.NoError(t, err) |
| 377 | |
| 378 | // verify that existing destination dir's metadata was not overwritten |
| 379 | st, err := os.Lstat(filepath.Join(t2, "foo")) |
| 380 | require.NoError(t, err) |
| 381 | if runtime.GOOS != "windows" { |
| 382 | require.Equal(t, os.FileMode(0644), st.Mode()&os.ModePerm) |
| 383 | } |
| 384 | require.Equal(t, 0, int(st.Mode()&os.ModeSymlink)) |
| 385 | dt, err := os.ReadFile(filepath.Join(t2, "foo")) |
| 386 | require.NoError(t, err) |
| 387 | require.Equal(t, "foo-contents", string(dt)) |
| 388 | |
| 389 | t3 := t.TempDir() |
| 390 | |
| 391 | err = Copy(context.TODO(), t1, "link/link2", t3, "foo", WithCopyInfo(CopyInfo{})) |
| 392 | require.NoError(t, err) |
| 393 | |
| 394 | // verify that existing destination dir's metadata was not overwritten |
| 395 | st, err = os.Lstat(filepath.Join(t3, "foo")) |
| 396 | require.NoError(t, err) |
| 397 | require.Equal(t, os.ModeSymlink, st.Mode()&os.ModeSymlink) |
| 398 | link, err := os.Readlink(filepath.Join(t3, "foo")) |
| 399 | require.NoError(t, err) |
| 400 | require.Equal(t, "foo.txt", link) |
| 401 | } |
| 402 | |
| 403 | func TestCopyWithAlwaysReplaceExistingDestPaths(t *testing.T) { |
| 404 | destDir := t.TempDir() |
| 405 | require.NoError(t, fstest.Apply( |
| 406 | fstest.CreateDir("root", 0755), |
| 407 | fstest.CreateDir("root/overwritedir", 0755), |
| 408 | fstest.CreateFile("root/overwritedir/subfile", nil, 0755), |
| 409 | fstest.CreateFile("root/overwritefile", nil, 0755), |
| 410 | fstest.Symlink("dir", "root/overwritesymlink"), |
| 411 | fstest.CreateDir("root/dir", 0755), |
| 412 | fstest.CreateFile("root/dir/dirfile1", nil, 0755), |
| 413 | fstest.CreateDir("root/dir/overwritesubdir", 0755), |
| 414 | fstest.CreateFile("root/dir/overwritesubfile", nil, 0755), |
| 415 | fstest.Symlink("dirfile1", "root/dir/overwritesymlink"), |
| 416 | ).Apply(destDir)) |
| 417 | |
| 418 | srcDir := t.TempDir() |
| 419 | require.NoError(t, fstest.Apply( |
| 420 | fstest.CreateDir("root", 0755), |
| 421 | fstest.CreateFile("root/overwritedir", nil, 0755), |
| 422 | fstest.CreateDir("root/overwritefile", 0755), |
| 423 | fstest.CreateFile("root/overwritefile/foo", nil, 0755), |
| 424 | fstest.CreateDir("root/overwritesymlink", 0755), |
| 425 | fstest.CreateDir("root/dir", 0755), |
| 426 | fstest.CreateFile("root/dir/dirfile2", nil, 0755), |
| 427 | fstest.CreateFile("root/dir/overwritesubdir", nil, 0755), |
| 428 | fstest.CreateDir("root/dir/overwritesubfile", 0755), |
| 429 | fstest.CreateDir("root/dir/overwritesymlink", 0755), |
| 430 | ).Apply(srcDir)) |
| 431 | |
| 432 | expectedDir := t.TempDir() |
| 433 | require.NoError(t, fstest.Apply( |
| 434 | fstest.CreateDir("root", 0755), |
| 435 | fstest.CreateFile("root/overwritedir", nil, 0755), |
| 436 | fstest.CreateDir("root/overwritefile", 0755), |
| 437 | fstest.CreateFile("root/overwritefile/foo", nil, 0755), |
| 438 | fstest.CreateDir("root/overwritesymlink", 0755), |
| 439 | fstest.CreateDir("root/dir", 0755), |
| 440 | fstest.CreateFile("root/dir/dirfile1", nil, 0755), |
| 441 | fstest.CreateFile("root/dir/dirfile2", nil, 0755), |
| 442 | fstest.CreateFile("root/dir/overwritesubdir", nil, 0755), |
| 443 | fstest.CreateDir("root/dir/overwritesubfile", 0755), |
| 444 | fstest.CreateDir("root/dir/overwritesymlink", 0755), |
| 445 | ).Apply(expectedDir)) |
| 446 | |
| 447 | err := Copy(context.TODO(), srcDir, "root", destDir, "root", WithCopyInfo(CopyInfo{ |
| 448 | AlwaysReplaceExistingDestPaths: true, |
| 449 | CopyDirContents: true, |
| 450 | })) |
| 451 | require.NoError(t, err) |
| 452 | |
| 453 | err = fstest.CheckDirectoryEqual(destDir, expectedDir) |
| 454 | require.NoError(t, err) |
| 455 | } |
| 456 | |
| 457 | func testCopy(t *testing.T, apply fstest.Applier, exp string) error { |
| 458 | t1 := t.TempDir() |
| 459 | t2 := t.TempDir() |
| 460 | |
| 461 | if err := apply.Apply(t1); err != nil { |
| 462 | return errors.Wrap(err, "failed to apply changes") |
| 463 | } |
| 464 | |
| 465 | ch := &changeCollector{} |
| 466 | if err := Copy(context.TODO(), t1, "/.", t2, "/", WithChangeNotifier(ch.onChange)); err != nil { |
| 467 | return errors.Wrap(err, "failed to copy") |
| 468 | } |
| 469 | |
| 470 | if exp != ch.String() { |
| 471 | return errors.Errorf("unexpected changes: %s", ch) |
| 472 | } |
| 473 | |
| 474 | return fstest.CheckDirectoryEqual(t1, t2) |
| 475 | } |
| 476 | |
| 477 | func TestCopyIncludeExclude(t *testing.T) { |
| 478 | t1 := t.TempDir() |
| 479 | |
| 480 | apply := fstest.Apply( |
| 481 | fstest.CreateDir("bar", 0755), |
| 482 | fstest.CreateFile("bar/foo", []byte("foo-contents"), 0755), |
| 483 | fstest.CreateDir("bar/baz", 0755), |
| 484 | fstest.CreateFile("bar/baz/foo3", []byte("foo3-contents"), 0755), |
| 485 | fstest.CreateFile("foo2", []byte("foo2-contents"), 0755), |
| 486 | ) |
| 487 | |
| 488 | require.NoError(t, apply.Apply(t1)) |
| 489 | |
| 490 | testCases := []struct { |
| 491 | name string |
| 492 | opts []Opt |
| 493 | expectedResults []string |
| 494 | expectedChanges string |
| 495 | }{ |
| 496 | { |
| 497 | name: "include bar", |
| 498 | opts: []Opt{WithIncludePattern("bar")}, |
| 499 | expectedResults: []string{"bar", "bar/foo", "bar/baz", "bar/baz/foo3"}, |
| 500 | expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo"), |
| 501 | }, |
| 502 | { |
| 503 | name: "include *", |
| 504 | opts: []Opt{WithIncludePattern("*")}, |
| 505 | expectedResults: []string{"bar", "bar/foo", "bar/baz", "bar/baz/foo3", "foo2"}, |
| 506 | expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo,add:/foo2"), |
| 507 | }, |
| 508 | { |
| 509 | name: "include bar/foo", |
| 510 | opts: []Opt{WithIncludePattern("bar/foo")}, |
| 511 | expectedResults: []string{"bar", "bar/foo"}, |
| 512 | expectedChanges: filepath.FromSlash("add:/bar/foo"), |
| 513 | }, |
| 514 | { |
| 515 | name: "include bar except bar/foo", |
| 516 | opts: []Opt{WithIncludePattern("bar"), WithIncludePattern("!bar/foo")}, |
| 517 | expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3"}, |
| 518 | expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3"), |
| 519 | }, |
| 520 | { |
| 521 | name: "include bar/foo and foo*", |
| 522 | opts: []Opt{WithIncludePattern("bar/foo"), WithIncludePattern("foo*")}, |
| 523 | expectedResults: []string{"bar", "bar/foo", "foo2"}, |
| 524 | expectedChanges: filepath.FromSlash("add:/bar/foo,add:/foo2"), |
| 525 | }, |
| 526 | { |
| 527 | name: "include b*", |
| 528 | opts: []Opt{WithIncludePattern("b*")}, |
| 529 | expectedResults: []string{"bar", "bar/foo", "bar/baz", "bar/baz/foo3"}, |
| 530 | expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo"), |
| 531 | }, |
| 532 | { |
| 533 | name: "include bar/f*", |
| 534 | opts: []Opt{WithIncludePattern("bar/f*")}, |
| 535 | expectedResults: []string{"bar", "bar/foo"}, |
| 536 | }, |
| 537 | { |
| 538 | name: "include bar/g*", |
| 539 | opts: []Opt{WithIncludePattern("bar/g*")}, |
| 540 | expectedResults: nil, |
| 541 | }, |
| 542 | { |
| 543 | name: "include b*/f*", |
| 544 | opts: []Opt{WithIncludePattern("b*/f*")}, |
| 545 | expectedResults: []string{"bar", "bar/foo"}, |
| 546 | }, |
| 547 | { |
| 548 | name: "include b*/foo", |
| 549 | opts: []Opt{WithIncludePattern("b*/foo")}, |
| 550 | expectedResults: []string{"bar", "bar/foo"}, |
| 551 | }, |
| 552 | { |
| 553 | name: "include b*/", |
| 554 | opts: []Opt{WithIncludePattern("b*/")}, |
| 555 | expectedResults: []string{"bar", "bar/foo", "bar/baz", "bar/baz/foo3"}, |
| 556 | }, |
| 557 | { |
| 558 | name: "include bar/*/foo3", |
| 559 | opts: []Opt{WithIncludePattern("bar/*/foo3")}, |
| 560 | expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3"}, |
| 561 | }, |
| 562 | { |
| 563 | name: "exclude bar*, !bar/baz", |
| 564 | opts: []Opt{WithExcludePattern("bar*"), WithExcludePattern("!bar/baz")}, |
| 565 | expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3", "foo2"}, |
| 566 | }, |
| 567 | { |
| 568 | name: "exclude **, !bar/baz", |
| 569 | opts: []Opt{WithExcludePattern("**"), WithExcludePattern("!bar/baz")}, |
| 570 | expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3"}, |
| 571 | }, |
| 572 | { |
| 573 | name: "exclude **, !bar/baz, bar/baz/foo3", |
| 574 | opts: []Opt{WithExcludePattern("**"), WithExcludePattern("!bar/baz"), WithExcludePattern("bar/baz/foo3")}, |
| 575 | expectedResults: []string{"bar", "bar/baz"}, |
| 576 | }, |
| 577 | { |
| 578 | name: "include bar, exclude bar/baz", |
| 579 | opts: []Opt{WithIncludePattern("bar"), WithExcludePattern("bar/baz")}, |
| 580 | expectedResults: []string{"bar", "bar/foo"}, |
| 581 | }, |
| 582 | { |
| 583 | name: "doublestar include", |
| 584 | opts: []Opt{WithIncludePattern("**/foo3")}, |
| 585 | expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3"}, |
| 586 | }, |
| 587 | { |
| 588 | name: "doublestar matching second item in path", |
| 589 | opts: []Opt{WithIncludePattern("**/baz")}, |
| 590 | expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3"}, |
| 591 | expectedChanges: filepath.FromSlash("add:/bar/baz,add:/bar/baz/foo3"), |
| 592 | }, |
| 593 | { |
| 594 | name: "doublestar matching first item in path", |
| 595 | opts: []Opt{WithIncludePattern("**/bar")}, |
| 596 | expectedResults: []string{"bar", "bar/foo", "bar/baz", "bar/baz/foo3"}, |
| 597 | expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo"), |
| 598 | }, |
| 599 | { |
| 600 | name: "doublestar exclude", |
| 601 | opts: []Opt{WithIncludePattern("bar"), WithExcludePattern("**/foo3")}, |
| 602 | expectedResults: []string{"bar", "bar/foo", "bar/baz"}, |
| 603 | expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/foo"), |
| 604 | }, |
| 605 | { |
| 606 | name: "exclude bar/baz", |
| 607 | opts: []Opt{WithExcludePattern("bar/baz")}, |
| 608 | expectedResults: []string{"bar", "bar/foo", "foo2"}, |
| 609 | expectedChanges: filepath.FromSlash("add:/bar,add:/bar/foo,add:/foo2"), |
| 610 | }, |
| 611 | } |
| 612 | |
| 613 | for _, tc := range testCases { |
| 614 | t.Run(tc.name, func(t *testing.T) { |
| 615 | t2 := t.TempDir() |
| 616 | |
| 617 | ch := &changeCollector{} |
| 618 | tc.opts = append(tc.opts, WithChangeNotifier(ch.onChange)) |
| 619 | |
| 620 | err := Copy(context.Background(), t1, "/", t2, "/", tc.opts...) |
| 621 | require.NoError(t, err, tc.name) |
| 622 | |
| 623 | var results []string |
| 624 | for _, path := range []string{"bar", "bar/foo", "bar/baz", "bar/baz/asdf", "bar/baz/asdf/x", "bar/baz/foo3", "foo2"} { |
| 625 | _, err := os.Stat(filepath.Join(t2, path)) |
| 626 | if err == nil { |
| 627 | results = append(results, path) |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | require.Equal(t, tc.expectedResults, results, tc.name) |
| 632 | |
| 633 | if tc.expectedChanges != "" { |
| 634 | require.Equal(t, tc.expectedChanges, ch.String()) |
| 635 | } |
| 636 | }) |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | func TestCopyFileWithPathTimestamp(t *testing.T) { |
| 641 | timestamp := time.Unix(0, 0) |
| 642 | apply := fstest.Apply( |
| 643 | fstest.CreateDir("/foo/", 0755), |
| 644 | fstest.CreateFile("/foo/bar", []byte{}, 0644), |
| 645 | ) |
| 646 | |
| 647 | t1 := t.TempDir() |
| 648 | t2 := t.TempDir() |
| 649 | |
| 650 | require.NoError(t, apply.Apply(t1)) |
| 651 | require.NoError(t, Copy(context.TODO(), t1, "/foo/bar", t2, "/test1/test2/test3/bar", WithCopyInfo(CopyInfo{ |
| 652 | CopyDirContents: true, |
| 653 | Utime: ×tamp, |
| 654 | }))) |
| 655 | |
| 656 | for _, s := range []string{"/test1/test2/test3/bar", "/test1/test2/test3", "/test1/test2", "/test1"} { |
| 657 | stat, _ := os.Stat(filepath.Join(t2, s)) |
| 658 | require.Equal(t, timestamp, stat.ModTime(), "path: %s", s) |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | type changeCollector struct { |
| 663 | changes []string |
| 664 | } |
| 665 | |
| 666 | func (c *changeCollector) onChange(kind fsutil.ChangeKind, path string, _ os.FileInfo, _ error) error { |
| 667 | c.changes = append(c.changes, fmt.Sprintf("%s:%s", kind, path)) |
| 668 | return nil |
| 669 | } |
| 670 | |
| 671 | func (c *changeCollector) String() string { |
| 672 | sort.Strings(c.changes) |
| 673 | return strings.Join(c.changes, ",") |
| 674 | } |