-
Notifications
You must be signed in to change notification settings - Fork 4k
/
test_rest_utils.py
324 lines (280 loc) · 11 KB
/
test_rest_utils.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python
from unittest import mock
import numpy
import pytest
from mlflow.exceptions import MlflowException, RestException
from mlflow.pyfunc.scoring_server import NumpyEncoder
from mlflow.utils.rest_utils import (
http_request,
http_request_safe,
MlflowHostCreds,
_DEFAULT_HEADERS,
call_endpoint,
call_endpoints,
_can_parse_as_json_object,
)
from mlflow.protos.service_pb2 import GetRun
from mlflow.protos.databricks_pb2 import ENDPOINT_NOT_FOUND, ErrorCode
from tests import helper_functions
def test_well_formed_json_error_response():
with mock.patch("requests.Session.request") as request_mock:
host_only = MlflowHostCreds("http://my-host")
response_mock = mock.MagicMock()
response_mock.status_code = 400
response_mock.text = "{}" # well-formed JSON error response
request_mock.return_value = response_mock
response_proto = GetRun.Response()
with pytest.raises(RestException, match="INTERNAL_ERROR"):
call_endpoint(host_only, "/my/endpoint", "GET", "", response_proto)
def test_non_json_ok_response():
with mock.patch("requests.Session.request") as request_mock:
host_only = MlflowHostCreds("http://my-host")
response_mock = mock.MagicMock()
response_mock.status_code = 200
response_mock.text = "<html></html>"
request_mock.return_value = response_mock
response_proto = GetRun.Response()
with pytest.raises(
MlflowException,
match="API request to endpoint was successful but the response body was not "
"in a valid JSON format",
):
call_endpoint(host_only, "/api/2.0/fetch-model", "GET", "", response_proto)
@pytest.mark.parametrize(
"response_mock",
[
helper_functions.create_mock_response(400, "Error message but not a JSON string"),
helper_functions.create_mock_response(400, ""),
helper_functions.create_mock_response(400, None),
],
)
def test_malformed_json_error_response(response_mock):
with mock.patch("requests.Session.request") as request_mock:
host_only = MlflowHostCreds("http://my-host")
request_mock.return_value = response_mock
response_proto = GetRun.Response()
with pytest.raises(
MlflowException, match="API request to endpoint /my/endpoint failed with error code 400"
):
call_endpoint(host_only, "/my/endpoint", "GET", "", response_proto)
def test_call_endpoints():
with mock.patch("mlflow.utils.rest_utils.call_endpoint") as mock_call_endpoint:
response_proto = GetRun.Response()
mock_call_endpoint.side_effect = [
RestException({"error_code": ErrorCode.Name(ENDPOINT_NOT_FOUND)}),
None,
]
host_only = MlflowHostCreds("http://my-host")
endpoints = [("/my/endpoint", "POST"), ("/my/endpoint", "GET")]
resp = call_endpoints(host_only, endpoints, "", response_proto)
mock_call_endpoint.assert_has_calls(
[
mock.call(host_only, endpoint, method, "", response_proto)
for endpoint, method in endpoints
]
)
assert resp is None
def test_call_endpoints_raises_exceptions():
with mock.patch("mlflow.utils.rest_utils.call_endpoint") as mock_call_endpoint:
response_proto = GetRun.Response()
mock_call_endpoint.side_effect = [
RestException({"error_code": ErrorCode.Name(ENDPOINT_NOT_FOUND)}),
RestException({"error_code": ErrorCode.Name(ENDPOINT_NOT_FOUND)}),
]
host_only = MlflowHostCreds("http://my-host")
endpoints = [("/my/endpoint", "POST"), ("/my/endpoint", "GET")]
with pytest.raises(RestException, match="ENDPOINT_NOT_FOUND"):
call_endpoints(host_only, endpoints, "", response_proto)
mock_call_endpoint.side_effect = [RestException({}), None]
with pytest.raises(RestException, match="INTERNAL_ERROR"):
call_endpoints(host_only, endpoints, "", response_proto)
@mock.patch("requests.Session.request")
def test_http_request_hostonly(request):
host_only = MlflowHostCreds("http://my-host")
response = mock.MagicMock()
response.status_code = 200
request.return_value = response
http_request(host_only, "/my/endpoint", "GET")
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify=True,
headers=_DEFAULT_HEADERS,
timeout=120,
)
@mock.patch("requests.Session.request")
def test_http_request_cleans_hostname(request):
# Add a trailing slash, should be removed.
host_only = MlflowHostCreds("http://my-host/")
response = mock.MagicMock()
response.status_code = 200
request.return_value = response
http_request(host_only, "/my/endpoint", "GET")
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify=True,
headers=_DEFAULT_HEADERS,
timeout=120,
)
@mock.patch("requests.Session.request")
def test_http_request_with_basic_auth(request):
host_only = MlflowHostCreds("http://my-host", username="user", password="pass")
response = mock.MagicMock()
response.status_code = 200
request.return_value = response
http_request(host_only, "/my/endpoint", "GET")
headers = dict(_DEFAULT_HEADERS)
headers["Authorization"] = "Basic dXNlcjpwYXNz"
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify=True,
headers=headers,
timeout=120,
)
@mock.patch("requests.Session.request")
def test_http_request_with_token(request):
host_only = MlflowHostCreds("http://my-host", token="my-token")
response = mock.MagicMock()
response.status_code = 200
request.return_value = response
http_request(host_only, "/my/endpoint", "GET")
headers = dict(_DEFAULT_HEADERS)
headers["Authorization"] = "Bearer my-token"
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify=True,
headers=headers,
timeout=120,
)
@mock.patch("requests.Session.request")
def test_http_request_with_insecure(request):
host_only = MlflowHostCreds("http://my-host", ignore_tls_verification=True)
response = mock.MagicMock()
response.status_code = 200
request.return_value = response
http_request(host_only, "/my/endpoint", "GET")
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify=False,
headers=_DEFAULT_HEADERS,
timeout=120,
)
@mock.patch("requests.Session.request")
def test_http_request_client_cert_path(request):
host_only = MlflowHostCreds("http://my-host", client_cert_path="/some/path")
response = mock.MagicMock()
response.status_code = 200
request.return_value = response
http_request(host_only, "/my/endpoint", "GET")
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify=True,
cert="/some/path",
headers=_DEFAULT_HEADERS,
timeout=120,
)
@mock.patch("requests.Session.request")
def test_http_request_server_cert_path(request):
host_only = MlflowHostCreds("http://my-host", server_cert_path="/some/path")
response = mock.MagicMock()
response.status_code = 200
request.return_value = response
http_request(host_only, "/my/endpoint", "GET")
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify="/some/path",
headers=_DEFAULT_HEADERS,
timeout=120,
)
@pytest.mark.large
@mock.patch("requests.Session.request")
def test_http_request_request_headers(request):
"""This test requires the package in tests/resources/mlflow-test-plugin to be installed"""
from mlflow_test_plugin.request_header_provider import PluginRequestHeaderProvider
# The test plugin's request header provider always returns False from in_context to avoid
# polluting request headers in developers' environments. The following mock overrides this to
# perform the integration test.
with mock.patch.object(PluginRequestHeaderProvider, "in_context", return_value=True):
host_only = MlflowHostCreds("http://my-host", server_cert_path="/some/path")
response = mock.MagicMock()
response.status_code = 200
request.return_value = response
http_request(host_only, "/my/endpoint", "GET")
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify="/some/path",
headers={**_DEFAULT_HEADERS, "test": "header"},
timeout=120,
)
def test_ignore_tls_verification_not_server_cert_path():
with pytest.raises(
MlflowException,
match="When 'ignore_tls_verification' is true then 'server_cert_path' must not be set",
):
MlflowHostCreds(
"http://my-host",
ignore_tls_verification=True,
server_cert_path="/some/path",
)
@mock.patch("requests.Session.request")
def test_http_request_wrapper(request):
host_only = MlflowHostCreds("http://my-host", ignore_tls_verification=True)
response = mock.MagicMock()
response.status_code = 200
response.text = "{}"
request.return_value = response
http_request_safe(host_only, "/my/endpoint", "GET")
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify=False,
headers=_DEFAULT_HEADERS,
timeout=120,
)
response.text = "non json"
request.return_value = response
http_request_safe(host_only, "/my/endpoint", "GET")
request.assert_called_with(
"GET",
"http://my-host/my/endpoint",
verify=False,
headers=_DEFAULT_HEADERS,
timeout=120,
)
response.status_code = 400
response.text = ""
request.return_value = response
with pytest.raises(MlflowException, match="Response body"):
http_request_safe(host_only, "/my/endpoint", "GET")
response.text = (
'{"error_code": "RESOURCE_DOES_NOT_EXIST", "message": "Node type not supported"}'
)
request.return_value = response
with pytest.raises(RestException, match="RESOURCE_DOES_NOT_EXIST: Node type not supported"):
http_request_safe(host_only, "/my/endpoint", "GET")
def test_numpy_encoder():
test_number = numpy.int64(42)
ne = NumpyEncoder()
defaulted_val = ne.default(test_number)
assert defaulted_val == 42
def test_numpy_encoder_fail():
if not hasattr(numpy, "float128"):
pytest.skip("numpy on exit" "this platform has no float128")
test_number = numpy.float128
with pytest.raises(TypeError, match="not JSON serializable"):
ne = NumpyEncoder()
ne.default(test_number)
def test_can_parse_as_json_object():
assert _can_parse_as_json_object("{}")
assert _can_parse_as_json_object('{"a": "b"}')
assert _can_parse_as_json_object('{"a": {"b": "c"}}')
assert not _can_parse_as_json_object("[0, 1, 2]")
assert not _can_parse_as_json_object('"abc"')
assert not _can_parse_as_json_object("123")