...

Text file src/github.com/datawire/ambassador/v2/python/tests/unit/test_route.py

Documentation: github.com/datawire/ambassador/v2/python/tests/unit

     1import pytest
     2
     3from tests.utils import (
     4    SUPPORTED_ENVOY_VERSIONS,
     5    econf_compile,
     6    econf_foreach_hcm,
     7    module_and_mapping_manifests,
     8)
     9
    10
    11def _test_route(yaml, expectations={}):
    12    for v in SUPPORTED_ENVOY_VERSIONS:
    13        econf = econf_compile(yaml, envoy_version=v)
    14
    15        def check(typed_config):
    16            # Find the one and virtual host in the route config
    17            vhosts = typed_config["route_config"]["virtual_hosts"]
    18            assert len(vhosts) == 1
    19
    20            # Find the httpbin route. Run our expectations over that.
    21            routes = vhosts[0]["routes"]
    22            for r in routes:
    23                # Keep going until we find a real route
    24                if "route" not in r:
    25                    continue
    26
    27                # Keep going until we find a prefix match for /httpbin/
    28                match = r["match"]
    29                if "prefix" not in match or match["prefix"] != "/httpbin/":
    30                    continue
    31
    32                assert "route" in r
    33                route = r["route"]
    34                for key, expected in expectations.items():
    35                    print("checking key %s" % key)
    36                    assert key in route
    37                    assert route[key] == expected
    38                break
    39
    40        econf_foreach_hcm(econf, check, envoy_version=v)
    41
    42
    43@pytest.mark.compilertest
    44def test_timeout_ms():
    45    # If we do not set the config, we should get the default 3000ms.
    46    yaml = module_and_mapping_manifests(None, [])
    47    _test_route(yaml, expectations={"timeout": "3.000s"})
    48
    49
    50@pytest.mark.compilertest
    51def test_timeout_ms_module():
    52    # If we set a default on the Module, it should override the usual default of 3000ms.
    53    yaml = module_and_mapping_manifests(["cluster_request_timeout_ms: 4000"], [])
    54    _test_route(yaml, expectations={"timeout": "4.000s"})
    55
    56
    57@pytest.mark.compilertest
    58def test_timeout_ms_mapping():
    59    # If we set a default on the Module, it should override the usual default of 3000ms.
    60    yaml = module_and_mapping_manifests(None, ["timeout_ms: 1234"])
    61    _test_route(yaml, expectations={"timeout": "1.234s"})
    62
    63
    64@pytest.mark.compilertest
    65def test_timeout_ms_both():
    66    # If we set a default on the Module, it should override the usual default of 3000ms.
    67    yaml = module_and_mapping_manifests(["cluster_request_timeout_ms: 9000"], ["timeout_ms: 5001"])
    68    _test_route(yaml, expectations={"timeout": "5.001s"})

View as plain text