...
1import importlib
2import logging
3import os
4
5import pytest
6
7logger = logging.getLogger("ambassador")
8
9import ambassador.envoy.v3.v3ready
10from ambassador import IR, Config, EnvoyConfig
11from ambassador.utils import NullSecretHandler
12
13
14def _get_envoy_config() -> EnvoyConfig:
15 """Helper function for getting an envoy config with correctly initialized ready listener"""
16 aconf = Config()
17
18 secret_handler = NullSecretHandler(logger, None, None, "0")
19 ir = IR(aconf, file_checker=lambda path: True, secret_handler=secret_handler)
20 assert ir
21
22 # This is required in order to reload module-level variables that depend on os.environ.
23 # Otherwise the module variables can have stale values.
24 importlib.reload(ambassador.envoy.v3.v3ready)
25 return EnvoyConfig.generate(ir)
26
27
28def _validate_ready_listener_config(econf: EnvoyConfig, expectedPort: int, readyLogEnabled: bool):
29 """
30 Helper function that fails if the envoy config does not match an expected ready listener config
31 """
32 conf = econf.as_dict()
33 readyListener = conf["static_resources"]["listeners"][0]
34 assert readyListener["address"]["socket_address"]["address"] == "127.0.0.1"
35 assert readyListener["address"]["socket_address"]["port_value"] == expectedPort
36 assert readyListener["name"] == "ambassador-listener-ready-127.0.0.1-%d" % expectedPort
37
38 filterTypedConfig = readyListener["filter_chains"][0]["filters"][0]["typed_config"]
39 assert (
40 filterTypedConfig["http_filters"][0]["typed_config"]["headers"][0]["exact_match"]
41 == "/ready"
42 )
43 if readyLogEnabled:
44 assert "access_log" in filterTypedConfig
45 else:
46 assert "access_log" not in filterTypedConfig
47
48
49@pytest.mark.compilertest
50def test_ready_listener_custom():
51 """
52 Test to ensure that the ready endpoint is configurable using the AMBASSADOR_READY_PORT and
53 AMBASSADOR_READY_LOG environment variables.
54 """
55 os.environ["AMBASSADOR_READY_PORT"] = str(8010)
56 os.environ["AMBASSADOR_READY_LOG"] = str(True)
57 econf = _get_envoy_config()
58 _validate_ready_listener_config(econf, 8010, True)
59
60
61@pytest.mark.compilertest
62def test_ready_listener_default():
63 """Test to ensure that the ready endpoint is configured with sensible defaults."""
64 # Unset environment variables that could have been set in an earlier test
65 os.environ.pop("AMBASSADOR_READY_PORT", None)
66 os.environ.pop("AMBASSADOR_READY_LOG", None)
67 econf = _get_envoy_config()
68 _validate_ready_listener_config(econf, 8006, False)
69
70
71@pytest.mark.compilertest
72def test_ready_listener_high_port():
73 """
74 Test to ensure that the ready endpoint falls back to the default port if a port above 32767 is
75 used.
76 """
77 os.environ["AMBASSADOR_READY_PORT"] = str(32767)
78 os.environ.pop("AMBASSADOR_READY_LOG", None)
79 econf = _get_envoy_config()
80 _validate_ready_listener_config(econf, 8006, False)
81
82
83@pytest.mark.compilertest
84def test_ready_listener_invalid_logs():
85 """
86 Test to ensure that invalid values passed to AMBASSADOR_READY_LOG are handled gracefully.
87 """
88 os.environ.pop("AMBASSADOR_READY_PORT")
89 os.environ["AMBASSADOR_READY_LOG"] = "0.6666666666666666"
90 econf = _get_envoy_config()
91 _validate_ready_listener_config(econf, 8006, False)
92
93
94@pytest.mark.compilertest
95def test_ready_listener_invalid_port():
96 """
97 Test to ensure that an error is raised when an invalid port string is passed to
98 AMBASSADOR_READY_PORT.
99 """
100 os.environ["AMBASSADOR_READY_PORT"] = "420/1337.abcd"
101 os.environ.pop("AMBASSADOR_READY_LOG", None)
102 with pytest.raises(ValueError):
103 econf = _get_envoy_config()
104 _validate_ready_listener_config(econf, 0, False)
105
106
107@pytest.mark.compilertest
108def test_ready_listener_zero_port():
109 """
110 Test to ensure that the ready endpoint falls back to the default port if a zero port is used.
111 """
112 os.environ["AMBASSADOR_READY_PORT"] = str(0)
113 os.environ.pop("AMBASSADOR_READY_LOG", None)
114 econf = _get_envoy_config()
115 _validate_ready_listener_config(econf, 8006, False)
View as plain text