-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathconftest.py
352 lines (295 loc) · 13.1 KB
/
conftest.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import json
import time
from concurrent import futures
from typing import Generator, Mapping
import grpc
import pytest
from grpc import ServicerContext
from grpc_health.v1.health_pb2 import HealthCheckResponse, HealthCheckRequest
from grpc_health.v1.health_pb2_grpc import HealthServicer, add_HealthServicer_to_server
from pytest_httpserver import HTTPServer, HeaderValueMatcher
from werkzeug.wrappers import Request, Response
import weaviate
from weaviate.connect.base import ConnectionParams, ProtocolParams
from weaviate.proto.v1 import (
batch_pb2,
batch_delete_pb2,
properties_pb2,
tenants_pb2,
search_get_pb2,
weaviate_pb2_grpc,
)
from mock_tests.mock_data import mock_class
MOCK_IP = "127.0.0.1"
MOCK_PORT = 23536
MOCK_PORT_GRPC = 23537
CLIENT_ID = "DoesNotMatter"
MOCK_SERVER_URL = "http://" + MOCK_IP + ":" + str(MOCK_PORT)
# only http endpoint is tested, grpc port doesnt matter but needs to be supplied
MOCK_SERVER_CONNECTION_PARAMS = ConnectionParams(
http=ProtocolParams(host=MOCK_IP, port=MOCK_PORT, secure=False),
grpc=ProtocolParams(host=MOCK_IP, port=MOCK_PORT + 1, secure=False),
)
# pytest_httpserver 'Authorization' HeaderValueMatcher does not work with Bearer tokens.
# Hence, overwrite it with the default header value matcher that just compares for equality.
HeaderValueMatcher.DEFAULT_MATCHERS["Authorization"] = (
HeaderValueMatcher.default_header_value_matcher
)
@pytest.fixture(scope="session")
def httpserver_listen_address():
return MOCK_IP, MOCK_PORT
@pytest.fixture(scope="function")
def ready_mock(httpserver: HTTPServer):
httpserver.expect_request("/v1/.well-known/ready").respond_with_json({})
yield httpserver
@pytest.fixture(scope="function")
def weaviate_mock(ready_mock: HTTPServer):
ready_mock.expect_request("/v1/meta").respond_with_json({"version": "1.25"})
ready_mock.expect_request("/v1/nodes").respond_with_json({"nodes": [{"gitHash": "ABC"}]})
yield ready_mock
@pytest.fixture(scope="function")
def weaviate_no_auth_mock(weaviate_mock: HTTPServer):
weaviate_mock.expect_request("/v1/meta").respond_with_json({"version": "1.25"})
weaviate_mock.expect_request("/v1/.well-known/openid-configuration").respond_with_response(
Response(json.dumps({}), status=404)
)
yield weaviate_mock
@pytest.fixture(scope="function")
def weaviate_auth_mock(weaviate_mock: HTTPServer):
weaviate_mock.expect_request("/v1/.well-known/openid-configuration").respond_with_json(
{
"href": MOCK_SERVER_URL + "/endpoints",
"clientId": CLIENT_ID,
}
)
weaviate_mock.expect_request("/endpoints").respond_with_json(
{"token_endpoint": MOCK_SERVER_URL + "/auth"}
)
yield weaviate_mock
@pytest.fixture(scope="function")
def weaviate_timeouts_mock(weaviate_no_auth_mock: HTTPServer):
def slow_get(request: Request) -> Response:
time.sleep(1)
return Response(json.dumps({"doesn't": "matter"}), content_type="application/json")
def slow_post(request: Request) -> Response:
time.sleep(2)
return Response(json.dumps({"doesn't": "matter"}), content_type="application/json")
weaviate_no_auth_mock.expect_request(
f"/v1/schema/{mock_class['class']}", method="GET"
).respond_with_handler(slow_get)
weaviate_no_auth_mock.expect_request("/v1/objects", method="POST").respond_with_handler(
slow_post
)
yield weaviate_no_auth_mock
@pytest.fixture(scope="function")
def start_grpc_server() -> Generator[grpc.Server, None, None]:
# Create a gRPC server
server: grpc.Server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
# Implement the health check service
class MockHealthServicer(HealthServicer):
def Check(
self, request: HealthCheckRequest, context: ServicerContext
) -> HealthCheckResponse:
return HealthCheckResponse(status=HealthCheckResponse.SERVING)
# Add the health check service to the server
add_HealthServicer_to_server(MockHealthServicer(), server)
# Listen on a specific port
server.add_insecure_port(f"[::]:{MOCK_PORT_GRPC}")
server.start()
yield server
# Teardown - stop the server
server.stop(0)
@pytest.fixture(scope="function")
def weaviate_client(
weaviate_mock: HTTPServer, start_grpc_server: grpc.Server
) -> Generator[weaviate.WeaviateClient, None, None]:
client = weaviate.connect_to_local(port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC)
yield client
client.close()
@pytest.fixture(scope="function")
def weaviate_timeouts_client(
weaviate_timeouts_mock: HTTPServer, start_grpc_server: grpc.Server
) -> Generator[weaviate.WeaviateClient, None, None]:
client = weaviate.connect_to_local(
host=MOCK_IP,
port=MOCK_PORT,
grpc_port=MOCK_PORT_GRPC,
additional_config=weaviate.classes.init.AdditionalConfig(
timeout=weaviate.classes.init.Timeout(query=0.5, insert=1.5)
),
)
yield client
client.close()
@pytest.fixture(scope="function")
def tenants_collection(
weaviate_client: weaviate.WeaviateClient, start_grpc_server: grpc.Server
) -> weaviate.collections.Collection:
class MockWeaviateService(weaviate_pb2_grpc.WeaviateServicer):
def TenantsGet(
self, request: tenants_pb2.TenantsGetRequest, context: ServicerContext
) -> tenants_pb2.TenantsGetReply:
return tenants_pb2.TenantsGetReply(
tenants=[
tenants_pb2.Tenant(
name="tenant1", activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_HOT
),
tenants_pb2.Tenant(
name="tenant2", activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_COLD
),
tenants_pb2.Tenant(
name="tenant3", activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_FROZEN
),
tenants_pb2.Tenant(
name="tenant4", activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_FREEZING
),
tenants_pb2.Tenant(
name="tenant5",
activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_UNFREEZING,
),
tenants_pb2.Tenant(
name="tenant6", activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_ACTIVE
),
tenants_pb2.Tenant(
name="tenant7", activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_INACTIVE
),
tenants_pb2.Tenant(
name="tenant8", activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_OFFLOADED
),
tenants_pb2.Tenant(
name="tenant9",
activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_OFFLOADING,
),
tenants_pb2.Tenant(
name="tenant10",
activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_ONLOADING,
),
]
)
weaviate_pb2_grpc.add_WeaviateServicer_to_server(MockWeaviateService(), start_grpc_server)
return weaviate_client.collections.use("TenantsGetCollectionName")
@pytest.fixture(scope="function")
def year_zero_collection(
weaviate_client: weaviate.WeaviateClient, start_grpc_server: grpc.Server
) -> weaviate.collections.Collection:
class MockWeaviateService(weaviate_pb2_grpc.WeaviateServicer):
def Search(
self, request: search_get_pb2.SearchRequest, context: grpc.ServicerContext
) -> search_get_pb2.SearchReply:
zero_date: properties_pb2.Value.date_value = properties_pb2.Value(
date_value="0000-01-30T00:00:00Z"
)
date_prop: Mapping[str, properties_pb2.Value.date_value] = {"date": zero_date}
return search_get_pb2.SearchReply(
results=[
search_get_pb2.SearchResult(
properties=search_get_pb2.PropertiesResult(
non_ref_props=properties_pb2.Properties(fields=date_prop)
)
),
]
)
weaviate_pb2_grpc.add_WeaviateServicer_to_server(MockWeaviateService(), start_grpc_server)
return weaviate_client.collections.use("YearZeroCollection")
@pytest.fixture(scope="function")
def timeouts_collection(
weaviate_timeouts_client: weaviate.WeaviateClient, start_grpc_server: grpc.Server
) -> weaviate.collections.Collection:
class MockWeaviateService(weaviate_pb2_grpc.WeaviateServicer):
def Search(
self, request: search_get_pb2.SearchRequest, context: grpc.ServicerContext
) -> search_get_pb2.SearchReply:
time.sleep(1)
return search_get_pb2.SearchReply()
def BatchObjects(
self, request: batch_pb2.BatchObjectsRequest, context: grpc.ServicerContext
) -> batch_pb2.BatchObjectsReply:
time.sleep(2)
return batch_pb2.BatchObjectsReply()
weaviate_pb2_grpc.add_WeaviateServicer_to_server(MockWeaviateService(), start_grpc_server)
return weaviate_timeouts_client.collections.use(mock_class["class"])
class MockRetriesWeaviateService(weaviate_pb2_grpc.WeaviateServicer):
search_count = 0
tenants_count = 0
def Search(
self, request: search_get_pb2.SearchRequest, context: grpc.ServicerContext
) -> search_get_pb2.SearchReply:
if self.search_count == 0:
self.search_count += 1
context.set_code(grpc.StatusCode.INTERNAL)
context.set_details("Internal server error")
return search_get_pb2.SearchReply()
if self.search_count == 1:
self.search_count += 1
context.set_code(grpc.StatusCode.UNAVAILABLE)
context.set_details("Service is unavailable")
return search_get_pb2.SearchReply()
return search_get_pb2.SearchReply(
results=[
search_get_pb2.SearchResult(
properties=search_get_pb2.PropertiesResult(
non_ref_props=properties_pb2.Properties(
fields={"name": properties_pb2.Value(text_value="test")}
)
)
)
]
)
def TenantsGet(
self, request: tenants_pb2.TenantsGetRequest, context: ServicerContext
) -> tenants_pb2.TenantsGetReply:
if self.tenants_count == 0:
self.tenants_count += 1
context.set_code(grpc.StatusCode.INTERNAL)
context.set_details("Internal server error")
return tenants_pb2.TenantsGetReply()
if self.tenants_count == 1:
self.tenants_count += 1
context.set_code(grpc.StatusCode.UNAVAILABLE)
context.set_details("Service is unavailable")
return tenants_pb2.TenantsGetReply()
return tenants_pb2.TenantsGetReply(
tenants=[
tenants_pb2.Tenant(
name="tenant1", activity_status=tenants_pb2.TENANT_ACTIVITY_STATUS_ACTIVE
)
]
)
@pytest.fixture(scope="function")
def retries(
weaviate_client: weaviate.WeaviateClient, start_grpc_server: grpc.Server
) -> tuple[weaviate.collections.Collection, MockRetriesWeaviateService]:
service = MockRetriesWeaviateService()
weaviate_pb2_grpc.add_WeaviateServicer_to_server(service, start_grpc_server)
return weaviate_client.collections.use("RetriesCollection"), service
class MockForbiddenWeaviateService(weaviate_pb2_grpc.WeaviateServicer):
def Search(
self, request: search_get_pb2.SearchRequest, context: grpc.ServicerContext
) -> search_get_pb2.SearchReply:
context.set_code(grpc.StatusCode.PERMISSION_DENIED)
context.set_details("Permission denied")
return search_get_pb2.SearchReply()
def TenantsGet(
self, request: tenants_pb2.TenantsGetRequest, context: ServicerContext
) -> tenants_pb2.TenantsGetReply:
context.set_code(grpc.StatusCode.PERMISSION_DENIED)
context.set_details("Permission denied")
return tenants_pb2.TenantsGetReply()
def BatchObjects(
self, request: batch_pb2.BatchObjectsRequest, context: grpc.ServicerContext
) -> batch_pb2.BatchObjectsReply:
context.set_code(grpc.StatusCode.PERMISSION_DENIED)
context.set_details("Permission denied")
return batch_pb2.BatchObjectsReply()
def BatchDelete(
self, request: batch_delete_pb2.BatchDeleteRequest, context: grpc.ServicerContext
) -> batch_delete_pb2.BatchDeleteReply:
context.set_code(grpc.StatusCode.PERMISSION_DENIED)
context.set_details("Permission denied")
return batch_delete_pb2.BatchDeleteReply()
@pytest.fixture(scope="function")
def forbidden(
weaviate_client: weaviate.WeaviateClient, start_grpc_server: grpc.Server
) -> weaviate.collections.Collection:
service = MockForbiddenWeaviateService()
weaviate_pb2_grpc.add_WeaviateServicer_to_server(service, start_grpc_server)
return weaviate_client.collections.use("ForbiddenCollection")