|
7 | 7 | from inline_snapshot import snapshot |
8 | 8 |
|
9 | 9 | from tests import respx2 |
10 | | -from openai import OpenAI, OAuthError |
| 10 | +from openai import OpenAI, OAuthError, SubjectTokenProviderError |
11 | 11 | from openai.auth import WorkloadIdentity, WorkloadIdentityAuth, SubjectTokenWorkloadIdentity |
12 | 12 | from tests.respx2.models import Call |
13 | 13 | from openai.auth._workload import ( |
@@ -198,3 +198,44 @@ def test_gcp_id_token_provider() -> None: |
198 | 198 |
|
199 | 199 | assert provider["token_type"] == "id" |
200 | 200 | assert provider["get_token"]() == "gcp-token" |
| 201 | + |
| 202 | + |
| 203 | +def test_k8s_service_account_token_provider_empty_file(tmp_path: Path) -> None: |
| 204 | + token_file = tmp_path / "token" |
| 205 | + token_file.write_text("") |
| 206 | + |
| 207 | + provider = k8s_service_account_token_provider(token_file) |
| 208 | + with pytest.raises(SubjectTokenProviderError) as exc_info: |
| 209 | + provider["get_token"]() |
| 210 | + assert str(exc_info.value) == f"The token file at {token_file} is empty." |
| 211 | + |
| 212 | + |
| 213 | +@respx2.mock |
| 214 | +def test_azure_managed_identity_token_provider_error() -> None: |
| 215 | + respx2.get("http://169.254.169.254/metadata/identity/oauth2/token").mock( |
| 216 | + return_value=httpx2.Response(500, text="Internal Server Error") |
| 217 | + ) |
| 218 | + |
| 219 | + provider = azure_managed_identity_token_provider() |
| 220 | + |
| 221 | + with pytest.raises(SubjectTokenProviderError) as exc_info: |
| 222 | + provider["get_token"]() |
| 223 | + assert exc_info.value.response is not None |
| 224 | + assert exc_info.value.response.status_code == 500 |
| 225 | + assert str(exc_info.value) == "Failed to fetch Azure subject token from IMDS: HTTP 500" |
| 226 | + |
| 227 | + |
| 228 | +@respx2.mock |
| 229 | +def test_gcp_id_token_provider_error() -> None: |
| 230 | + respx2.get("http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity").mock( |
| 231 | + return_value=httpx2.Response(404, text="Not Found") |
| 232 | + ) |
| 233 | + |
| 234 | + provider = gcp_id_token_provider() |
| 235 | + |
| 236 | + with pytest.raises(SubjectTokenProviderError) as exc_info: |
| 237 | + provider["get_token"]() |
| 238 | + assert exc_info.value.response is not None |
| 239 | + assert exc_info.value.response.status_code == 404 |
| 240 | + assert str(exc_info.value) == "Failed to fetch GCP subject token from metadata server: HTTP 404" |
| 241 | + |
0 commit comments