|
| 1 | +import functools |
1 | 2 | import os |
2 | 3 | import textwrap |
3 | 4 | import unittest.mock |
4 | 5 | from collections.abc import Generator |
| 6 | +from typing import Any |
5 | 7 | from unittest.mock import Mock |
6 | 8 |
|
7 | 9 | import pytest |
@@ -94,6 +96,40 @@ def invalid_urls(request: pytest.FixtureRequest) -> tuple[str, str]: |
94 | 96 | return request.param |
95 | 97 |
|
96 | 98 |
|
| 99 | +@pytest.fixture( |
| 100 | + params=[ |
| 101 | + "websockets.endpoint", |
| 102 | + "websockets.ssl.cert_file", |
| 103 | + "metrics.otlp.endpoint", |
| 104 | + "metrics.prometheus.endpoint", |
| 105 | + "health.endpoint", |
| 106 | + ] |
| 107 | +) |
| 108 | +def config_env_vars(request: pytest.FixtureRequest) -> tuple[str, dict[str, str], Any]: |
| 109 | + match request.param: |
| 110 | + case "websockets.endpoint": |
| 111 | + expected = fake.uri(["wss", "ws"]) |
| 112 | + env_vars = {"CLOUDPIN_WEBSOCKETS_ENDPOINT": expected} |
| 113 | + case "websockets.ssl.cert_file": |
| 114 | + expected = fake.file_path() |
| 115 | + env_vars = { |
| 116 | + "CLOUDPIN_WEBSOCKETS_SSL_CERT_FILE": expected, |
| 117 | + "CLOUDPIN_WEBSOCKETS_SSL_KEY_FILE": fake.file_path(), |
| 118 | + } |
| 119 | + case "metrics.otlp.endpoint": |
| 120 | + expected = fake.uri(["http"]) |
| 121 | + env_vars = {"CLOUDPIN_METRICS_OTLP_ENDPOINT": expected} |
| 122 | + case "metrics.prometheus.endpoint": |
| 123 | + expected = fake.uri(["http"]) |
| 124 | + env_vars = {"CLOUDPIN_METRICS_PROMETHEUS_ENDPOINT": expected} |
| 125 | + case "health.endpoint": |
| 126 | + expected = fake.uri(["http"]) |
| 127 | + env_vars = {"CLOUDPIN_HEALTH_ENDPOINT": expected} |
| 128 | + case _: |
| 129 | + raise ValueError |
| 130 | + return request.param, env_vars, expected |
| 131 | + |
| 132 | + |
97 | 133 | def test_load_config_when_valid_urls( |
98 | 134 | valid_urls: tuple[str, str], some_cli_config: dict[str, str] |
99 | 135 | ) -> None: |
@@ -121,18 +157,19 @@ def test_load_config_when_invalid_urls( |
121 | 157 | load_config(cli_args) |
122 | 158 |
|
123 | 159 |
|
124 | | -def test_load_config_with_environ_var(some_cli_config: dict[str, str]) -> None: |
125 | | - endpoint = fake.uri(["wss", "ws"]) |
126 | | - environ_vars = {"CLOUDPIN_WEBSOCKETS_ENDPOINT": endpoint} |
| 160 | +def test_load_config_with_environ_var( |
| 161 | + some_cli_config: dict[str, str], config_env_vars: tuple[str, str, Any] |
| 162 | +) -> None: |
| 163 | + attr, env_vars, expected = config_env_vars |
127 | 164 | cli_config = some_cli_config.copy() |
128 | | - del cli_config["websockets.endpoint"] |
| 165 | + cli_config.pop(attr, None) |
129 | 166 | cli_args = ["=".join(arg) for arg in cli_config.items()] |
130 | 167 |
|
131 | | - with unittest.mock.patch.dict(os.environ, environ_vars): |
| 168 | + with unittest.mock.patch.dict(os.environ, env_vars): |
132 | 169 | result = load_config(cli_args) |
133 | 170 |
|
134 | 171 | assert isinstance(result, (ServerServiceConfig, ClientServiceConfig)) |
135 | | - assert result.websockets.endpoint == endpoint |
| 172 | + assert expected == functools.reduce(getattr, attr.split("."), result) |
136 | 173 |
|
137 | 174 |
|
138 | 175 | def test_load_config_with_file(some_cli_config: dict[str, str]) -> None: |
|
0 commit comments