-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_configuration.py
406 lines (328 loc) · 11.1 KB
/
test_configuration.py
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from __future__ import annotations
import logging
import os
import pathlib
import subprocess
from unittest import mock
import pytest
import zocalo.configuration
sample_configuration = """
version: 1
graylog:
plugin: graylog
protocol: UDP
host: localhost
port: 12201
constants:
plugin: storage
order:
- cream
- jam
oxford_comma: undecided
sane-constants:
plugin: storage
oxford_comma: yes
units: metric
environments:
default: live
live:
plugins:
- constants
- sane-constants
partial:
plugins:
- constants
alias: partial
part-2:
- sane-constants
empty: {}
"""
def _assert_configuration_is_empty(zc):
assert zc.environments == frozenset({})
assert "0 environments" in str(zc)
assert "0 plugin configurations" in str(zc)
def test_return_empty_configuration_if_no_path_specified():
with mock.patch.dict(os.environ, {"ZOCALO_CONFIG": ""}):
zc = zocalo.configuration.from_file()
_assert_configuration_is_empty(zc)
def test_loading_minimal_valid_configuration():
zc = zocalo.configuration.from_string("version: 1")
_assert_configuration_is_empty(zc)
def test_cannot_load_unversioned_yaml_files():
with pytest.raises(zocalo.ConfigurationError, match="Invalid configuration"):
zocalo.configuration.from_string("value: 1")
def test_cannot_load_unknown_configuration_file_versions():
with pytest.raises(zocalo.ConfigurationError, match="not understand"):
zocalo.configuration.from_string("version: 0")
def test_loading_minimal_valid_configuration_from_file(tmp_path):
config_file = tmp_path.joinpath("config.yml")
config_file.write_text("version: 1")
zc = zocalo.configuration.from_file(os.fspath(config_file))
_assert_configuration_is_empty(zc)
zc = zocalo.configuration.from_file(config_file)
_assert_configuration_is_empty(zc)
with mock.patch.dict(os.environ, {"ZOCALO_CONFIG": os.fspath(config_file)}):
zc = zocalo.configuration.from_file()
_assert_configuration_is_empty(zc)
def test_cannot_load_missing_file(tmp_path):
with pytest.raises(zocalo.ConfigurationError, match="not found"):
zocalo.configuration.from_file(tmp_path / "missing.yml")
def test_cannot_load_invalid_file(tmp_path):
config = tmp_path / "invalid.yml"
config.write_text("x: y: z:")
with pytest.raises(zocalo.ConfigurationError, match="invalid.yml"):
zocalo.configuration.from_file(config)
def test_loading_sample_configuration():
zc = zocalo.configuration.from_string(sample_configuration)
assert zc.environments == frozenset(
{"live", "partial", "part-2", "empty", "alias", "default"}
)
assert "6 environments" in str(zc)
assert "3 plugin configurations" in str(zc)
def test_cannot_load_inconsistent_configuration():
with pytest.raises(zocalo.ConfigurationError):
zocalo.configuration.from_string(
"""
version: 1
environments:
failure:
- unreferenced-plugin
"""
)
def test_detect_circular_aliasing_in_environment_configuration():
with pytest.raises(zocalo.ConfigurationError, match="circular"):
zocalo.configuration.from_string(
"""
version: 1
environments:
broken: circular
circular: broken
"""
)
def test_detect_undefined_alias_target_in_environment_configuration():
with pytest.raises(zocalo.ConfigurationError, match="undefined"):
zocalo.configuration.from_string(
"""
version: 1
environments:
broken: unresolvable-alias
"""
)
def test_cannot_load_configuration_where_environments_specifies_plugin_as_string():
with pytest.raises(zocalo.ConfigurationError, match="invalid-spec"):
zocalo.configuration.from_string(
"""
version: 1
environments:
invalid-spec: constants
constants:
plugin: storage
"""
)
def test_cannot_activate_missing_environment():
zc = zocalo.configuration.from_string("version: 1")
with pytest.raises(ValueError):
zc.activate_environment("live")
assert zc.active_environments == ()
assert "live" not in str(zc)
def test_activate_an_aliased_environment():
zc = zocalo.configuration.from_string(sample_configuration)
zc.activate_environment("default")
assert zc.active_environments == ("default",)
assert "default" in str(zc)
def test_activate_an_empty_environment():
zc = zocalo.configuration.from_string(sample_configuration)
zc.activate_environment("empty")
assert zc.active_environments == ("empty",)
assert "empty" in str(zc)
def test_activate_one_environment():
zc = zocalo.configuration.from_string(sample_configuration)
zc.activate_environment("live")
assert zc.active_environments == ("live",)
with pytest.raises(AttributeError):
zc.active_environments = ("this-should-not-be-writeable",)
assert "live" in str(zc)
def test_activate_two_environments():
zc = zocalo.configuration.from_string(sample_configuration)
zc.activate_environment("partial")
zc.activate_environment("part-2")
assert zc.active_environments == ("partial", "part-2")
assert "partial" in str(zc)
assert "part-2" in str(zc)
def test_activate_multiple_environments():
zc = zocalo.configuration.from_string(sample_configuration)
e = zc.activate(envs=["partial", "part-2"])
assert e == ("partial", "part-2")
assert zc.active_environments == ("partial", "part-2")
assert "partial" in str(zc)
assert "part-2" in str(zc)
def test_activate_additional_environments():
zc = zocalo.configuration.from_string(sample_configuration)
zc.activate_environment("default")
e = zc.activate(envs=["partial", "part-2"])
assert e == ("partial", "part-2")
assert zc.active_environments == ("default", "partial", "part-2")
assert "partial" in str(zc)
assert "part-2" in str(zc)
def test_activate_default_environment():
zc = zocalo.configuration.from_string(sample_configuration)
e = zc.activate([])
assert e == ("default",)
assert zc.active_environments == ("default",)
assert "default" in str(zc)
def test_activate_call_honours_default_flag():
zc = zocalo.configuration.from_string(sample_configuration)
e = zc.activate([], default=False)
assert e == ()
assert zc.active_environments == ()
assert "default" not in str(zc)
def test_activate_call_works_without_default_environment():
zc = zocalo.configuration.from_string("version: 1")
e = zc.activate([])
assert e == ()
assert zc.active_environments == ()
@pytest.mark.parametrize("name", ("include", "environments", "version"))
def test_environment_can_not_reference_reserved_name(name):
with pytest.raises(zocalo.ConfigurationError, match="reserved"):
zocalo.configuration.from_string(
f"""
version: 1
environments:
test:
- {name}
"""
)
def test_unknown_plugin_definition_triggers_a_warning(caplog):
unique_plugin_name = "testcase_for_an_unknown_plugin"
with caplog.at_level(logging.WARNING):
zc = zocalo.configuration.from_string(
f"""
version: 1
undefined-plugin:
plugin: {unique_plugin_name}
"""
)
assert unique_plugin_name in caplog.text
assert not hasattr(zc, unique_plugin_name)
def test_configuration_can_specify_a_missing_resolution_file(tmp_path):
zocalo.configuration.from_string(
f"""
version: 1
unused-plugin:
{tmp_path / 'missing_file'}
"""
)
def test_configuration_can_specify_an_unreadable_resolution_file(tmp_path):
forbidden_file = tmp_path / "forbidden_file"
forbidden_file.write_text("This should not be accessible")
zc = zocalo.configuration.from_string(
f"""
version: 1
forbidden-plugin:
{forbidden_file}
environments:
forbidden:
- forbidden-plugin
"""
)
try:
if os.name == "nt":
subprocess.run(
("icacls", os.fspath(forbidden_file), "/deny", "Everyone:(R)"),
check=True,
)
else:
forbidden_file.chmod(0o000)
with pytest.raises(PermissionError):
zc.activate_environment("forbidden")
finally:
if os.name == "nt":
subprocess.run(
("icacls", os.fspath(forbidden_file), "/remove:d", "Everyone"),
check=True,
)
else:
forbidden_file.chmod(0o664)
def test_plugins_can_be_configured_in_an_external_file(tmp_path):
external_plugin = tmp_path / "external.yml"
external_plugin.write_text(
"""
plugin: storage
value: sentinel
"""
)
zc = zocalo.configuration.from_string(
f"""
version: 1
external:
{tmp_path / 'external.yml'}
environments:
ext:
- external
"""
)
assert "1 of" in str(zc)
zc.activate_environment("ext")
assert "1 of" not in str(zc)
assert zc.storage["value"] == "sentinel"
@pytest.mark.xfail(raises=NotImplementedError, strict=True)
def test_loading_modular_configuration_from_string(tmp_path):
secondary_file = tmp_path / "config.yml"
secondary_file.write_text(sample_configuration)
zc = zocalo.configuration.from_string(
f"""
version: 1
include:
- {secondary_file}
"""
)
assert zc.environments
@pytest.mark.xfail(raises=NotImplementedError, strict=True)
def test_loading_modular_configuration_from_file(tmp_path):
secondary_file = tmp_path / "config.yml"
secondary_file.write_text(sample_configuration)
primary_file = tmp_path / "primary.yml"
primary_file.write_text(
"""
version: 1
include:
- config.yml
"""
)
zc = zocalo.configuration.from_file(primary_file)
assert zc.environments
def test_cannot_load_modular_configuration_with_missing_reference(tmp_path):
secondary_file = tmp_path / "non-existing-file.yml"
with pytest.raises(FileNotFoundError):
zocalo.configuration.from_string(
f"""
version: 1
include:
- {secondary_file}
"""
)
def test_cannot_load_modular_configuration_with_broken_reference(tmp_path):
secondary_file = tmp_path / "invalid.yml"
secondary_file.write_text("x: y: z:")
with pytest.raises(zocalo.ConfigurationError, match="invalid.yml"):
zocalo.configuration.from_string(
f"""
version: 1
include:
- {secondary_file}
"""
)
def test_resolve_external_references_into_home_directory():
merged = zocalo.configuration._merge_configuration(
"""
version: 1
foo: ~/bar.yml
""",
None,
pathlib.Path.cwd(),
)
assert merged == {
"version": 1,
"foo": pathlib.Path("~/bar.yml").expanduser(),
"environments": {},
}