...

Text file src/github.com/datawire/ambassador/v2/python/tests/unit/test_common_http_protocol_options.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_cluster,
     7    module_and_mapping_manifests,
     8)
     9
    10
    11def _test_common_http_protocol_options(yaml, expectations={}, envoy_version="V2"):
    12    econf = econf_compile(yaml, envoy_version=envoy_version)
    13
    14    def check(cluster):
    15        if expectations:
    16            assert "common_http_protocol_options" in cluster
    17        else:
    18            assert "common_http_protocol_options" not in cluster
    19        for key, expected in expectations.items():
    20            print("checking key %s" % key)
    21            assert key in cluster["common_http_protocol_options"]
    22            assert cluster["common_http_protocol_options"][key] == expected
    23
    24    econf_foreach_cluster(econf, check)
    25
    26
    27@pytest.mark.compilertest
    28def test_cluster_max_connection_lifetime_ms_missing():
    29    # If we do not set the config, it should not appear in the Envoy conf.
    30    yaml = module_and_mapping_manifests(None, [])
    31    for v in SUPPORTED_ENVOY_VERSIONS:
    32        _test_common_http_protocol_options(yaml, expectations={}, envoy_version=v)
    33
    34
    35@pytest.mark.compilertest
    36def test_cluster_max_connection_lifetime_ms_module_only():
    37    # If we only set the config on the Module, it should show up.
    38    yaml = module_and_mapping_manifests(["cluster_max_connection_lifetime_ms: 2005"], [])
    39    for v in SUPPORTED_ENVOY_VERSIONS:
    40        _test_common_http_protocol_options(
    41            yaml, expectations={"max_connection_duration": "2.005s"}, envoy_version=v
    42        )
    43
    44
    45@pytest.mark.compilertest
    46def test_cluster_max_connection_lifetime_ms_mapping_only():
    47    # If we only set the config on the Mapping, it should show up.
    48    yaml = module_and_mapping_manifests(None, ["cluster_max_connection_lifetime_ms: 2005"])
    49    for v in SUPPORTED_ENVOY_VERSIONS:
    50        _test_common_http_protocol_options(
    51            yaml, expectations={"max_connection_duration": "2.005s"}, envoy_version=v
    52        )
    53
    54
    55@pytest.mark.compilertest
    56def test_cluster_max_connection_lifetime_ms_mapping_override():
    57    # If we set the config on the Module and Mapping, the Mapping value wins.
    58    yaml = module_and_mapping_manifests(
    59        ["cluster_max_connection_lifetime_ms: 2005"], ["cluster_max_connection_lifetime_ms: 17005"]
    60    )
    61    for v in SUPPORTED_ENVOY_VERSIONS:
    62        _test_common_http_protocol_options(
    63            yaml, expectations={"max_connection_duration": "17.005s"}, envoy_version=v
    64        )
    65
    66
    67@pytest.mark.compilertest
    68def test_cluster_idle_timeout_ms_missing():
    69    # If we do not set the config, it should not appear in the Envoy conf.
    70    yaml = module_and_mapping_manifests(None, [])
    71    for v in SUPPORTED_ENVOY_VERSIONS:
    72        _test_common_http_protocol_options(yaml, expectations={}, envoy_version=v)
    73
    74
    75@pytest.mark.compilertest
    76def test_cluster_idle_timeout_ms_module_only():
    77    # If we only set the config on the Module, it should show up.
    78    yaml = module_and_mapping_manifests(["cluster_idle_timeout_ms: 4005"], [])
    79    for v in SUPPORTED_ENVOY_VERSIONS:
    80        _test_common_http_protocol_options(
    81            yaml, expectations={"idle_timeout": "4.005s"}, envoy_version=v
    82        )
    83
    84
    85@pytest.mark.compilertest
    86def test_cluster_idle_timeout_ms_mapping_only():
    87    # If we only set the config on the Mapping, it should show up.
    88    yaml = module_and_mapping_manifests(None, ["cluster_idle_timeout_ms: 4005"])
    89    for v in SUPPORTED_ENVOY_VERSIONS:
    90        _test_common_http_protocol_options(
    91            yaml, expectations={"idle_timeout": "4.005s"}, envoy_version=v
    92        )
    93
    94
    95@pytest.mark.compilertest
    96def test_cluster_idle_timeout_ms_mapping_override():
    97    # If we set the config on the Module and Mapping, the Mapping value wins.
    98    yaml = module_and_mapping_manifests(
    99        ["cluster_idle_timeout_ms: 4005"], ["cluster_idle_timeout_ms: 19105"]
   100    )
   101    for v in SUPPORTED_ENVOY_VERSIONS:
   102        _test_common_http_protocol_options(
   103            yaml, expectations={"idle_timeout": "19.105s"}, envoy_version=v
   104        )
   105
   106
   107@pytest.mark.compilertest
   108def test_both_module():
   109    # If we set both configs on the Module, both should show up.
   110    yaml = module_and_mapping_manifests(
   111        ["cluster_idle_timeout_ms: 4005", "cluster_max_connection_lifetime_ms: 2005"], None
   112    )
   113    for v in SUPPORTED_ENVOY_VERSIONS:
   114        _test_common_http_protocol_options(
   115            yaml,
   116            expectations={"max_connection_duration": "2.005s", "idle_timeout": "4.005s"},
   117            envoy_version=v,
   118        )
   119
   120
   121@pytest.mark.compilertest
   122def test_both_mapping():
   123    # If we set both configs on the Mapping, both should show up.
   124    yaml = module_and_mapping_manifests(
   125        None, ["cluster_idle_timeout_ms: 4005", "cluster_max_connection_lifetime_ms: 2005"]
   126    )
   127    for v in SUPPORTED_ENVOY_VERSIONS:
   128        _test_common_http_protocol_options(
   129            yaml,
   130            expectations={"max_connection_duration": "2.005s", "idle_timeout": "4.005s"},
   131            envoy_version=v,
   132        )
   133
   134
   135@pytest.mark.compilertest
   136def test_both_one_module_one_mapping():
   137    # If we set both configs, one on a Module, one on a Mapping, both should show up.
   138    yaml = module_and_mapping_manifests(
   139        ["cluster_idle_timeout_ms: 4005"], ["cluster_max_connection_lifetime_ms: 2005"]
   140    )
   141    for v in SUPPORTED_ENVOY_VERSIONS:
   142        _test_common_http_protocol_options(
   143            yaml,
   144            expectations={"max_connection_duration": "2.005s", "idle_timeout": "4.005s"},
   145            envoy_version=v,
   146        )

View as plain text