aboutsummaryrefslogtreecommitdiffstats
path: root/molecule/default/tests/test_client_setup.py
blob: 290e3dcfd1435f15f8b7f7753315f5db7972bdb9 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Tests for client setup 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',
}

CLIENT_SSH_KEY_TYPE_MAP = {
    'borg-client': 'rsa',
    'borg-client-2': 'ed25519',
    'borg-client-multi': 'rsa',
    'borg-client-nonroot': 'rsa',
}


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


def get_client_ssh_key_type(host):
    hostname = host.backend.get_hostname()
    return CLIENT_SSH_KEY_TYPE_MAP.get(hostname, 'rsa')


def get_client_home(host):
    user = get_client_user(host)
    return f'/home/{user}' if user != 'root' else '/root'


class TestSSHSetup:
    def test_ssh_directory_exists(self, host):
        client_home = get_client_home(host)
        client_user = get_client_user(host)
        ssh_dir = host.file(f'{client_home}/.ssh')
        assert ssh_dir.exists
        assert ssh_dir.is_directory
        assert ssh_dir.user == client_user
        assert ssh_dir.group == client_user
        assert ssh_dir.mode == 0o700

    def test_ssh_private_key_exists(self, host):
        client_home = get_client_home(host)
        client_user = get_client_user(host)
        key_type = get_client_ssh_key_type(host)
        key = host.file(f'{client_home}/.ssh/id_{key_type}')
        assert key.exists
        assert key.user == client_user
        assert key.group == client_user
        assert key.mode == 0o600

    def test_ssh_public_key_exists(self, host):
        client_home = get_client_home(host)
        client_user = get_client_user(host)
        key_type = get_client_ssh_key_type(host)
        key = host.file(f'{client_home}/.ssh/id_{key_type}.pub')
        assert key.exists
        assert key.user == client_user
        assert key.group == client_user

    def test_known_hosts_contains_borg_server(self, host):
        client_home = get_client_home(host)
        known_hosts = host.file(f'{client_home}/.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()
        client_user = get_client_user(host)

        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 == client_user
            assert script1.group == client_user
            assert script1.mode == 0o711
            assert script2.exists
            assert script2.user == client_user
            assert script2.group == client_user
            assert script2.mode == 0o711
        elif hostname == 'borg-client-nonroot':
            script = host.file('/usr/local/bin/run_borg_backup@borg-server')
            assert script.exists
            assert script.user == client_user
            assert script.group == client_user
            assert script.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 == client_user
            assert script.group == client_user
            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 == 'borg-client-nonroot':
            script = host.file('/usr/local/bin/run_borg_backup@borg-server')
            assert script.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 in ('borg-client', 'borg-client-nonroot'):
            script = host.file('/usr/local/bin/run_borg_backup') if hostname == 'borg-client' else host.file('/usr/local/bin/run_borg_backup@borg-server')
            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 == 'borg-client-nonroot':
            script = host.file('/usr/local/bin/run_borg_backup@borg-server')
            assert script.contains('borg@borg-server')
            assert script.contains('/opt/borg')
        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 == 'borg-client-nonroot':
            script = host.file('/usr/local/bin/run_borg_backup@borg-server')
            content = script.content_string
            assert '/etc' in content
        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 == 'borg-client-nonroot':
            script = host.file('/usr/local/bin/run_borg_backup@borg-server')
            assert script.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

        client_user = get_client_user(host)
        base_script = host.file('/usr/local/bin/run_borg_backup')
        assert base_script.exists
        assert base_script.user == client_user
        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