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
|
"""Tests for systemd service and timer configuration"""
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,
},
}
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',
},
},
}
@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, 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:
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:
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')
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:
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, config):
if config['type'] != 'single':
return
service = host.file(f"/etc/systemd/system/borg_backup@{config['server']}.service")
assert service.exists
if config['success_exit_status']:
assert service.contains('SuccessExitStatus=1 TEMPFAIL')
else:
assert not service.contains('SuccessExitStatus=')
class TestSystemdTimerFile:
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:
for repo in config['repos']:
timer = host.file(f"/etc/systemd/system/borg_backup@{repo}.timer")
assert timer.exists
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:
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=')
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:
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, 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:
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:
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, config):
c = host.run('systemctl daemon-reload')
assert c.rc == 0
|