diff options
Diffstat (limited to 'molecule/default/tests/test_systemd.py')
| -rw-r--r-- | molecule/default/tests/test_systemd.py | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/molecule/default/tests/test_systemd.py b/molecule/default/tests/test_systemd.py new file mode 100644 index 0000000..9421959 --- /dev/null +++ b/molecule/default/tests/test_systemd.py @@ -0,0 +1,168 @@ +"""Tests for systemd service and timer configuration""" + +import pytest + +testinfra_hosts = ['borg-client', 'borg-client-2', 'borg-client-multi'] + + +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 in ('borg-client', 'borg-client-2'): + 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 + 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 in ('borg-client', 'borg-client-2'): + 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') + else: + pytest.fail(f"Unexpected hostname: {hostname}") + + def test_success_exit_status(self, host): + hostname = host.backend.get_hostname() + if hostname == 'borg-client-multi': + return + + service = host.file('/etc/systemd/system/borg_backup@borg-server.service') + assert service.exists + + if hostname == 'borg-client-2': + 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)) + + +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 in ('borg-client', 'borg-client-2'): + timer = host.file('/etc/systemd/system/borg_backup@borg-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() + + 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 in ('borg-client', 'borg-client-2'): + timer = host.file('/etc/systemd/system/borg_backup@borg-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() + + 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-2': + timer = host.file('/etc/systemd/system/borg_backup@borg-server.timer') + assert 'OnCalendar=*-*-* 03:00:00' in timer.content_string + elif hostname == 'borg-client': + timer = host.file('/etc/systemd/system/borg_backup@borg-server.timer') + assert 'OnCalendar=*-*-* 02:00:00' in timer.content_string + else: + pytest.fail(f"Unexpected hostname: {hostname}") + + +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 in ('borg-client', 'borg-client-2'): + timer_name = 'borg_backup@borg-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 in ('borg-client', 'borg-client-2'): + timer_name = 'borg_backup@borg-server.timer' + c = host.run(f"systemctl is-active {timer_name}") + assert c.rc == 0 + else: + pytest.fail(f"Unexpected hostname: {hostname}") + + def test_daemon_reload_ok(self, host): + c = host.run('systemctl daemon-reload') + assert c.rc == 0 |