-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathtest_github_oidc_token_retry.py
More file actions
105 lines (75 loc) · 2.93 KB
/
Copy pathtest_github_oidc_token_retry.py
File metadata and controls
105 lines (75 loc) · 2.93 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
94
95
96
97
98
99
100
101
102
103
104
105
"""Tests for the retry behavior when minting a GitHub Actions OIDC token."""
from __future__ import annotations
import json
import urllib.error
import urllib.request
from unittest import mock
import pytest
from feldera.testutils import _OIDC_MINT_MAX_RETRIES, _mint_github_oidc_token
class _FakeResponse:
def __init__(self, value: str):
self._body = json.dumps({"value": value}).encode()
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def read(self, *args, **kwargs):
return self._body
def _http_error(request: urllib.request.Request, code: int) -> urllib.error.HTTPError:
return urllib.error.HTTPError(request.full_url, code, "error", {}, None)
def _request() -> urllib.request.Request:
return urllib.request.Request("https://example.test/token")
class TestOidcMintRetry:
def test_retries_on_503_then_succeeds(self):
calls = []
def fake_urlopen(request, timeout):
calls.append(request)
if len(calls) == 1:
raise _http_error(request, 503)
return _FakeResponse("minted-token")
with (
mock.patch("urllib.request.urlopen", side_effect=fake_urlopen),
mock.patch("time.sleep"),
):
token = _mint_github_oidc_token(_request())
assert token == "minted-token"
assert len(calls) == 2
def test_does_not_retry_non_transient_status(self):
calls = []
def fake_urlopen(request, timeout):
calls.append(request)
raise _http_error(request, 401)
with (
mock.patch("urllib.request.urlopen", side_effect=fake_urlopen),
mock.patch("time.sleep"),
):
with pytest.raises(urllib.error.HTTPError) as exc_info:
_mint_github_oidc_token(_request())
assert exc_info.value.code == 401
assert len(calls) == 1
def test_exhausts_retries_and_raises(self):
calls = []
def fake_urlopen(request, timeout):
calls.append(request)
raise _http_error(request, 503)
with (
mock.patch("urllib.request.urlopen", side_effect=fake_urlopen),
mock.patch("time.sleep"),
):
with pytest.raises(urllib.error.HTTPError):
_mint_github_oidc_token(_request())
assert len(calls) == _OIDC_MINT_MAX_RETRIES + 1
def test_retries_on_connection_error(self):
calls = []
def fake_urlopen(request, timeout):
calls.append(request)
if len(calls) == 1:
raise urllib.error.URLError("connection reset")
return _FakeResponse("minted-token")
with (
mock.patch("urllib.request.urlopen", side_effect=fake_urlopen),
mock.patch("time.sleep"),
):
token = _mint_github_oidc_token(_request())
assert token == "minted-token"
assert len(calls) == 2