|
1 | 1 | import unittest |
2 | 2 | import struct |
| 3 | +import os |
3 | 4 | from unittest.mock import Mock, ANY |
4 | 5 |
|
5 | 6 | from pyarrow import ( |
|
18 | 19 | ) |
19 | 20 |
|
20 | 21 | from influxdb_client_3 import InfluxDBClient3 |
| 22 | +from influxdb_client_3.query.query_api import QueryApiOptionsBuilder, QueryApi |
21 | 23 | from influxdb_client_3.version import USER_AGENT |
22 | 24 |
|
23 | 25 |
|
@@ -115,6 +117,27 @@ def test_influx_default_query_headers(): |
115 | 117 |
|
116 | 118 | class TestQuery(unittest.TestCase): |
117 | 119 |
|
| 120 | + sample_cert = """-----BEGIN CERTIFICATE----- |
| 121 | +MIIDUzCCAjugAwIBAgIUZB55ULutbc9gy6xLp1BkTQU7siowDQYJKoZIhvcNAQEL |
| 122 | +BQAwNjE0MDIGA1UEAwwraW5mbHV4ZGIzLWNsdXN0ZXJlZC1zd2FuLmJyYW1ib3Jh |
| 123 | +LnpvbmEtYi5ldTAeFw0yNTAyMTgxNTIyMTJaFw0yNjAyMTgxNTIyMTJaMDYxNDAy |
| 124 | +BgNVBAMMK2luZmx1eGRiMy1jbHVzdGVyZWQtc3dhbi5icmFtYm9yYS56b25hLWIu |
| 125 | +ZXUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCugeNrx0ZfyyP8H4e0 |
| 126 | +zDSkKWnEXlVdjMi+ZSHhMbjvvqMkUQGLc/W59AEmMJ0Uiljka9d+F7jdu+oqDq9p |
| 127 | +4kGPhO3Oh7zIG0IGbncj8AwIXMGDNkNyL8s7C1+LoYotlSWDpWwkEKXUeAzdqS63 |
| 128 | +CSJFqSJM2dss8qe9BpM6zHWJAKS1I30QT3SXQFEsF5m2F62dXCEEI6pO7jlik8/w |
| 129 | +aI47dTM20QyimVzea48SC/ELO/T4AjbmMeBGlTyCm39KOElOKRTJvB4KESEWaL3r |
| 130 | +EvPZbTh+72PUyrjxiDa56+RmtDPo7EN3uxuRVFX/HWiNnFk7orQLKZg5Kr8wE46R |
| 131 | +KmVvAgMBAAGjWTBXMDYGA1UdEQQvMC2CK2luZmx1eGRiMy1jbHVzdGVyZWQtc3dh |
| 132 | +bi5icmFtYm9yYS56b25hLWIuZXUwHQYDVR0OBBYEFH8et6JCzGD7Ny84aNRtq5Nj |
| 133 | +hvS/MA0GCSqGSIb3DQEBCwUAA4IBAQCuDwARea/Xr3+hmte9A0H+XB8wMPAJ64e8 |
| 134 | +QA0qi0oy0gGdLfQHhsBWWmKSYLv7HygTNzb+7uFOTtq1UPLt18F+POPeLIj74QZV |
| 135 | +z89Pbo1TwUMzQ2pgbu0yRvraXIpqXGrPm5GWYp5mopX0rBWKdimbmEMkhZA0sVeH |
| 136 | +IdKIRUY6EyIVG+Z/nbuVqUlgnIWOMp0yg4RRC91zHy3Xvykf3Vai25H/jQpa6cbU |
| 137 | +//MIodzUIqT8Tja5cHXE51bLdUkO1rtNKdM7TUdjzkZ+bAOpqKl+c0FlYZI+F7Ly |
| 138 | ++MdCcNgKFc8o8jGiyP6uyAJeg+tSICpFDw00LyuKmU62c7VKuyo7 |
| 139 | +-----END CERTIFICATE-----""" |
| 140 | + |
118 | 141 | def setUp(self): |
119 | 142 | self.client = InfluxDBClient3( |
120 | 143 | host="localhost", |
@@ -164,3 +187,84 @@ def test_query_proxy_base_client(self): |
164 | 187 | assert client._query_api._proxy == test_proxy |
165 | 188 | assert ('grpc.http_proxy', test_proxy) in\ |
166 | 189 | client._query_api._flight_client_options.get('generic_options') |
| 190 | + |
| 191 | + def create_cert_file(self, file_name): |
| 192 | + f = open(file_name, "w") |
| 193 | + f.write(self.sample_cert) |
| 194 | + f.close() |
| 195 | + |
| 196 | + def remove_cert_file(self, file_name): |
| 197 | + os.remove(file_name) |
| 198 | + |
| 199 | + def test_query_api_options_builder(self): |
| 200 | + proxy_name = "http://my.proxy.org" |
| 201 | + cert_file = "cert_test.pem" |
| 202 | + self.create_cert_file(cert_file) |
| 203 | + builder = QueryApiOptionsBuilder() |
| 204 | + options = builder.proxy(proxy_name)\ |
| 205 | + .root_certs(cert_file)\ |
| 206 | + .tls_verify(False)\ |
| 207 | + .build() |
| 208 | + |
| 209 | + print(f"\nDEBUG options {vars(options)}") |
| 210 | + try: |
| 211 | + assert(options.tls_root_certs.decode('utf-8') == self.sample_cert) |
| 212 | + assert(options.tls_verify == False) |
| 213 | + assert(options.proxy == proxy_name) |
| 214 | + finally: |
| 215 | + self.remove_cert_file(cert_file) |
| 216 | + |
| 217 | + def test_query_client_with_options(self): |
| 218 | + connection = "grpc+tls://localhost:9999" |
| 219 | + token = "my_token" |
| 220 | + proxy_name = "http://my.proxy.org" |
| 221 | + cert_file = "cert_test.pem" |
| 222 | + self.create_cert_file(cert_file) |
| 223 | + options = QueryApiOptionsBuilder()\ |
| 224 | + .proxy(proxy_name) \ |
| 225 | + .root_certs(cert_file) \ |
| 226 | + .tls_verify(False) \ |
| 227 | + .build() |
| 228 | + |
| 229 | + client = QueryApi(connection, |
| 230 | + token, |
| 231 | + None, |
| 232 | + None, |
| 233 | + options |
| 234 | + ) |
| 235 | + |
| 236 | + print(f"\nDEBUG client {vars(client)}") |
| 237 | + try: |
| 238 | + assert(client._token == token) |
| 239 | + assert(client._flight_client_options['tls_root_certs'].decode('utf-8') == self.sample_cert) |
| 240 | + assert(client._proxy == proxy_name) |
| 241 | + # print(f"DEBUG client._flight_client_options['generic_options'] {dict(client._flight_client_options['generic_options'])['grpc.secondary_user_agent']}") |
| 242 | + assert(dict(client._flight_client_options['generic_options'])['grpc.secondary_user_agent'].startswith('influxdb3-python/')) |
| 243 | + assert(dict(client._flight_client_options['generic_options'])['grpc.http_proxy'] == proxy_name) |
| 244 | + finally: |
| 245 | + self.remove_cert_file(cert_file) |
| 246 | + |
| 247 | + def test_client_with_ssl_args(self): |
| 248 | + cert_name = "cert-test.pem" |
| 249 | + self.create_cert_file(cert_name) |
| 250 | + proxy = "http://localhost:9999" |
| 251 | + local_client = InfluxDBClient3( |
| 252 | + host="localhost", |
| 253 | + org="my_org", |
| 254 | + database="my_db", |
| 255 | + token="my_token", |
| 256 | + proxy=proxy, |
| 257 | + ssl_ca_cert = cert_name, |
| 258 | + verify_ssl = False |
| 259 | + ) |
| 260 | + |
| 261 | + try: |
| 262 | + qapi = local_client._query_api |
| 263 | + fc_opts = qapi._flight_client_options |
| 264 | + assert(qapi._proxy == proxy) |
| 265 | + assert(fc_opts['tls_root_certs'].decode('utf-8') == self.sample_cert) |
| 266 | + assert(fc_opts['disable_server_verification'] == True) |
| 267 | + assert(dict(fc_opts['generic_options'])['grpc.secondary_user_agent'].startswith('influxdb3-python/')) |
| 268 | + assert(dict(fc_opts['generic_options'])['grpc.http_proxy'] == proxy) |
| 269 | + finally: |
| 270 | + self.remove_cert_file(cert_name) |
0 commit comments