...
1import pytest
2
3from tests.utils import (
4 SUPPORTED_ENVOY_VERSIONS,
5 econf_compile,
6 econf_foreach_hcm,
7 module_and_mapping_manifests,
8 zipkin_tracing_service_manifest,
9)
10
11
12def _test_router(yaml, expectations={}):
13 for v in SUPPORTED_ENVOY_VERSIONS:
14 econf = econf_compile(yaml, envoy_version=v)
15
16 def check(typed_config):
17 http_filters = typed_config["http_filters"]
18 assert len(http_filters) == 2
19
20 # Find the typed router config, and run our uexpecations over that.
21 for http_filter in http_filters:
22 if http_filter["name"] != "envoy.filters.http.router":
23 continue
24
25 # If we expect nothing, then the typed config should be missing entirely.
26 if len(expectations) == 0:
27 assert "typed_config" not in http_filter
28 break
29
30 assert "typed_config" in http_filter
31 typed_config = http_filter["typed_config"]
32 if v == "V2":
33 assert (
34 typed_config["@type"]
35 == "type.googleapis.com/envoy.config.filter.http.router.v2.Router"
36 )
37 else:
38 assert (
39 typed_config["@type"]
40 == "type.googleapis.com/envoy.extensions.filters.http.router.v3.Router"
41 )
42 for key, expected in expectations.items():
43 print("checking key %s" % key)
44 assert key in typed_config
45 assert typed_config[key] == expected
46 break
47
48 econf_foreach_hcm(econf, check, envoy_version=v)
49
50
51@pytest.mark.compilertest
52def test_suppress_envoy_headers():
53 # If we do not set the config, it should not appear.
54 yaml = module_and_mapping_manifests(None, [])
55 _test_router(yaml, expectations={})
56
57 # If we set the config to false, it should not appear.
58 yaml = module_and_mapping_manifests(["suppress_envoy_headers: false"], [])
59 _test_router(yaml, expectations={})
60
61 # If we set the config to true, it should appear.
62 yaml = module_and_mapping_manifests(["suppress_envoy_headers: true"], [])
63 _test_router(yaml, expectations={"suppress_envoy_headers": True})
64
65
66@pytest.mark.compilertest
67def test_tracing_service():
68 # If we have a tracing service, we should see start_child_span
69 yaml = module_and_mapping_manifests(None, []) + "\n" + zipkin_tracing_service_manifest()
70 _test_router(yaml, expectations={"start_child_span": True})
71
72
73@pytest.mark.compilertest
74def test_tracing_service_and_suppress_envoy_headers():
75 # If we set both suppress_envoy_headers and include a TracingService,
76 # we should see both suppress_envoy_headers and the default start_child_span
77 # value (True).
78 yaml = (
79 module_and_mapping_manifests(["suppress_envoy_headers: true"], [])
80 + "\n"
81 + zipkin_tracing_service_manifest()
82 )
83 _test_router(yaml, expectations={"start_child_span": True, "suppress_envoy_headers": True})
View as plain text