| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Client unit tests.""" |
| 3 |
|
| 4 |
import json |
| 5 |
import socket |
| 6 |
import sys |
| 7 |
import unittest |
| 8 |
import random |
| 9 |
import warnings |
| 10 |
|
| •11 |
from unittest import mock |
| 12 |
import requests |
| 13 |
import requests.exceptions |
| 14 |
import requests_mock |
| 15 |
|
| •16 |
from functools import wraps |
| 17 |
|
| 18 |
def raises(*exceptions): |
| 19 |
valid = ' or '.join([e.__name__ for e in exceptions]) |
| 20 |
|
| 21 |
def decorate(func): |
| 22 |
name = func.__name__ |
| 23 |
def newfunc(*arg, **kw): |
| 24 |
try: |
| 25 |
func(*arg, **kw) |
| 26 |
except exceptions: |
| 27 |
pass |
| 28 |
except: |
| 29 |
raise |
| 30 |
else: |
| 31 |
message = "%s() did not raise %s" % (name, valid) |
| 32 |
raise AssertionError(message) |
| 33 |
newfunc = wraps(func)(newfunc) |
| 34 |
return newfunc |
| 35 |
return decorate |
| 36 |
|
| •37 |
from unittest.mock import patch |
| 38 |
|
| 39 |
from influxdb.influxdb08 import InfluxDBClient |
| 40 |
from influxdb.influxdb08.client import session |
| 41 |
|
| 42 |
if sys.version < '3': |
| 43 |
import codecs |
| 44 |
|
| 45 |
def u(x): |
| 46 |
"""Test codec.""" |
| 47 |
return codecs.unicode_escape_decode(x)[0] |
| 48 |
else: |
| 49 |
def u(x): |
| 50 |
"""Test codec.""" |
| 51 |
return x |
| 52 |
|
| 53 |
|
| 54 |
def _build_response_object(status_code=200, content=""): |
| 55 |
resp = requests.Response() |
| 56 |
resp.status_code = status_code |
| 57 |
resp._content = content.encode("utf8") |
| 58 |
return resp |
| 59 |
|
| 60 |
|
| 61 |
def _mocked_session(method="GET", status_code=200, content=""): |
| 62 |
method = method.upper() |
| 63 |
|
| 64 |
def request(*args, **kwargs): |
| 65 |
"""Define a request for the _mocked_session.""" |
| 66 |
c = content |
| 67 |
|
| 68 |
# Check method |
| 69 |
assert method == kwargs.get('method', 'GET') |
| 70 |
|
| 71 |
if method == 'POST': |
| 72 |
data = kwargs.get('data', None) |
| 73 |
|
| 74 |
if data is not None: |
| 75 |
# Data must be a string |
| 76 |
assert isinstance(data, str) |
| 77 |
|
| 78 |
# Data must be a JSON string |
| 79 |
assert c == json.loads(data, strict=True) |
| 80 |
|
| 81 |
c = data |
| 82 |
|
| 83 |
# Anyway, Content must be a JSON string (or empty string) |
| 84 |
if not isinstance(c, str): |
| 85 |
c = json.dumps(c) |
| 86 |
|
| 87 |
return _build_response_object(status_code=status_code, content=c) |
| 88 |
|
| 89 |
mocked = patch.object( |
| 90 |
session, |
| 91 |
'request', |
| 92 |
side_effect=request |
| 93 |
) |
| 94 |
|
| 95 |
return mocked |
| 96 |
|
| 97 |
|
| 98 |
class TestInfluxDBClient(unittest.TestCase): |
| 99 |
"""Define a TestInfluxDBClient object.""" |
| 100 |
|
| 101 |
def setUp(self): |
| 102 |
"""Set up a TestInfluxDBClient object.""" |
| 103 |
# By default, raise exceptions on warnings |
| 104 |
warnings.simplefilter('error', FutureWarning) |
| 105 |
|
| 106 |
self.dummy_points = [ |
| 107 |
{ |
| 108 |
"points": [ |
| 109 |
["1", 1, 1.0], |
| 110 |
["2", 2, 2.0] |
| 111 |
], |
| 112 |
"name": "foo", |
| 113 |
"columns": ["column_one", "column_two", "column_three"] |
| 114 |
} |
| 115 |
] |
| 116 |
|
| 117 |
self.dsn_string = 'influxdb://uSr:pWd@host:1886/db' |
| 118 |
|
| 119 |
def test_scheme(self): |
| 120 |
"""Test database scheme for TestInfluxDBClient object.""" |
| 121 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'database') |
| 122 |
self.assertEqual(cli._baseurl, 'http://host:8086') |
| 123 |
|
| 124 |
cli = InfluxDBClient( |
| 125 |
'host', 8086, 'username', 'password', 'database', ssl=True |
| 126 |
) |
| 127 |
self.assertEqual(cli._baseurl, 'https://host:8086') |
| 128 |
|
| 129 |
def test_dsn(self): |
| 130 |
"""Test datasource name for TestInfluxDBClient object.""" |
| 131 |
cli = InfluxDBClient.from_dsn(self.dsn_string) |
| 132 |
self.assertEqual('http://host:1886', cli._baseurl) |
| 133 |
self.assertEqual('uSr', cli._username) |
| 134 |
self.assertEqual('pWd', cli._password) |
| 135 |
self.assertEqual('db', cli._database) |
| 136 |
self.assertFalse(cli._use_udp) |
| 137 |
|
| 138 |
cli = InfluxDBClient.from_dsn('udp+' + self.dsn_string) |
| 139 |
self.assertTrue(cli._use_udp) |
| 140 |
|
| 141 |
cli = InfluxDBClient.from_dsn('https+' + self.dsn_string) |
| 142 |
self.assertEqual('https://host:1886', cli._baseurl) |
| 143 |
|
| 144 |
cli = InfluxDBClient.from_dsn('https+' + self.dsn_string, |
| 145 |
**{'ssl': False}) |
| 146 |
self.assertEqual('http://host:1886', cli._baseurl) |
| 147 |
|
| 148 |
def test_switch_database(self): |
| 149 |
"""Test switch database for TestInfluxDBClient object.""" |
| 150 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'database') |
| 151 |
cli.switch_database('another_database') |
| 152 |
self.assertEqual(cli._database, 'another_database') |
| 153 |
|
| 154 |
@raises(FutureWarning) |
| 155 |
def test_switch_db_deprecated(self): |
| 156 |
"""Test deprecated switch database for TestInfluxDBClient object.""" |
| 157 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'database') |
| 158 |
cli.switch_db('another_database') |
| 159 |
self.assertEqual(cli._database, 'another_database') |
| 160 |
|
| 161 |
def test_switch_user(self): |
| 162 |
"""Test switch user for TestInfluxDBClient object.""" |
| 163 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'database') |
| 164 |
cli.switch_user('another_username', 'another_password') |
| 165 |
self.assertEqual(cli._username, 'another_username') |
| 166 |
self.assertEqual(cli._password, 'another_password') |
| 167 |
|
| 168 |
def test_write(self): |
| 169 |
"""Test write to database for TestInfluxDBClient object.""" |
| 170 |
with requests_mock.Mocker() as m: |
| 171 |
m.register_uri( |
| 172 |
requests_mock.POST, |
| 173 |
"http://localhost:8086/write" |
| 174 |
) |
| 175 |
cli = InfluxDBClient(database='db') |
| 176 |
cli.write( |
| 177 |
{"database": "mydb", |
| 178 |
"retentionPolicy": "mypolicy", |
| 179 |
"points": [{"name": "cpu_load_short", |
| 180 |
"tags": {"host": "server01", |
| 181 |
"region": "us-west"}, |
| 182 |
"timestamp": "2009-11-10T23:00:00Z", |
| 183 |
"values": {"value": 0.64}}]} |
| 184 |
) |
| 185 |
|
| 186 |
self.assertEqual( |
| 187 |
json.loads(m.last_request.body), |
| 188 |
{"database": "mydb", |
| 189 |
"retentionPolicy": "mypolicy", |
| 190 |
"points": [{"name": "cpu_load_short", |
| 191 |
"tags": {"host": "server01", |
| 192 |
"region": "us-west"}, |
| 193 |
"timestamp": "2009-11-10T23:00:00Z", |
| 194 |
"values": {"value": 0.64}}]} |
| 195 |
) |
| 196 |
|
| 197 |
def test_write_points(self): |
| 198 |
"""Test write points for TestInfluxDBClient object.""" |
| 199 |
with requests_mock.Mocker() as m: |
| 200 |
m.register_uri( |
| 201 |
requests_mock.POST, |
| 202 |
"http://localhost:8086/db/db/series" |
| 203 |
) |
| 204 |
|
| 205 |
cli = InfluxDBClient(database='db') |
| 206 |
cli.write_points( |
| 207 |
self.dummy_points |
| 208 |
) |
| 209 |
|
| 210 |
self.assertListEqual( |
| 211 |
json.loads(m.last_request.body), |
| 212 |
self.dummy_points |
| 213 |
) |
| 214 |
|
| 215 |
def test_write_points_string(self): |
| 216 |
"""Test write string points for TestInfluxDBClient object.""" |
| 217 |
with requests_mock.Mocker() as m: |
| 218 |
m.register_uri( |
| 219 |
requests_mock.POST, |
| 220 |
"http://localhost:8086/db/db/series" |
| 221 |
) |
| 222 |
|
| 223 |
cli = InfluxDBClient(database='db') |
| 224 |
cli.write_points( |
| 225 |
str(json.dumps(self.dummy_points)) |
| 226 |
) |
| 227 |
|
| 228 |
self.assertListEqual( |
| 229 |
json.loads(m.last_request.body), |
| 230 |
self.dummy_points |
| 231 |
) |
| 232 |
|
| 233 |
def test_write_points_batch(self): |
| 234 |
"""Test write batch points for TestInfluxDBClient object.""" |
| 235 |
with requests_mock.Mocker() as m: |
| 236 |
m.register_uri(requests_mock.POST, |
| 237 |
"http://localhost:8086/db/db/series") |
| 238 |
cli = InfluxDBClient('localhost', 8086, |
| 239 |
'username', 'password', 'db') |
| 240 |
cli.write_points(data=self.dummy_points, batch_size=2) |
| 241 |
self.assertEqual(1, m.call_count) |
| 242 |
|
| 243 |
def test_write_points_batch_invalid_size(self): |
| 244 |
"""Test write batch points invalid size for TestInfluxDBClient.""" |
| 245 |
with requests_mock.Mocker() as m: |
| 246 |
m.register_uri(requests_mock.POST, |
| 247 |
"http://localhost:8086/db/db/series") |
| 248 |
cli = InfluxDBClient('localhost', 8086, |
| 249 |
'username', 'password', 'db') |
| 250 |
cli.write_points(data=self.dummy_points, batch_size=-2) |
| 251 |
self.assertEqual(1, m.call_count) |
| 252 |
|
| 253 |
def test_write_points_batch_multiple_series(self): |
| 254 |
"""Test write points batch multiple series.""" |
| 255 |
dummy_points = [ |
| 256 |
{"points": [["1", 1, 1.0], ["2", 2, 2.0], ["3", 3, 3.0], |
| 257 |
["4", 4, 4.0], ["5", 5, 5.0]], |
| 258 |
"name": "foo", |
| 259 |
"columns": ["val1", "val2", "val3"]}, |
| 260 |
{"points": [["1", 1, 1.0], ["2", 2, 2.0], ["3", 3, 3.0], |
| 261 |
["4", 4, 4.0], ["5", 5, 5.0], ["6", 6, 6.0], |
| 262 |
["7", 7, 7.0], ["8", 8, 8.0]], |
| 263 |
"name": "bar", |
| 264 |
"columns": ["val1", "val2", "val3"]}, |
| 265 |
] |
| 266 |
expected_last_body = [{'points': [['7', 7, 7.0], ['8', 8, 8.0]], |
| 267 |
'name': 'bar', |
| 268 |
'columns': ['val1', 'val2', 'val3']}] |
| 269 |
with requests_mock.Mocker() as m: |
| 270 |
m.register_uri(requests_mock.POST, |
| 271 |
"http://localhost:8086/db/db/series") |
| 272 |
cli = InfluxDBClient('localhost', 8086, |
| 273 |
'username', 'password', 'db') |
| 274 |
cli.write_points(data=dummy_points, batch_size=3) |
| 275 |
self.assertEqual(m.call_count, 5) |
| 276 |
self.assertEqual(expected_last_body, m.request_history[4].json()) |
| 277 |
|
| 278 |
def test_write_points_udp(self): |
| 279 |
"""Test write points UDP for TestInfluxDBClient object.""" |
| 280 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 281 |
port = random.randint(4000, 8000) |
| 282 |
s.bind(('0.0.0.0', port)) |
| 283 |
|
| 284 |
cli = InfluxDBClient( |
| 285 |
'localhost', 8086, 'root', 'root', |
| 286 |
'test', use_udp=True, udp_port=port |
| 287 |
) |
| 288 |
cli.write_points(self.dummy_points) |
| 289 |
|
| 290 |
received_data, addr = s.recvfrom(1024) |
| 291 |
|
| 292 |
self.assertEqual(self.dummy_points, |
| 293 |
json.loads(received_data.decode(), strict=True)) |
| 294 |
|
| 295 |
def test_write_bad_precision_udp(self): |
| 296 |
"""Test write UDP w/bad precision.""" |
| 297 |
cli = InfluxDBClient( |
| 298 |
'localhost', 8086, 'root', 'root', |
| 299 |
'test', use_udp=True, udp_port=4444 |
| 300 |
) |
| 301 |
|
| •302 |
with self.assertRaisesRegex( |
| 303 |
Exception, |
| 304 |
"InfluxDB only supports seconds precision for udp writes" |
| 305 |
): |
| 306 |
cli.write_points( |
| 307 |
self.dummy_points, |
| 308 |
time_precision='ms' |
| 309 |
) |
| 310 |
|
| 311 |
@raises(Exception) |
| 312 |
def test_write_points_fails(self): |
| 313 |
"""Test failed write points for TestInfluxDBClient object.""" |
| 314 |
with _mocked_session('post', 500): |
| 315 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 316 |
cli.write_points([]) |
| 317 |
|
| 318 |
def test_write_points_with_precision(self): |
| 319 |
"""Test write points with precision.""" |
| 320 |
with _mocked_session('post', 200, self.dummy_points): |
| 321 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 322 |
self.assertTrue(cli.write_points(self.dummy_points)) |
| 323 |
|
| 324 |
def test_write_points_bad_precision(self): |
| 325 |
"""Test write points with bad precision.""" |
| 326 |
cli = InfluxDBClient() |
| •327 |
with self.assertRaisesRegex( |
| 328 |
Exception, |
| •329 |
r"Invalid time precision is given. \(use 's', 'm', 'ms' or 'u'\)" |
| 330 |
): |
| 331 |
cli.write_points( |
| 332 |
self.dummy_points, |
| 333 |
time_precision='g' |
| 334 |
) |
| 335 |
|
| 336 |
@raises(Exception) |
| 337 |
def test_write_points_with_precision_fails(self): |
| 338 |
"""Test write points where precision fails.""" |
| 339 |
with _mocked_session('post', 500): |
| 340 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 341 |
cli.write_points_with_precision([]) |
| 342 |
|
| 343 |
def test_delete_points(self): |
| 344 |
"""Test delete points for TestInfluxDBClient object.""" |
| 345 |
with _mocked_session('delete', 204) as mocked: |
| 346 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 347 |
self.assertTrue(cli.delete_points("foo")) |
| 348 |
|
| 349 |
self.assertEqual(len(mocked.call_args_list), 1) |
| 350 |
args, kwds = mocked.call_args_list[0] |
| 351 |
|
| 352 |
self.assertEqual(kwds['params'], |
| 353 |
{'u': 'username', 'p': 'password'}) |
| 354 |
self.assertEqual(kwds['url'], 'http://host:8086/db/db/series/foo') |
| 355 |
|
| 356 |
@raises(Exception) |
| 357 |
def test_delete_points_with_wrong_name(self): |
| 358 |
"""Test delete points with wrong name.""" |
| 359 |
with _mocked_session('delete', 400): |
| 360 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 361 |
cli.delete_points("nonexist") |
| 362 |
|
| 363 |
@raises(NotImplementedError) |
| 364 |
def test_create_scheduled_delete(self): |
| 365 |
"""Test create scheduled deletes.""" |
| 366 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 367 |
cli.create_scheduled_delete([]) |
| 368 |
|
| 369 |
@raises(NotImplementedError) |
| 370 |
def test_get_list_scheduled_delete(self): |
| 371 |
"""Test get schedule list of deletes TestInfluxDBClient.""" |
| 372 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 373 |
cli.get_list_scheduled_delete() |
| 374 |
|
| 375 |
@raises(NotImplementedError) |
| 376 |
def test_remove_scheduled_delete(self): |
| 377 |
"""Test remove scheduled delete TestInfluxDBClient.""" |
| 378 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 379 |
cli.remove_scheduled_delete(1) |
| 380 |
|
| 381 |
def test_query(self): |
| 382 |
"""Test query for TestInfluxDBClient object.""" |
| 383 |
data = [ |
| 384 |
{ |
| 385 |
"name": "foo", |
| 386 |
"columns": ["time", "sequence_number", "column_one"], |
| 387 |
"points": [ |
| 388 |
[1383876043, 16, "2"], [1383876043, 15, "1"], |
| 389 |
[1383876035, 14, "2"], [1383876035, 13, "1"] |
| 390 |
] |
| 391 |
} |
| 392 |
] |
| 393 |
with _mocked_session('get', 200, data): |
| 394 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 395 |
result = cli.query('select column_one from foo;') |
| 396 |
self.assertEqual(len(result[0]['points']), 4) |
| 397 |
|
| 398 |
def test_query_chunked(self): |
| 399 |
"""Test chunked query for TestInfluxDBClient object.""" |
| 400 |
cli = InfluxDBClient(database='db') |
| 401 |
example_object = { |
| 402 |
'points': [ |
| 403 |
[1415206250119, 40001, 667], |
| 404 |
[1415206244555, 30001, 7], |
| 405 |
[1415206228241, 20001, 788], |
| 406 |
[1415206212980, 10001, 555], |
| 407 |
[1415197271586, 10001, 23] |
| 408 |
], |
| 409 |
'name': 'foo', |
| 410 |
'columns': [ |
| 411 |
'time', |
| 412 |
'sequence_number', |
| 413 |
'val' |
| 414 |
] |
| 415 |
} |
| 416 |
example_response = \ |
| 417 |
json.dumps(example_object) + json.dumps(example_object) |
| 418 |
|
| 419 |
with requests_mock.Mocker() as m: |
| 420 |
m.register_uri( |
| 421 |
requests_mock.GET, |
| 422 |
"http://localhost:8086/db/db/series", |
| 423 |
text=example_response |
| 424 |
) |
| 425 |
|
| 426 |
self.assertListEqual( |
| 427 |
cli.query('select * from foo', chunked=True), |
| 428 |
[example_object, example_object] |
| 429 |
) |
| 430 |
|
| 431 |
def test_query_chunked_unicode(self): |
| 432 |
"""Test unicode chunked query for TestInfluxDBClient object.""" |
| 433 |
cli = InfluxDBClient(database='db') |
| 434 |
example_object = { |
| 435 |
'points': [ |
| 436 |
[1415206212980, 10001, u('unicode-\xcf\x89')], |
| 437 |
[1415197271586, 10001, u('more-unicode-\xcf\x90')] |
| 438 |
], |
| 439 |
'name': 'foo', |
| 440 |
'columns': [ |
| 441 |
'time', |
| 442 |
'sequence_number', |
| 443 |
'val' |
| 444 |
] |
| 445 |
} |
| 446 |
example_response = \ |
| 447 |
json.dumps(example_object) + json.dumps(example_object) |
| 448 |
|
| 449 |
with requests_mock.Mocker() as m: |
| 450 |
m.register_uri( |
| 451 |
requests_mock.GET, |
| 452 |
"http://localhost:8086/db/db/series", |
| 453 |
text=example_response |
| 454 |
) |
| 455 |
|
| 456 |
self.assertListEqual( |
| 457 |
cli.query('select * from foo', chunked=True), |
| 458 |
[example_object, example_object] |
| 459 |
) |
| 460 |
|
| 461 |
@raises(Exception) |
| 462 |
def test_query_fail(self): |
| 463 |
"""Test failed query for TestInfluxDBClient.""" |
| 464 |
with _mocked_session('get', 401): |
| 465 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 466 |
cli.query('select column_one from foo;') |
| 467 |
|
| 468 |
def test_query_bad_precision(self): |
| 469 |
"""Test query with bad precision for TestInfluxDBClient.""" |
| 470 |
cli = InfluxDBClient() |
| •471 |
with self.assertRaisesRegex( |
| 472 |
Exception, |
| •473 |
r"Invalid time precision is given. \(use 's', 'm', 'ms' or 'u'\)" |
| 474 |
): |
| 475 |
cli.query('select column_one from foo', time_precision='g') |
| 476 |
|
| 477 |
def test_create_database(self): |
| 478 |
"""Test create database for TestInfluxDBClient.""" |
| 479 |
with _mocked_session('post', 201, {"name": "new_db"}): |
| 480 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 481 |
self.assertTrue(cli.create_database('new_db')) |
| 482 |
|
| 483 |
@raises(Exception) |
| 484 |
def test_create_database_fails(self): |
| 485 |
"""Test failed create database for TestInfluxDBClient.""" |
| 486 |
with _mocked_session('post', 401): |
| 487 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 488 |
cli.create_database('new_db') |
| 489 |
|
| 490 |
def test_delete_database(self): |
| 491 |
"""Test delete database for TestInfluxDBClient.""" |
| 492 |
with _mocked_session('delete', 204): |
| 493 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 494 |
self.assertTrue(cli.delete_database('old_db')) |
| 495 |
|
| 496 |
@raises(Exception) |
| 497 |
def test_delete_database_fails(self): |
| 498 |
"""Test failed delete database for TestInfluxDBClient.""" |
| 499 |
with _mocked_session('delete', 401): |
| 500 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 501 |
cli.delete_database('old_db') |
| 502 |
|
| 503 |
def test_get_list_database(self): |
| 504 |
"""Test get list of databases for TestInfluxDBClient.""" |
| 505 |
data = [ |
| 506 |
{"name": "a_db"} |
| 507 |
] |
| 508 |
with _mocked_session('get', 200, data): |
| 509 |
cli = InfluxDBClient('host', 8086, 'username', 'password') |
| 510 |
self.assertEqual(len(cli.get_list_database()), 1) |
| 511 |
self.assertEqual(cli.get_list_database()[0]['name'], 'a_db') |
| 512 |
|
| 513 |
@raises(Exception) |
| 514 |
def test_get_list_database_fails(self): |
| 515 |
"""Test failed get list of databases for TestInfluxDBClient.""" |
| 516 |
with _mocked_session('get', 401): |
| 517 |
cli = InfluxDBClient('host', 8086, 'username', 'password') |
| 518 |
cli.get_list_database() |
| 519 |
|
| 520 |
@raises(FutureWarning) |
| 521 |
def test_get_database_list_deprecated(self): |
| 522 |
"""Test deprecated get database list for TestInfluxDBClient.""" |
| 523 |
data = [ |
| 524 |
{"name": "a_db"} |
| 525 |
] |
| 526 |
with _mocked_session('get', 200, data): |
| 527 |
cli = InfluxDBClient('host', 8086, 'username', 'password') |
| 528 |
self.assertEqual(len(cli.get_database_list()), 1) |
| 529 |
self.assertEqual(cli.get_database_list()[0]['name'], 'a_db') |
| 530 |
|
| 531 |
def test_delete_series(self): |
| 532 |
"""Test delete series for TestInfluxDBClient.""" |
| 533 |
with _mocked_session('delete', 204): |
| 534 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 535 |
cli.delete_series('old_series') |
| 536 |
|
| 537 |
@raises(Exception) |
| 538 |
def test_delete_series_fails(self): |
| 539 |
"""Test failed delete series for TestInfluxDBClient.""" |
| 540 |
with _mocked_session('delete', 401): |
| 541 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 542 |
cli.delete_series('old_series') |
| 543 |
|
| 544 |
def test_get_series_list(self): |
| 545 |
"""Test get list of series for TestInfluxDBClient.""" |
| 546 |
cli = InfluxDBClient(database='db') |
| 547 |
|
| 548 |
with requests_mock.Mocker() as m: |
| 549 |
example_response = \ |
| 550 |
'[{"name":"list_series_result","columns":' \ |
| 551 |
'["time","name"],"points":[[0,"foo"],[0,"bar"]]}]' |
| 552 |
|
| 553 |
m.register_uri( |
| 554 |
requests_mock.GET, |
| 555 |
"http://localhost:8086/db/db/series", |
| 556 |
text=example_response |
| 557 |
) |
| 558 |
|
| 559 |
self.assertListEqual( |
| 560 |
cli.get_list_series(), |
| 561 |
['foo', 'bar'] |
| 562 |
) |
| 563 |
|
| 564 |
def test_get_continuous_queries(self): |
| 565 |
"""Test get continuous queries for TestInfluxDBClient.""" |
| 566 |
cli = InfluxDBClient(database='db') |
| 567 |
|
| 568 |
with requests_mock.Mocker() as m: |
| 569 |
|
| 570 |
# Tip: put this in a json linter! |
| 571 |
example_response = '[ { "name": "continuous queries", "columns"' \ |
| 572 |
': [ "time", "id", "query" ], "points": [ [ ' \ |
| 573 |
'0, 1, "select foo(bar,95) from \\"foo_bar' \ |
| 574 |
's\\" group by time(5m) into response_times.' \ |
| 575 |
'percentiles.5m.95" ], [ 0, 2, "select perce' \ |
| 576 |
'ntile(value,95) from \\"response_times\\" g' \ |
| 577 |
'roup by time(5m) into response_times.percen' \ |
| 578 |
'tiles.5m.95" ] ] } ]' |
| 579 |
|
| 580 |
m.register_uri( |
| 581 |
requests_mock.GET, |
| 582 |
"http://localhost:8086/db/db/series", |
| 583 |
text=example_response |
| 584 |
) |
| 585 |
|
| 586 |
self.assertListEqual( |
| 587 |
cli.get_list_continuous_queries(), |
| 588 |
[ |
| 589 |
'select foo(bar,95) from "foo_bars" group ' |
| 590 |
'by time(5m) into response_times.percentiles.5m.95', |
| 591 |
|
| 592 |
'select percentile(value,95) from "response_times" group ' |
| 593 |
'by time(5m) into response_times.percentiles.5m.95' |
| 594 |
] |
| 595 |
) |
| 596 |
|
| 597 |
def test_get_list_cluster_admins(self): |
| 598 |
"""Test get list of cluster admins, not implemented.""" |
| 599 |
pass |
| 600 |
|
| 601 |
def test_add_cluster_admin(self): |
| 602 |
"""Test add cluster admin for TestInfluxDBClient.""" |
| 603 |
with requests_mock.Mocker() as m: |
| 604 |
m.register_uri( |
| 605 |
requests_mock.POST, |
| 606 |
"http://localhost:8086/cluster_admins" |
| 607 |
) |
| 608 |
|
| 609 |
cli = InfluxDBClient(database='db') |
| 610 |
cli.add_cluster_admin( |
| 611 |
new_username='paul', |
| 612 |
new_password='laup' |
| 613 |
) |
| 614 |
|
| 615 |
self.assertDictEqual( |
| 616 |
json.loads(m.last_request.body), |
| 617 |
{ |
| 618 |
'name': 'paul', |
| 619 |
'password': 'laup' |
| 620 |
} |
| 621 |
) |
| 622 |
|
| 623 |
def test_update_cluster_admin_password(self): |
| 624 |
"""Test update cluster admin pass for TestInfluxDBClient.""" |
| 625 |
with requests_mock.Mocker() as m: |
| 626 |
m.register_uri( |
| 627 |
requests_mock.POST, |
| 628 |
"http://localhost:8086/cluster_admins/paul" |
| 629 |
) |
| 630 |
|
| 631 |
cli = InfluxDBClient(database='db') |
| 632 |
cli.update_cluster_admin_password( |
| 633 |
username='paul', |
| 634 |
new_password='laup' |
| 635 |
) |
| 636 |
|
| 637 |
self.assertDictEqual( |
| 638 |
json.loads(m.last_request.body), |
| 639 |
{'password': 'laup'} |
| 640 |
) |
| 641 |
|
| 642 |
def test_delete_cluster_admin(self): |
| 643 |
"""Test delete cluster admin for TestInfluxDBClient.""" |
| 644 |
with requests_mock.Mocker() as m: |
| 645 |
m.register_uri( |
| 646 |
requests_mock.DELETE, |
| 647 |
"http://localhost:8086/cluster_admins/paul", |
| 648 |
status_code=200, |
| 649 |
) |
| 650 |
|
| 651 |
cli = InfluxDBClient(database='db') |
| 652 |
cli.delete_cluster_admin(username='paul') |
| 653 |
|
| 654 |
self.assertIsNone(m.last_request.body) |
| 655 |
|
| 656 |
def test_set_database_admin(self): |
| 657 |
"""Test set database admin for TestInfluxDBClient.""" |
| 658 |
pass |
| 659 |
|
| 660 |
def test_unset_database_admin(self): |
| 661 |
"""Test unset database admin for TestInfluxDBClient.""" |
| 662 |
pass |
| 663 |
|
| 664 |
def test_alter_database_admin(self): |
| 665 |
"""Test alter database admin for TestInfluxDBClient.""" |
| 666 |
with requests_mock.Mocker() as m: |
| 667 |
m.register_uri( |
| 668 |
requests_mock.POST, |
| 669 |
"http://localhost:8086/db/db/users/paul" |
| 670 |
) |
| 671 |
|
| 672 |
cli = InfluxDBClient(database='db') |
| 673 |
cli.alter_database_admin( |
| 674 |
username='paul', |
| 675 |
is_admin=False |
| 676 |
) |
| 677 |
|
| 678 |
self.assertDictEqual( |
| 679 |
json.loads(m.last_request.body), |
| 680 |
{ |
| 681 |
'admin': False |
| 682 |
} |
| 683 |
) |
| 684 |
|
| 685 |
@raises(NotImplementedError) |
| 686 |
def test_get_list_database_admins(self): |
| 687 |
"""Test get list of database admins for TestInfluxDBClient.""" |
| 688 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 689 |
cli.get_list_database_admins() |
| 690 |
|
| 691 |
@raises(NotImplementedError) |
| 692 |
def test_add_database_admin(self): |
| 693 |
"""Test add database admins for TestInfluxDBClient.""" |
| 694 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 695 |
cli.add_database_admin('admin', 'admin_secret_password') |
| 696 |
|
| 697 |
@raises(NotImplementedError) |
| 698 |
def test_update_database_admin_password(self): |
| 699 |
"""Test update database admin pass for TestInfluxDBClient.""" |
| 700 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 701 |
cli.update_database_admin_password('admin', 'admin_secret_password') |
| 702 |
|
| 703 |
@raises(NotImplementedError) |
| 704 |
def test_delete_database_admin(self): |
| 705 |
"""Test delete database admin for TestInfluxDBClient.""" |
| 706 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 707 |
cli.delete_database_admin('admin') |
| 708 |
|
| 709 |
def test_get_database_users(self): |
| 710 |
"""Test get database users for TestInfluxDBClient.""" |
| 711 |
cli = InfluxDBClient('localhost', 8086, 'username', 'password', 'db') |
| 712 |
|
| 713 |
example_response = \ |
| 714 |
'[{"name":"paul","isAdmin":false,"writeTo":".*","readFrom":".*"},'\ |
| 715 |
'{"name":"bobby","isAdmin":false,"writeTo":".*","readFrom":".*"}]' |
| 716 |
|
| 717 |
with requests_mock.Mocker() as m: |
| 718 |
m.register_uri( |
| 719 |
requests_mock.GET, |
| 720 |
"http://localhost:8086/db/db/users", |
| 721 |
text=example_response |
| 722 |
) |
| 723 |
users = cli.get_database_users() |
| 724 |
|
| 725 |
self.assertEqual(json.loads(example_response), users) |
| 726 |
|
| 727 |
def test_add_database_user(self): |
| 728 |
"""Test add database user for TestInfluxDBClient.""" |
| 729 |
with requests_mock.Mocker() as m: |
| 730 |
m.register_uri( |
| 731 |
requests_mock.POST, |
| 732 |
"http://localhost:8086/db/db/users" |
| 733 |
) |
| 734 |
cli = InfluxDBClient(database='db') |
| 735 |
cli.add_database_user( |
| 736 |
new_username='paul', |
| 737 |
new_password='laup', |
| 738 |
permissions=('.*', '.*') |
| 739 |
) |
| 740 |
|
| 741 |
self.assertDictEqual( |
| 742 |
json.loads(m.last_request.body), |
| 743 |
{ |
| 744 |
'writeTo': '.*', |
| 745 |
'password': 'laup', |
| 746 |
'readFrom': '.*', |
| 747 |
'name': 'paul' |
| 748 |
} |
| 749 |
) |
| 750 |
|
| 751 |
def test_add_database_user_bad_permissions(self): |
| 752 |
"""Test add database user with bad perms for TestInfluxDBClient.""" |
| 753 |
cli = InfluxDBClient() |
| 754 |
|
| •755 |
with self.assertRaisesRegex( |
| 756 |
Exception, |
| •757 |
r"'permissions' must be \(readFrom, writeTo\) tuple" |
| 758 |
): |
| 759 |
cli.add_database_user( |
| 760 |
new_password='paul', |
| 761 |
new_username='paul', |
| 762 |
permissions=('hello', 'hello', 'hello') |
| 763 |
) |
| 764 |
|
| 765 |
def test_alter_database_user_password(self): |
| 766 |
"""Test alter database user pass for TestInfluxDBClient.""" |
| 767 |
with requests_mock.Mocker() as m: |
| 768 |
m.register_uri( |
| 769 |
requests_mock.POST, |
| 770 |
"http://localhost:8086/db/db/users/paul" |
| 771 |
) |
| 772 |
|
| 773 |
cli = InfluxDBClient(database='db') |
| 774 |
cli.alter_database_user( |
| 775 |
username='paul', |
| 776 |
password='n3wp4ss!' |
| 777 |
) |
| 778 |
|
| 779 |
self.assertDictEqual( |
| 780 |
json.loads(m.last_request.body), |
| 781 |
{ |
| 782 |
'password': 'n3wp4ss!' |
| 783 |
} |
| 784 |
) |
| 785 |
|
| 786 |
def test_alter_database_user_permissions(self): |
| 787 |
"""Test alter database user perms for TestInfluxDBClient.""" |
| 788 |
with requests_mock.Mocker() as m: |
| 789 |
m.register_uri( |
| 790 |
requests_mock.POST, |
| 791 |
"http://localhost:8086/db/db/users/paul" |
| 792 |
) |
| 793 |
|
| 794 |
cli = InfluxDBClient(database='db') |
| 795 |
cli.alter_database_user( |
| 796 |
username='paul', |
| 797 |
permissions=('^$', '.*') |
| 798 |
) |
| 799 |
|
| 800 |
self.assertDictEqual( |
| 801 |
json.loads(m.last_request.body), |
| 802 |
{ |
| 803 |
'readFrom': '^$', |
| 804 |
'writeTo': '.*' |
| 805 |
} |
| 806 |
) |
| 807 |
|
| 808 |
def test_alter_database_user_password_and_permissions(self): |
| 809 |
"""Test alter database user pass and perms for TestInfluxDBClient.""" |
| 810 |
with requests_mock.Mocker() as m: |
| 811 |
m.register_uri( |
| 812 |
requests_mock.POST, |
| 813 |
"http://localhost:8086/db/db/users/paul" |
| 814 |
) |
| 815 |
|
| 816 |
cli = InfluxDBClient(database='db') |
| 817 |
cli.alter_database_user( |
| 818 |
username='paul', |
| 819 |
password='n3wp4ss!', |
| 820 |
permissions=('^$', '.*') |
| 821 |
) |
| 822 |
|
| 823 |
self.assertDictEqual( |
| 824 |
json.loads(m.last_request.body), |
| 825 |
{ |
| 826 |
'password': 'n3wp4ss!', |
| 827 |
'readFrom': '^$', |
| 828 |
'writeTo': '.*' |
| 829 |
} |
| 830 |
) |
| 831 |
|
| 832 |
def test_update_database_user_password_current_user(self): |
| 833 |
"""Test update database user pass for TestInfluxDBClient.""" |
| 834 |
cli = InfluxDBClient( |
| 835 |
username='root', |
| 836 |
password='hello', |
| 837 |
database='database' |
| 838 |
) |
| 839 |
with requests_mock.Mocker() as m: |
| 840 |
m.register_uri( |
| 841 |
requests_mock.POST, |
| 842 |
"http://localhost:8086/db/database/users/root" |
| 843 |
) |
| 844 |
|
| 845 |
cli.update_database_user_password( |
| 846 |
username='root', |
| 847 |
new_password='bye' |
| 848 |
) |
| 849 |
|
| 850 |
self.assertEqual(cli._password, 'bye') |
| 851 |
|
| 852 |
def test_delete_database_user(self): |
| 853 |
"""Test delete database user for TestInfluxDBClient.""" |
| 854 |
with requests_mock.Mocker() as m: |
| 855 |
m.register_uri( |
| 856 |
requests_mock.DELETE, |
| 857 |
"http://localhost:8086/db/db/users/paul" |
| 858 |
) |
| 859 |
|
| 860 |
cli = InfluxDBClient(database='db') |
| 861 |
cli.delete_database_user(username='paul') |
| 862 |
|
| 863 |
self.assertIsNone(m.last_request.body) |
| 864 |
|
| 865 |
@raises(NotImplementedError) |
| 866 |
def test_update_permission(self): |
| 867 |
"""Test update permission for TestInfluxDBClient.""" |
| 868 |
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') |
| 869 |
cli.update_permission('admin', []) |
| 870 |
|
| 871 |
@mock.patch('requests.Session.request') |
| 872 |
def test_request_retry(self, mock_request): |
| 873 |
"""Test that two connection errors will be handled.""" |
| 874 |
class CustomMock(object): |
| 875 |
"""Define CustomMock object.""" |
| 876 |
|
| 877 |
def __init__(self): |
| 878 |
self.i = 0 |
| 879 |
|
| 880 |
def connection_error(self, *args, **kwargs): |
| 881 |
"""Test connection error in CustomMock.""" |
| 882 |
self.i += 1 |
| 883 |
|
| 884 |
if self.i < 3: |
| 885 |
raise requests.exceptions.ConnectionError |
| 886 |
else: |
| 887 |
r = requests.Response() |
| 888 |
r.status_code = 200 |
| 889 |
return r |
| 890 |
|
| 891 |
mock_request.side_effect = CustomMock().connection_error |
| 892 |
|
| 893 |
cli = InfluxDBClient(database='db') |
| 894 |
cli.write_points( |
| 895 |
self.dummy_points |
| 896 |
) |
| 897 |
|
| 898 |
@mock.patch('requests.Session.request') |
| 899 |
def test_request_retry_raises(self, mock_request): |
| 900 |
"""Test that three connection errors will not be handled.""" |
| 901 |
class CustomMock(object): |
| 902 |
"""Define CustomMock object.""" |
| 903 |
|
| 904 |
def __init__(self): |
| 905 |
"""Initialize the object.""" |
| 906 |
self.i = 0 |
| 907 |
|
| 908 |
def connection_error(self, *args, **kwargs): |
| 909 |
"""Test the connection error for CustomMock.""" |
| 910 |
self.i += 1 |
| 911 |
|
| 912 |
if self.i < 4: |
| 913 |
raise requests.exceptions.ConnectionError |
| 914 |
else: |
| 915 |
r = requests.Response() |
| 916 |
r.status_code = 200 |
| 917 |
return r |
| 918 |
|
| 919 |
mock_request.side_effect = CustomMock().connection_error |
| 920 |
|
| 921 |
cli = InfluxDBClient(database='db') |
| 922 |
|
| 923 |
with self.assertRaises(requests.exceptions.ConnectionError): |
| 924 |
cli.write_points(self.dummy_points) |