...

Text file src/github.com/datawire/ambassador/v2/python/tests/unit/test_cluster_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
    11# Tests if `setting` exists within the cluster config and has `expected` as the value for that setting
    12# Use `exists` to test if you expect a setting to not exist
    13def _test_cluster_setting(yaml, setting, expected, exists=True, envoy_version="V2"):
    14    econf = econf_compile(yaml, envoy_version=envoy_version)
    15
    16    def check(cluster):
    17        if exists:
    18            assert setting in cluster
    19            assert cluster[setting] == expected
    20        else:
    21            assert setting not in cluster
    22
    23    econf_foreach_cluster(econf, check)
    24
    25
    26# Tests a setting in a cluster that has it's own fields. Example: common_http_protocol_options has multiple subfields
    27def _test_cluster_subfields(yaml, setting, expectations={}, exists=True, envoy_version="V2"):
    28    econf = econf_compile(yaml, envoy_version=envoy_version)
    29
    30    def check(cluster):
    31        if exists:
    32            assert setting in cluster
    33        else:
    34            assert setting not in cluster
    35        for key, expected in expectations.items():
    36            print("Checking key: {} for the {} setting in Envoy cluster".format(key, setting))
    37            assert key in cluster[setting]
    38            assert cluster[setting][key] == expected
    39
    40    econf_foreach_cluster(econf, check)
    41
    42
    43# Test dns_type setting in Mapping
    44@pytest.mark.compilertest
    45def test_logical_dns_type():
    46    yaml = module_and_mapping_manifests(None, ["dns_type: logical_dns"])
    47    for v in SUPPORTED_ENVOY_VERSIONS:
    48        # The dns type is listed as just "type"
    49        _test_cluster_setting(
    50            yaml, setting="type", expected="LOGICAL_DNS", exists=True, envoy_version=v
    51        )
    52
    53
    54@pytest.mark.compilertest
    55def test_strict_dns_type():
    56    # Make sure we can configure strict dns as well even though it's the default
    57    yaml = module_and_mapping_manifests(None, ["dns_type: strict_dns"])
    58    for v in SUPPORTED_ENVOY_VERSIONS:
    59        # The dns type is listed as just "type"
    60        _test_cluster_setting(
    61            yaml, setting="type", expected="STRICT_DNS", exists=True, envoy_version=v
    62        )
    63
    64
    65@pytest.mark.compilertest
    66def test_dns_type_wrong():
    67    # Ensure we fallback to strict_dns as the setting when an invalid string is passed
    68    # This is preferable to invalid config and an error is logged
    69    yaml = module_and_mapping_manifests(None, ["dns_type: something_new"])
    70    for v in SUPPORTED_ENVOY_VERSIONS:
    71        # The dns type is listed as just "type"
    72        _test_cluster_setting(
    73            yaml, setting="type", expected="STRICT_DNS", exists=True, envoy_version=v
    74        )
    75
    76
    77@pytest.mark.compilertest
    78def test_logical_dns_type_endpoints():
    79    # Ensure we use endpoint discovery instead of this value when using the endpoint resolver.
    80    yaml = module_and_mapping_manifests(None, ["dns_type: logical_dns", "resolver: endpoint"])
    81    for v in SUPPORTED_ENVOY_VERSIONS:
    82        # The dns type is listed as just "type"
    83        _test_cluster_setting(yaml, setting="type", expected="EDS", exists=True, envoy_version=v)
    84
    85
    86@pytest.mark.compilertest
    87def test_dns_ttl_module():
    88    # Test configuring the respect_dns_ttl generates an Envoy config
    89    yaml = module_and_mapping_manifests(None, ["respect_dns_ttl: true"])
    90    for v in SUPPORTED_ENVOY_VERSIONS:
    91        # The dns type is listed as just "type"
    92        _test_cluster_setting(
    93            yaml, setting="respect_dns_ttl", expected=True, exists=True, envoy_version=v
    94        )
    95
    96
    97@pytest.mark.compilertest
    98def test_dns_ttl_mapping():
    99    # Test dns_ttl is not configured when not applied in the Mapping
   100    yaml = module_and_mapping_manifests(None, None)
   101    for v in SUPPORTED_ENVOY_VERSIONS:
   102        # The dns type is listed as just "type"
   103        _test_cluster_setting(
   104            yaml, setting="respect_dns_ttl", expected=False, exists=False, envoy_version=v
   105        )

View as plain text