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
|
"""Tests for client setup configuration"""
import pytest
testinfra_hosts = ['borg-client', 'borg-client-2', 'borg-client-multi']
class TestSSHSetup:
def test_ssh_directory_exists(self, host):
ssh_dir = host.file('/root/.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
def test_ssh_private_key_exists(self, host):
key = host.file('/root/.ssh/id_rsa')
assert key.exists
assert key.user == 'root'
assert key.group == 'root'
assert key.mode == 0o600
def test_ssh_public_key_exists(self, host):
key = host.file('/root/.ssh/id_rsa.pub')
assert key.exists
assert key.user == 'root'
assert key.group == 'root'
def test_known_hosts_contains_borg_server(self, host):
known_hosts = host.file('/root/.ssh/known_hosts')
assert known_hosts.exists
assert known_hosts.contains('borg-server')
class TestBackupScript:
def test_backup_script_exists(self, host):
hostname = host.backend.get_hostname()
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.mode == 0o711
assert script2.exists
assert script2.user == 'root'
assert script2.group == 'root'
assert script2.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.mode == 0o711
else:
pytest.fail(f"Unexpected hostname: {hostname}")
def test_backup_script_contains_borg_command(self, host):
hostname = host.backend.get_hostname()
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.contains('borg create')
assert script2.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')
else:
pytest.fail(f"Unexpected hostname: {hostname}")
def test_backup_script_contains_compression(self, host):
hostname = host.backend.get_hostname()
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.contains('-C zstd')
assert script2.contains('-C lz4')
elif hostname == 'borg-client-2':
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')
assert script.contains('-C')
assert script.contains('zstd')
else:
pytest.fail(f"Unexpected hostname: {hostname}")
def test_backup_script_contains_repo_path(self, host):
hostname = host.backend.get_hostname()
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.contains('borg@borg-server')
assert script1.contains('/opt/borg/configs')
assert script2.contains('borg@borg-server')
assert script2.contains('/opt/borg/home-data')
elif hostname in ('borg-client', 'borg-client-2'):
script = host.file('/usr/local/bin/run_borg_backup')
assert script.contains('borg@borg-server')
assert script.contains('/opt/borg')
else:
pytest.fail(f"Unexpected hostname: {hostname}")
def test_backup_script_contains_backup_paths(self, host):
hostname = host.backend.get_hostname()
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 '/etc' in script1.content_string
assert '/home' in script2.content_string
assert '--exclude' in script2.content_string
elif hostname in ('borg-client', 'borg-client-2'):
script = host.file('/usr/local/bin/run_borg_backup')
content = script.content_string
assert '/etc' in content or '/home' in content
else:
pytest.fail(f"Unexpected hostname: {hostname}")
def test_backup_script_is_executable(self, host):
hostname = host.backend.get_hostname()
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.mode == 0o711
assert script2.mode == 0o711
elif hostname in ('borg-client', 'borg-client-2'):
script = host.file('/usr/local/bin/run_borg_backup')
assert script.mode == 0o711
else:
pytest.fail(f"Unexpected hostname: {hostname}")
class TestMultiInstanceBaseScript:
def test_base_script_exists(self, host):
hostname = host.backend.get_hostname()
if hostname != 'borg-client-multi':
return
base_script = host.file('/usr/local/bin/run_borg_backup')
assert base_script.exists
assert base_script.user == 'root'
assert base_script.mode == 0o711
def test_base_script_contains_both_blocks(self, host):
hostname = host.backend.get_hostname()
if hostname != 'borg-client-multi':
return
base_script = host.file('/usr/local/bin/run_borg_backup')
content = base_script.content_string
assert 'borg-server/configs' in content
assert 'borg-server/home-data' in content
def test_base_script_contains_both_repos(self, host):
hostname = host.backend.get_hostname()
if hostname != 'borg-client-multi':
return
base_script = host.file('/usr/local/bin/run_borg_backup')
content = base_script.content_string
assert '/opt/borg/configs' in content
assert '/opt/borg/home-data' in content
def test_base_script_contains_both_compressions(self, host):
hostname = host.backend.get_hostname()
if hostname != 'borg-client-multi':
return
base_script = host.file('/usr/local/bin/run_borg_backup')
content = base_script.content_string
assert '-C zstd' in content
assert '-C lz4' in content
def test_base_script_two_borg_create_commands(self, host):
hostname = host.backend.get_hostname()
if hostname != 'borg-client-multi':
return
base_script = host.file('/usr/local/bin/run_borg_backup')
content = base_script.content_string
assert content.count('borg create') == 2
|