-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_insufficient_balance.py
More file actions
93 lines (79 loc) · 3.36 KB
/
Copy pathtest_insufficient_balance.py
File metadata and controls
93 lines (79 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""HTTP 483 (insufficient balance) must fail fast — no orch fallback."""
from __future__ import annotations
from io import BytesIO
from unittest.mock import MagicMock, patch
from urllib.error import HTTPError
import pytest
from livepeer_gateway.byoc import _create_byoc_payment
from livepeer_gateway.errors import InsufficientBalance, NoOrchestratorAvailableError
from livepeer_gateway.lv2v import StartJobRequest, start_lv2v
from livepeer_gateway.orchestrator import request_json
def test_request_json_maps_483_to_insufficient_balance() -> None:
body = b'{"error":"Starter allowance exhausted"}'
err = HTTPError(
"https://signer.example/generate-live-payment",
483,
"Insufficient Balance",
hdrs=None, # type: ignore[arg-type]
fp=BytesIO(body),
)
with patch("livepeer_gateway.orchestrator.urlopen", side_effect=err):
with pytest.raises(InsufficientBalance, match="Starter allowance exhausted"):
request_json(
"https://signer.example/generate-live-payment",
payload={},
)
def test_create_byoc_payment_preserves_483_as_insufficient_balance() -> None:
info = MagicMock()
info.HasField.return_value = True
info.ticket_params.face_value = b"\x01"
info.SerializeToString.return_value = b"orchestrator-info"
err = HTTPError(
"https://signer.example/generate-live-payment",
483,
"Insufficient Balance",
hdrs=None, # type: ignore[arg-type]
fp=BytesIO(b"Starter allowance exhausted"),
)
with (
patch("livepeer_gateway.orch_info.get_orch_info", return_value=info),
patch("livepeer_gateway.byoc.urlopen", side_effect=err),
):
with pytest.raises(InsufficientBalance, match="Starter allowance exhausted"):
_create_byoc_payment(
orch_origin="https://orch.example:8936",
capability="noop",
livepeer_hdr="header",
signer_url="https://signer.example",
)
def test_start_lv2v_fails_fast_on_483_without_orch_fallback() -> None:
info_a = MagicMock()
info_a.transcoder = "https://orch-a.example:8935"
info_b = MagicMock()
info_b.transcoder = "https://orch-b.example:8935"
cursor = MagicMock()
cursor.next.side_effect = [
("https://orch-a.example:8935", info_a),
("https://orch-b.example:8935", info_b),
NoOrchestratorAvailableError("exhausted", rejections=[]),
]
payment_session = MagicMock()
payment_session.get_payment.side_effect = InsufficientBalance(
"Signer returned HTTP 483 (insufficient balance) "
"(url=https://signer.example/generate-live-payment); "
"body='Starter allowance exhausted'"
)
with (
patch("livepeer_gateway.lv2v.orchestrator_selector", return_value=cursor),
patch("livepeer_gateway.lv2v.PaymentSession", return_value=payment_session),
patch("livepeer_gateway.lv2v.build_capabilities"),
):
with pytest.raises(InsufficientBalance, match="Starter allowance exhausted"):
start_lv2v(
None,
StartJobRequest(model_id="noop"),
signer_url="https://signer.example",
discovery_url="https://discovery.example/raw",
)
assert cursor.next.call_count == 1
assert payment_session.get_payment.call_count == 1