...
1import os
2
3from tests.utils import assert_valid_envoy_config, econf_compile, module_and_mapping_manifests
4
5
6def _test_bootstrap(yaml, expectations={}):
7 # Compile an envoy config
8 econf = econf_compile(yaml)
9
10 # Get just the bootstrap config...
11 bootstrap = econf["bootstrap"]
12
13 # ...and make sure that Envoy thinks it is valid (it doesn't like the @type field)
14 bootstrap.pop("@type", None)
15 assert_valid_envoy_config(bootstrap)
16
17 for key, expected in expectations.items():
18 if expected is None:
19 assert key not in bootstrap
20 else:
21 pass
22
23 assert key in bootstrap
24 assert bootstrap[key] == expected
25
26
27def _test_dd_entity_id(val, expected):
28 # Setup by setting dd / statsd vars
29 os.environ["STATSD_ENABLED"] = "true"
30 os.environ["STATSD_HOST"] = "0.0.0.0"
31 os.environ["DOGSTATSD"] = "true"
32 if val:
33 os.environ["DD_ENTITY_ID"] = val
34
35 # Run the bootstrap test. We don't need any special yaml
36 # since all of this behavior is driven by env vars.
37 yaml = module_and_mapping_manifests(None, [])
38 _test_bootstrap(yaml, expectations={"stats_config": expected})
39
40 # Teardown by removing dd / statsd vars
41 del os.environ["STATSD_ENABLED"]
42 del os.environ["STATSD_HOST"]
43 del os.environ["DOGSTATSD"]
44 if val:
45 del os.environ["DD_ENTITY_ID"]
46
47
48def test_dd_entity_id_missing():
49 # If we do not set the env var, then stats config should be missing.
50 _test_dd_entity_id(None, None)
51
52
53def test_dd_entity_id_empty():
54 # If we set the env var to the empty string, the stats config should be missing.
55 _test_dd_entity_id("", None)
56
57
58def test_dd_entity_id_set():
59 # If we set the env var, then it should show up the config.
60 _test_dd_entity_id(
61 "my.cool.1234.entity-id",
62 {
63 "stats_tags": [
64 {"tag_name": "dd.internal.entity_id", "fixed_value": "my.cool.1234.entity-id"}
65 ]
66 },
67 )
68
69
70def test_dd_entity_id_set_typical():
71 # If we set the env var to a typical pod UID, then it should show up int he config.
72 _test_dd_entity_id(
73 "1fb8f8d8-00b3-44ef-bc8b-3659e4a3c2bd",
74 {
75 "stats_tags": [
76 {
77 "tag_name": "dd.internal.entity_id",
78 "fixed_value": "1fb8f8d8-00b3-44ef-bc8b-3659e4a3c2bd",
79 }
80 ]
81 },
82 )
View as plain text