|
| 1 | +# -*- coding: utf-8; -*- |
| 2 | +# |
| 3 | +# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor |
| 4 | +# license agreements. See the NOTICE file distributed with this work for |
| 5 | +# additional information regarding copyright ownership. Crate licenses |
| 6 | +# this file to you under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. You may |
| 8 | +# obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | +# License for the specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# |
| 18 | +# However, if you have executed another commercial license agreement |
| 19 | +# with Crate these terms will supersede the license and you may use the |
| 20 | +# software solely pursuant to the terms of the relevant commercial agreement. |
| 21 | + |
| 22 | +import socket |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +from crate.client._options import ( |
| 27 | + credentials_from_url, |
| 28 | + keepalive_socket_options, |
| 29 | + server_url, |
| 30 | + sql_path, |
| 31 | + to_server_list, |
| 32 | + validate_compress, |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "server, expected", |
| 38 | + [ |
| 39 | + ("a", "http://a"), |
| 40 | + ("a/path", "http://a/path"), |
| 41 | + ("a:9345", "http://a:9345"), |
| 42 | + ("a:9345/path", "http://a:9345/path"), |
| 43 | + ("https://a:9345", "https://a:9345"), |
| 44 | + ("https://a:9345/path", "https://a:9345/path"), |
| 45 | + ("https://a", "https://a"), |
| 46 | + ("https://a/path", "https://a/path"), |
| 47 | + ("HTTPS://a", "https://a"), |
| 48 | + ("demo.crate.io", "http://demo.crate.io"), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_server_url(server, expected): |
| 52 | + assert server_url(server) == expected |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "servers", |
| 57 | + [ |
| 58 | + "localhost:4200 https://localhost:4201", |
| 59 | + ["localhost:4200", "https://localhost:4201"], |
| 60 | + ("localhost:4200", "https://localhost:4201"), |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_to_server_list(servers): |
| 64 | + assert to_server_list(servers) == [ |
| 65 | + "http://localhost:4200", |
| 66 | + "https://localhost:4201", |
| 67 | + ] |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + "url, username, password, expected", |
| 72 | + [ |
| 73 | + ("http://localhost:4200", None, None, (None, None)), |
| 74 | + ("http://crate@localhost:4200", None, None, ("crate", None)), |
| 75 | + ( |
| 76 | + "http://crate:s3cr3t@localhost:4200", |
| 77 | + None, |
| 78 | + None, |
| 79 | + ("crate", "s3cr3t"), |
| 80 | + ), |
| 81 | + ("http://localhost:4200", None, "s3cr3t", (None, "s3cr3t")), |
| 82 | + ("http://crate@localhost:4200", None, "s3cr3t", ("crate", "s3cr3t")), |
| 83 | + ( |
| 84 | + "http://crate:from-url@localhost:4200", |
| 85 | + None, |
| 86 | + "s3cr3t", |
| 87 | + ("crate", "from-url"), |
| 88 | + ), |
| 89 | + ("http://localhost:4200", "", None, ("", None)), |
| 90 | + ], |
| 91 | +) |
| 92 | +def test_credentials_from_url(url, username, password, expected): |
| 93 | + assert credentials_from_url(url, username, password) == expected |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.parametrize("compress", [True, False, 0, 8192]) |
| 97 | +def test_validate_compress(compress): |
| 98 | + assert validate_compress(compress) is compress |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize( |
| 102 | + "compress, type_name", [("8192", "'str'"), (None, "'NoneType'")] |
| 103 | +) |
| 104 | +def test_validate_compress_invalid(compress, type_name): |
| 105 | + with pytest.raises( |
| 106 | + TypeError, match=f"compress must be bool or int, got {type_name}" |
| 107 | + ): |
| 108 | + validate_compress(compress) |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.parametrize( |
| 112 | + "error_trace, expected", |
| 113 | + [ |
| 114 | + (False, "/_sql?types=true"), |
| 115 | + (True, "/_sql?types=true&error_trace=true"), |
| 116 | + ], |
| 117 | +) |
| 118 | +def test_sql_path(error_trace, expected): |
| 119 | + assert sql_path(error_trace) == expected |
| 120 | + |
| 121 | + |
| 122 | +def test_sql_path_with_given_path(): |
| 123 | + assert sql_path(True, "/_sql") == "/_sql&error_trace=true" |
| 124 | + |
| 125 | + |
| 126 | +def test_keepalive_socket_options_off(): |
| 127 | + assert keepalive_socket_options(False, 30, 10, 5) == [] |
| 128 | + |
| 129 | + |
| 130 | +def test_keepalive_socket_options_defaults(): |
| 131 | + assert keepalive_socket_options() == [ |
| 132 | + (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) |
| 133 | + ] |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.skipif( |
| 137 | + not all( |
| 138 | + hasattr(socket, name) |
| 139 | + for name in ("TCP_KEEPIDLE", "TCP_KEEPINTVL", "TCP_KEEPCNT") |
| 140 | + ), |
| 141 | + reason="Platform does not support all TCP keepalive options", |
| 142 | +) |
| 143 | +def test_keepalive_socket_options_tcp(): |
| 144 | + assert keepalive_socket_options(True, 30, 10, 5) == [ |
| 145 | + (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1), |
| 146 | + (socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 30), |
| 147 | + (socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10), |
| 148 | + (socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5), |
| 149 | + ] |
0 commit comments