...

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

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

     1import os
     2
     3from tests.utils import assert_valid_envoy_config, econf_compile, module_and_mapping_manifests
     4
     5
     6def _test_bootstrap(yaml, v2, expectations={}):
     7    # Compile an envoy config
     8    if v2:
     9        econf = econf_compile(yaml, envoy_version="V2")
    10    else:
    11        econf = econf_compile(yaml)
    12
    13    # Get just the bootstrap config...
    14    bootstrap = econf["bootstrap"]
    15
    16    # ...and make sure that Envoy thinks it is valid (it doesn't like the @type field)
    17    bootstrap.pop("@type", None)
    18    assert_valid_envoy_config(bootstrap, v2=v2)
    19
    20    for key, expected in expectations.items():
    21        if expected is None:
    22            assert key not in bootstrap
    23        else:
    24            assert key in bootstrap
    25            assert bootstrap[key] == expected
    26
    27
    28def _test_dd_entity_id(val, expected, v2):
    29    # Setup by setting dd / statsd vars
    30    os.environ["STATSD_ENABLED"] = "true"
    31    os.environ["STATSD_HOST"] = "0.0.0.0"
    32    os.environ["DOGSTATSD"] = "true"
    33    if val:
    34        os.environ["DD_ENTITY_ID"] = val
    35
    36    # Run the bootstrap test. We don't need any special yaml
    37    # since all of this behavior is driven by env vars.
    38    yaml = module_and_mapping_manifests(None, [])
    39    _test_bootstrap(yaml, v2, expectations={"stats_config": expected})
    40
    41    # Teardown by removing dd / statsd vars
    42    del os.environ["STATSD_ENABLED"]
    43    del os.environ["STATSD_HOST"]
    44    del os.environ["DOGSTATSD"]
    45    if val:
    46        del os.environ["DD_ENTITY_ID"]
    47
    48
    49def test_dd_entity_id_missing_v3():
    50    # If we do not set the env var, then stats config should be missing.
    51    _test_dd_entity_id(None, None, False)
    52
    53
    54def test_dd_entity_id_empty_v3():
    55    # If we set the env var to the empty string, the stats config should be missing.
    56    _test_dd_entity_id("", None, False)
    57
    58
    59def test_dd_entity_id_set_v3():
    60    # If we set the env var, then it should show up the config.
    61    _test_dd_entity_id(
    62        "my.cool.1234.entity-id",
    63        {
    64            "stats_tags": [
    65                {"tag_name": "dd.internal.entity_id", "fixed_value": "my.cool.1234.entity-id"}
    66            ]
    67        },
    68        False,
    69    )
    70
    71
    72def test_dd_entity_id_set_typical_v3():
    73    # If we set the env var to a typical pod UID, then it should show up int he config.
    74    _test_dd_entity_id(
    75        "1fb8f8d8-00b3-44ef-bc8b-3659e4a3c2bd",
    76        {
    77            "stats_tags": [
    78                {
    79                    "tag_name": "dd.internal.entity_id",
    80                    "fixed_value": "1fb8f8d8-00b3-44ef-bc8b-3659e4a3c2bd",
    81                }
    82            ]
    83        },
    84        False,
    85    )
    86
    87
    88def test_dd_entity_id_missing_v2():
    89    # If we do not set the env var, then stats config should be missing.
    90    _test_dd_entity_id(None, None, True)
    91
    92
    93def test_dd_entity_id_empty_v2():
    94    # If we set the env var to the empty string, the stats config should be missing.
    95    _test_dd_entity_id("", None, True)
    96
    97
    98def test_dd_entity_id_set_v2():
    99    # If we set the env var, then it should show up the config.
   100    _test_dd_entity_id(
   101        "my.cool.1234.entity-id",
   102        {
   103            "stats_tags": [
   104                {"tag_name": "dd.internal.entity_id", "fixed_value": "my.cool.1234.entity-id"}
   105            ]
   106        },
   107        True,
   108    )
   109
   110
   111def test_dd_entity_id_set_typical_v2():
   112    # If we set the env var to a typical pod UID, then it should show up int he config.
   113    _test_dd_entity_id(
   114        "1fb8f8d8-00b3-44ef-bc8b-3659e4a3c2bd",
   115        {
   116            "stats_tags": [
   117                {
   118                    "tag_name": "dd.internal.entity_id",
   119                    "fixed_value": "1fb8f8d8-00b3-44ef-bc8b-3659e4a3c2bd",
   120                }
   121            ]
   122        },
   123        True,
   124    )

View as plain text