...
1import pytest
2
3from tests.utils import econf_compile, econf_foreach_cluster, module_and_mapping_manifests
4
5
6# Tests if `setting` exists within the cluster config and has `expected` as the value for that setting
7# Use `exists` to test if you expect a setting to not exist
8def _test_cluster_setting(yaml, setting, expected, exists=True):
9 econf = econf_compile(yaml)
10
11 def check(cluster):
12 if exists:
13 assert setting in cluster
14 assert cluster[setting] == expected
15 else:
16 assert setting not in cluster
17
18 econf_foreach_cluster(econf, check)
19
20
21# Tests a setting in a cluster that has it's own fields. Example: common_http_protocol_options has multiple subfields
22def _test_cluster_subfields(yaml, setting, expectations={}, exists=True):
23 econf = econf_compile(yaml)
24
25 def check(cluster):
26 if exists:
27 assert setting in cluster
28 else:
29 assert setting not in cluster
30 for key, expected in expectations.items():
31 print("Checking key: {} for the {} setting in Envoy cluster".format(key, setting))
32 assert key in cluster[setting]
33 assert cluster[setting][key] == expected
34
35 econf_foreach_cluster(econf, check)
36
37
38# Test dns_type setting in Mapping
39@pytest.mark.compilertest
40def test_logical_dns_type():
41 yaml = module_and_mapping_manifests(None, ["dns_type: logical_dns"])
42 # The dns type is listed as just "type"
43 _test_cluster_setting(yaml, setting="type", expected="LOGICAL_DNS", exists=True)
44
45
46@pytest.mark.compilertest
47def test_strict_dns_type():
48 # Make sure we can configure strict dns as well even though it's the default
49 yaml = module_and_mapping_manifests(None, ["dns_type: strict_dns"])
50 # The dns type is listed as just "type"
51 _test_cluster_setting(yaml, setting="type", expected="STRICT_DNS", exists=True)
52
53
54@pytest.mark.compilertest
55def test_dns_type_wrong():
56 # Ensure we fallback to strict_dns as the setting when an invalid string is passed
57 # This is preferable to invalid config and an error is logged
58 yaml = module_and_mapping_manifests(None, ["dns_type: something_new"])
59 # The dns type is listed as just "type"
60 _test_cluster_setting(yaml, setting="type", expected="STRICT_DNS", exists=True)
61
62
63@pytest.mark.compilertest
64def test_logical_dns_type_endpoints():
65 # Ensure we use endpoint discovery instead of this value when using the endpoint resolver.
66 yaml = module_and_mapping_manifests(None, ["dns_type: logical_dns", "resolver: endpoint"])
67 # The dns type is listed as just "type"
68 _test_cluster_setting(yaml, setting="type", expected="EDS", exists=True)
69
70
71@pytest.mark.compilertest
72def test_dns_ttl_module():
73 # Test configuring the respect_dns_ttl generates an Envoy config
74 yaml = module_and_mapping_manifests(None, ["respect_dns_ttl: true"])
75 # The dns type is listed as just "type"
76 _test_cluster_setting(yaml, setting="respect_dns_ttl", expected=True, exists=True)
77
78
79@pytest.mark.compilertest
80def test_dns_ttl_mapping():
81 # Test dns_ttl is not configured when not applied in the Mapping
82 yaml = module_and_mapping_manifests(None, None)
83 # The dns type is listed as just "type"
84 _test_cluster_setting(yaml, setting="respect_dns_ttl", expected=False, exists=False)
View as plain text