-
Notifications
You must be signed in to change notification settings - Fork 14.4k
/
test_trino.py
343 lines (296 loc) · 12.7 KB
/
test_trino.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
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import json
import os
import re
from tempfile import TemporaryDirectory
from unittest import mock
from unittest.mock import patch
import pytest
from trino.transaction import IsolationLevel
from airflow import AirflowException
from airflow.models import Connection
from airflow.providers.trino.hooks.trino import TrinoHook
HOOK_GET_CONNECTION = "airflow.providers.trino.hooks.trino.TrinoHook.get_connection"
BASIC_AUTHENTICATION = "airflow.providers.trino.hooks.trino.trino.auth.BasicAuthentication"
KERBEROS_AUTHENTICATION = "airflow.providers.trino.hooks.trino.trino.auth.KerberosAuthentication"
TRINO_DBAPI_CONNECT = "airflow.providers.trino.hooks.trino.trino.dbapi.connect"
JWT_AUTHENTICATION = "airflow.providers.trino.hooks.trino.trino.auth.JWTAuthentication"
CERT_AUTHENTICATION = "airflow.providers.trino.hooks.trino.trino.auth.CertificateAuthentication"
@pytest.fixture()
def jwt_token_file():
# Couldn't get this working with TemporaryFile, using TemporaryDirectory instead
# Save a phony jwt to a temporary file for the trino hook to read from
with TemporaryDirectory() as tmp_dir:
tmp_jwt_file = os.path.join(tmp_dir, "jwt.json")
with open(tmp_jwt_file, "w") as tmp_file:
tmp_file.write('{"phony":"jwt"}')
yield tmp_jwt_file
class TestTrinoHookConn:
@patch(BASIC_AUTHENTICATION)
@patch(TRINO_DBAPI_CONNECT)
@patch(HOOK_GET_CONNECTION)
def test_get_conn_basic_auth(self, mock_get_connection, mock_connect, mock_basic_auth):
self.set_get_connection_return_value(mock_get_connection, password="password")
TrinoHook().get_conn()
self.assert_connection_called_with(mock_connect, auth=mock_basic_auth)
mock_basic_auth.assert_called_once_with("login", "password")
@patch("airflow.providers.trino.hooks.trino.generate_trino_client_info")
@patch(BASIC_AUTHENTICATION)
@patch(TRINO_DBAPI_CONNECT)
@patch(HOOK_GET_CONNECTION)
def test_http_headers(
self,
mock_get_connection,
mock_connect,
mock_basic_auth,
mocked_generate_airflow_trino_client_info_header,
):
mock_get_connection.return_value = Connection(
login="login", password="password", host="host", schema="hive"
)
client = json.dumps(
{
"dag_id": "dag-id",
"execution_date": "2022-01-01T00:00:00",
"task_id": "task-id",
"try_number": "1",
"dag_run_id": "dag-run-id",
"dag_owner": "dag-owner",
},
sort_keys=True,
)
http_headers = {"X-Trino-Client-Info": client}
mocked_generate_airflow_trino_client_info_header.return_value = http_headers["X-Trino-Client-Info"]
conn = TrinoHook().get_conn()
self.assert_connection_called_with(mock_connect, auth=mock_basic_auth, http_headers=http_headers)
mock_basic_auth.assert_called_once_with("login", "password")
assert mock_connect.return_value == conn
@patch(HOOK_GET_CONNECTION)
def test_get_conn_invalid_auth(self, mock_get_connection):
extras = {"auth": "kerberos"}
self.set_get_connection_return_value(
mock_get_connection,
password="password",
extra=json.dumps(extras),
)
with pytest.raises(
AirflowException, match=re.escape("The 'kerberos' authorization type doesn't support password.")
):
TrinoHook().get_conn()
@patch(JWT_AUTHENTICATION)
@patch(TRINO_DBAPI_CONNECT)
@patch(HOOK_GET_CONNECTION)
def test_get_conn_jwt_auth(self, mock_get_connection, mock_connect, mock_jwt_auth):
extras = {
"auth": "jwt",
"jwt__token": "TEST_JWT_TOKEN",
}
self.set_get_connection_return_value(
mock_get_connection,
extra=json.dumps(extras),
)
TrinoHook().get_conn()
self.assert_connection_called_with(mock_connect, auth=mock_jwt_auth)
@patch(JWT_AUTHENTICATION)
@patch(TRINO_DBAPI_CONNECT)
@patch(HOOK_GET_CONNECTION)
def test_get_conn_jwt_file(self, mock_get_connection, mock_connect, mock_jwt_auth, jwt_token_file):
extras = {
"auth": "jwt",
"jwt__file": jwt_token_file,
}
self.set_get_connection_return_value(
mock_get_connection,
extra=json.dumps(extras),
)
TrinoHook().get_conn()
self.assert_connection_called_with(mock_connect, auth=mock_jwt_auth)
@patch(CERT_AUTHENTICATION)
@patch(TRINO_DBAPI_CONNECT)
@patch(HOOK_GET_CONNECTION)
def test_get_conn_cert_auth(self, mock_get_connection, mock_connect, mock_cert_auth):
extras = {
"auth": "certs",
"certs__client_cert_path": "/path/to/client.pem",
"certs__client_key_path": "/path/to/client.key",
}
self.set_get_connection_return_value(
mock_get_connection,
extra=json.dumps(extras),
)
TrinoHook().get_conn()
self.assert_connection_called_with(mock_connect, auth=mock_cert_auth)
mock_cert_auth.assert_called_once_with("/path/to/client.pem", "/path/to/client.key")
@patch(KERBEROS_AUTHENTICATION)
@patch(TRINO_DBAPI_CONNECT)
@patch(HOOK_GET_CONNECTION)
def test_get_conn_kerberos_auth(self, mock_get_connection, mock_connect, mock_auth):
extras = {
"auth": "kerberos",
"kerberos__config": "TEST_KERBEROS_CONFIG",
"kerberos__service_name": "TEST_SERVICE_NAME",
"kerberos__mutual_authentication": "TEST_MUTUAL_AUTHENTICATION",
"kerberos__force_preemptive": True,
"kerberos__hostname_override": "TEST_HOSTNAME_OVERRIDE",
"kerberos__sanitize_mutual_error_response": True,
"kerberos__principal": "TEST_PRINCIPAL",
"kerberos__delegate": "TEST_DELEGATE",
"kerberos__ca_bundle": "TEST_CA_BUNDLE",
"verify": "true",
}
self.set_get_connection_return_value(
mock_get_connection,
extra=json.dumps(extras),
)
TrinoHook().get_conn()
self.assert_connection_called_with(mock_connect, auth=mock_auth)
@patch(HOOK_GET_CONNECTION)
@patch(TRINO_DBAPI_CONNECT)
def test_get_conn_session_properties(self, mock_connect, mock_get_connection):
extras = {
"session_properties": {
"scale_writers": "true",
"task_writer_count": "1",
"writer_min_size": "100MB",
},
}
self.set_get_connection_return_value(mock_get_connection, extra=extras)
TrinoHook().get_conn()
self.assert_connection_called_with(mock_connect, session_properties=extras["session_properties"])
@patch(HOOK_GET_CONNECTION)
@patch(TRINO_DBAPI_CONNECT)
def test_get_conn_client_tags(self, mock_connect, mock_get_connection):
extras = {"client_tags": ["abc", "xyz"]}
self.set_get_connection_return_value(mock_get_connection, extra=extras)
TrinoHook().get_conn()
self.assert_connection_called_with(mock_connect, client_tags=extras["client_tags"])
@pytest.mark.parametrize(
"current_verify, expected_verify",
[
("False", False),
("false", False),
("true", True),
("true", True),
("/tmp/cert.crt", "/tmp/cert.crt"),
],
)
@patch(HOOK_GET_CONNECTION)
@patch(TRINO_DBAPI_CONNECT)
def test_get_conn_verify(self, mock_connect, mock_get_connection, current_verify, expected_verify):
extras = {"verify": current_verify}
self.set_get_connection_return_value(mock_get_connection, extra=json.dumps(extras))
TrinoHook().get_conn()
self.assert_connection_called_with(mock_connect, verify=expected_verify)
@staticmethod
def set_get_connection_return_value(mock_get_connection, extra=None, password=None):
mocked_connection = Connection(
login="login", password=password, host="host", schema="hive", extra=extra or "{}"
)
mock_get_connection.return_value = mocked_connection
@staticmethod
def assert_connection_called_with(
mock_connect, http_headers=mock.ANY, auth=None, verify=True, session_properties=None, client_tags=None
):
mock_connect.assert_called_once_with(
catalog="hive",
host="host",
port=None,
http_scheme="http",
http_headers=http_headers,
schema="hive",
source="airflow",
user="login",
isolation_level=IsolationLevel.AUTOCOMMIT,
auth=None if not auth else auth.return_value,
verify=verify,
session_properties=session_properties,
client_tags=client_tags,
)
class TestTrinoHook:
def setup_method(self):
self.cur = mock.MagicMock(rowcount=0)
self.conn = mock.MagicMock()
self.conn.cursor.return_value = self.cur
conn = self.conn
class UnitTestTrinoHook(TrinoHook):
conn_name_attr = "test_conn_id"
def get_conn(self):
return conn
def get_isolation_level(self):
return IsolationLevel.READ_COMMITTED
self.db_hook = UnitTestTrinoHook()
@patch("airflow.providers.common.sql.hooks.sql.DbApiHook.insert_rows")
def test_insert_rows(self, mock_insert_rows):
table = "table"
rows = [("hello",), ("world",)]
target_fields = None
commit_every = 10
replace = True
self.db_hook.insert_rows(table, rows, target_fields, commit_every, replace)
mock_insert_rows.assert_called_once_with(table, rows, None, 10, True)
def test_get_first_record(self):
statement = "SQL"
result_sets = [("row1",), ("row2",)]
self.cur.fetchone.return_value = result_sets[0]
assert result_sets[0] == self.db_hook.get_first(statement)
self.conn.close.assert_called_once_with()
self.cur.close.assert_called_once_with()
self.cur.execute.assert_called_once_with(statement)
def test_get_records(self):
statement = "SQL"
result_sets = [("row1",), ("row2",)]
self.cur.fetchall.return_value = result_sets
assert result_sets == self.db_hook.get_records(statement)
self.conn.close.assert_called_once_with()
self.cur.close.assert_called_once_with()
self.cur.execute.assert_called_once_with(statement)
def test_get_pandas_df(self):
statement = "SQL"
column = "col"
result_sets = [("row1",), ("row2",)]
self.cur.description = [(column,)]
self.cur.fetchall.return_value = result_sets
df = self.db_hook.get_pandas_df(statement)
assert column == df.columns[0]
assert result_sets[0][0] == df.values.tolist()[0][0]
assert result_sets[1][0] == df.values.tolist()[1][0]
self.cur.execute.assert_called_once_with(statement, None)
@patch("airflow.providers.trino.hooks.trino.TrinoHook.run")
def test_run(self, mock_run):
sql = "SELECT 1"
autocommit = False
parameters = ("hello", "world")
handler = list
self.db_hook.run(sql, autocommit, parameters, list)
mock_run.assert_called_once_with(sql, autocommit, parameters, handler)
def test_connection_success(self):
status, msg = self.db_hook.test_connection()
assert status is True
assert msg == "Connection successfully tested"
@patch("airflow.providers.trino.hooks.trino.TrinoHook.get_conn")
def test_connection_failure(self, mock_conn):
mock_conn.side_effect = Exception("Test")
self.db_hook.get_conn = mock_conn
status, msg = self.db_hook.test_connection()
assert status is False
assert msg == "Test"
def test_serialize_cell(self):
assert "foo" == self.db_hook._serialize_cell("foo", None)
assert 1 == self.db_hook._serialize_cell(1, None)