...
1import logging
2
3import pytest
4
5logging.basicConfig(
6 level=logging.INFO,
7 format="%(asctime)s test %(levelname)s: %(message)s",
8 datefmt="%Y-%m-%d %H:%M:%S",
9)
10
11logger = logging.getLogger("ambassador")
12
13from ambassador import IR, Config, EnvoyConfig
14from ambassador.fetch import ResourceFetcher
15from ambassador.utils import NullSecretHandler
16from tests.utils import default_listener_manifests
17
18
19def _get_envoy_config(yaml):
20 aconf = Config()
21 fetcher = ResourceFetcher(logger, aconf)
22 fetcher.parse_yaml(default_listener_manifests() + yaml, k8s=True)
23 aconf.load_all(fetcher.sorted())
24 secret_handler = NullSecretHandler(logger, None, None, "0")
25 ir = IR(aconf, file_checker=lambda path: True, secret_handler=secret_handler)
26
27 assert ir
28
29 econf = EnvoyConfig.generate(ir)
30 assert econf, "could not create an econf"
31
32 return econf
33
34
35@pytest.mark.compilertest
36def test_setting_buffer_limit():
37 yaml = """
38---
39apiVersion: getambassador.io/v3alpha1
40kind: Module
41metadata:
42 name: ambassador
43 namespace: default
44spec:
45 config:
46 buffer_limit_bytes: 5242880
47---
48apiVersion: getambassador.io/v3alpha1
49kind: Mapping
50metadata:
51 name: ambassador
52 namespace: default
53spec:
54 prefix: /test/
55 hostname: "*"
56 service: test:9999
57"""
58 econf = _get_envoy_config(yaml)
59 expected = 5242880
60 key_found = False
61
62 conf = econf.as_dict()
63
64 for listener in conf["static_resources"]["listeners"]:
65 per_connection_buffer_limit_bytes = listener.get("per_connection_buffer_limit_bytes", None)
66 assert (
67 per_connection_buffer_limit_bytes is not None
68 ), f"per_connection_buffer_limit_bytes not found on listener: {listener['name']}"
69 print(f"Found per_connection_buffer_limit_bytes = {per_connection_buffer_limit_bytes}")
70 key_found = True
71 assert expected == int(
72 per_connection_buffer_limit_bytes
73 ), "per_connection_buffer_limit_bytes must equal the value set on the ambassador Module"
74 assert key_found, "per_connection_buffer_limit_bytes must be found in the envoy config"
75
76
77@pytest.mark.compilertest
78def test_setting_buffer_limit_V3():
79 yaml = """
80---
81apiVersion: getambassador.io/v3alpha1
82kind: Module
83metadata:
84 name: ambassador
85 namespace: default
86spec:
87 config:
88 buffer_limit_bytes: 5242880
89---
90apiVersion: getambassador.io/v3alpha1
91kind: Mapping
92metadata:
93 name: ambassador
94 namespace: default
95spec:
96 prefix: /test/
97 hostname: "*"
98 service: test:9999
99
100
101"""
102 econf = _get_envoy_config(yaml)
103 expected = 5242880
104 key_found = False
105
106 conf = econf.as_dict()
107
108 for listener in conf["static_resources"]["listeners"]:
109 per_connection_buffer_limit_bytes = listener.get("per_connection_buffer_limit_bytes", None)
110 assert (
111 per_connection_buffer_limit_bytes is not None
112 ), f"per_connection_buffer_limit_bytes not found on listener: {listener['name']}"
113 print(f"Found per_connection_buffer_limit_bytes = {per_connection_buffer_limit_bytes}")
114 key_found = True
115 assert expected == int(
116 per_connection_buffer_limit_bytes
117 ), "per_connection_buffer_limit_bytes must equal the value set on the ambassador Module"
118 assert key_found, "per_connection_buffer_limit_bytes must be found in the envoy config"
119
120
121# Tests that the default value of per_connection_buffer_limit_bytes is disabled when there is not Module config for it.
122@pytest.mark.compilertest
123def test_default_buffer_limit():
124 yaml = """
125---
126apiVersion: getambassador.io/v3alpha1
127kind: Mapping
128metadata:
129 name: ambassador
130 namespace: default
131spec:
132 prefix: /test/
133 hostname: "*"
134 service: test:9999
135"""
136 econf = _get_envoy_config(yaml)
137
138 conf = econf.as_dict()
139
140 for listener in conf["static_resources"]["listeners"]:
141 per_connection_buffer_limit_bytes = listener.get("per_connection_buffer_limit_bytes", None)
142 assert (
143 per_connection_buffer_limit_bytes is None
144 ), f"per_connection_buffer_limit_bytes found on listener (should not exist unless configured in the module): {listener['name']}"
145
146
147@pytest.mark.compilertest
148def test_default_buffer_limit_V3():
149 yaml = """
150---
151apiVersion: getambassador.io/v3alpha1
152kind: Mapping
153metadata:
154 name: ambassador
155 namespace: default
156spec:
157 prefix: /test/
158 hostname: "*"
159 service: test:9999
160"""
161 econf = _get_envoy_config(yaml)
162
163 conf = econf.as_dict()
164
165 for listener in conf["static_resources"]["listeners"]:
166 per_connection_buffer_limit_bytes = listener.get("per_connection_buffer_limit_bytes", None)
167 assert (
168 per_connection_buffer_limit_bytes is None
169 ), f"per_connection_buffer_limit_bytes found on listener (should not exist unless configured in the module): {listener['name']}"
170
171
172# Tests that the default value of per_connection_buffer_limit_bytes is disabled when there is not Module config for it (and that there are no issues when we dont make a listener).
173@pytest.mark.compilertest
174def test_buffer_limit_no_listener():
175 yaml = """
176---
177apiVersion: getambassador.io/v3alpha1
178kind: Mapping
179metadata:
180 name: ambassador
181 namespace: default
182spec:
183 prefix: /test/
184 hostname: "*"
185 service: test:9999
186"""
187 econf = _get_envoy_config(yaml)
188
189 conf = econf.as_dict()
190
191 for listener in conf["static_resources"]["listeners"]:
192 per_connection_buffer_limit_bytes = listener.get("per_connection_buffer_limit_bytes", None)
193 assert (
194 per_connection_buffer_limit_bytes is None
195 ), f"per_connection_buffer_limit_bytes found on listener (should not exist unless configured in the module): {listener['name']}"
196
197
198@pytest.mark.compilertest
199def test_buffer_limit_no_listener_V3():
200 yaml = """
201---
202apiVersion: getambassador.io/v3alpha1
203kind: Mapping
204metadata:
205 name: ambassador
206 namespace: default
207spec:
208 prefix: /test/
209 hostname: "*"
210 service: test:9999
211"""
212 econf = _get_envoy_config(yaml)
213
214 conf = econf.as_dict()
215
216 for listener in conf["static_resources"]["listeners"]:
217 per_connection_buffer_limit_bytes = listener.get("per_connection_buffer_limit_bytes", None)
218 assert (
219 per_connection_buffer_limit_bytes is None
220 ), f"per_connection_buffer_limit_bytes found on listener (should not exist unless configured in the module): {listener['name']}"
View as plain text