Files
mergerfs/tests/TEST_cfg_link_rename_exdev
T
Antonio SJ Musumeci ef1e01b5a5 Add unit and integration tests for recently changed code
Unit tests (29 new in tests/tests.cpp):
- str::eq() and str::startswith() char* nullptr safety
- str::from() u64/s64/int suffix parsing (K/M/G/T, overflow, invalid)
- num::humanize() byte formatting (0, 512, 1K, 1M, 1G, 1T, non-exact)
- RmdirErr priority error accumulation (ENOTEMPTY/EEXIST override rules)
- HashSet inline-to-heap growth, empty string, and large insertion
- fs::inode algorithm switching (valid/invalid names, passthrough raw ino,
  distinct inodes across algorithms)
- RND::rand64() bounds (range, max, shrink_to_rand_elem)
- Branches::Impl copy and move assignment re-links default_minfreespace

Integration tests (5 new in tests/):
- TEST_policy_lup: LUP policy create and search
- TEST_rmdir_enotempty_priority: rmdir returns ENOTEMPTY for non-empty dirs
- TEST_open_after_unlink: read/write via fd after unlink
- TEST_api_xattr: mergerfs xattr API (fullpath, relpath, basepath, allpaths)
- TEST_movefile_enospc: moveonenospc configuration round-trip

Also: remove xattr nosys checks from TEST_cfg_xattr_modes, reformat
integration test scripts to use temp_dir/cleanup_dir helpers, and
minor parameter alignment changes in fuse_getxattr, fuse_init,
fuse_rename.
2026-04-29 17:41:00 -04:00

152 lines
5.2 KiB
Python
Executable File

#!/usr/bin/env python3
import errno
import os
import sys
from posix_parity import cleanup_dir
from posix_parity import fail
from posix_parity import join
from posix_parity import mergerfs_get_option
from posix_parity import mergerfs_set_option
from posix_parity import parse_allpaths
from posix_parity import temp_dir
from posix_parity import touch
def get_errno(func):
try:
func()
return 0
except OSError as exc:
return exc.errno
def allpaths(path):
raw = os.getxattr(path, "user.mergerfs.allpaths")
return parse_allpaths(raw)
def must_two_branches(paths, op):
if len(paths) < 2:
return f"{op}: requires at least 2 branches; got {paths!r}"
return None
def pick_cross_branch_paths(mount, merge_base):
src = join(merge_base, "src")
dst = join(merge_base, "sub/dst")
touch(src, b"src")
os.makedirs(os.path.dirname(dst), exist_ok=True)
try:
src_paths = allpaths(src)
except OSError as exc:
if exc.errno in (errno.EOPNOTSUPP, errno.ENOTSUP, errno.ENOSYS):
raise RuntimeError("allpaths-xattr-unsupported")
raise
if len(src_paths) < 2:
# Try to create destination first to force different existing-path decisions.
touch(dst, b"old")
os.unlink(dst)
try:
src_paths = allpaths(src)
except OSError as exc:
if exc.errno in (errno.EOPNOTSUPP, errno.ENOTSUP, errno.ENOSYS):
raise RuntimeError("allpaths-xattr-unsupported")
raise
err = must_two_branches(src_paths, "cross-branch setup")
if err:
raise RuntimeError(err)
return src, dst
def main():
if len(sys.argv) != 2:
print("usage: TEST_cfg_link_rename_exdev <mountpoint>", file=sys.stderr)
return 1
mount = sys.argv[1]
merge_base = temp_dir(mount)
try:
try:
src, dst = pick_cross_branch_paths(mount, merge_base)
except PermissionError:
return 0
except RuntimeError as exc:
# Environment-dependent; treat as soft skip.
msg = str(exc)
if "requires at least 2 branches" in msg or msg == "allpaths-xattr-unsupported":
return 0
return fail(msg)
except OSError:
return 0
try:
orig_create = mergerfs_get_option(mount, "category.create")
orig_link = mergerfs_get_option(mount, "link-exdev")
orig_rename = mergerfs_get_option(mount, "rename-exdev")
except (PermissionError, FileNotFoundError, OSError):
return 0
try:
# Maximize chance of EXDEV path by path-preserving create policy.
mergerfs_set_option(mount, "category.create", "epmfs")
# link-exdev passthrough => EXDEV when cross-device path preserving applies.
mergerfs_set_option(mount, "link-exdev", "passthrough")
err = get_errno(lambda: os.link(src, dst + ".link.pass"))
if err not in (0, errno.EXDEV):
return fail(f"link-exdev=passthrough unexpected errno={err}")
link_exdev_active = (err == errno.EXDEV)
# rel-symlink should not return EXDEV if fallback triggers.
mergerfs_set_option(mount, "link-exdev", "rel-symlink")
rel_link = dst + ".link.rel"
err = get_errno(lambda: os.link(src, rel_link))
if link_exdev_active and err == errno.EXDEV:
return fail("link-exdev=rel-symlink still returned EXDEV")
if link_exdev_active and err == 0 and not os.path.islink(rel_link):
return fail("link-exdev=rel-symlink expected symlink fallback")
# rename-exdev passthrough => EXDEV in cross-device path preserving case.
src_ren = src + ".ren"
touch(src_ren, b"src")
mergerfs_set_option(mount, "rename-exdev", "passthrough")
err = get_errno(lambda: os.rename(src_ren, dst + ".ren.pass"))
if err not in (0, errno.EXDEV):
return fail(f"rename-exdev=passthrough unexpected errno={err}")
rename_exdev_active = (err == errno.EXDEV)
# rel-symlink should avoid EXDEV by fallback when applicable.
src_ren2 = src + ".ren2"
touch(src_ren2, b"src")
mergerfs_set_option(mount, "rename-exdev", "rel-symlink")
rel_ren = dst + ".ren.rel"
err = get_errno(lambda: os.rename(src_ren2, rel_ren))
if rename_exdev_active and err == errno.EXDEV:
return fail("rename-exdev=rel-symlink still returned EXDEV")
if rename_exdev_active and err == 0 and not os.path.islink(rel_ren):
return fail("rename-exdev=rel-symlink expected symlink fallback")
except (PermissionError, FileNotFoundError, OSError):
return 0
finally:
try:
mergerfs_set_option(mount, "category.create", orig_create)
mergerfs_set_option(mount, "link-exdev", orig_link)
mergerfs_set_option(mount, "rename-exdev", orig_rename)
except OSError:
pass
return 0
finally:
cleanup_dir(merge_base)
if __name__ == "__main__":
raise SystemExit(main())