diff options
| author | Colin Wilk <colin@wilk.cx> | 2026-06-27 21:25:37 +0200 |
|---|---|---|
| committer | Colin Wilk <colin@wilk.cx> | 2026-06-27 21:25:37 +0200 |
| commit | cce7d2d258292c283d64ce8da14a6d1e366b564d (patch) | |
| tree | fe9de437636098066c6bf160d5787b6b35659835 /molecule/default/tests/test_client_setup.py | |
| parent | a22ff185f9836023817f9d4f8df3157b948f8cf2 (diff) | |
| download | ansible-role-borgbackup-cce7d2d258292c283d64ce8da14a6d1e366b564d.tar.gz ansible-role-borgbackup-cce7d2d258292c283d64ce8da14a6d1e366b564d.zip | |
Add support for non-root backup clients
Diffstat (limited to 'molecule/default/tests/test_client_setup.py')
| -rw-r--r-- | molecule/default/tests/test_client_setup.py | 103 |
1 files changed, 82 insertions, 21 deletions
diff --git a/molecule/default/tests/test_client_setup.py b/molecule/default/tests/test_client_setup.py index 25bf8e2..290e3dc 100644 --- a/molecule/default/tests/test_client_setup.py +++ b/molecule/default/tests/test_client_setup.py @@ -1,33 +1,72 @@ """Tests for client setup configuration""" import pytest -testinfra_hosts = ['borg-client', 'borg-client-2', 'borg-client-multi'] +testinfra_hosts = ['borg-client', 'borg-client-2', 'borg-client-multi', 'borg-client-nonroot'] + + +CLIENT_USER_MAP = { + 'borg-client': 'root', + 'borg-client-2': 'root', + 'borg-client-multi': 'root', + 'borg-client-nonroot': 'backupuser', +} + +CLIENT_SSH_KEY_TYPE_MAP = { + 'borg-client': 'rsa', + 'borg-client-2': 'ed25519', + 'borg-client-multi': 'rsa', + 'borg-client-nonroot': 'rsa', +} + + +def get_client_user(host): + hostname = host.backend.get_hostname() + return CLIENT_USER_MAP.get(hostname, 'root') + + +def get_client_ssh_key_type(host): + hostname = host.backend.get_hostname() + return CLIENT_SSH_KEY_TYPE_MAP.get(hostname, 'rsa') + + +def get_client_home(host): + user = get_client_user(host) + return f'/home/{user}' if user != 'root' else '/root' class TestSSHSetup: def test_ssh_directory_exists(self, host): - ssh_dir = host.file('/root/.ssh') + client_home = get_client_home(host) + client_user = get_client_user(host) + ssh_dir = host.file(f'{client_home}/.ssh') assert ssh_dir.exists assert ssh_dir.is_directory - assert ssh_dir.user == 'root' - assert ssh_dir.group == 'root' - assert ssh_dir.mode == 0o640 + assert ssh_dir.user == client_user + assert ssh_dir.group == client_user + assert ssh_dir.mode == 0o700 def test_ssh_private_key_exists(self, host): - key = host.file('/root/.ssh/id_rsa') + client_home = get_client_home(host) + client_user = get_client_user(host) + key_type = get_client_ssh_key_type(host) + key = host.file(f'{client_home}/.ssh/id_{key_type}') assert key.exists - assert key.user == 'root' - assert key.group == 'root' + assert key.user == client_user + assert key.group == client_user assert key.mode == 0o600 def test_ssh_public_key_exists(self, host): - key = host.file('/root/.ssh/id_rsa.pub') + client_home = get_client_home(host) + client_user = get_client_user(host) + key_type = get_client_ssh_key_type(host) + key = host.file(f'{client_home}/.ssh/id_{key_type}.pub') assert key.exists - assert key.user == 'root' - assert key.group == 'root' + assert key.user == client_user + assert key.group == client_user def test_known_hosts_contains_borg_server(self, host): - known_hosts = host.file('/root/.ssh/known_hosts') + client_home = get_client_home(host) + known_hosts = host.file(f'{client_home}/.ssh/known_hosts') assert known_hosts.exists assert known_hosts.contains('borg-server') @@ -35,23 +74,30 @@ class TestSSHSetup: class TestBackupScript: def test_backup_script_exists(self, host): hostname = host.backend.get_hostname() + client_user = get_client_user(host) if hostname == 'borg-client-multi': script1 = host.file('/usr/local/bin/run_borg_backup@configs') script2 = host.file('/usr/local/bin/run_borg_backup@home-data') assert script1.exists - assert script1.user == 'root' - assert script1.group == 'root' + assert script1.user == client_user + assert script1.group == client_user assert script1.mode == 0o711 assert script2.exists - assert script2.user == 'root' - assert script2.group == 'root' + assert script2.user == client_user + assert script2.group == client_user assert script2.mode == 0o711 + elif hostname == 'borg-client-nonroot': + script = host.file('/usr/local/bin/run_borg_backup@borg-server') + assert script.exists + assert script.user == client_user + assert script.group == client_user + assert script.mode == 0o711 elif hostname in ('borg-client', 'borg-client-2'): script = host.file('/usr/local/bin/run_borg_backup') assert script.exists - assert script.user == 'root' - assert script.group == 'root' + assert script.user == client_user + assert script.group == client_user assert script.mode == 0o711 else: pytest.fail(f"Unexpected hostname: {hostname}") @@ -64,6 +110,9 @@ class TestBackupScript: script2 = host.file('/usr/local/bin/run_borg_backup@home-data') assert script1.contains('borg create') assert script2.contains('borg create') + elif hostname == 'borg-client-nonroot': + script = host.file('/usr/local/bin/run_borg_backup@borg-server') + assert script.contains('borg create') elif hostname in ('borg-client', 'borg-client-2'): script = host.file('/usr/local/bin/run_borg_backup') assert script.contains('borg create') @@ -82,8 +131,8 @@ class TestBackupScript: script = host.file('/usr/local/bin/run_borg_backup') assert script.contains('-C') assert script.contains('lz4') - elif hostname == 'borg-client': - script = host.file('/usr/local/bin/run_borg_backup') + elif hostname in ('borg-client', 'borg-client-nonroot'): + script = host.file('/usr/local/bin/run_borg_backup') if hostname == 'borg-client' else host.file('/usr/local/bin/run_borg_backup@borg-server') assert script.contains('-C') assert script.contains('zstd') else: @@ -99,6 +148,10 @@ class TestBackupScript: assert script1.contains('/opt/borg/configs') assert script2.contains('borg@borg-server') assert script2.contains('/opt/borg/home-data') + elif hostname == 'borg-client-nonroot': + script = host.file('/usr/local/bin/run_borg_backup@borg-server') + assert script.contains('borg@borg-server') + assert script.contains('/opt/borg') elif hostname in ('borg-client', 'borg-client-2'): script = host.file('/usr/local/bin/run_borg_backup') assert script.contains('borg@borg-server') @@ -115,6 +168,10 @@ class TestBackupScript: assert '/etc' in script1.content_string assert '/home' in script2.content_string assert '--exclude' in script2.content_string + elif hostname == 'borg-client-nonroot': + script = host.file('/usr/local/bin/run_borg_backup@borg-server') + content = script.content_string + assert '/etc' in content elif hostname in ('borg-client', 'borg-client-2'): script = host.file('/usr/local/bin/run_borg_backup') content = script.content_string @@ -130,6 +187,9 @@ class TestBackupScript: script2 = host.file('/usr/local/bin/run_borg_backup@home-data') assert script1.mode == 0o711 assert script2.mode == 0o711 + elif hostname == 'borg-client-nonroot': + script = host.file('/usr/local/bin/run_borg_backup@borg-server') + assert script.mode == 0o711 elif hostname in ('borg-client', 'borg-client-2'): script = host.file('/usr/local/bin/run_borg_backup') assert script.mode == 0o711 @@ -143,9 +203,10 @@ class TestMultiInstanceBaseScript: if hostname != 'borg-client-multi': return + client_user = get_client_user(host) base_script = host.file('/usr/local/bin/run_borg_backup') assert base_script.exists - assert base_script.user == 'root' + assert base_script.user == client_user assert base_script.mode == 0o711 def test_base_script_contains_both_blocks(self, host): |