Skip to content

Commit

Permalink
Add buffer_limit_bytes tests
Browse files Browse the repository at this point in the history
Signed-off-by: Flynn <[email protected]>
  • Loading branch information
Alice-Lilith authored and Flynn committed Oct 7, 2021
1 parent 9e37c18 commit a471f6a
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 0 deletions.
39 changes: 39 additions & 0 deletions python/tests/kat/t_bufferlimitbytes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from kat.harness import Query
from abstract_tests import AmbassadorTest, ServiceType, HTTP
import json

class BufferLimitBytesTest(AmbassadorTest):
target: ServiceType

def init(self):
self.target = HTTP(name="target")

# Test generating config with an increased buffer and that the lua body() funciton runs to buffer the request body
def config(self):
yield self, self.format("""
---
apiVersion: getambassador.io/v3alpha1
kind: Module
name: ambassador
config:
buffer_limit_bytes: 5242880
lua_scripts: |
function envoy_on_request(request_handle)
request_handle:headers():add("request_body_size", request_handle:body():length())
end
---
apiVersion: getambassador.io/v3alpha1
kind: Mapping
name: {self.target.path.k8s}-foo
hostname: "*"
prefix: /foo/
service: {self.target.path.fqdn}
""")

def queries(self):
yield Query(self.url("foo/"))
yield Query(self.url("ambassador/v0/diag/"))

def check(self):
assert self.results[0].status == 200
assert self.results[1].status == 200
1 change: 1 addition & 0 deletions python/tests/kat/test_ambassador.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

if letter_range in ["all","ah"]:
import t_basics
import t_bufferlimitbytes
import t_chunked_length
import t_circuitbreaker
import t_cluster_tag
Expand Down
210 changes: 210 additions & 0 deletions python/tests/unit/test_buffer_limit_bytes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import logging

import pytest

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s test %(levelname)s: %(message)s",
datefmt='%Y-%m-%d %H:%M:%S'
)

logger = logging.getLogger("ambassador")

from ambassador import Config, IR, EnvoyConfig
from ambassador.fetch import ResourceFetcher
from ambassador.utils import NullSecretHandler

from tests.utils import default_listener_manifests

def _get_envoy_config(yaml, version='V3'):
aconf = Config()
fetcher = ResourceFetcher(logger, aconf)
fetcher.parse_yaml(default_listener_manifests() + yaml, k8s=True)
aconf.load_all(fetcher.sorted())
secret_handler = NullSecretHandler(logger, None, None, "0")
ir = IR(aconf, file_checker=lambda path: True, secret_handler=secret_handler)

assert ir

econf = EnvoyConfig.generate(ir, version)
assert econf, "could not create an econf"

return econf


@pytest.mark.compilertest
def test_setting_buffer_limit():
yaml = """
---
apiVersion: getambassador.io/v2
kind: Module
metadata:
name: ambassador
namespace: default
spec:
config:
buffer_limit_bytes: 5242880
---
apiVersion: x.getambassador.io/v3alpha1
kind: Mapping
metadata:
name: ambassador
namespace: default
spec:
prefix: /test/
hostname: "*"
service: test:9999
"""
econf = _get_envoy_config(yaml, version='V2')
expected = 5242880
key_found = False

conf = econf.as_dict()

for listener in conf['static_resources']['listeners']:
per_connection_buffer_limit_bytes = listener.get('per_connection_buffer_limit_bytes', None)
assert per_connection_buffer_limit_bytes is not None, \
f"per_connection_buffer_limit_bytes not found on listener: {listener.name}"
print(f"Found per_connection_buffer_limit_bytes = {per_connection_buffer_limit_bytes}")
key_found = True
assert expected == int(per_connection_buffer_limit_bytes), \
"per_connection_buffer_limit_bytes must equal the value set on the ambassador Module"
assert key_found, 'per_connection_buffer_limit_bytes must be found in the envoy config'


@pytest.mark.compilertest
def test_setting_buffer_limit_V3():
yaml = """
---
apiVersion: getambassador.io/v2
kind: Module
metadata:
name: ambassador
namespace: default
spec:
config:
buffer_limit_bytes: 5242880
---
apiVersion: x.getambassador.io/v3alpha1
kind: Mapping
metadata:
name: ambassador
namespace: default
spec:
prefix: /test/
hostname: "*"
service: test:9999
"""
econf = _get_envoy_config(yaml)
expected = 5242880
key_found = False

conf = econf.as_dict()

for listener in conf['static_resources']['listeners']:
per_connection_buffer_limit_bytes = listener.get('per_connection_buffer_limit_bytes', None)
assert per_connection_buffer_limit_bytes is not None, \
f"per_connection_buffer_limit_bytes not found on listener: {listener.name}"
print(f"Found per_connection_buffer_limit_bytes = {per_connection_buffer_limit_bytes}")
key_found = True
assert expected == int(per_connection_buffer_limit_bytes), \
"per_connection_buffer_limit_bytes must equal the value set on the ambassador Module"
assert key_found, 'per_connection_buffer_limit_bytes must be found in the envoy config'

# Tests that the default value of per_connection_buffer_limit_bytes is disabled when there is not Module config for it.
@pytest.mark.compilertest
def test_default_buffer_limit():
yaml = """
---
apiVersion: x.getambassador.io/v3alpha1
kind: Mapping
metadata:
name: ambassador
namespace: default
spec:
prefix: /test/
hostname: "*"
service: test:9999
"""
econf = _get_envoy_config(yaml, version='V2')

conf = econf.as_dict()

for listener in conf['static_resources']['listeners']:
per_connection_buffer_limit_bytes = listener.get('per_connection_buffer_limit_bytes', None)
assert per_connection_buffer_limit_bytes is None, \
f"per_connection_buffer_limit_bytes found on listener (should not exist unless configured in the module): {listener.name}"


@pytest.mark.compilertest
def test_default_buffer_limit_V3():
yaml = """
---
apiVersion: x.getambassador.io/v3alpha1
kind: Mapping
metadata:
name: ambassador
namespace: default
spec:
prefix: /test/
hostname: "*"
service: test:9999
"""
econf = _get_envoy_config(yaml)

conf = econf.as_dict()

for listener in conf['static_resources']['listeners']:
per_connection_buffer_limit_bytes = listener.get('per_connection_buffer_limit_bytes', None)
assert per_connection_buffer_limit_bytes is None, \
f"per_connection_buffer_limit_bytes found on listener (should not exist unless configured in the module): {listener.name}"

# 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).
@pytest.mark.compilertest
def test_buffer_limit_no_listener():
yaml = """
---
apiVersion: x.getambassador.io/v3alpha1
kind: Mapping
metadata:
name: ambassador
namespace: default
spec:
prefix: /test/
hostname: "*"
service: test:9999
"""
econf = _get_envoy_config(yaml, version='V2')

conf = econf.as_dict()

for listener in conf['static_resources']['listeners']:
per_connection_buffer_limit_bytes = listener.get('per_connection_buffer_limit_bytes', None)
assert per_connection_buffer_limit_bytes is None, \
f"per_connection_buffer_limit_bytes found on listener (should not exist unless configured in the module): {listener.name}"


@pytest.mark.compilertest
def test_buffer_limit_no_listener_V3():
yaml = """
---
apiVersion: x.getambassador.io/v3alpha1
kind: Mapping
metadata:
name: ambassador
namespace: default
spec:
prefix: /test/
hostname: "*"
service: test:9999
"""
econf = _get_envoy_config(yaml)

conf = econf.as_dict()

for listener in conf['static_resources']['listeners']:
per_connection_buffer_limit_bytes = listener.get('per_connection_buffer_limit_bytes', None)
assert per_connection_buffer_limit_bytes is None, \
f"per_connection_buffer_limit_bytes found on listener (should not exist unless configured in the module): {listener.name}"

0 comments on commit a471f6a

Please sign in to comment.