-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathconftest.py
More file actions
308 lines (252 loc) · 8.62 KB
/
conftest.py
File metadata and controls
308 lines (252 loc) · 8.62 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
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
import json
import logging
import multiprocessing
import os
import platform
import socket
import ssl
import sys
import tarfile
import threading
import time
import zipfile
from contextlib import contextmanager
from http.server import BaseHTTPRequestHandler, HTTPServer
from pathlib import Path
from unittest.mock import MagicMock
from urllib.request import urlretrieve
import pytest
import urllib3
import crate
from crate.client import connect
from crate.testing.layer import CrateLayer
from tests.client.settings import assets_path
log = logging.getLogger("tests.conftest")
REQUEST_PATH = "crate.client.http.Server.request"
URL_TMPL = "https://cdn.crate.io/downloads/releases/cratedb/{arch}_{os}/crate-6.1.2.{ext}"
project_root = Path(__file__).parent.parent
cratedb_path = project_root / "parts/crate"
crate_port = 44209
crate_transport_port = 44309
localhost = "127.0.0.1"
crate_host = f"http://{localhost}:{crate_port}"
def fake_response(
status: int, reason: str = None, content_type: str = "application/json"
) -> MagicMock:
"""
Returns a mocked `urllib3.response.HTTPResponse` HTTP response.
"""
m = MagicMock(spec=urllib3.response.HTTPResponse)
m.status = status
m.reason = reason or ""
m.headers = {"content-type": content_type}
return m
@pytest.fixture
def mocked_connection():
"""
Returns a crate `Connection` with a mocked `Client`
Example:
def test_conn(mocked_connection):
cursor = mocked_connection.cursor()
statement = "select * from locations where position = ?"
cursor.execute(statement, 1)
mocked_connection.client.sql.called_with(statement, 1, None)
"""
yield crate.client.connect(client=MagicMock(spec=crate.client.http.Client))
@pytest.fixture
def serve_http():
"""
Returns a context manager that start an http server running
in another thread that returns CrateDB successful responses.
It accepts an optional parameter, the handler class, it has to be an
instance of `BaseHTTPRequestHandler`
The port will be an unused random port.
Example:
def test_http(serve_http):
with serve_http() as url:
urllib3.urlopen(url)
See `test_http.test_keep_alive` for more advance example.
"""
@contextmanager
def _serve(handler_cls=BaseHTTPRequestHandler):
assert issubclass(handler_cls, BaseHTTPRequestHandler) # noqa: S101
sock = socket.socket()
sock.bind(("127.0.0.1", 0))
host, port = sock.getsockname()
sock.close()
manager = multiprocessing.Manager()
SHARED = manager.dict()
SHARED["count"] = 0
SHARED["usernameFromXUser"] = None
SHARED["username"] = None
SHARED["password"] = None
SHARED["schema"] = None
server = HTTPServer((host, port), handler_cls)
server.SHARED = SHARED
thread = threading.Thread(target=server.serve_forever, daemon=False)
thread.start()
try:
yield server, f"http://{host}:{port}"
finally:
server.shutdown()
thread.join()
return _serve
def get_crate_url() -> str:
extension = "tar.gz"
machine = platform.machine()
if machine.startswith("arm") or machine == "aarch64":
arch = "aarch64"
else:
arch = "x64"
if sys.platform.startswith("linux"):
os = "linux"
elif sys.platform.startswith("win32"):
os = "windows"
extension = "zip"
elif sys.platform.startswith("darwin"):
os = "mac"
# there are no aarch64/arm64 distributions available
# x64 should work via emulation layer
arch = "x64"
else:
raise ValueError(f"Unsupported platform: {sys.platform}")
return URL_TMPL.format(arch=arch, os=os, ext=extension)
def download_cratedb(path: Path):
url = get_crate_url()
if path.exists():
return
if not url.startswith("https:"):
raise ValueError("Invalid url")
filename, _msg = urlretrieve(url)
if sys.platform.startswith("win32"):
with zipfile.ZipFile(filename) as z:
first_file = z.namelist()[0]
folder_name = os.path.dirname(first_file)
z.extractall(path.parent)
(path.parent / folder_name).rename(path)
else:
with tarfile.open(filename) as t:
first_file = t.getnames()[0]
folder_name = os.path.dirname(first_file)
t.extractall(path.parent, filter="data")
(path.parent / folder_name).rename(path)
def create_test_data(cursor):
with open(project_root / "tests/assets/mappings/locations.sql") as s:
stmt = s.read()
cursor.execute(stmt)
stmt = (
"select count(*) from information_schema.tables "
"where table_name = 'locations'"
)
cursor.execute(stmt)
assert cursor.fetchall()[0][0] == 1 # noqa: S101
data_path = str(project_root / "tests/assets/import/test_a.json")
# load testing data into crate
cursor.execute("copy locations from ?", (data_path,))
# refresh location table so imported data is visible immediately
cursor.execute("refresh table locations")
# create blob table
cursor.execute(
"create blob table myfiles clustered into 1 shards "
+ "with (number_of_replicas=0)"
)
# create users
cursor.execute("CREATE USER me WITH (password = 'my_secret_pw')")
cursor.execute("CREATE USER trusted_me")
@pytest.fixture()
def doctest_node():
download_cratedb(cratedb_path)
settings = {
"udc.enabled": "false",
"lang.js.enabled": "true",
"auth.host_based.enabled": "true",
"auth.host_based.config.0.user": "crate",
"auth.host_based.config.0.method": "trust",
"auth.host_based.config.98.user": "trusted_me",
"auth.host_based.config.98.method": "trust",
"auth.host_based.config.99.user": "me",
"auth.host_based.config.99.method": "password",
"discovery.type": "single-node",
}
crate_layer = CrateLayer(
"crate",
crate_home=cratedb_path,
port=crate_port,
host=localhost,
transport_port=crate_transport_port,
settings=settings,
)
crate_layer.start()
with connect(crate_host) as conn:
cursor = conn.cursor()
create_test_data(cursor)
cursor.close()
yield crate_layer
crate_layer.stop()
class HttpsServer(HTTPServer):
PORT = 65534
HOST = "localhost"
CERT_FILE = assets_path("pki/server_valid.pem")
CACERT_FILE = assets_path("pki/cacert_valid.pem")
def get_request(self):
# Prepare SSL context.
context = ssl._create_unverified_context( # noqa: S323
protocol=ssl.PROTOCOL_TLS_SERVER,
cert_reqs=ssl.CERT_OPTIONAL,
check_hostname=False,
purpose=ssl.Purpose.CLIENT_AUTH,
certfile=HttpsServer.CERT_FILE,
keyfile=HttpsServer.CERT_FILE,
cafile=HttpsServer.CACERT_FILE,
) # noqa: S323
# Set minimum protocol version, TLSv1 and TLSv1.1 are unsafe.
context.minimum_version = ssl.TLSVersion.TLSv1_2
# Wrap TLS encryption around socket.
socket, client_address = HTTPServer.get_request(self)
socket = context.wrap_socket(socket, server_side=True)
return socket, client_address
class HttpsHandler(BaseHTTPRequestHandler):
payload = json.dumps(
{
"name": "test",
"status": 200,
}
)
def do_GET(self):
self.send_response(200)
payload = self.payload.encode("UTF-8")
self.send_header("Content-Length", f"{len(payload)}")
self.send_header("Content-Type", "application/json; charset=UTF-8")
self.end_headers()
self.wfile.write(payload)
def is_up(host: str, port: int) -> bool:
"""
Test if a host is up.
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ex = s.connect_ex((host, port))
s.close()
return ex == 0
@pytest.fixture
def https_server():
port = HttpsServer.PORT
host = HttpsServer.HOST
server_address = (host, port)
server = HttpsServer(server_address, HttpsHandler)
thread = threading.Thread(target=server.serve_forever)
thread.daemon = True
thread.start()
start = time.monotonic()
timeout = 5
while True:
if is_up(host, port):
break
now = time.monotonic()
if now - start > timeout:
raise TimeoutError(
"Could not properly start embedded webserver "
"within {} seconds".format(timeout)
)
yield server
server.shutdown()
server.server_close()