File: test_config.py

package info (click to toggle)
borgmatic 2.0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,752 kB
  • sloc: python: 58,506; sh: 150; makefile: 8; javascript: 5
file content (321 lines) | stat: -rw-r--r-- 11,200 bytes parent folder | download
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from subprocess import CalledProcessError

import pytest
from flexmock import flexmock

from borgmatic.hooks.data_source import config as module


def test_resolve_database_option_uses_config_value():
    data_source = {'option': 'original_value', 'restore_option': 'restore_value'}

    assert module.resolve_database_option('option', data_source) == 'original_value'


def test_resolve_database_option_with_restore_uses_restore_value():
    data_source = {'option': 'original_value', 'restore_option': 'restore_value'}

    assert module.resolve_database_option('option', data_source, restore=True) == 'restore_value'


def test_resolve_database_option_with_connection_params_uses_connection_params_value():
    data_source = {'option': 'original_value', 'restore_option': 'restore_value'}
    connection_params = {'option': 'connection_value'}

    assert (
        module.resolve_database_option('option', data_source, connection_params)
        == 'connection_value'
    )


def test_resolve_database_option_with_restore_and_connection_params_uses_connection_params_value():
    data_source = {'option': 'original_value', 'restore_option': 'restore_value'}
    connection_params = {'option': 'connection_value'}

    assert (
        module.resolve_database_option('option', data_source, connection_params, restore=True)
        == 'connection_value'
    )


def test_resolve_database_option_with_hostname_uses_hostname_specific_function():
    data_source = {'hostname': 'original_value'}
    connection_params = {'hostname': 'connection_value'}

    flexmock(module).should_receive('get_hostname_from_config').and_return('special_value').once()

    assert (
        module.resolve_database_option('hostname', data_source, connection_params, restore=True)
        == 'special_value'
    )


def test_get_hostname_from_config_gets_container_ip():
    data_source = {
        'container': 'original_container',
        'hostname': 'original_hostname',
        'restore_container': 'restore_container',
        'restore_hostname': 'restore_hostname',
    }

    flexmock(module).should_receive('get_ip_from_container').with_args(
        'original_container'
    ).and_return('container_ip_1')

    assert module.get_hostname_from_config(data_source) == 'container_ip_1'


def test_get_hostname_from_config_gets_connection_params_container_ip():
    data_source = {
        'container': 'original_container',
        'hostname': 'original_hostname',
        'restore_container': 'restore_container',
        'restore_hostname': 'restore_hostname',
    }
    connection_params = {'container': 'connection_container', 'hostname': 'connection_hostname'}

    flexmock(module).should_receive('get_ip_from_container').with_args('original_container').never()
    flexmock(module).should_receive('get_ip_from_container').with_args(
        'connection_container'
    ).and_return('container_ip_2')

    assert module.get_hostname_from_config(data_source, connection_params) == 'container_ip_2'


def test_get_hostname_from_config_gets_restore_container_ip():
    data_source = {
        'container': 'original_container',
        'hostname': 'original_hostname',
        'restore_container': 'restore_container',
        'restore_hostname': 'restore_hostname',
    }

    flexmock(module).should_receive('get_ip_from_container').with_args('original_container').never()
    flexmock(module).should_receive('get_ip_from_container').with_args(
        'restore_container'
    ).and_return('container_ip_3')

    assert module.get_hostname_from_config(data_source, restore=True) == 'container_ip_3'


def test_get_ip_from_container_without_engines_errors():
    flexmock(module.shutil).should_receive('which').and_return(None).and_return(None)

    with pytest.raises(ValueError):
        module.get_ip_from_container('yolo')


def test_get_ip_from_container_parses_top_level_ip_address():
    flexmock(module.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')

    flexmock(module).should_receive('execute_command_and_capture_output').and_return(
        '{"IPAddress": "1.2.3.4"}'
    )

    assert module.get_ip_from_container('yolo') == '1.2.3.4'


def test_get_ip_from_container_parses_network_ip_address():
    flexmock(module.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')

    flexmock(module).should_receive('execute_command_and_capture_output').and_return(
        '{"Networks": {"my_network": {"IPAddress": "5.6.7.8"}}}'
    )

    assert module.get_ip_from_container('yolo') == '5.6.7.8'


def test_get_ip_from_container_without_container_errors():
    flexmock(module.shutil).should_receive('which').and_return('/usr/bin/podman')
    flexmock(module).should_receive('execute_command_and_capture_output').and_raise(
        CalledProcessError, 1, ['/usr/bin/podman', 'inspect', 'yolo'], None, 'No such object'
    )

    with pytest.raises(CalledProcessError):
        module.get_ip_from_container('does not exist')


def test_get_ip_from_container_without_network_errors():
    flexmock(module.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')

    flexmock(module).should_receive('execute_command_and_capture_output').and_return('{}')

    with pytest.raises(ValueError) as exc_info:
        module.get_ip_from_container('yolo')

    assert 'Could not determine ip address for container' in str(exc_info.value)


def test_get_ip_from_container_with_broken_output_errors():
    flexmock(module.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')

    flexmock(module).should_receive('execute_command_and_capture_output').and_return('abc')

    with pytest.raises(ValueError) as exc_info:
        module.get_ip_from_container('yolo')

    assert 'Could not decode JSON output' in str(exc_info.value)


def test_inject_pattern_prepends_pattern_in_list():
    patterns = [
        module.borgmatic.borg.pattern.Pattern('/etc'),
        module.borgmatic.borg.pattern.Pattern('/var'),
    ]

    module.inject_pattern(
        patterns,
        module.borgmatic.borg.pattern.Pattern(
            '/foo/bar',
            type=module.borgmatic.borg.pattern.Pattern_type.EXCLUDE,
        ),
    )

    assert patterns == [
        module.borgmatic.borg.pattern.Pattern(
            '/foo/bar',
            type=module.borgmatic.borg.pattern.Pattern_type.EXCLUDE,
        ),
        module.borgmatic.borg.pattern.Pattern('/etc'),
        module.borgmatic.borg.pattern.Pattern('/var'),
    ]


def test_inject_pattern_with_root_pattern_prepends_it_along_with_corresponding_include_pattern():
    patterns = [
        module.borgmatic.borg.pattern.Pattern('/etc'),
        module.borgmatic.borg.pattern.Pattern('/var'),
    ]

    module.inject_pattern(
        patterns,
        module.borgmatic.borg.pattern.Pattern('/foo/bar'),
    )

    assert patterns == [
        module.borgmatic.borg.pattern.Pattern('/foo/bar'),
        module.borgmatic.borg.pattern.Pattern(
            '/foo/bar',
            type=module.borgmatic.borg.pattern.Pattern_type.INCLUDE,
        ),
        module.borgmatic.borg.pattern.Pattern('/etc'),
        module.borgmatic.borg.pattern.Pattern('/var'),
    ]


def test_get_last_pattern_index_with_ordered_subset_patterns_finds_last_one():
    patterns = [
        module.borgmatic.borg.pattern.Pattern('/foo'),
        module.borgmatic.borg.pattern.Pattern('/bar'),
        module.borgmatic.borg.pattern.Pattern('/baz'),
        module.borgmatic.borg.pattern.Pattern('/quux'),
    ]
    patterns_subset = [
        module.borgmatic.borg.pattern.Pattern('/bar'),
        module.borgmatic.borg.pattern.Pattern('/baz'),
    ]

    assert module.get_last_pattern_index(patterns, patterns_subset) == 2


def test_get_last_pattern_index_with_unordered_subset_patterns_finds_last_one():
    patterns = [
        module.borgmatic.borg.pattern.Pattern('/foo'),
        module.borgmatic.borg.pattern.Pattern('/bar'),
        module.borgmatic.borg.pattern.Pattern('/baz'),
        module.borgmatic.borg.pattern.Pattern('/quux'),
    ]
    patterns_subset = [
        module.borgmatic.borg.pattern.Pattern('/baz'),
        module.borgmatic.borg.pattern.Pattern('/bar'),
    ]

    assert module.get_last_pattern_index(patterns, patterns_subset) == 2


def test_get_last_pattern_index_with_unknown_subset_patterns_skips_it():
    patterns = [
        module.borgmatic.borg.pattern.Pattern('/foo'),
        module.borgmatic.borg.pattern.Pattern('/bar'),
        module.borgmatic.borg.pattern.Pattern('/baz'),
        module.borgmatic.borg.pattern.Pattern('/quux'),
    ]
    patterns_subset = [
        module.borgmatic.borg.pattern.Pattern('/baz'),
        module.borgmatic.borg.pattern.Pattern('/unknown'),
        module.borgmatic.borg.pattern.Pattern('/bar'),
    ]

    assert module.get_last_pattern_index(patterns, patterns_subset) == 2


def test_replace_pattern_swaps_out_pattern_in_place():
    patterns = [
        module.borgmatic.borg.pattern.Pattern('/etc'),
        module.borgmatic.borg.pattern.Pattern('/var'),
        module.borgmatic.borg.pattern.Pattern('/lib'),
    ]

    module.replace_pattern(
        patterns,
        module.borgmatic.borg.pattern.Pattern('/var'),
        module.borgmatic.borg.pattern.Pattern(
            '/foo/bar',
            type=module.borgmatic.borg.pattern.Pattern_type.EXCLUDE,
        ),
        0,
    )

    assert patterns == [
        module.borgmatic.borg.pattern.Pattern('/etc'),
        module.borgmatic.borg.pattern.Pattern(
            '/foo/bar',
            type=module.borgmatic.borg.pattern.Pattern_type.EXCLUDE,
        ),
        module.borgmatic.borg.pattern.Pattern('/lib'),
    ]


def test_replace_pattern_with_unknown_pattern_falls_back_to_injecting():
    patterns = [
        module.borgmatic.borg.pattern.Pattern('/etc'),
        module.borgmatic.borg.pattern.Pattern('/var'),
        module.borgmatic.borg.pattern.Pattern('/lib'),
    ]
    flexmock(module).should_receive('inject_pattern').with_args(
        patterns, module.borgmatic.borg.pattern.Pattern('/foo/bar')
    ).once()

    module.replace_pattern(
        patterns,
        module.borgmatic.borg.pattern.Pattern('/unknown'),
        module.borgmatic.borg.pattern.Pattern('/foo/bar'),
        0,
    )


def test_replace_pattern_with_root_pattern_swaps_it_in_along_with_corresponding_include_pattern():
    patterns = [
        module.borgmatic.borg.pattern.Pattern('/etc'),
        module.borgmatic.borg.pattern.Pattern('/var'),
        module.borgmatic.borg.pattern.Pattern('/lib'),
        module.borgmatic.borg.pattern.Pattern('/run'),
    ]

    module.replace_pattern(
        patterns,
        module.borgmatic.borg.pattern.Pattern('/var'),
        module.borgmatic.borg.pattern.Pattern('/foo/bar'),
        2,
    )

    assert patterns == [
        module.borgmatic.borg.pattern.Pattern('/etc'),
        module.borgmatic.borg.pattern.Pattern('/foo/bar'),
        module.borgmatic.borg.pattern.Pattern('/lib'),
        module.borgmatic.borg.pattern.Pattern(
            '/foo/bar',
            type=module.borgmatic.borg.pattern.Pattern_type.INCLUDE,
        ),
        module.borgmatic.borg.pattern.Pattern('/run'),
    ]