move restic test script to common variable
This commit is contained in:
parent
5e076ebcf4
commit
f998689e9b
1 changed files with 98 additions and 194 deletions
|
|
@ -8,6 +8,102 @@ let
|
||||||
base = testLib.base [
|
base = testLib.base [
|
||||||
../../modules/blocks/restic.nix
|
../../modules/blocks/restic.nix
|
||||||
];
|
];
|
||||||
|
|
||||||
|
commonTestScript = ''
|
||||||
|
from dictdiffer import diff
|
||||||
|
|
||||||
|
def list_files(dir):
|
||||||
|
files_and_content = {}
|
||||||
|
|
||||||
|
files = machine.succeed(f"""
|
||||||
|
find {dir} -type f
|
||||||
|
""").split("\n")[:-1]
|
||||||
|
|
||||||
|
for f in files:
|
||||||
|
content = machine.succeed(f"""
|
||||||
|
cat {f}
|
||||||
|
""").strip()
|
||||||
|
files_and_content[f] = content
|
||||||
|
|
||||||
|
return files_and_content
|
||||||
|
|
||||||
|
def assert_files(dir, files):
|
||||||
|
result = list(diff(list_files(dir), files))
|
||||||
|
if len(result) > 0:
|
||||||
|
raise Exception("Unexpected files:", result)
|
||||||
|
|
||||||
|
with subtest("Create initial content"):
|
||||||
|
machine.succeed("""
|
||||||
|
mkdir -p /opt/files/A
|
||||||
|
mkdir -p /opt/files/B
|
||||||
|
|
||||||
|
echo repoA_fileA_1 > /opt/files/A/fileA
|
||||||
|
echo repoA_fileB_1 > /opt/files/A/fileB
|
||||||
|
echo repoB_fileA_1 > /opt/files/B/fileA
|
||||||
|
echo repoB_fileB_1 > /opt/files/B/fileB
|
||||||
|
|
||||||
|
# chown :backup -R /opt/files
|
||||||
|
""")
|
||||||
|
|
||||||
|
assert_files("/opt/files", {
|
||||||
|
'/opt/files/B/fileA': 'repoB_fileA_1',
|
||||||
|
'/opt/files/B/fileB': 'repoB_fileB_1',
|
||||||
|
'/opt/files/A/fileA': 'repoA_fileA_1',
|
||||||
|
'/opt/files/A/fileB': 'repoA_fileB_1',
|
||||||
|
})
|
||||||
|
|
||||||
|
with subtest("First backup in repo A"):
|
||||||
|
machine.succeed("systemctl start restic-backups-testinstance_opt_repos_A")
|
||||||
|
|
||||||
|
with subtest("New content"):
|
||||||
|
machine.succeed("""
|
||||||
|
echo repoA_fileA_2 > /opt/files/A/fileA
|
||||||
|
echo repoA_fileB_2 > /opt/files/A/fileB
|
||||||
|
echo repoB_fileA_2 > /opt/files/B/fileA
|
||||||
|
echo repoB_fileB_2 > /opt/files/B/fileB
|
||||||
|
""")
|
||||||
|
|
||||||
|
assert_files("/opt/files", {
|
||||||
|
'/opt/files/B/fileA': 'repoB_fileA_2',
|
||||||
|
'/opt/files/B/fileB': 'repoB_fileB_2',
|
||||||
|
'/opt/files/A/fileA': 'repoA_fileA_2',
|
||||||
|
'/opt/files/A/fileB': 'repoA_fileB_2',
|
||||||
|
})
|
||||||
|
|
||||||
|
with subtest("Second backup in repo B"):
|
||||||
|
machine.succeed("systemctl start restic-backups-testinstance_opt_repos_B")
|
||||||
|
|
||||||
|
with subtest("Delete content"):
|
||||||
|
machine.succeed("""
|
||||||
|
rm -r /opt/files/A /opt/files/B
|
||||||
|
""")
|
||||||
|
|
||||||
|
assert_files("/opt/files", {})
|
||||||
|
|
||||||
|
with subtest("Restore initial content from repo A"):
|
||||||
|
machine.succeed("""
|
||||||
|
restic-testinstance_opt_repos_A restore latest -t /
|
||||||
|
""")
|
||||||
|
|
||||||
|
assert_files("/opt/files", {
|
||||||
|
'/opt/files/B/fileA': 'repoB_fileA_1',
|
||||||
|
'/opt/files/B/fileB': 'repoB_fileB_1',
|
||||||
|
'/opt/files/A/fileA': 'repoA_fileA_1',
|
||||||
|
'/opt/files/A/fileB': 'repoA_fileB_1',
|
||||||
|
})
|
||||||
|
|
||||||
|
with subtest("Restore initial content from repo B"):
|
||||||
|
machine.succeed("""
|
||||||
|
restic-testinstance_opt_repos_B restore latest -t /
|
||||||
|
""")
|
||||||
|
|
||||||
|
assert_files("/opt/files", {
|
||||||
|
'/opt/files/B/fileA': 'repoB_fileA_2',
|
||||||
|
'/opt/files/B/fileB': 'repoB_fileB_2',
|
||||||
|
'/opt/files/A/fileA': 'repoA_fileA_2',
|
||||||
|
'/opt/files/A/fileB': 'repoA_fileB_2',
|
||||||
|
})
|
||||||
|
'';
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
backupAndRestoreRoot = pkgs.testers.runNixOSTest {
|
backupAndRestoreRoot = pkgs.testers.runNixOSTest {
|
||||||
|
|
@ -77,103 +173,7 @@ in
|
||||||
extraPythonPackages = p: [ p.dictdiffer ];
|
extraPythonPackages = p: [ p.dictdiffer ];
|
||||||
skipTypeCheck = true;
|
skipTypeCheck = true;
|
||||||
|
|
||||||
testScript = { nodes, ... }: let
|
testScript = commonTestScript;
|
||||||
instanceCfg = nodes.machine.shb.restic.instances."testinstance";
|
|
||||||
in ''
|
|
||||||
from dictdiffer import diff
|
|
||||||
|
|
||||||
def list_files(dir):
|
|
||||||
files_and_content = {}
|
|
||||||
|
|
||||||
files = machine.succeed(f"""
|
|
||||||
find {dir} -type f
|
|
||||||
""").split("\n")[:-1]
|
|
||||||
|
|
||||||
for f in files:
|
|
||||||
content = machine.succeed(f"""
|
|
||||||
cat {f}
|
|
||||||
""").strip()
|
|
||||||
files_and_content[f] = content
|
|
||||||
|
|
||||||
return files_and_content
|
|
||||||
|
|
||||||
def assert_files(dir, files):
|
|
||||||
result = list(diff(list_files(dir), files))
|
|
||||||
if len(result) > 0:
|
|
||||||
raise Exception("Unexpected files:", result)
|
|
||||||
|
|
||||||
with subtest("Create initial content"):
|
|
||||||
machine.succeed("""
|
|
||||||
mkdir -p /opt/files/A
|
|
||||||
mkdir -p /opt/files/B
|
|
||||||
|
|
||||||
echo repoA_fileA_1 > /opt/files/A/fileA
|
|
||||||
echo repoA_fileB_1 > /opt/files/A/fileB
|
|
||||||
echo repoB_fileA_1 > /opt/files/B/fileA
|
|
||||||
echo repoB_fileB_1 > /opt/files/B/fileB
|
|
||||||
|
|
||||||
# chown :backup -R /opt/files
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {
|
|
||||||
'/opt/files/B/fileA': 'repoB_fileA_1',
|
|
||||||
'/opt/files/B/fileB': 'repoB_fileB_1',
|
|
||||||
'/opt/files/A/fileA': 'repoA_fileA_1',
|
|
||||||
'/opt/files/A/fileB': 'repoA_fileB_1',
|
|
||||||
})
|
|
||||||
|
|
||||||
with subtest("First backup in repo A"):
|
|
||||||
machine.succeed("systemctl start restic-backups-testinstance_opt_repos_A")
|
|
||||||
|
|
||||||
with subtest("New content"):
|
|
||||||
machine.succeed("""
|
|
||||||
echo repoA_fileA_2 > /opt/files/A/fileA
|
|
||||||
echo repoA_fileB_2 > /opt/files/A/fileB
|
|
||||||
echo repoB_fileA_2 > /opt/files/B/fileA
|
|
||||||
echo repoB_fileB_2 > /opt/files/B/fileB
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {
|
|
||||||
'/opt/files/B/fileA': 'repoB_fileA_2',
|
|
||||||
'/opt/files/B/fileB': 'repoB_fileB_2',
|
|
||||||
'/opt/files/A/fileA': 'repoA_fileA_2',
|
|
||||||
'/opt/files/A/fileB': 'repoA_fileB_2',
|
|
||||||
})
|
|
||||||
|
|
||||||
with subtest("Second backup in repo B"):
|
|
||||||
machine.succeed("systemctl start restic-backups-testinstance_opt_repos_B")
|
|
||||||
|
|
||||||
with subtest("Delete content"):
|
|
||||||
machine.succeed("""
|
|
||||||
rm -r /opt/files/A /opt/files/B
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {})
|
|
||||||
|
|
||||||
with subtest("Restore initial content from repo A"):
|
|
||||||
machine.succeed("""
|
|
||||||
restic-testinstance_opt_repos_A restore latest -t /
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {
|
|
||||||
'/opt/files/B/fileA': 'repoB_fileA_1',
|
|
||||||
'/opt/files/B/fileB': 'repoB_fileB_1',
|
|
||||||
'/opt/files/A/fileA': 'repoA_fileA_1',
|
|
||||||
'/opt/files/A/fileB': 'repoA_fileB_1',
|
|
||||||
})
|
|
||||||
|
|
||||||
with subtest("Restore initial content from repo B"):
|
|
||||||
machine.succeed("""
|
|
||||||
restic-testinstance_opt_repos_B restore latest -t /
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {
|
|
||||||
'/opt/files/B/fileA': 'repoB_fileA_2',
|
|
||||||
'/opt/files/B/fileB': 'repoB_fileB_2',
|
|
||||||
'/opt/files/A/fileA': 'repoA_fileA_2',
|
|
||||||
'/opt/files/A/fileB': 'repoA_fileB_2',
|
|
||||||
})
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
backupAndRestoreUser = pkgs.testers.runNixOSTest {
|
backupAndRestoreUser = pkgs.testers.runNixOSTest {
|
||||||
|
|
@ -243,102 +243,6 @@ in
|
||||||
extraPythonPackages = p: [ p.dictdiffer ];
|
extraPythonPackages = p: [ p.dictdiffer ];
|
||||||
skipTypeCheck = true;
|
skipTypeCheck = true;
|
||||||
|
|
||||||
testScript = { nodes, ... }: let
|
testScript = commonTestScript;
|
||||||
instanceCfg = nodes.machine.shb.restic.instances."testinstance";
|
|
||||||
in ''
|
|
||||||
from dictdiffer import diff
|
|
||||||
|
|
||||||
def list_files(dir):
|
|
||||||
files_and_content = {}
|
|
||||||
|
|
||||||
files = machine.succeed(f"""
|
|
||||||
find {dir} -type f
|
|
||||||
""").split("\n")[:-1]
|
|
||||||
|
|
||||||
for f in files:
|
|
||||||
content = machine.succeed(f"""
|
|
||||||
cat {f}
|
|
||||||
""").strip()
|
|
||||||
files_and_content[f] = content
|
|
||||||
|
|
||||||
return files_and_content
|
|
||||||
|
|
||||||
def assert_files(dir, files):
|
|
||||||
result = list(diff(list_files(dir), files))
|
|
||||||
if len(result) > 0:
|
|
||||||
raise Exception("Unexpected files:", result)
|
|
||||||
|
|
||||||
with subtest("Create initial content"):
|
|
||||||
machine.succeed("""
|
|
||||||
mkdir -p /opt/files/A
|
|
||||||
mkdir -p /opt/files/B
|
|
||||||
|
|
||||||
echo repoA_fileA_1 > /opt/files/A/fileA
|
|
||||||
echo repoA_fileB_1 > /opt/files/A/fileB
|
|
||||||
echo repoB_fileA_1 > /opt/files/B/fileA
|
|
||||||
echo repoB_fileB_1 > /opt/files/B/fileB
|
|
||||||
|
|
||||||
# chown :backup -R /opt/files
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {
|
|
||||||
'/opt/files/B/fileA': 'repoB_fileA_1',
|
|
||||||
'/opt/files/B/fileB': 'repoB_fileB_1',
|
|
||||||
'/opt/files/A/fileA': 'repoA_fileA_1',
|
|
||||||
'/opt/files/A/fileB': 'repoA_fileB_1',
|
|
||||||
})
|
|
||||||
|
|
||||||
with subtest("First backup in repo A"):
|
|
||||||
machine.succeed("systemctl start restic-backups-testinstance_opt_repos_A")
|
|
||||||
|
|
||||||
with subtest("New content"):
|
|
||||||
machine.succeed("""
|
|
||||||
echo repoA_fileA_2 > /opt/files/A/fileA
|
|
||||||
echo repoA_fileB_2 > /opt/files/A/fileB
|
|
||||||
echo repoB_fileA_2 > /opt/files/B/fileA
|
|
||||||
echo repoB_fileB_2 > /opt/files/B/fileB
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {
|
|
||||||
'/opt/files/B/fileA': 'repoB_fileA_2',
|
|
||||||
'/opt/files/B/fileB': 'repoB_fileB_2',
|
|
||||||
'/opt/files/A/fileA': 'repoA_fileA_2',
|
|
||||||
'/opt/files/A/fileB': 'repoA_fileB_2',
|
|
||||||
})
|
|
||||||
|
|
||||||
with subtest("Second backup in repo B"):
|
|
||||||
machine.succeed("systemctl start restic-backups-testinstance_opt_repos_B")
|
|
||||||
|
|
||||||
with subtest("Delete content"):
|
|
||||||
machine.succeed("""
|
|
||||||
rm -r /opt/files/A /opt/files/B
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {})
|
|
||||||
|
|
||||||
with subtest("Restore initial content from repo A"):
|
|
||||||
machine.succeed("""
|
|
||||||
restic-testinstance_opt_repos_A restore latest -t /
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {
|
|
||||||
'/opt/files/B/fileA': 'repoB_fileA_1',
|
|
||||||
'/opt/files/B/fileB': 'repoB_fileB_1',
|
|
||||||
'/opt/files/A/fileA': 'repoA_fileA_1',
|
|
||||||
'/opt/files/A/fileB': 'repoA_fileB_1',
|
|
||||||
})
|
|
||||||
|
|
||||||
with subtest("Restore initial content from repo B"):
|
|
||||||
machine.succeed("""
|
|
||||||
restic-testinstance_opt_repos_B restore latest -t /
|
|
||||||
""")
|
|
||||||
|
|
||||||
assert_files("/opt/files", {
|
|
||||||
'/opt/files/B/fileA': 'repoB_fileA_2',
|
|
||||||
'/opt/files/B/fileB': 'repoB_fileB_2',
|
|
||||||
'/opt/files/A/fileA': 'repoA_fileA_2',
|
|
||||||
'/opt/files/A/fileB': 'repoA_fileB_2',
|
|
||||||
})
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue