| from __future__ import annotations | |
| import pytest | |
| from ylff.services.orchestration.lambda_cloud import ( | |
| LambdaCloudClient, | |
| LambdaCloudClientConfig, | |
| LambdaCloudError, | |
| ) | |
| class _Resp: | |
| def __init__(self, *, status_code: int, json_obj=None, text: str = "", headers=None): | |
| self.status_code = int(status_code) | |
| self._json = json_obj | |
| self.text = text | |
| self.headers = dict(headers or {}) | |
| def json(self): | |
| if isinstance(self._json, Exception): | |
| raise self._json | |
| return self._json | |
| def test_lambda_cloud_client_success(monkeypatch): | |
| def _fake_request(method, url, headers=None, json=None, timeout=None): | |
| assert method == "GET" | |
| assert url.endswith("/instances") | |
| return _Resp(status_code=200, json_obj={"data": [{"id": "i-1"}]}, text="ok") | |
| import ylff.services.orchestration.lambda_cloud as lc | |
| monkeypatch.setattr(lc.requests, "request", _fake_request) | |
| c = LambdaCloudClient(LambdaCloudClientConfig(api_key="k", min_interval_s=0.0, max_retries=0)) | |
| out = c.list_instances() | |
| assert out["data"][0]["id"] == "i-1" | |
| def test_lambda_cloud_client_error_code_normalization(monkeypatch): | |
| def _fake_request(method, url, headers=None, json=None, timeout=None): | |
| return _Resp( | |
| status_code=400, | |
| json_obj={"error": {"code": "bad_request", "message": "nope"}}, | |
| text="err", | |
| ) | |
| import ylff.services.orchestration.lambda_cloud as lc | |
| monkeypatch.setattr(lc.requests, "request", _fake_request) | |
| c = LambdaCloudClient(LambdaCloudClientConfig(api_key="k", min_interval_s=0.0, max_retries=0)) | |
| with pytest.raises(LambdaCloudError) as ei: | |
| c.list_instances() | |
| assert ei.value.code == "bad_request" | |
| assert ei.value.status_code == 400 | |