1import pytest
2
3from tests.utils import econf_compile, econf_foreach_cluster, module_and_mapping_manifests
4
5
6def _test_common_http_protocol_options(yaml, expectations={}):
7 econf = econf_compile(yaml)
8
9 def check(cluster):
10 if expectations:
11 assert "common_http_protocol_options" in cluster
12 else:
13 assert "common_http_protocol_options" not in cluster
14 for key, expected in expectations.items():
15 print("checking key %s" % key)
16 assert key in cluster["common_http_protocol_options"]
17 assert cluster["common_http_protocol_options"][key] == expected
18
19 econf_foreach_cluster(econf, check)
20
21
22@pytest.mark.compilertest
23def test_cluster_max_connection_lifetime_ms_missing():
24 # If we do not set the config, it should not appear in the Envoy conf.
25 yaml = module_and_mapping_manifests(None, [])
26 _test_common_http_protocol_options(yaml, expectations={})
27
28
29@pytest.mark.compilertest
30def test_cluster_max_connection_lifetime_ms_module_only():
31 # If we only set the config on the Module, it should show up.
32 yaml = module_and_mapping_manifests(["cluster_max_connection_lifetime_ms: 2005"], [])
33 _test_common_http_protocol_options(yaml, expectations={"max_connection_duration": "2.005s"})
34
35
36@pytest.mark.compilertest
37def test_cluster_max_connection_lifetime_ms_mapping_only():
38 # If we only set the config on the Mapping, it should show up.
39 yaml = module_and_mapping_manifests(None, ["cluster_max_connection_lifetime_ms: 2005"])
40 _test_common_http_protocol_options(yaml, expectations={"max_connection_duration": "2.005s"})
41
42
43@pytest.mark.compilertest
44def test_cluster_max_connection_lifetime_ms_mapping_override():
45 # If we set the config on the Module and Mapping, the Mapping value wins.
46 yaml = module_and_mapping_manifests(
47 ["cluster_max_connection_lifetime_ms: 2005"], ["cluster_max_connection_lifetime_ms: 17005"]
48 )
49 _test_common_http_protocol_options(yaml, expectations={"max_connection_duration": "17.005s"})
50
51
52@pytest.mark.compilertest
53def test_cluster_idle_timeout_ms_missing():
54 # If we do not set the config, it should not appear in the Envoy conf.
55 yaml = module_and_mapping_manifests(None, [])
56 _test_common_http_protocol_options(yaml, expectations={})
57
58
59@pytest.mark.compilertest
60def test_cluster_idle_timeout_ms_module_only():
61 # If we only set the config on the Module, it should show up.
62 yaml = module_and_mapping_manifests(["cluster_idle_timeout_ms: 4005"], [])
63 _test_common_http_protocol_options(yaml, expectations={"idle_timeout": "4.005s"})
64
65
66@pytest.mark.compilertest
67def test_cluster_idle_timeout_ms_mapping_only():
68 # If we only set the config on the Mapping, it should show up.
69 yaml = module_and_mapping_manifests(None, ["cluster_idle_timeout_ms: 4005"])
70 _test_common_http_protocol_options(yaml, expectations={"idle_timeout": "4.005s"})
71
72
73@pytest.mark.compilertest
74def test_cluster_idle_timeout_ms_mapping_override():
75 # If we set the config on the Module and Mapping, the Mapping value wins.
76 yaml = module_and_mapping_manifests(
77 ["cluster_idle_timeout_ms: 4005"], ["cluster_idle_timeout_ms: 19105"]
78 )
79 _test_common_http_protocol_options(yaml, expectations={"idle_timeout": "19.105s"})
80
81
82@pytest.mark.compilertest
83def test_both_module():
84 # If we set both configs on the Module, both should show up.
85 yaml = module_and_mapping_manifests(
86 ["cluster_idle_timeout_ms: 4005", "cluster_max_connection_lifetime_ms: 2005"], None
87 )
88 _test_common_http_protocol_options(
89 yaml, expectations={"max_connection_duration": "2.005s", "idle_timeout": "4.005s"}
90 )
91
92
93@pytest.mark.compilertest
94def test_both_mapping():
95 # If we set both configs on the Mapping, both should show up.
96 yaml = module_and_mapping_manifests(
97 None, ["cluster_idle_timeout_ms: 4005", "cluster_max_connection_lifetime_ms: 2005"]
98 )
99 _test_common_http_protocol_options(
100 yaml, expectations={"max_connection_duration": "2.005s", "idle_timeout": "4.005s"}
101 )
102
103
104@pytest.mark.compilertest
105def test_both_one_module_one_mapping():
106 # If we set both configs, one on a Module, one on a Mapping, both should show up.
107 yaml = module_and_mapping_manifests(
108 ["cluster_idle_timeout_ms: 4005"], ["cluster_max_connection_lifetime_ms: 2005"]
109 )
110 _test_common_http_protocol_options(
111 yaml, expectations={"max_connection_duration": "2.005s", "idle_timeout": "4.005s"}
112 )
View as plain text