2019-10-01 09:12:18 -04:00
|
|
|
"""Test alt"""
|
|
|
|
import os
|
|
|
|
import string
|
2023-07-10 10:14:33 -04:00
|
|
|
|
2019-10-01 09:12:18 -04:00
|
|
|
import py
|
|
|
|
import pytest
|
|
|
|
import utils
|
|
|
|
|
|
|
|
TEST_PATHS = [utils.ALT_FILE1, utils.ALT_FILE2, utils.ALT_DIR]
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("ds1_copy")
|
|
|
|
@pytest.mark.parametrize("yadm_alt", [True, False], ids=["alt", "worktree"])
|
2019-10-01 09:12:18 -04:00
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"tracked,encrypt,exclude",
|
|
|
|
[
|
2019-10-01 09:12:18 -04:00
|
|
|
(False, False, False),
|
|
|
|
(True, False, False),
|
|
|
|
(False, True, False),
|
|
|
|
(False, True, True),
|
2023-07-10 15:43:17 -04:00
|
|
|
],
|
|
|
|
ids=["untracked", "tracked", "encrypted", "excluded"],
|
|
|
|
)
|
|
|
|
def test_alt_source(runner, paths, tracked, encrypt, exclude, yadm_alt):
|
2019-10-01 09:12:18 -04:00
|
|
|
"""Test yadm alt operates on all expected sources of alternates"""
|
2020-11-17 15:57:34 -05:00
|
|
|
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
utils.create_alt_files(
|
2023-07-10 15:43:17 -04:00
|
|
|
paths, "##default", tracked=tracked, encrypt=encrypt, exclude=exclude, yadm_alt=yadm_alt, yadm_dir=yadm_dir
|
|
|
|
)
|
|
|
|
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-10-01 09:12:18 -04:00
|
|
|
linked = utils.parse_alt_output(run.out)
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
basepath = yadm_dir.join("alt") if yadm_alt else paths.work
|
2019-10-10 09:23:36 -04:00
|
|
|
|
2019-10-01 09:12:18 -04:00
|
|
|
for link_path in TEST_PATHS:
|
2023-07-10 15:43:17 -04:00
|
|
|
source_file_content = link_path + "##default"
|
2019-10-10 09:23:36 -04:00
|
|
|
source_file = basepath.join(source_file_content)
|
|
|
|
link_file = paths.work.join(link_path)
|
2019-10-01 09:12:18 -04:00
|
|
|
if tracked or (encrypt and not exclude):
|
2019-10-10 09:23:36 -04:00
|
|
|
assert link_file.islink()
|
2019-11-17 14:07:04 -05:00
|
|
|
target = py.path.local(os.path.realpath(link_file))
|
2019-10-01 09:12:18 -04:00
|
|
|
if target.isfile():
|
2019-10-10 09:23:36 -04:00
|
|
|
assert link_file.read() == source_file_content
|
|
|
|
assert str(source_file) in linked
|
2019-10-01 09:12:18 -04:00
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert link_file.join(utils.CONTAINED).read() == source_file_content
|
2019-10-10 09:23:36 -04:00
|
|
|
assert str(source_file) in linked
|
2019-10-01 09:12:18 -04:00
|
|
|
else:
|
2019-10-10 09:23:36 -04:00
|
|
|
assert not link_file.exists()
|
|
|
|
assert str(source_file) not in linked
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("ds1_copy")
|
|
|
|
@pytest.mark.parametrize("yadm_alt", [True, False], ids=["alt", "worktree"])
|
2019-11-17 14:07:04 -05:00
|
|
|
def test_relative_link(runner, paths, yadm_alt):
|
|
|
|
"""Confirm links created are relative"""
|
2020-11-17 15:57:34 -05:00
|
|
|
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
|
2019-11-17 14:07:04 -05:00
|
|
|
|
|
|
|
utils.create_alt_files(
|
2023-07-10 15:43:17 -04:00
|
|
|
paths, "##default", tracked=True, encrypt=False, exclude=False, yadm_alt=yadm_alt, yadm_dir=yadm_dir
|
|
|
|
)
|
|
|
|
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
|
2019-11-17 14:07:04 -05:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-11-17 14:07:04 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
basepath = yadm_dir.join("alt") if yadm_alt else paths.work
|
2019-11-17 14:07:04 -05:00
|
|
|
|
|
|
|
for link_path in TEST_PATHS:
|
2023-07-10 15:43:17 -04:00
|
|
|
source_file_content = link_path + "##default"
|
2019-11-17 14:07:04 -05:00
|
|
|
source_file = basepath.join(source_file_content)
|
|
|
|
link_file = paths.work.join(link_path)
|
|
|
|
link = link_file.readlink()
|
2023-07-10 15:43:17 -04:00
|
|
|
relpath = os.path.relpath(source_file, start=os.path.dirname(link_file))
|
2019-11-17 14:07:04 -05:00
|
|
|
assert link == relpath
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("ds1_copy")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"suffix",
|
|
|
|
[
|
|
|
|
"##default",
|
|
|
|
"##default,e.txt",
|
|
|
|
"##default,extension.txt",
|
|
|
|
"##a.$tst_arch",
|
|
|
|
"##arch.$tst_arch",
|
|
|
|
"##o.$tst_sys",
|
|
|
|
"##os.$tst_sys",
|
|
|
|
"##d.$tst_distro",
|
|
|
|
"##distro.$tst_distro",
|
|
|
|
"##f.$tst_distro_family",
|
|
|
|
"##distro_family.$tst_distro_family",
|
|
|
|
"##c.$tst_class",
|
|
|
|
"##class.$tst_class",
|
|
|
|
"##h.$tst_host",
|
|
|
|
"##hostname.$tst_host",
|
|
|
|
"##u.$tst_user",
|
|
|
|
"##user.$tst_user",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_alt_conditions(runner, paths, tst_arch, tst_sys, tst_distro, tst_distro_family, tst_host, tst_user, suffix):
|
2019-10-01 09:12:18 -04:00
|
|
|
"""Test conditions supported by yadm alt"""
|
2020-11-17 15:57:34 -05:00
|
|
|
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
# set the class
|
2023-07-10 15:43:17 -04:00
|
|
|
tst_class = "testclass"
|
|
|
|
utils.set_local(paths, "class", tst_class + ".before")
|
|
|
|
utils.set_local(paths, "class", tst_class, add=True)
|
|
|
|
utils.set_local(paths, "class", tst_class + ".after", add=True)
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
suffix = string.Template(suffix).substitute(
|
2020-02-21 20:10:39 -05:00
|
|
|
tst_arch=tst_arch,
|
2019-10-01 09:12:18 -04:00
|
|
|
tst_sys=tst_sys,
|
2019-10-06 12:04:21 -04:00
|
|
|
tst_distro=tst_distro,
|
2022-01-17 14:46:31 -05:00
|
|
|
tst_distro_family=tst_distro_family,
|
2019-10-01 09:12:18 -04:00
|
|
|
tst_class=tst_class,
|
|
|
|
tst_host=tst_host,
|
|
|
|
tst_user=tst_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.create_alt_files(paths, suffix)
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-10-01 09:12:18 -04:00
|
|
|
linked = utils.parse_alt_output(run.out)
|
|
|
|
|
|
|
|
for link_path in TEST_PATHS:
|
|
|
|
source_file = link_path + suffix
|
|
|
|
assert paths.work.join(link_path).islink()
|
2019-11-17 14:07:04 -05:00
|
|
|
target = py.path.local(os.path.realpath(paths.work.join(link_path)))
|
2019-10-01 09:12:18 -04:00
|
|
|
if target.isfile():
|
|
|
|
assert paths.work.join(link_path).read() == source_file
|
|
|
|
assert str(paths.work.join(source_file)) in linked
|
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert paths.work.join(link_path).join(utils.CONTAINED).read() == source_file
|
2019-10-01 09:12:18 -04:00
|
|
|
assert str(paths.work.join(source_file)) in linked
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("ds1_copy")
|
|
|
|
@pytest.mark.parametrize("kind", ["default", "", None, "envtpl", "j2cli", "j2", "esh"])
|
2019-10-12 10:14:45 -04:00
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"label",
|
|
|
|
[
|
|
|
|
"t",
|
|
|
|
"template",
|
|
|
|
"yadm",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_alt_templates(runner, paths, kind, label):
|
2019-10-01 09:12:18 -04:00
|
|
|
"""Test templates supported by yadm alt"""
|
2020-11-17 15:57:34 -05:00
|
|
|
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
suffix = f"##{label}.{kind}"
|
2019-10-12 10:14:45 -04:00
|
|
|
if kind is None:
|
2023-07-10 15:43:17 -04:00
|
|
|
suffix = f"##{label}"
|
2019-10-01 09:12:18 -04:00
|
|
|
utils.create_alt_files(paths, suffix)
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-10-01 09:12:18 -04:00
|
|
|
created = utils.parse_alt_output(run.out, linked=False)
|
|
|
|
|
|
|
|
for created_path in TEST_PATHS:
|
|
|
|
if created_path != utils.ALT_DIR:
|
|
|
|
source_file = created_path + suffix
|
|
|
|
assert paths.work.join(created_path).isfile()
|
|
|
|
assert paths.work.join(created_path).read().strip() == source_file
|
|
|
|
assert str(paths.work.join(source_file)) in created
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("ds1_copy")
|
|
|
|
@pytest.mark.parametrize("autoalt", [None, "true", "false"])
|
2020-11-17 15:38:31 -05:00
|
|
|
def test_auto_alt(runner, yadm_cmd, paths, autoalt):
|
2019-10-01 09:12:18 -04:00
|
|
|
"""Test auto alt"""
|
|
|
|
|
|
|
|
# set the value of auto-alt
|
|
|
|
if autoalt:
|
2023-07-10 15:43:17 -04:00
|
|
|
os.system(" ".join(yadm_cmd("config", "yadm.auto-alt", autoalt)))
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
utils.create_alt_files(paths, "##default")
|
|
|
|
run = runner(yadm_cmd("status"))
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-10-01 09:12:18 -04:00
|
|
|
linked = utils.parse_alt_output(run.out)
|
|
|
|
|
|
|
|
for link_path in TEST_PATHS:
|
2023-07-10 15:43:17 -04:00
|
|
|
source_file = link_path + "##default"
|
|
|
|
if autoalt == "false":
|
2019-10-01 09:12:18 -04:00
|
|
|
assert not paths.work.join(link_path).exists()
|
|
|
|
else:
|
|
|
|
assert paths.work.join(link_path).islink()
|
2023-07-10 15:43:17 -04:00
|
|
|
target = py.path.local(os.path.realpath(paths.work.join(link_path)))
|
2019-10-01 09:12:18 -04:00
|
|
|
if target.isfile():
|
|
|
|
assert paths.work.join(link_path).read() == source_file
|
|
|
|
# no linking output when run via auto-alt
|
|
|
|
assert str(paths.work.join(source_file)) not in linked
|
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert paths.work.join(link_path).join(utils.CONTAINED).read() == source_file
|
2019-10-01 09:12:18 -04:00
|
|
|
# no linking output when run via auto-alt
|
|
|
|
assert str(paths.work.join(source_file)) not in linked
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("ds1_copy")
|
2020-11-17 15:38:31 -05:00
|
|
|
def test_stale_link_removal(runner, yadm_cmd, paths):
|
2019-10-01 09:12:18 -04:00
|
|
|
"""Stale links to alternative files are removed
|
|
|
|
|
|
|
|
This test ensures that when an already linked alternative becomes invalid
|
|
|
|
due to a change in class, the alternate link is removed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# set the class
|
2023-07-10 15:43:17 -04:00
|
|
|
tst_class = "testclass"
|
|
|
|
utils.set_local(paths, "class", tst_class)
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
# create files which match the test class
|
2023-07-10 15:43:17 -04:00
|
|
|
utils.create_alt_files(paths, f"##class.{tst_class}")
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
# run alt to trigger linking
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(yadm_cmd("alt"))
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-10-01 09:12:18 -04:00
|
|
|
linked = utils.parse_alt_output(run.out)
|
|
|
|
|
|
|
|
# assert the proper linking has occurred
|
|
|
|
for stale_path in TEST_PATHS:
|
2023-07-10 15:43:17 -04:00
|
|
|
source_file = stale_path + "##class." + tst_class
|
2019-10-01 09:12:18 -04:00
|
|
|
assert paths.work.join(stale_path).islink()
|
2019-11-17 14:07:04 -05:00
|
|
|
target = py.path.local(os.path.realpath(paths.work.join(stale_path)))
|
2019-10-01 09:12:18 -04:00
|
|
|
if target.isfile():
|
|
|
|
assert paths.work.join(stale_path).read() == source_file
|
|
|
|
assert str(paths.work.join(source_file)) in linked
|
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert paths.work.join(stale_path).join(utils.CONTAINED).read() == source_file
|
2019-10-01 09:12:18 -04:00
|
|
|
assert str(paths.work.join(source_file)) in linked
|
|
|
|
|
|
|
|
# change the class so there are no valid alternates
|
2023-07-10 15:43:17 -04:00
|
|
|
utils.set_local(paths, "class", "changedclass")
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
# run alt to trigger linking
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(yadm_cmd("alt"))
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2019-10-01 09:12:18 -04:00
|
|
|
linked = utils.parse_alt_output(run.out)
|
|
|
|
|
|
|
|
# assert the linking is removed
|
|
|
|
for stale_path in TEST_PATHS:
|
2023-07-10 15:43:17 -04:00
|
|
|
source_file = stale_path + "##class." + tst_class
|
2019-10-01 09:12:18 -04:00
|
|
|
assert not paths.work.join(stale_path).exists()
|
|
|
|
assert str(paths.work.join(source_file)) not in linked
|
2019-10-11 08:20:03 -04:00
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("ds1_repo_copy")
|
2020-11-17 15:38:31 -05:00
|
|
|
def test_template_overwrite_symlink(runner, yadm_cmd, paths, tst_sys):
|
2019-10-11 08:20:03 -04:00
|
|
|
"""Remove symlinks before processing a template
|
|
|
|
|
|
|
|
If a symlink is in the way of the output of a template, the target of the
|
|
|
|
symlink will get the template content. To prevent this, the symlink should
|
|
|
|
be removed just before processing a template.
|
|
|
|
"""
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
target = paths.work.join(f"test_link##os.{tst_sys}")
|
|
|
|
target.write("target")
|
2019-10-11 08:20:03 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
link = paths.work.join("test_link")
|
2019-10-11 08:20:03 -04:00
|
|
|
link.mksymlinkto(target, absolute=1)
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
template = paths.work.join("test_link##template.default")
|
|
|
|
template.write("test-data")
|
2019-10-11 08:20:03 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(yadm_cmd("add", target, template))
|
2019-10-11 08:20:03 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert run.out == ""
|
2019-10-11 08:20:03 -04:00
|
|
|
assert not link.islink()
|
2023-07-10 15:43:17 -04:00
|
|
|
assert target.read().strip() == "target"
|
|
|
|
assert link.read().strip() == "test-data"
|
2019-10-10 09:23:36 -04:00
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("ds1_copy")
|
|
|
|
@pytest.mark.parametrize("style", ["symlink", "template"])
|
2019-10-12 19:22:02 -04:00
|
|
|
def test_ensure_alt_path(runner, paths, style):
|
|
|
|
"""Test that directories are created before making alternates"""
|
2020-11-17 15:57:34 -05:00
|
|
|
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
|
2023-07-10 15:43:17 -04:00
|
|
|
suffix = "default" if style == "symlink" else "template"
|
|
|
|
filename = "a/b/c/file"
|
|
|
|
source = yadm_dir.join(f"alt/{filename}##{suffix}")
|
|
|
|
source.write("test-data", ensure=True)
|
|
|
|
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "add", source])
|
2019-10-12 19:22:02 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert run.out == ""
|
|
|
|
assert paths.work.join(filename).read().strip() == "test-data"
|
2019-10-12 19:22:02 -04:00
|
|
|
|
2023-11-08 21:21:26 -05:00
|
|
|
@pytest.mark.usefixtures("ds1_repo_copy")
|
|
|
|
@pytest.mark.parametrize("readonly", [None, "true", "false"])
|
|
|
|
def test_template_readonly(runner, yadm_cmd, paths, tst_sys, readonly):
|
|
|
|
"""Remove write permission for template result file.
|
|
|
|
|
|
|
|
If the `yadm.template-read-only` configuration is not set to false,
|
|
|
|
the resulting file from processing a template should has no write permission.
|
|
|
|
"""
|
|
|
|
# set the value of template read-only
|
|
|
|
if readonly:
|
|
|
|
runner(yadm_cmd("config", "yadm.template-read-only", readonly))
|
|
|
|
|
|
|
|
utils.create_alt_files(paths, f"##template.default")
|
|
|
|
run = runner(yadm_cmd("alt"))
|
|
|
|
|
|
|
|
for stale_path in [utils.ALT_FILE1, utils.ALT_FILE2]:
|
|
|
|
write_perm_mask = os.stat(paths.work.join(stale_path)).st_mode & 0o222
|
|
|
|
if readonly == "false":
|
|
|
|
assert write_perm_mask > 0
|
|
|
|
else:
|
|
|
|
assert write_perm_mask == 0
|
|
|
|
|
2019-10-12 19:22:02 -04:00
|
|
|
|
2019-10-10 09:23:36 -04:00
|
|
|
def setup_standard_yadm_dir(paths):
|
|
|
|
"""Configure a yadm home within the work tree"""
|
2023-07-10 15:43:17 -04:00
|
|
|
std_yadm_dir = paths.work.mkdir(".config").mkdir("yadm")
|
|
|
|
std_yadm_data = paths.work.mkdir(".local").mkdir("share").mkdir("yadm")
|
|
|
|
std_yadm_data.join("repo.git").mksymlinkto(paths.repo, absolute=1)
|
|
|
|
std_yadm_dir.join("encrypt").mksymlinkto(paths.encrypt, absolute=1)
|
2020-11-17 15:57:34 -05:00
|
|
|
return std_yadm_dir, std_yadm_data
|