- Updated Selectel documentation to clarify error responses related to balance issues. - Introduced `peek_balance_rub` function for best-effort balance checks, handling API failures gracefully. - Improved error messages for insufficient funds and quota issues, specifying Selectel's 403 policy response. - Added balance checks in the doctor command to ensure users are informed about their balance status before attempting GPU creation. - Refactored exception handling in cloud operations to provide clearer feedback on balance-related errors. Co-authored-by: Cursor <cursoragent@cursor.com>
52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
from types import SimpleNamespace
|
|
|
|
from gpu_rent.doctor import Check, _check_balance
|
|
from gpu_rent.errors import CloudError
|
|
|
|
|
|
def test_check_balance_skips_without_token():
|
|
checks: list[Check] = []
|
|
_check_balance(SimpleNamespace(selectel_api_token=""), checks)
|
|
assert checks[0].name == "баланс ₽"
|
|
assert checks[0].ok is True
|
|
assert checks[0].blocking is False
|
|
|
|
|
|
def test_check_balance_blocks_when_empty(monkeypatch):
|
|
monkeypatch.setattr("gpu_rent.balance.fetch_balance_rub", lambda *_a, **_k: 0.0)
|
|
checks: list[Check] = []
|
|
_check_balance(SimpleNamespace(selectel_api_token="tok"), checks)
|
|
assert checks[0].ok is False
|
|
assert checks[0].blocking is True
|
|
assert "0 ₽" in checks[0].detail
|
|
assert "Пополни" in checks[0].detail
|
|
|
|
|
|
def test_check_balance_warns_when_low(monkeypatch):
|
|
monkeypatch.setattr("gpu_rent.balance.fetch_balance_rub", lambda *_a, **_k: 40.0)
|
|
checks: list[Check] = []
|
|
_check_balance(SimpleNamespace(selectel_api_token="tok"), checks)
|
|
assert checks[0].ok is True
|
|
assert checks[0].blocking is False
|
|
assert "40 ₽" in checks[0].detail
|
|
|
|
|
|
def test_check_balance_ok_when_healthy(monkeypatch):
|
|
monkeypatch.setattr("gpu_rent.balance.fetch_balance_rub", lambda *_a, **_k: 1200.0)
|
|
checks: list[Check] = []
|
|
_check_balance(SimpleNamespace(selectel_api_token="tok"), checks)
|
|
assert checks[0].ok is True
|
|
assert "1200 ₽" in checks[0].detail
|
|
|
|
|
|
def test_check_balance_fetch_error_is_warning(monkeypatch):
|
|
def boom(*_a, **_k):
|
|
raise CloudError("сеть")
|
|
|
|
monkeypatch.setattr("gpu_rent.balance.fetch_balance_rub", boom)
|
|
checks: list[Check] = []
|
|
_check_balance(SimpleNamespace(selectel_api_token="tok"), checks)
|
|
assert checks[0].ok is True
|
|
assert checks[0].blocking is False
|
|
assert "не прочитать" in checks[0].detail
|