aboutsummaryrefslogtreecommitdiffstats
path: root/molecule/default/tests/test_systemd.py
diff options
context:
space:
mode:
Diffstat (limited to 'molecule/default/tests/test_systemd.py')
-rw-r--r--molecule/default/tests/test_systemd.py317
1 files changed, 113 insertions, 204 deletions
diff --git a/molecule/default/tests/test_systemd.py b/molecule/default/tests/test_systemd.py
index b713c27..94b82c4 100644
--- a/molecule/default/tests/test_systemd.py
+++ b/molecule/default/tests/test_systemd.py
@@ -4,263 +4,172 @@ import pytest
testinfra_hosts = ['borg-client', 'borg-client-2', 'borg-client-multi', 'borg-client-nonroot', 'borg-client-multi-keys']
+CLIENT_CONFIGS = {
+ 'borg-client': {
+ 'user': 'root',
+ 'server': 'borg-server',
+ 'schedule': '*-*-* 02:00:00',
+ 'success_exit_status': False,
+ },
+ 'borg-client-2': {
+ 'user': 'root',
+ 'server': 'borg-server-2',
+ 'schedule': '*-*-* 03:00:00',
+ 'success_exit_status': True,
+ },
+ 'borg-client-nonroot': {
+ 'user': 'backupuser',
+ 'server': 'borg-server',
+ 'schedule': '*-*-* 02:00:00',
+ 'success_exit_status': False,
+ },
+}
-CLIENT_USER_MAP = {
- 'borg-client': 'root',
- 'borg-client-2': 'root',
- 'borg-client-multi': 'root',
- 'borg-client-nonroot': 'backupuser',
- 'borg-client-multi-keys': 'root',
+MULTI_INSTANCE_CONFIGS = {
+ 'borg-client-multi': {
+ 'user': 'root',
+ 'repos': ['configs', 'home-data'],
+ 'schedules': {
+ 'configs': '*-*-* 02:00:00',
+ 'home-data': '*-*-* 04:00:00',
+ },
+ },
+ 'borg-client-multi-keys': {
+ 'user': 'root',
+ 'repos': ['configs-keys', 'home-data-keys'],
+ 'schedules': {
+ 'configs-keys': '*-*-* 02:00:00',
+ 'home-data-keys': '*-*-* 04:00:00',
+ },
+ },
}
-def get_client_user(host):
- hostname = host.backend.get_hostname()
- return CLIENT_USER_MAP.get(hostname, 'root')
+@pytest.fixture
+def config(host):
+ hostname = host.backend.hostname
+ if hostname in CLIENT_CONFIGS:
+ return {'type': 'single', **CLIENT_CONFIGS[hostname]}
+ return {'type': 'multi', **MULTI_INSTANCE_CONFIGS[hostname]}
class TestSystemdServiceFile:
- def test_service_file_exists(self, host):
- hostname = host.backend.get_hostname()
-
- if hostname == 'borg-client-multi':
- service1 = host.file('/etc/systemd/system/borg_backup@configs.service')
- service2 = host.file('/etc/systemd/system/borg_backup@home-data.service')
- assert service1.exists
- assert service1.user == 'root'
- assert service1.group == 'root'
- assert service2.exists
- assert service2.user == 'root'
- assert service2.group == 'root'
- elif hostname == 'borg-client-multi-keys':
- service1 = host.file('/etc/systemd/system/borg_backup@configs-keys.service')
- service2 = host.file('/etc/systemd/system/borg_backup@home-data-keys.service')
- assert service1.exists
- assert service1.user == 'root'
- assert service1.group == 'root'
- assert service2.exists
- assert service2.user == 'root'
- assert service2.group == 'root'
- elif hostname == 'borg-client-nonroot':
- service = host.file('/etc/systemd/system/borg_backup@borg-server.service')
- assert service.exists
- assert service.user == 'root'
- assert service.group == 'root'
- assert service.mode == 0o644
- elif hostname in ('borg-client', 'borg-client-2'):
- service = host.file('/etc/systemd/system/borg_backup@borg-server.service')
+ def test_service_file_exists(self, host, config):
+ if config['type'] == 'single':
+ service = host.file(f"/etc/systemd/system/borg_backup@{config['server']}.service")
assert service.exists
assert service.user == 'root'
assert service.group == 'root'
assert service.mode == 0o644
else:
- pytest.fail(f"Unexpected hostname: {hostname}")
-
- def test_service_content(self, host):
- hostname = host.backend.get_hostname()
-
- if hostname == 'borg-client-multi':
- service1 = host.file('/etc/systemd/system/borg_backup@configs.service')
- service2 = host.file('/etc/systemd/system/borg_backup@home-data.service')
- for service in [service1, service2]:
- assert service.contains('[Unit]')
- assert service.contains('[Service]')
- assert service.contains('[Install]')
- assert service.contains('Type=oneshot')
- elif hostname == 'borg-client-multi-keys':
- service1 = host.file('/etc/systemd/system/borg_backup@configs-keys.service')
- service2 = host.file('/etc/systemd/system/borg_backup@home-data-keys.service')
- for service in [service1, service2]:
- assert service.contains('[Unit]')
- assert service.contains('[Service]')
- assert service.contains('[Install]')
- assert service.contains('Type=oneshot')
- elif hostname == 'borg-client-nonroot':
- service = host.file('/etc/systemd/system/borg_backup@borg-server.service')
- assert service.contains('[Unit]')
- assert service.contains('[Service]')
- assert service.contains('[Install]')
- assert service.contains('Type=oneshot')
- assert service.contains('ExecStart=/usr/local/bin/run_borg_backup')
- elif hostname in ('borg-client', 'borg-client-2'):
- service = host.file('/etc/systemd/system/borg_backup@borg-server.service')
+ for repo in config['repos']:
+ service = host.file(f"/etc/systemd/system/borg_backup@{repo}.service")
+ assert service.exists
+ assert service.user == 'root'
+ assert service.group == 'root'
+
+ def test_service_content(self, host, config):
+ if config['type'] == 'single':
+ service = host.file(f"/etc/systemd/system/borg_backup@{config['server']}.service")
assert service.contains('[Unit]')
assert service.contains('[Service]')
assert service.contains('[Install]')
assert service.contains('Type=oneshot')
assert service.contains('ExecStart=/usr/local/bin/run_borg_backup')
else:
- pytest.fail(f"Unexpected hostname: {hostname}")
-
- def test_service_user(self, host):
- hostname = host.backend.get_hostname()
- client_user = get_client_user(host)
+ for repo in config['repos']:
+ service = host.file(f"/etc/systemd/system/borg_backup@{repo}.service")
+ assert service.contains('[Unit]')
+ assert service.contains('[Service]')
+ assert service.contains('[Install]')
+ assert service.contains('Type=oneshot')
- if hostname == 'borg-client-multi':
- service1 = host.file('/etc/systemd/system/borg_backup@configs.service')
- service2 = host.file('/etc/systemd/system/borg_backup@home-data.service')
- for service in [service1, service2]:
- assert service.contains(f'User={client_user}')
- assert service.contains(f'Group={client_user}')
- elif hostname == 'borg-client-multi-keys':
- service1 = host.file('/etc/systemd/system/borg_backup@configs-keys.service')
- service2 = host.file('/etc/systemd/system/borg_backup@home-data-keys.service')
- for service in [service1, service2]:
- assert service.contains(f'User={client_user}')
- assert service.contains(f'Group={client_user}')
- elif hostname in ('borg-client', 'borg-client-2', 'borg-client-nonroot'):
- service = host.file('/etc/systemd/system/borg_backup@borg-server.service')
- assert service.contains(f'User={client_user}')
- assert service.contains(f'Group={client_user}')
+ def test_service_user(self, host, config):
+ if config['type'] == 'single':
+ service = host.file(f"/etc/systemd/system/borg_backup@{config['server']}.service")
+ assert service.contains(f"User={config['user']}")
+ assert service.contains(f"Group={config['user']}")
else:
- pytest.fail(f"Unexpected hostname: {hostname}")
+ for repo in config['repos']:
+ service = host.file(f"/etc/systemd/system/borg_backup@{repo}.service")
+ assert service.contains(f"User={config['user']}")
+ assert service.contains(f"Group={config['user']}")
- def test_success_exit_status(self, host):
- hostname = host.backend.get_hostname()
- if hostname in ('borg-client-multi', 'borg-client-nonroot', 'borg-client-multi-keys'):
+ def test_success_exit_status(self, host, config):
+ if config['type'] != 'single':
return
-
- service = host.file('/etc/systemd/system/borg_backup@borg-server.service')
+ service = host.file(f"/etc/systemd/system/borg_backup@{config['server']}.service")
assert service.exists
- if hostname == 'borg-client-2':
+ if config['success_exit_status']:
assert service.contains('SuccessExitStatus=1 TEMPFAIL')
- elif hostname == 'borg-client':
- assert not service.contains('SuccessExitStatus=')
else:
- pytest.fail(f"Unexpected hostname: {hostname}")
-
- @pytest.mark.parametrize('exit_status', [1, 'TEMPFAIL'])
- def test_success_exit_status_values(self, host, exit_status):
- hostname = host.backend.get_hostname()
- if hostname == 'borg-client-2':
- service = host.file('/etc/systemd/system/borg_backup@borg-server.service')
- assert service.contains(str(exit_status))
+ assert not service.contains('SuccessExitStatus=')
class TestSystemdTimerFile:
- def test_timer_file_exists(self, host):
- hostname = host.backend.get_hostname()
-
- if hostname == 'borg-client-multi':
- timer1 = host.file('/etc/systemd/system/borg_backup@configs.timer')
- timer2 = host.file('/etc/systemd/system/borg_backup@home-data.timer')
- assert timer1.exists
- assert timer2.exists
- elif hostname == 'borg-client-multi-keys':
- timer1 = host.file('/etc/systemd/system/borg_backup@configs-keys.timer')
- timer2 = host.file('/etc/systemd/system/borg_backup@home-data-keys.timer')
- assert timer1.exists
- assert timer2.exists
- elif hostname in ('borg-client', 'borg-client-2', 'borg-client-nonroot'):
- timer = host.file('/etc/systemd/system/borg_backup@borg-server.timer')
+ def test_timer_file_exists(self, host, config):
+ if config['type'] == 'single':
+ timer = host.file(f"/etc/systemd/system/borg_backup@{config['server']}.timer")
assert timer.exists
assert timer.user == 'root'
assert timer.group == 'root'
assert timer.mode == 0o644
else:
- pytest.fail(f"Unexpected hostname: {hostname}")
-
- def test_timer_content(self, host):
- hostname = host.backend.get_hostname()
+ for repo in config['repos']:
+ timer = host.file(f"/etc/systemd/system/borg_backup@{repo}.timer")
+ assert timer.exists
- if hostname == 'borg-client-multi':
- timer1 = host.file('/etc/systemd/system/borg_backup@configs.timer')
- timer2 = host.file('/etc/systemd/system/borg_backup@home-data.timer')
- for timer in [timer1, timer2]:
- assert timer.contains('[Unit]')
- assert timer.contains('[Timer]')
- assert timer.contains('[Install]')
- assert timer.contains('OnCalendar=')
- assert timer.contains('AccuracySec=')
- elif hostname == 'borg-client-multi-keys':
- timer1 = host.file('/etc/systemd/system/borg_backup@configs-keys.timer')
- timer2 = host.file('/etc/systemd/system/borg_backup@home-data-keys.timer')
- for timer in [timer1, timer2]:
- assert timer.contains('[Unit]')
- assert timer.contains('[Timer]')
- assert timer.contains('[Install]')
- assert timer.contains('OnCalendar=')
- assert timer.contains('AccuracySec=')
- elif hostname in ('borg-client', 'borg-client-2', 'borg-client-nonroot'):
- timer = host.file('/etc/systemd/system/borg_backup@borg-server.timer')
+ def test_timer_content(self, host, config):
+ if config['type'] == 'single':
+ timer = host.file(f"/etc/systemd/system/borg_backup@{config['server']}.timer")
assert timer.contains('[Unit]')
assert timer.contains('[Timer]')
assert timer.contains('[Install]')
assert timer.contains('OnCalendar=')
assert timer.contains('AccuracySec=')
else:
- pytest.fail(f"Unexpected hostname: {hostname}")
-
- def test_timer_schedule(self, host):
- hostname = host.backend.get_hostname()
+ for repo in config['repos']:
+ timer = host.file(f"/etc/systemd/system/borg_backup@{repo}.timer")
+ assert timer.contains('[Unit]')
+ assert timer.contains('[Timer]')
+ assert timer.contains('[Install]')
+ assert timer.contains('OnCalendar=')
+ assert timer.contains('AccuracySec=')
- if hostname == 'borg-client-multi':
- timer1 = host.file('/etc/systemd/system/borg_backup@configs.timer')
- timer2 = host.file('/etc/systemd/system/borg_backup@home-data.timer')
- assert 'OnCalendar=*-*-* 02:00:00' in timer1.content_string
- assert 'OnCalendar=*-*-* 04:00:00' in timer2.content_string
- elif hostname == 'borg-client-multi-keys':
- timer1 = host.file('/etc/systemd/system/borg_backup@configs-keys.timer')
- timer2 = host.file('/etc/systemd/system/borg_backup@home-data-keys.timer')
- assert 'OnCalendar=*-*-* 02:00:00' in timer1.content_string
- assert 'OnCalendar=*-*-* 04:00:00' in timer2.content_string
- elif hostname == 'borg-client-2':
- timer = host.file('/etc/systemd/system/borg_backup@borg-server.timer')
- assert 'OnCalendar=*-*-* 03:00:00' in timer.content_string
- elif hostname in ('borg-client', 'borg-client-nonroot'):
- timer = host.file('/etc/systemd/system/borg_backup@borg-server.timer')
- assert 'OnCalendar=*-*-* 02:00:00' in timer.content_string
+ def test_timer_schedule(self, host, config):
+ if config['type'] == 'single':
+ timer = host.file(f"/etc/systemd/system/borg_backup@{config['server']}.timer")
+ assert f"OnCalendar={config['schedule']}" in timer.content_string
else:
- pytest.fail(f"Unexpected hostname: {hostname}")
+ for repo in config['repos']:
+ timer = host.file(f"/etc/systemd/system/borg_backup@{repo}.timer")
+ assert f"OnCalendar={config['schedules'][repo]}" in timer.content_string
class TestSystemdState:
- def test_timer_enabled(self, host):
- hostname = host.backend.get_hostname()
-
- if hostname == 'borg-client-multi':
- c1 = host.run('systemctl is-enabled borg_backup@configs.timer')
- c2 = host.run('systemctl is-enabled borg_backup@home-data.timer')
- assert c1.rc == 0
- assert c1.stdout.strip() == 'enabled'
- assert c2.rc == 0
- assert c2.stdout.strip() == 'enabled'
- elif hostname == 'borg-client-multi-keys':
- c1 = host.run('systemctl is-enabled borg_backup@configs-keys.timer')
- c2 = host.run('systemctl is-enabled borg_backup@home-data-keys.timer')
- assert c1.rc == 0
- assert c1.stdout.strip() == 'enabled'
- assert c2.rc == 0
- assert c2.stdout.strip() == 'enabled'
- elif hostname in ('borg-client', 'borg-client-2', 'borg-client-nonroot'):
- timer_name = 'borg_backup@borg-server.timer'
+ def test_timer_enabled(self, host, config):
+ if config['type'] == 'single':
+ timer_name = f"borg_backup@{config['server']}.timer"
c = host.run(f"systemctl is-enabled {timer_name}")
assert c.rc == 0
assert c.stdout.strip() == 'enabled'
else:
- pytest.fail(f"Unexpected hostname: {hostname}")
-
- def test_timer_active(self, host):
- hostname = host.backend.get_hostname()
-
- if hostname == 'borg-client-multi':
- c1 = host.run('systemctl is-active borg_backup@configs.timer')
- c2 = host.run('systemctl is-active borg_backup@home-data.timer')
- assert c1.rc == 0
- assert c2.rc == 0
- elif hostname == 'borg-client-multi-keys':
- c1 = host.run('systemctl is-active borg_backup@configs-keys.timer')
- c2 = host.run('systemctl is-active borg_backup@home-data-keys.timer')
- assert c1.rc == 0
- assert c2.rc == 0
- elif hostname in ('borg-client', 'borg-client-2', 'borg-client-nonroot'):
- timer_name = 'borg_backup@borg-server.timer'
+ for repo in config['repos']:
+ c = host.run(f"systemctl is-enabled borg_backup@{repo}.timer")
+ assert c.rc == 0
+ assert c.stdout.strip() == 'enabled'
+
+ def test_timer_active(self, host, config):
+ if config['type'] == 'single':
+ timer_name = f"borg_backup@{config['server']}.timer"
c = host.run(f"systemctl is-active {timer_name}")
assert c.rc == 0
else:
- pytest.fail(f"Unexpected hostname: {hostname}")
+ for repo in config['repos']:
+ c = host.run(f"systemctl is-active borg_backup@{repo}.timer")
+ assert c.rc == 0
- def test_daemon_reload_ok(self, host):
+ def test_daemon_reload_ok(self, host, config):
c = host.run('systemctl daemon-reload')
assert c.rc == 0