-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_config.py
527 lines (428 loc) · 15.1 KB
/
test_config.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
from __future__ import annotations
import json
import time
import textwrap
from pathlib import Path as P
from unittest.mock import patch
from typing import Any, Iterable, Optional
import click
import jwt
import pytest
import responses
from commodore.config import (
Config,
set_fact_value,
parse_dynamic_fact_value,
parse_dynamic_facts_from_cli,
)
from commodore.package import Package
from commodore.multi_dependency import dependency_key
def test_verify_component_aliases_no_instance(config):
alias_data = {"bar": "bar"}
config.register_component_aliases(alias_data)
params = {"bar": {"namespace": "syn-bar"}}
config.verify_component_aliases(params)
def test_verify_component_aliases_explicit_no_instance(config):
alias_data = {"bar": "bar"}
config.register_component_aliases(alias_data)
params = {"bar": {"_metadata": {"multi_instance": False}, "namespace": "syn-bar"}}
config.verify_component_aliases(params)
def test_verify_component_aliases_explicit_no_multiversion_exception(config):
alias_data = {"baz": "bar"}
config.register_component_aliases(alias_data)
params = {
"components": {
"bar": {"url": "foo", "version": "v1.0.0"},
"baz": {"version": "v1.1.0"},
},
"bar": {
"_metadata": {"multi_instance": True, "multi_versioning": False},
"namespace": "syn-bar",
},
"baz": {
"_metadata": {"multi_instance": True, "multi_versioning": False},
"namespace": "syn-baz",
},
}
with pytest.raises(click.ClickException) as e:
config.verify_component_aliases(params)
assert (
"Component bar with alias baz does not support overriding instance version."
in str(e.value)
)
def test_verify_component_aliases_explicit_no_multiversion_in_alias_exception(config):
alias_data = {"baz": "bar"}
config.register_component_aliases(alias_data)
params = {
"components": {
"bar": {"url": "foo", "version": "v1.0.0"},
"baz": {"version": "v1.1.0"},
},
"bar": {
"_metadata": {"multi_instance": True, "multi_versioning": True},
"namespace": "syn-bar",
},
"baz": {
"_metadata": {"multi_instance": True, "multi_versioning": False},
"namespace": "syn-baz",
},
}
with pytest.raises(click.ClickException) as e:
config.verify_component_aliases(params)
assert (
"Component bar with alias baz does not support overriding instance version."
in str(e.value)
)
def test_verify_component_multiversion_exception(config):
alias_data = {"baz": "bar"}
config.register_component_aliases(alias_data)
params = {
"components": {
"bar": {"url": "foo", "version": "v1.0.0"},
"baz": {"version": "v1.1.0"},
},
"bar": {"_metadata": {"multi_instance": True}},
}
with pytest.raises(click.ClickException) as e:
config.verify_component_aliases(params)
assert (
"Component bar with alias baz does not support overriding instance version."
in str(e.value)
)
def test_verify_component_multiversion(config):
alias_data = {"baz": "bar"}
config.register_component_aliases(alias_data)
params = {
"components": {
"bar": {"url": "foo", "version": "v1.0.0"},
"baz": {"version": "v1.1.0"},
},
"bar": {
"_metadata": {"multi_instance": True, "multi_versioning": True},
"namespace": "syn-bar",
},
"baz": {"_metadata": {"multi_versioning": True}, "namespace": "syn-baz"},
}
config.verify_component_aliases(params)
def test_verify_component_aliases_metadata(config):
alias_data = {"baz": "bar"}
config.register_component_aliases(alias_data)
params = {"bar": {"_metadata": {"multi_instance": True}, "namespace": "syn-bar"}}
config.verify_component_aliases(params)
assert len(config._deprecation_notices) == 0
def test_verify_toplevel_component_aliases_exception(config):
alias_data = {"baz": "bar"}
config.register_component_aliases(alias_data)
params = {"bar": {"multi_instance": True, "namespace": "syn-bar"}}
with pytest.raises(click.ClickException) as e:
config.verify_component_aliases(params)
assert "Component bar with alias baz does not support instantiation." in str(
e.value
)
def test_verify_component_aliases_error(config):
alias_data = {"baz": "bar"}
config.register_component_aliases(alias_data)
params = {"bar": {"namespace": "syn-bar"}}
with pytest.raises(click.ClickException):
config.verify_component_aliases(params)
def test_verify_component_aliases_explicit_no_instance_error(config):
alias_data = {"baz": "bar"}
config.register_component_aliases(alias_data)
params = {"bar": {"_metadata": {"multi_instance": False}, "namespace": "syn-bar"}}
with pytest.raises(click.ClickException):
config.verify_component_aliases(params)
@pytest.mark.parametrize(
"params,expected",
[
(
{
"bar": {"namespace": "syn-bar"},
"foo": {},
},
[],
),
(
{
"bar": {
"namespace": "syn-bar",
"_metadata": {"deprecated": False, "replaced_by": "irrelevant"},
},
"foo": {},
},
[],
),
(
{
"bar": {
"namespace": "syn-bar",
"_metadata": {"deprecated": True},
},
"foo": {},
},
["Component bar is deprecated."],
),
(
{
"bar": {
"namespace": "syn-bar",
"_metadata": {"deprecated": True, "replaced_by": "foo"},
},
"foo": {},
},
["Component bar is deprecated. Use component foo instead."],
),
(
{
"bar": {
"namespace": "syn-bar",
"_metadata": {
"deprecated": True,
"replaced_by": "foo",
"deprecation_notice": "See https://example.com/migrate-from-bar.html for a migration guide.",
},
},
"foo": {},
},
[
"Component bar is deprecated. Use component foo instead. "
+ "See https://example.com/migrate-from-bar.html for a migration guide."
],
),
(
{
"bar": {
"namespace": "syn-bar",
"_metadata": {
"deprecated": True,
},
},
"foo": {
"namespace": "syn-foo",
"_metadata": {
"deprecated": True,
},
},
},
["Component bar is deprecated.", "Component foo is deprecated."],
),
],
)
def test_register_component_deprecations(config, params, expected):
alias_data = {"baz": "bar", "qux": "foo"}
config.register_component_aliases(alias_data)
config.register_component_deprecations(params)
assert len(expected) == len(config._deprecation_notices)
for en, an in zip(sorted(expected), sorted(config._deprecation_notices)):
assert en == an
def _setup_deprecation_notices(config):
config.register_deprecation_notice("test 1")
config.register_deprecation_notice("test 2")
def test_register_deprecation_notices(config):
_setup_deprecation_notices(config)
assert ["test 1", "test 2"] == config._deprecation_notices
def test_print_deprecation_notices_no_notices(config, capsys):
config.print_deprecation_notices()
captured = capsys.readouterr()
assert "" == captured.out
def test_print_deprecation_notices(config, capsys):
_setup_deprecation_notices(config)
config.print_deprecation_notices()
captured = capsys.readouterr()
assert (
textwrap.dedent(
"""
Commodore notices:
> test 1
> test 2
"""
)
== captured.out
)
def mock_get_token(url: str) -> Optional[str]:
if url == "https://syn.example.com":
return {
"id_token": jwt.encode(
{"exp": time.time() + 100, "from_cache": True},
"secret",
algorithm="HS256",
)
}
elif url == "https://expired.example.com":
return {
"id_token": jwt.encode(
{"exp": time.time() - 100, "from_cache": True},
"secret",
algorithm="HS256",
)
}
else:
return None
@patch("commodore.tokencache.get")
def test_use_token_cache(test_patch):
test_patch.side_effect = mock_get_token
conf = Config(P("."), api_url="https://syn.example.com")
t = jwt.decode(
conf.api_token, algorithms=["RS256"], options={"verify_signature": False}
)
assert t["from_cache"]
@patch("commodore.tokencache.get")
def test_expired_token_cache(test_patch):
test_patch.side_effect = mock_get_token
conf = Config(P("."), api_url="https://expired.example.com")
assert conf.api_token is None
def test_register_get_package(config: Config, tmp_path: P, mockdep):
# No preregistered packages
assert config.get_packages() == {}
p = Package("test", mockdep, tmp_path / "pkg")
config.register_package("test", p)
assert config.get_packages() == {"test": p}
def test_register_get_dependency(config: Config, tmp_path: P):
repo_url = "https://git.example.com/repo.git"
# No dependencies registered initially
assert len(config._dependency_repos) == 0
md = config.register_dependency_repo(repo_url)
depkey = dependency_key(repo_url)
assert len(config._dependency_repos) == 1
assert config._dependency_repos.get(depkey) == md
def test_register_get_dependency_deduplicates(config: Config, tmp_path: P):
repo_url_1 = "https://git.example.com/repo1.git"
repo_url_2 = "https://git.example.com/repo2.git"
assert len(config._dependency_repos) == 0
md = config.register_dependency_repo(repo_url_1)
depkey = dependency_key(repo_url_1)
assert len(config._dependency_repos) == 1
assert config._dependency_repos.get(depkey) == md
md1_dup = config.register_dependency_repo(repo_url_1)
assert len(config._dependency_repos) == 1
assert md1_dup == md
md2 = config.register_dependency_repo(repo_url_2)
depkey2 = dependency_key(repo_url_2)
assert len(config._dependency_repos) == 2
assert config._dependency_repos.get(depkey2) == md2
assert set(config._dependency_repos.keys()) == {depkey, depkey2}
def test_register_dependency_prefer_ssh(config: Config, tmp_path: P):
repo_url_https = "https://git.example.com/repo.git"
repo_url_ssh = "ssh://git@git.example.com/repo.git"
md = config.register_dependency_repo(repo_url_https)
assert md.url == repo_url_https
md2 = config.register_dependency_repo(repo_url_ssh)
assert md2 == md
assert md.url == repo_url_ssh
md3 = config.register_dependency_repo(repo_url_https)
assert md3 == md
assert md.url == repo_url_ssh
@pytest.mark.parametrize(
"key,base_dict,expected_dict",
[
("toplevel", {}, {"toplevel": "sentinel"}),
("path.to.key", {}, {"path": {"to": {"key": "sentinel"}}}),
("path.to.key", {"path": {"to": "value"}}, {"path": {"to": "value"}}),
(
"path.to.key",
{"path": {"to": {"other": "value"}}},
{"path": {"to": {"other": "value", "key": "sentinel"}}},
),
("path.", {}, {}),
("path..foo", {}, {}),
(".foo", {}, {}),
],
)
def test_set_fact_value(
key: str,
base_dict: dict[str, Any],
expected_dict: dict[str, Any],
):
set_fact_value(base_dict, key, "sentinel")
assert base_dict == expected_dict
@pytest.mark.parametrize(
"value,expected",
[
("foo", "foo"),
("test:foo", "test:foo"),
("json:foo", None),
('json:{"foo":"bar"', None),
('json:"foo"', "foo"),
("json:1", 1),
('json:["a"]', ["a"]),
('json:{"test":{"key":"value"}}', {"test": {"key": "value"}}),
],
)
def test_parse_dynamic_fact_value(value: str, expected: Any):
parsed = parse_dynamic_fact_value(value)
assert parsed == expected
@pytest.mark.parametrize(
"args,expected",
[
([], {}),
(["key"], {}),
(["key="], {}),
(["="], {}),
(["=value"], {}),
(['key=json:""'], {"key": ""}),
(["key=value"], {"key": "value"}),
(["key=value", "foo=bar"], {"key": "value", "foo": "bar"}),
(["key=value=x"], {"key": "value=x"}),
(["key=value", "key=another"], {"key": "another"}),
(["key=value", "key.foo=bar"], {"key": "value"}),
(["key.foo=bar", "key=value"], {"key": "value"}),
(["key.foo=bar", "key.baz=qux"], {"key": {"foo": "bar", "baz": "qux"}}),
(["key=json:[1,2,3]"], {"key": [1, 2, 3]}),
(["key=json:[1,2,3"], {}),
(["path.to.key=json:foo"], {}),
],
)
def test_parse_dynamic_facts_from_cli(args: Iterable[str], expected: dict[str, Any]):
dynamic_facts = parse_dynamic_facts_from_cli(args)
assert dynamic_facts == expected
@responses.activate
@pytest.mark.parametrize(
"api_url,discovery_resp,expected_client,expected_url",
[
("https://syn.example.com", {}, None, None),
# Non-JSON is ignored
("https://syn.example.com", "oidc", None, None),
# Broken JSON is ignored
("https://syn.example.com", '"oidc":{"tes: 1}', None, None),
# Unexpected data format is ignored
("https://syn.example.com", {"oidc": {"client_id": "test"}}, None, None),
# Partial responses are propagated into the config object
(
"https://syn.example.com",
{"oidc": {"clientId": "test-client"}},
"test-client",
None,
),
(
"https://syn.example.com",
{
"oidc": {
"clientId": "test-client",
"discoveryUrl": "https://oidc.example.com",
},
},
"test-client",
"https://oidc.example.com",
),
],
)
def test_config_discover_oidc_config(
tmp_path: P,
api_url: str,
discovery_resp: Any,
expected_client: str,
expected_url: str,
):
if isinstance(discovery_resp, dict):
ct = "application/json"
resp_body = json.dumps(discovery_resp)
else:
resp_body = f"{discovery_resp}"
ct = "text/plain"
responses.add(
responses.GET, url=api_url, content_type=ct, body=resp_body, status=200
)
c = Config(tmp_path, api_url=api_url)
c.discover_oidc_config()
assert len(responses.calls) == 1
assert c.oidc_client == expected_client
assert c.oidc_discovery_url == expected_url