forked from konflux-ci/release-service-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pyxis.py
235 lines (169 loc) · 6.65 KB
/
test_pyxis.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
import pyxis
from requests import HTTPError, Response, Session
API_URL = "https://foo.com/v1/bar"
REQUEST_BODY = {
"a": "1",
"b": "2",
}
QUERY = "myquery"
@patch("os.path.exists")
def test_get_session_cert(mock_path_exists: MagicMock, monkeypatch: Any) -> None:
mock_path_exists.return_value = True
monkeypatch.setenv("PYXIS_CERT_PATH", "/path/to/cert.pem")
monkeypatch.setenv("PYXIS_KEY_PATH", "/path/to/key.key")
session = pyxis._get_session()
assert session.cert == ("/path/to/cert.pem", "/path/to/key.key")
@patch("os.path.exists")
def test_get_session_cert_not_exist(mock_path_exists: MagicMock, monkeypatch: Any) -> None:
mock_path_exists.return_value = False
monkeypatch.setenv("PYXIS_CERT_PATH", "/path/to/cert.pem")
monkeypatch.setenv("PYXIS_KEY_PATH", "/path/to/key.key")
with pytest.raises(Exception):
pyxis._get_session()
def test_get_session_no_auth(monkeypatch: Any) -> None:
session = pyxis._get_session(auth_required=False)
assert session.cert is None
@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_post(mock_get_session: MagicMock) -> None:
resp = pyxis.post(API_URL, {})
assert resp == mock_get_session.return_value.post.return_value
mock_get_session.assert_called_once_with()
@patch("pyxis.session")
@patch("pyxis._get_session")
def test_post_existing_session(mock_get_session, mock_session: MagicMock) -> None:
resp = pyxis.post(API_URL, {})
assert resp == mock_session.post.return_value
mock_get_session.assert_not_called()
@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_post_error(mock_get_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_get_session.return_value.post.return_value.raise_for_status.side_effect = HTTPError(
response=response
)
with pytest.raises(HTTPError):
pyxis.post(API_URL, {})
@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_patch(mock_get_session: MagicMock) -> None:
resp = pyxis.patch(API_URL, {})
assert resp == mock_get_session.return_value.patch.return_value
mock_get_session.assert_called_once_with()
@patch("pyxis.session")
@patch("pyxis._get_session")
def test_patch_existing_session(mock_get_session, mock_session: MagicMock) -> None:
resp = pyxis.patch(API_URL, {})
assert resp == mock_session.patch.return_value
mock_get_session.assert_not_called()
@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_patch_error(mock_get_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_get_session.return_value.patch.return_value.raise_for_status.side_effect = HTTPError(
response=response
)
with pytest.raises(HTTPError):
pyxis.patch(API_URL, {})
@patch("pyxis.post")
def test_graphql_query__success(mock_post: MagicMock):
mock_data = {
"output": "something",
}
mock_post.return_value.json.return_value = {
"data": {
QUERY: {
"data": mock_data,
"error": None,
}
}
}
data = pyxis.graphql_query(API_URL, REQUEST_BODY)
assert data[QUERY]["data"] == mock_data
mock_post.assert_called_once_with(API_URL, REQUEST_BODY)
@patch("pyxis.post")
def test_graphql_query__general_graphql_error(mock_post: MagicMock):
"""For example, if there is a syntax error in the query,
the response won't even include the query property"""
mock_post.return_value.json.return_value = {
"data": None,
"errors": [{"message": "Major failure"}],
}
with pytest.raises(RuntimeError):
pyxis.graphql_query(API_URL, REQUEST_BODY)
mock_post.assert_called_once_with(API_URL, REQUEST_BODY)
@patch("pyxis.post")
def test_graphql_query__pyxis_error(mock_post: MagicMock):
"""For example, if the image id does not exist in Pyxis
there will be an error property under the query property"""
mock_post.return_value.json.return_value = {
"data": {
QUERY: {
"data": None,
"error": {"detail": "Not found"},
}
}
}
with pytest.raises(RuntimeError):
pyxis.graphql_query(API_URL, REQUEST_BODY)
mock_post.assert_called_once_with(API_URL, REQUEST_BODY)
@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_put(mock_get_session: MagicMock) -> None:
mock_get_session.return_value.put.return_value.json.return_value = {"key": "val"}
resp = pyxis.put(API_URL, {})
assert resp == {"key": "val"}
mock_get_session.assert_called_once_with()
@patch("pyxis.session")
@patch("pyxis._get_session")
def test_put_existing_session(mock_get_session, mock_session: MagicMock) -> None:
mock_session.put.return_value.json.return_value = {"key": "val"}
resp = pyxis.put(API_URL, {})
assert resp == {"key": "val"}
mock_get_session.assert_not_called()
@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_put_error(mock_get_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_get_session.return_value.put.return_value.raise_for_status.side_effect = HTTPError(
response=response
)
with pytest.raises(HTTPError):
pyxis.put(API_URL, {})
@patch("pyxis.session", None)
@patch("pyxis._get_session")
def test_get(mock_get_session: MagicMock) -> None:
mock_get_session.return_value.get.return_value = {"key": "val"}
resp = pyxis.get(API_URL)
assert resp == {"key": "val"}
mock_get_session.assert_called_once_with()
@patch("pyxis.session")
@patch("pyxis._get_session")
def test_get_existing_session(mock_get_session, mock_session: MagicMock) -> None:
mock_session.get.return_value = {"key": "val"}
resp = pyxis.get(API_URL)
assert resp == {"key": "val"}
mock_get_session.assert_not_called()
def test_add_session_retries() -> None:
status_forcelist = (404, 503)
total = 3
backoff_factor = 0.5
session = Session()
pyxis.add_session_retries(
session,
total=total,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
assert session.adapters["http://"].max_retries.total == total
assert session.adapters["http://"].max_retries.backoff_factor == backoff_factor
assert session.adapters["http://"].max_retries.status_forcelist == status_forcelist
assert session.adapters["https://"].max_retries.total == total
assert session.adapters["https://"].max_retries.backoff_factor == backoff_factor
assert session.adapters["https://"].max_retries.status_forcelist == status_forcelist