|
4 | 4 | import sys |
5 | 5 | import warnings |
6 | 6 |
|
| 7 | +import pytest |
| 8 | + |
7 | 9 |
|
8 | 10 | def _reload_urls_module(): |
9 | 11 | """Reload core.urls so module-level env reads pick up the current env.""" |
@@ -118,3 +120,103 @@ def test_default_console_when_neither_set(self, monkeypatch): |
118 | 120 | monkeypatch.delenv("CONSOLE_BASE_URL", raising=False) |
119 | 121 | mod = _reload_urls_module() |
120 | 122 | assert mod.RUNPOD_CONSOLE_URL == "https://console.runpod.io" |
| 123 | + |
| 124 | + |
| 125 | +class TestPartialOverrideWarning: |
| 126 | + """A partial URL override is a common misconfiguration signal.""" |
| 127 | + |
| 128 | + @pytest.fixture(autouse=True) |
| 129 | + def clear_all_url_env(self, monkeypatch): |
| 130 | + for name in ( |
| 131 | + "RUNPOD_API_BASE_URL", |
| 132 | + "RUNPOD_ENDPOINT_BASE_URL", |
| 133 | + "RUNPOD_REST_API_URL", |
| 134 | + "RUNPOD_HAPI_URL", |
| 135 | + "RUNPOD_HAPI_BASE_URL", |
| 136 | + "RUNPOD_CONSOLE_URL", |
| 137 | + "CONSOLE_BASE_URL", |
| 138 | + ): |
| 139 | + monkeypatch.delenv(name, raising=False) |
| 140 | + |
| 141 | + @pytest.fixture |
| 142 | + def prod_runpod_endpoint(self, monkeypatch): |
| 143 | + """Pin runpod.endpoint_url_base to prod default. |
| 144 | +
|
| 145 | + ``runpod.endpoint_url_base`` is captured once at runpod-package |
| 146 | + import time and never re-read, so deleting ``RUNPOD_ENDPOINT_BASE_URL`` |
| 147 | + from the environment does not reset it. Tests that want the endpoint |
| 148 | + URL to look like prod must force it here; otherwise a leaked env var |
| 149 | + in the host shell or CI runner makes the resolved endpoint URL |
| 150 | + "overridden" and silently corrupts assertions. |
| 151 | + """ |
| 152 | + import runpod |
| 153 | + |
| 154 | + monkeypatch.setattr(runpod, "endpoint_url_base", "https://api.runpod.ai/v2") |
| 155 | + |
| 156 | + @pytest.fixture |
| 157 | + def custom_runpod_endpoint(self, monkeypatch): |
| 158 | + """Pin runpod.endpoint_url_base to a custom dev-like URL. |
| 159 | +
|
| 160 | + Same caching caveat as ``prod_runpod_endpoint``: setting |
| 161 | + ``RUNPOD_ENDPOINT_BASE_URL`` via ``monkeypatch.setenv`` is not |
| 162 | + sufficient because ``runpod.endpoint_url_base`` was already cached. |
| 163 | + """ |
| 164 | + import runpod |
| 165 | + |
| 166 | + monkeypatch.setattr( |
| 167 | + runpod, "endpoint_url_base", "https://endpoint.custom.test/v2" |
| 168 | + ) |
| 169 | + |
| 170 | + def test_no_warning_when_all_at_default(self, prod_runpod_endpoint): |
| 171 | + with warnings.catch_warnings(record=True) as captured: |
| 172 | + warnings.simplefilter("always") |
| 173 | + _reload_urls_module() |
| 174 | + assert not any( |
| 175 | + issubclass(w.category, RuntimeWarning) |
| 176 | + and "Partial Runpod URL override" in str(w.message) |
| 177 | + for w in captured |
| 178 | + ) |
| 179 | + |
| 180 | + def test_warning_when_partially_overridden(self, monkeypatch, prod_runpod_endpoint): |
| 181 | + monkeypatch.setenv("RUNPOD_API_BASE_URL", "https://api.custom.test") |
| 182 | + |
| 183 | + with warnings.catch_warnings(record=True) as captured: |
| 184 | + warnings.simplefilter("always") |
| 185 | + _reload_urls_module() |
| 186 | + |
| 187 | + matches = [ |
| 188 | + w |
| 189 | + for w in captured |
| 190 | + if issubclass(w.category, RuntimeWarning) |
| 191 | + and "Partial Runpod URL override" in str(w.message) |
| 192 | + ] |
| 193 | + assert matches, "Expected a partial-override RuntimeWarning" |
| 194 | + msg = str(matches[0].message) |
| 195 | + overridden_section = msg.split("Overridden:")[1].split("Still at default:")[0] |
| 196 | + default_section = msg.split("Still at default:")[1] |
| 197 | + assert "RUNPOD_API_BASE_URL" in overridden_section |
| 198 | + for name in ( |
| 199 | + "RUNPOD_ENDPOINT_BASE_URL", |
| 200 | + "RUNPOD_REST_API_URL", |
| 201 | + "RUNPOD_HAPI_URL", |
| 202 | + "RUNPOD_CONSOLE_URL", |
| 203 | + ): |
| 204 | + assert name in default_section, f"{name} miscategorized" |
| 205 | + |
| 206 | + def test_no_warning_when_all_overridden(self, monkeypatch, custom_runpod_endpoint): |
| 207 | + monkeypatch.setenv("RUNPOD_API_BASE_URL", "https://api.custom.test") |
| 208 | + monkeypatch.setenv( |
| 209 | + "RUNPOD_ENDPOINT_BASE_URL", "https://endpoint.custom.test/v2" |
| 210 | + ) |
| 211 | + monkeypatch.setenv("RUNPOD_REST_API_URL", "https://rest.custom.test/v1") |
| 212 | + monkeypatch.setenv("RUNPOD_HAPI_URL", "https://hapi.custom.test") |
| 213 | + monkeypatch.setenv("RUNPOD_CONSOLE_URL", "https://console.custom.test") |
| 214 | + |
| 215 | + with warnings.catch_warnings(record=True) as captured: |
| 216 | + warnings.simplefilter("always") |
| 217 | + _reload_urls_module() |
| 218 | + assert not any( |
| 219 | + issubclass(w.category, RuntimeWarning) |
| 220 | + and "Partial Runpod URL override" in str(w.message) |
| 221 | + for w in captured |
| 222 | + ) |
0 commit comments