Skip to content

Commit

Permalink
cmd function now creates repo_location argument (#7734)
Browse files Browse the repository at this point in the history
f'repo={repo_location}' moved to 'cmd' function
  • Loading branch information
bigtedde committed Jul 24, 2023
1 parent e1cd38a commit 2e59a70
Show file tree
Hide file tree
Showing 28 changed files with 1,283 additions and 1,859 deletions.
49 changes: 24 additions & 25 deletions src/borg/testsuite/archiver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ def cmd(archiver, *args, **kw):
binary_output = kw.get("binary_output", False)
if fork is None:
fork = archiver.FORK_DEFAULT
ret, output = exec_cmd(*args, archiver=archiver.archiver, fork=fork, exe=archiver.EXE, **kw)
ret, output = exec_cmd(
f"--repo={archiver.repository_location}", *args, archiver=archiver.archiver, fork=fork, exe=archiver.EXE, **kw
)
if ret != exit_code:
print(output)
assert ret == exit_code
Expand All @@ -160,11 +162,10 @@ def cmd(archiver, *args, **kw):


def create_src_archive(archiver, name, ts=None):
repo_location, source_dir = archiver.repository_location, src_dir
if ts:
cmd(archiver, f"--repo={repo_location}", "create", "--compression=lz4", f"--timestamp={ts}", name, source_dir)
cmd(archiver, "create", "--compression=lz4", f"--timestamp={ts}", name, src_dir)
else:
cmd(archiver, f"--repo={repo_location}", "create", "--compression=lz4", name, source_dir)
cmd(archiver, "create", "--compression=lz4", name, src_dir)


def open_archive(repo_path, name):
Expand Down Expand Up @@ -270,8 +271,7 @@ def _set_repository_id(repo_path, id):


def _extract_hardlinks_setup(archiver):
repo_location, input_path = archiver.repository_location, archiver.input_path

input_path = archiver.input_path
os.mkdir(os.path.join(input_path, "dir1"))
os.mkdir(os.path.join(input_path, "dir1/subdir"))

Expand All @@ -283,49 +283,48 @@ def _extract_hardlinks_setup(archiver):
create_regular_file(input_path, "dir1/source2")
os.link(os.path.join(input_path, "dir1/source2"), os.path.join(input_path, "dir1/aaaa"))

cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, f"--repo={repo_location}", "create", "test", "input")
cmd(archiver, "rcreate", RK_ENCRYPTION)
cmd(archiver, "create", "test", "input")


def _create_test_caches(archiver):
repo_location, input_path = archiver.repository_location, archiver.input_path
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
input_path = archiver.input_path
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_regular_file(input_path, "file1", size=1024 * 80)
create_regular_file(input_path, "cache1/%s" % CACHE_TAG_NAME, contents=CACHE_TAG_CONTENTS + b" extra stuff")
create_regular_file(input_path, "cache2/%s" % CACHE_TAG_NAME, contents=b"invalid signature")
os.mkdir("input/cache3")
if are_hardlinks_supported():
os.link("input/cache1/%s" % CACHE_TAG_NAME, "input/cache3/%s" % CACHE_TAG_NAME)
else:
create_regular_file(
archiver.input_path, "cache3/%s" % CACHE_TAG_NAME, contents=CACHE_TAG_CONTENTS + b" extra stuff"
)
create_regular_file(input_path, "cache3/%s" % CACHE_TAG_NAME, contents=CACHE_TAG_CONTENTS + b" extra stuff")


def _assert_test_caches(archiver):
with changedir("output"):
cmd(archiver, f"--repo={archiver.repository_location}", "extract", "test")
cmd(archiver, "extract", "test")
assert sorted(os.listdir("output/input")) == ["cache2", "file1"]
assert sorted(os.listdir("output/input/cache2")) == [CACHE_TAG_NAME]


def _create_test_tagged(archiver):
cmd(archiver, f"--repo={archiver.repository_location}", "rcreate", RK_ENCRYPTION)
create_regular_file(archiver.input_path, "file1", size=1024 * 80)
create_regular_file(archiver.input_path, "tagged1/.NOBACKUP")
create_regular_file(archiver.input_path, "tagged2/00-NOBACKUP")
create_regular_file(archiver.input_path, "tagged3/.NOBACKUP/file2", size=1024)
input_path = archiver.input_path
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_regular_file(input_path, "file1", size=1024 * 80)
create_regular_file(input_path, "tagged1/.NOBACKUP")
create_regular_file(input_path, "tagged2/00-NOBACKUP")
create_regular_file(input_path, "tagged3/.NOBACKUP/file2", size=1024)


def _assert_test_tagged(archiver):
with changedir("output"):
cmd(archiver, f"--repo={archiver.repository_location}", "extract", "test")
cmd(archiver, "extract", "test")
assert sorted(os.listdir("output/input")) == ["file1"]


def _create_test_keep_tagged(archiver):
input_path = archiver.input_path
cmd(archiver, f"--repo={archiver.repository_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_regular_file(input_path, "file0", size=1024)
create_regular_file(input_path, "tagged1/.NOBACKUP1")
create_regular_file(input_path, "tagged1/file1", size=1024)
Expand All @@ -341,7 +340,7 @@ def _create_test_keep_tagged(archiver):

def _assert_test_keep_tagged(archiver):
with changedir("output"):
cmd(archiver, f"--repo={archiver.repository_location}", "extract", "test")
cmd(archiver, "extract", "test")
assert sorted(os.listdir("output/input")), ["file0", "tagged1", "tagged2", "tagged3", "taggedall"]
assert os.listdir("output/input/tagged1"), [".NOBACKUP1"]
assert os.listdir("output/input/tagged2"), [".NOBACKUP2"]
Expand All @@ -351,7 +350,7 @@ def _assert_test_keep_tagged(archiver):

def check_cache(archiver):
# First run a regular borg check
cmd(archiver, f"--repo={archiver.repository_location}", "check")
cmd(archiver, "check")
# Then check that the cache on disk matches exactly what's in the repo.
with open_repository(archiver) as repository:
manifest = Manifest.load(repository, Manifest.NO_OPERATION_CHECK)
Expand Down Expand Up @@ -483,7 +482,7 @@ def wait_for_mountstate(mountpoint, *, mounted, timeout=5):


@contextmanager
def fuse_mount(archiver, location, mountpoint=None, *options, fork=True, os_fork=False, **kwargs):
def fuse_mount(archiver, mountpoint=None, *options, fork=True, os_fork=False, **kwargs):
# For a successful mount, `fork = True` is required for
# the borg mount daemon to work properly or the tests
# will just freeze. Therefore, if argument `fork` is not
Expand All @@ -502,7 +501,7 @@ def fuse_mount(archiver, location, mountpoint=None, *options, fork=True, os_fork
mountpoint = tempfile.mkdtemp()
else:
os.mkdir(mountpoint)
args = [f"--repo={location}", "mount", mountpoint] + list(options)
args = ["mount", mountpoint] + list(options)
if os_fork:
# Do not spawn, but actually (OS) fork.
if os.fork() == 0:
Expand Down
20 changes: 8 additions & 12 deletions src/borg/testsuite/archiver/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,24 @@


def test_bad_filters(archiver):
repo_location = archiver.repository_location
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, f"--repo={repo_location}", "create", "test", "input")
cmd(archiver, f"--repo={repo_location}", "delete", "--first", "1", "--last", "1", fork=True, exit_code=2)
cmd(archiver, "rcreate", RK_ENCRYPTION)
cmd(archiver, "create", "test", "input")
cmd(archiver, "delete", "--first", "1", "--last", "1", fork=True, exit_code=2)


def test_highlander(archiver):
repo_location = archiver.repository_location
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, f"--repo={repo_location}", "create", "--comment", "comment 1", "test-1", __file__)
cmd(archiver, "rcreate", RK_ENCRYPTION)
cmd(archiver, "create", "--comment", "comment 1", "test-1", __file__)
error_msg = "There can be only one"
# Default umask value is 0077
# Test that it works with a one time specified default or custom value
output_default = cmd(archiver, f"--repo={repo_location}", "--umask", "0077", "rlist")
output_default = cmd(archiver, "--umask", "0077", "rlist")
assert error_msg not in output_default
output_custom = cmd(archiver, f"--repo={repo_location}", "--umask", "0007", "rlist")
output_custom = cmd(archiver, "--umask", "0007", "rlist")
assert error_msg not in output_custom
# Test that all combinations of custom and default values fail
for first, second in [("0007", "0007"), ("0007", "0077"), ("0077", "0007"), ("0077", "0077")]:
output_custom = cmd(
archiver, f"--repo={repo_location}", "--umask", first, "--umask", second, "rlist", exit_code=2
)
output_custom = cmd(archiver, "--umask", first, "--umask", second, "rlist", exit_code=2)
assert error_msg in output_custom


Expand Down
4 changes: 2 additions & 2 deletions src/borg/testsuite/archiver/benchmark_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@


def test_benchmark_crud(archiver):
cmd(archiver, f"--repo={archiver.repository_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, "rcreate", RK_ENCRYPTION)
with environment_variable(_BORG_BENCHMARK_CRUD_TEST="YES"):
cmd(archiver, f"--repo={archiver.repository_location}", "benchmark", "crud", archiver.input_path)
cmd(archiver, "benchmark", "crud", archiver.input_path)
80 changes: 35 additions & 45 deletions src/borg/testsuite/archiver/bypass_lock_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,132 +9,122 @@


def test_readonly_check(archiver):
repo_location, repo_path = archiver.repository_location, archiver.repository_path
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_src_archive(archiver, "test")

with read_only(repo_path):
with read_only(archiver.repository_path):
# verify that command normally doesn't work with read-only repo
if archiver.FORK_DEFAULT:
cmd(archiver, f"--repo={repo_location}", "check", "--verify-data", exit_code=EXIT_ERROR)
cmd(archiver, "check", "--verify-data", exit_code=EXIT_ERROR)
else:
with pytest.raises((LockFailed, RemoteRepository.RPCError)) as excinfo:
cmd(archiver, f"--repo={repo_location}", "check", "--verify-data")
cmd(archiver, "check", "--verify-data")
if isinstance(excinfo.value, RemoteRepository.RPCError):
assert excinfo.value.exception_class == "LockFailed"
# verify that command works with read-only repo when using --bypass-lock
cmd(archiver, f"--repo={repo_location}", "check", "--verify-data", "--bypass-lock")
cmd(archiver, "check", "--verify-data", "--bypass-lock")


def test_readonly_diff(archiver):
repo_location, repo_path = archiver.repository_location, archiver.repository_path
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_src_archive(archiver, "a")
create_src_archive(archiver, "b")

with read_only(repo_path):
with read_only(archiver.repository_path):
# verify that command normally doesn't work with read-only repo
if archiver.FORK_DEFAULT:
cmd(archiver, f"--repo={repo_location}", "diff", "a", "b", exit_code=EXIT_ERROR)
cmd(archiver, "diff", "a", "b", exit_code=EXIT_ERROR)
else:
with pytest.raises((LockFailed, RemoteRepository.RPCError)) as excinfo:
cmd(archiver, f"--repo={repo_location}", "diff", "a", "b")
cmd(archiver, "diff", "a", "b")
if isinstance(excinfo.value, RemoteRepository.RPCError):
assert excinfo.value.exception_class == "LockFailed"
# verify that command works with read-only repo when using --bypass-lock
cmd(archiver, f"--repo={repo_location}", "diff", "a", "b", "--bypass-lock")
cmd(archiver, "diff", "a", "b", "--bypass-lock")


def test_readonly_export_tar(archiver):
repo_location, repo_path = archiver.repository_location, archiver.repository_path
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_src_archive(archiver, "test")

with read_only(repo_path):
with read_only(archiver.repository_path):
# verify that command normally doesn't work with read-only repo
if archiver.FORK_DEFAULT:
cmd(archiver, f"--repo={repo_location}", "export-tar", "test", "test.tar", exit_code=EXIT_ERROR)
cmd(archiver, "export-tar", "test", "test.tar", exit_code=EXIT_ERROR)
else:
with pytest.raises((LockFailed, RemoteRepository.RPCError)) as excinfo:
cmd(archiver, f"--repo={repo_location}", "export-tar", "test", "test.tar")
cmd(archiver, "export-tar", "test", "test.tar")
if isinstance(excinfo.value, RemoteRepository.RPCError):
assert excinfo.value.exception_class == "LockFailed"
# verify that command works with read-only repo when using --bypass-lock
cmd(archiver, f"--repo={repo_location}", "export-tar", "test", "test.tar", "--bypass-lock")
cmd(archiver, "export-tar", "test", "test.tar", "--bypass-lock")


def test_readonly_extract(archiver):
repo_location, repo_path = archiver.repository_location, archiver.repository_path
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_src_archive(archiver, "test")

with read_only(repo_path):
with read_only(archiver.repository_path):
# verify that command normally doesn't work with read-only repo
if archiver.FORK_DEFAULT:
cmd(archiver, f"--repo={repo_location}", "extract", "test", exit_code=EXIT_ERROR)
cmd(archiver, "extract", "test", exit_code=EXIT_ERROR)
else:
with pytest.raises((LockFailed, RemoteRepository.RPCError)) as excinfo:
cmd(archiver, f"--repo={repo_location}", "extract", "test")
cmd(archiver, "extract", "test")
if isinstance(excinfo.value, RemoteRepository.RPCError):
assert excinfo.value.exception_class == "LockFailed"
# verify that command works with read-only repo when using --bypass-lock
cmd(archiver, f"--repo={repo_location}", "extract", "test", "--bypass-lock")
cmd(archiver, "extract", "test", "--bypass-lock")


def test_readonly_info(archiver):
repo_location, repo_path = archiver.repository_location, archiver.repository_path
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_src_archive(archiver, "test")

with read_only(repo_path):
with read_only(archiver.repository_path):
# verify that command normally doesn't work with read-only repo
if archiver.FORK_DEFAULT:
cmd(archiver, f"--repo={repo_location}", "rinfo", exit_code=EXIT_ERROR)
cmd(archiver, "rinfo", exit_code=EXIT_ERROR)
else:
with pytest.raises((LockFailed, RemoteRepository.RPCError)) as excinfo:
cmd(archiver, f"--repo={repo_location}", "rinfo")
cmd(archiver, "rinfo")
if isinstance(excinfo.value, RemoteRepository.RPCError):
assert excinfo.value.exception_class == "LockFailed"
# verify that command works with read-only repo when using --bypass-lock
cmd(archiver, f"--repo={repo_location}", "rinfo", "--bypass-lock")
cmd(archiver, "rinfo", "--bypass-lock")


def test_readonly_list(archiver):
repo_location, repo_path = archiver.repository_location, archiver.repository_path
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_src_archive(archiver, "test")

with read_only(repo_path):
with read_only(archiver.repository_path):
# verify that command normally doesn't work with read-only repo
if archiver.FORK_DEFAULT:
cmd(archiver, f"--repo={repo_location}", "rlist", exit_code=EXIT_ERROR)
cmd(archiver, "rlist", exit_code=EXIT_ERROR)
else:
with pytest.raises((LockFailed, RemoteRepository.RPCError)) as excinfo:
cmd(archiver, f"--repo={repo_location}", "rlist")
cmd(archiver, "rlist")
if isinstance(excinfo.value, RemoteRepository.RPCError):
assert excinfo.value.exception_class == "LockFailed"
# verify that command works with read-only repo when using --bypass-lock
cmd(archiver, f"--repo={repo_location}", "rlist", "--bypass-lock")
cmd(archiver, "rlist", "--bypass-lock")


@pytest.mark.skipif(not llfuse, reason="llfuse not installed")
def test_readonly_mount(archiver):
repo_location, repo_path = archiver.repository_location, archiver.repository_path
cmd(archiver, f"--repo={repo_location}", "rcreate", RK_ENCRYPTION)
cmd(archiver, "rcreate", RK_ENCRYPTION)
create_src_archive(archiver, "test")

with read_only(repo_path):
with read_only(archiver.repository_path):
# verify that command normally doesn't work with read-only repo
if archiver.FORK_DEFAULT:
with fuse_mount(archiver, repo_location, exit_code=EXIT_ERROR):
with fuse_mount(archiver, exit_code=EXIT_ERROR):
pass
else:
with pytest.raises((LockFailed, RemoteRepository.RPCError)) as excinfo:
# self.fuse_mount always assumes fork=True, so for this test we have to set fork=False manually
with fuse_mount(archiver, repo_location, fork=False):
with fuse_mount(archiver, fork=False):
pass
if isinstance(excinfo.value, RemoteRepository.RPCError):
assert excinfo.value.exception_class == "LockFailed"
# verify that command works with read-only repo when using --bypass-lock
with fuse_mount(archiver, repo_location, None, "--bypass-lock"):
with fuse_mount(archiver, None, "--bypass-lock"):
pass

0 comments on commit 2e59a70

Please sign in to comment.