aboutsummaryrefslogtreecommitdiffstats
path: root/molecule/default/tests/test_systemd.py
blob: 7b7b2ab397037eeb4349443e9d377923471ce42c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""Tests for systemd service and timer configuration"""

import pytest

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',
}


def get_client_user(host):
    hostname = host.backend.get_hostname()
    return CLIENT_USER_MAP.get(hostname, 'root')


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-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')
            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-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')
            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)

        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 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}')
        else:
            pytest.fail(f"Unexpected hostname: {hostname}")

    def test_success_exit_status(self, host):
        hostname = host.backend.get_hostname()
        if hostname in ('borg-client-multi', 'borg-client-nonroot'):
            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', 'borg-client-nonroot'):
            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', 'borg-client-nonroot'):
            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 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
        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', 'borg-client-nonroot'):
            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', 'borg-client-nonroot'):
            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