aboutsummaryrefslogtreecommitdiffstats
path: root/molecule/default/tests/test_systemd.py
diff options
context:
space:
mode:
authorColin Wilk <colin@wilk.cx>2026-09-01 21:01:05 +0200
committerColin Wilk <colin@wilk.cx>2026-09-01 21:43:57 +0200
commit9c7586c7c3a235672ec6490d1a8bc44a222ce5d1 (patch)
tree8b36c5ee3105da2ae769c0d9cd96683d213c949d /molecule/default/tests/test_systemd.py
parentfa137a92e1084a07608a4008ec0cb891baa76774 (diff)
downloadansible-role-borgbackup-9c7586c7c3a235672ec6490d1a8bc44a222ce5d1.tar.gz
ansible-role-borgbackup-9c7586c7c3a235672ec6490d1a8bc44a222ce5d1.zip
Add borg prune and compact jobs
Run repository retention either after a successful backup or from a dedicated systemd timer. Clean up script generation with templates and expand molecule test coverage. BREAKING CHANGE: Aggregate backup scripts are no longer managed, and state=preset now requires at least one readable included directory.
Diffstat (limited to 'molecule/default/tests/test_systemd.py')
-rw-r--r--molecule/default/tests/test_systemd.py84
1 files changed, 83 insertions, 1 deletions
diff --git a/molecule/default/tests/test_systemd.py b/molecule/default/tests/test_systemd.py
index e68e232..297ae0d 100644
--- a/molecule/default/tests/test_systemd.py
+++ b/molecule/default/tests/test_systemd.py
@@ -8,6 +8,7 @@ testinfra_hosts = [
"borg-client-multi",
"borg-client-nonroot",
"borg-client-multi-keys",
+ "borg-client-transition",
]
CLIENT_CONFIGS = {
@@ -16,18 +17,33 @@ CLIENT_CONFIGS = {
"server": "borg-server",
"schedule": "*-*-* 02:00:00",
"success_exit_status": False,
+ "prune_enabled": True,
+ "prune_timer": False,
},
"borg-client-2": {
"user": "root",
"server": "borg-server-2",
"schedule": "*-*-* 03:00:00",
"success_exit_status": True,
+ "prune_enabled": True,
+ "prune_timer": True,
+ "prune_schedule": "*-*-* 05:00:00",
},
"borg-client-nonroot": {
"user": "backupuser",
"server": "borg-server",
"schedule": "*-*-* 02:00:00",
"success_exit_status": False,
+ "prune_enabled": False,
+ "prune_timer": False,
+ },
+ "borg-client-transition": {
+ "user": "root",
+ "server": "transition-repo",
+ "schedule": "*-*-* 02:00:00",
+ "success_exit_status": False,
+ "prune_enabled": True,
+ "prune_timer": False,
},
}
@@ -85,7 +101,19 @@ class TestSystemdServiceFile:
assert service.contains("[Service]")
assert service.contains("[Install]")
assert service.contains("Type=oneshot")
- assert service.contains("ExecStart=/usr/local/bin/run_borg_backup")
+ assert service.contains(
+ f"ExecStart=/usr/local/bin/run_borg_backup@{config['server']}"
+ )
+ assert (
+ "ExecStart=/usr/local/bin/run_borg_backup\n"
+ not in service.content_string
+ )
+ if config["prune_enabled"] and not config["prune_timer"]:
+ assert service.contains(
+ f"OnSuccess=borg_prune@{config['server']}.service"
+ )
+ else:
+ assert not service.contains("OnSuccess=")
else:
for repo in config["repos"]:
service = host.file(f"/etc/systemd/system/borg_backup@{repo}.service")
@@ -93,6 +121,9 @@ class TestSystemdServiceFile:
assert service.contains("[Service]")
assert service.contains("[Install]")
assert service.contains("Type=oneshot")
+ assert service.contains(
+ f"ExecStart=/usr/local/bin/run_borg_backup@{repo}"
+ )
def test_service_user(self, host, config):
if config["type"] == "single":
@@ -193,3 +224,54 @@ class TestSystemdState:
def test_daemon_reload_ok(self, host, config):
c = host.run("systemctl daemon-reload")
assert c.rc == 0
+
+
+class TestPruneSystemd:
+ def test_prune_service_created_when_pruning_enabled(self, host, config):
+ if config["type"] != "single":
+ return
+
+ service = host.file(
+ f"/etc/systemd/system/borg_prune@{config['server']}.service"
+ )
+ assert service.exists == config["prune_enabled"]
+ if config["prune_enabled"]:
+ assert (
+ f"ExecStart=/usr/local/bin/run_borg_prune@{config['server']}"
+ in service.content_string
+ )
+
+ def test_prune_timer_created_when_configured(self, host, config):
+ if config["type"] != "single" or not config.get("prune_timer"):
+ return
+
+ timer = host.file(f"/etc/systemd/system/borg_prune@{config['server']}.timer")
+ service = host.file(
+ f"/etc/systemd/system/borg_prune@{config['server']}.service"
+ )
+ assert timer.exists
+ assert service.exists
+ assert f"OnCalendar={config['prune_schedule']}" in timer.content_string
+ assert (
+ f"ExecStart=/usr/local/bin/run_borg_prune@{config['server']}"
+ in service.content_string
+ )
+ assert "ExecStart=/usr/local/bin/run_borg_prune\n" not in service.content_string
+ assert "SuccessExitStatus=1 TEMPFAIL" in service.content_string
+
+ def test_prune_timer_not_created_for_after_backup(self, host, config):
+ if config["type"] != "single" or config.get("prune_timer"):
+ return
+
+ timer = host.file(f"/etc/systemd/system/borg_prune@{config['server']}.timer")
+ assert not timer.exists
+
+ def test_transition_host_stale_prune_timer_removed(self, host):
+ hostname = host.backend.get_hostname()
+ if hostname != "borg-client-transition":
+ return
+
+ timer = host.file("/etc/systemd/system/borg_prune@transition-repo.timer")
+ service = host.file("/etc/systemd/system/borg_prune@transition-repo.service")
+ assert not timer.exists
+ assert service.exists