-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_influxdb_client_3_integration.py
276 lines (234 loc) · 10.9 KB
/
test_influxdb_client_3_integration.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
import logging
import os
import random
import string
import time
import unittest
import pyarrow
import pytest
from pyarrow._flight import FlightError
from influxdb_client_3 import InfluxDBClient3, InfluxDBError, write_client_options, WriteOptions
from tests.util import asyncio_run, lp_to_py_object
def random_hex(len=6):
return ''.join(random.choice(string.hexdigits) for i in range(len))
@pytest.mark.integration
@pytest.mark.skipif(
not all(
[
os.getenv('TESTING_INFLUXDB_URL'),
os.getenv('TESTING_INFLUXDB_TOKEN'),
os.getenv('TESTING_INFLUXDB_DATABASE'),
]
),
reason="Integration test environment variables not set.",
)
class TestInfluxDBClient3Integration(unittest.TestCase):
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
self._caplog = caplog
def setUp(self):
self.host = os.getenv('TESTING_INFLUXDB_URL')
self.token = os.getenv('TESTING_INFLUXDB_TOKEN')
self.database = os.getenv('TESTING_INFLUXDB_DATABASE')
self.client = InfluxDBClient3(host=self.host, database=self.database, token=self.token)
def tearDown(self):
self._caplog.clear()
self._caplog.set_level(logging.ERROR)
if self.client:
self.client.close()
def test_write_and_query(self):
test_id = time.time_ns()
self.client.write(f"integration_test_python,type=used value=123.0,test_id={test_id}i")
sql = 'SELECT * FROM integration_test_python where type=$type and test_id=$test_id'
df = self.client.query(sql, mode="pandas", query_parameters={'type': 'used', 'test_id': test_id})
self.assertIsNotNone(df)
self.assertEqual(1, len(df))
self.assertEqual(test_id, df['test_id'][0])
self.assertEqual(123.0, df['value'][0])
def test_auth_error_token(self):
self.client = InfluxDBClient3(host=self.host, database=self.database, token='fake token')
test_id = time.time_ns()
with self.assertRaises(InfluxDBError) as err:
self.client.write(f"integration_test_python,type=used value=123.0,test_id={test_id}i")
self.assertEqual('unauthorized access', err.exception.message) # Cloud
def test_auth_error_auth_scheme(self):
self.client = InfluxDBClient3(host=self.host, database=self.database, token=self.token, auth_scheme='Any')
test_id = time.time_ns()
with self.assertRaises(InfluxDBError) as err:
self.client.write(f"integration_test_python,type=used value=123.0,test_id={test_id}i")
self.assertEqual('unauthorized access', err.exception.message) # Cloud
def test_error_headers(self):
self.client = InfluxDBClient3(host=self.host, database=self.database, token=self.token)
with self.assertRaises(InfluxDBError) as err:
self.client.write("integration_test_python,type=used value=123.0,test_id=")
self.assertIn("Could not parse entire line. Found trailing content:", err.exception.message)
headers = err.exception.getheaders()
try:
self.assertIsNotNone(headers)
self.assertRegex(headers['trace-id'], '[0-9a-f]{16}')
self.assertEqual('false', headers['trace-sampled'])
self.assertIsNotNone(headers['Strict-Transport-Security'])
self.assertRegex(headers['X-Influxdb-Request-ID'], '[0-9a-f]+')
self.assertIsNotNone(headers['X-Influxdb-Build'])
except KeyError as ke:
self.fail(f'Header {ke} not found')
def test_batch_write_callbacks(self):
write_success = False
write_error = False
write_count = 0
measurement = f'test{random_hex(6)}'
data_set_size = 40
batch_size = 10
def success(conf, data):
nonlocal write_success, write_count
write_success = True
write_count += 1
def error(conf, data, exception: InfluxDBError):
nonlocal write_error
write_error = True
w_opts = WriteOptions(
batch_size=batch_size,
flush_interval=1_000,
jitter_interval=500
)
wc_opts = write_client_options(
success_callback=success,
error_callback=error,
write_options=w_opts
)
now = time.time_ns()
with InfluxDBClient3(host=self.host,
database=self.database,
token=self.token,
write_client_options=wc_opts) as w_client:
for i in range(0, data_set_size):
w_client.write(f'{measurement},location=harfa val={i}i {now - (i * 1_000_000_000)}')
self.assertEqual(data_set_size / batch_size, write_count)
self.assertTrue(write_success)
self.assertFalse(write_error)
with InfluxDBClient3(host=self.host,
database=self.database,
token=self.token,
write_client_options=wc_opts) as r_client:
query = f"SELECT * FROM \"{measurement}\" WHERE time >= now() - interval '3 minute'"
reader: pyarrow.Table = r_client.query(query)
list_results = reader.to_pylist()
self.assertEqual(data_set_size, len(list_results))
def test_batch_write_closed(self):
self._caplog.set_level(logging.DEBUG)
# writing measurement for last cca 3hrs
# so repeat runs in that time frame could
# result in clashed result data if always
# using same measurement name
measurement = f'test{random_hex()}'
data_size = 10_000
w_opts = WriteOptions(
batch_size=100,
flush_interval=3_000,
jitter_interval=500
)
wc_opts = write_client_options(
write_options=w_opts
)
now = time.time_ns()
with InfluxDBClient3(host=self.host,
database=self.database,
token=self.token,
write_client_options=wc_opts,
debug=True) as w_client:
for i in range(0, data_size):
w_client.write(f'{measurement},location=harfa val={i}i {now - (i * 1_000_000_000)}')
lines = self._caplog.text.splitlines()
self.assertRegex(lines[len(lines) - 1], ".*the batching processor was disposed$")
logging.info("WRITING DONE")
with InfluxDBClient3(host=self.host,
database=self.database,
token=self.token,
write_client_options=wc_opts) as r_client:
logging.info("PREPARING QUERY")
query = f"SELECT * FROM \"{measurement}\" WHERE time >= now() - interval '3 hours'"
reader: pyarrow.Table = r_client.query(query, mode="")
list_results = reader.to_pylist()
self.assertEqual(data_size, len(list_results))
test_cert = """-----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IqT8Tja5cHXE51bLdUkO1rtNKdM7TUdjzkZ+bAOpqKl+c0FlYZI+F7Ly
+MdCcNgKFc8o8jGiyP6uyAJeg+tSICpFDw00LyuKmU62c7VKuyo7
-----END CERTIFICATE-----"""
def create_test_cert(self, cert_file):
f = open(cert_file, "w")
f.write(self.test_cert)
f.close()
def remove_test_cert(self, cert_file):
os.remove(cert_file)
def test_queries_w_bad_cert(self):
cert_file = "test_cert.pem"
self.create_test_cert(cert_file)
with InfluxDBClient3(host=self.host,
database=self.database,
token=self.token,
verify_ssl=True,
ssl_ca_cert=cert_file,
debug=True) as client:
try:
query = "SELECT table_name FROM information_schema.tables"
client.query(query, mode="")
assert False, "query should throw SSL_ERROR"
except FlightError as fe:
assert str(fe).__contains__('SSL_ERROR_SSL')
finally:
self.remove_test_cert(cert_file)
def test_verify_ssl_false(self):
cert_file = "test_cert.pem"
self.create_test_cert(cert_file)
measurement = f'test{random_hex(6)}'
with InfluxDBClient3(host=self.host,
database=self.database,
token=self.token,
verify_ssl=False,
ssl_ca_cert=cert_file,
debug=True) as client:
try:
now = time.time_ns()
client.write(f'{measurement},location=harfa val=42i {now - 1_000_000_000}')
query = f"SELECT * FROM \"{measurement}\""
reader: pyarrow.Table = client.query(query, mode="")
list_results = reader.to_pylist()
assert len(list_results) > 0
finally:
self.remove_test_cert(cert_file)
@asyncio_run
async def test_verify_query_async(self):
measurement = f'test{random_hex(6)}'
data = []
lp_template = "%s,location=%s val=%f,ival=%di,index=%di %d"
data_size = 10
interval = 1_000_000_000 * 10
ts = time.time_ns() - interval * data_size
locations = ['springfield', 'gotham', 'balbec', 'yonville']
for i in range(data_size):
data.append(lp_template % (measurement, locations[random.randint(0, len(locations) - 1)],
random.random() * 10,
random.randint(0, 6), i, ts))
ts = ts + interval
self.client.write(data)
query = f"SELECT * FROM \"{measurement}\" ORDER BY time DESC"
result = await self.client.query_async(query)
result_list = result.to_pylist()
for item in data:
assert lp_to_py_object(item) in result_list, f"original lp data \"{item}\" should be in result list"