| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Unit tests for checking the InfluxDB server. |
| 3 |
|
| 4 |
The good/expected interaction between: |
| 5 |
|
| 6 |
+ the python client.. (obviously) |
| 7 |
+ and a *_real_* server instance running. |
| 8 |
|
| 9 |
This basically duplicates what's in client_test.py |
| 10 |
but without mocking around every call. |
| 11 |
""" |
| 12 |
from __future__ import absolute_import |
| 13 |
from __future__ import division |
| 14 |
from __future__ import print_function |
| 15 |
from __future__ import unicode_literals |
| 16 |
|
| 17 |
from functools import partial |
| 18 |
import os |
| 19 |
import time |
| 20 |
import unittest |
| 21 |
import warnings |
| 22 |
|
| 23 |
from influxdb import InfluxDBClient |
| 24 |
from influxdb.exceptions import InfluxDBClientError |
| 25 |
|
| 26 |
from influxdb.tests import skip_if_pypy, using_pypy, skip_server_tests |
| 27 |
from influxdb.tests.server_tests.base import ManyTestCasesWithServerMixin |
| 28 |
from influxdb.tests.server_tests.base import SingleTestCaseWithServerMixin |
| 29 |
from influxdb.tests.server_tests.base import ManyTestCasesWithServerGzipMixin |
| 30 |
from influxdb.tests.server_tests.base import SingleTestCaseWithServerGzipMixin |
| 31 |
|
| 32 |
# By default, raise exceptions on warnings |
| 33 |
warnings.simplefilter('error', FutureWarning) |
| 34 |
|
| 35 |
if not using_pypy: |
| 36 |
import pandas as pd |
| •37 |
from pandas.testing import assert_frame_equal |
| 38 |
|
| 39 |
|
| 40 |
THIS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 41 |
|
| 42 |
|
| 43 |
def point(series_name, timestamp=None, tags=None, **fields): |
| 44 |
"""Define what a point looks like.""" |
| 45 |
res = {'measurement': series_name} |
| 46 |
|
| 47 |
if timestamp: |
| 48 |
res['time'] = timestamp |
| 49 |
|
| 50 |
if tags: |
| 51 |
res['tags'] = tags |
| 52 |
|
| 53 |
res['fields'] = fields |
| 54 |
return res |
| 55 |
|
| 56 |
|
| 57 |
dummy_point = [ # some dummy points |
| 58 |
{ |
| 59 |
"measurement": "cpu_load_short", |
| 60 |
"tags": { |
| 61 |
"host": "server01", |
| 62 |
"region": "us-west" |
| 63 |
}, |
| 64 |
"time": "2009-11-10T23:00:00Z", |
| 65 |
"fields": { |
| 66 |
"value": 0.64 |
| 67 |
} |
| 68 |
} |
| 69 |
] |
| 70 |
|
| 71 |
dummy_points = [ # some dummy points |
| 72 |
dummy_point[0], |
| 73 |
{ |
| 74 |
"measurement": "memory", |
| 75 |
"tags": { |
| 76 |
"host": "server01", |
| 77 |
"region": "us-west" |
| 78 |
}, |
| 79 |
"time": "2009-11-10T23:01:35Z", |
| 80 |
"fields": { |
| 81 |
"value": 33.0 |
| 82 |
} |
| 83 |
} |
| 84 |
] |
| 85 |
|
| 86 |
if not using_pypy: |
| 87 |
dummy_point_df = { |
| 88 |
"measurement": "cpu_load_short", |
| 89 |
"tags": {"host": "server01", |
| 90 |
"region": "us-west"}, |
| 91 |
"dataframe": pd.DataFrame( |
| 92 |
[[0.64]], columns=['value'], |
| 93 |
index=pd.to_datetime(["2009-11-10T23:00:00Z"])) |
| 94 |
} |
| 95 |
dummy_points_df = [{ |
| 96 |
"measurement": "cpu_load_short", |
| 97 |
"tags": {"host": "server01", "region": "us-west"}, |
| 98 |
"dataframe": pd.DataFrame( |
| 99 |
[[0.64]], columns=['value'], |
| 100 |
index=pd.to_datetime(["2009-11-10T23:00:00Z"])), |
| 101 |
}, { |
| 102 |
"measurement": "memory", |
| 103 |
"tags": {"host": "server01", "region": "us-west"}, |
| 104 |
"dataframe": pd.DataFrame( |
| 105 |
[[33]], columns=['value'], |
| 106 |
index=pd.to_datetime(["2009-11-10T23:01:35Z"]) |
| 107 |
) |
| 108 |
}] |
| 109 |
|
| 110 |
|
| 111 |
dummy_point_without_timestamp = [ |
| 112 |
{ |
| 113 |
"measurement": "cpu_load_short", |
| 114 |
"tags": { |
| 115 |
"host": "server02", |
| 116 |
"region": "us-west" |
| 117 |
}, |
| 118 |
"fields": { |
| 119 |
"value": 0.64 |
| 120 |
} |
| 121 |
} |
| 122 |
] |
| 123 |
|
| 124 |
|
| 125 |
@skip_server_tests |
| 126 |
class SimpleTests(SingleTestCaseWithServerMixin, unittest.TestCase): |
| 127 |
"""Define the class of simple tests.""" |
| 128 |
|
| 129 |
influxdb_template_conf = os.path.join(THIS_DIR, 'influxdb.conf.template') |
| 130 |
|
| 131 |
def test_fresh_server_no_db(self): |
| 132 |
"""Test a fresh server without database.""" |
| 133 |
self.assertEqual([], self.cli.get_list_database()) |
| 134 |
|
| 135 |
def test_create_database(self): |
| 136 |
"""Test create a database.""" |
| 137 |
self.assertIsNone(self.cli.create_database('new_db_1')) |
| 138 |
self.assertIsNone(self.cli.create_database('new_db_2')) |
| 139 |
self.assertEqual( |
| 140 |
self.cli.get_list_database(), |
| 141 |
[{'name': 'new_db_1'}, {'name': 'new_db_2'}] |
| 142 |
) |
| 143 |
|
| 144 |
def test_drop_database(self): |
| 145 |
"""Test drop a database.""" |
| 146 |
self.test_create_database() |
| 147 |
self.assertIsNone(self.cli.drop_database('new_db_1')) |
| 148 |
self.assertEqual([{'name': 'new_db_2'}], self.cli.get_list_database()) |
| 149 |
|
| 150 |
def test_query_fail(self): |
| 151 |
"""Test that a query failed.""" |
| 152 |
with self.assertRaises(InfluxDBClientError) as ctx: |
| 153 |
self.cli.query('select column_one from foo') |
| 154 |
self.assertIn('database not found: db', |
| 155 |
ctx.exception.content) |
| 156 |
|
| 157 |
def test_query_fail_ignore_errors(self): |
| 158 |
"""Test query failed but ignore errors.""" |
| 159 |
result = self.cli.query('select column_one from foo', |
| 160 |
raise_errors=False) |
| 161 |
self.assertEqual(result.error, 'database not found: db') |
| 162 |
|
| 163 |
def test_create_user(self): |
| 164 |
"""Test create user.""" |
| 165 |
self.cli.create_user('test_user', 'secret_password') |
| 166 |
rsp = list(self.cli.query("SHOW USERS")['results']) |
| 167 |
self.assertIn({'user': 'test_user', 'admin': False}, |
| 168 |
rsp) |
| 169 |
|
| 170 |
def test_create_user_admin(self): |
| 171 |
"""Test create admin user.""" |
| 172 |
self.cli.create_user('test_user', 'secret_password', True) |
| 173 |
rsp = list(self.cli.query("SHOW USERS")['results']) |
| 174 |
self.assertIn({'user': 'test_user', 'admin': True}, |
| 175 |
rsp) |
| 176 |
|
| 177 |
def test_create_user_blank_password(self): |
| 178 |
"""Test create user with a blank pass.""" |
| 179 |
self.cli.create_user('test_user', '') |
| 180 |
rsp = list(self.cli.query("SHOW USERS")['results']) |
| 181 |
self.assertIn({'user': 'test_user', 'admin': False}, |
| 182 |
rsp) |
| 183 |
|
| 184 |
def test_get_list_users_empty(self): |
| 185 |
"""Test get list of users, but empty.""" |
| 186 |
rsp = self.cli.get_list_users() |
| 187 |
self.assertEqual([], rsp) |
| 188 |
|
| 189 |
def test_get_list_users(self): |
| 190 |
"""Test get list of users.""" |
| 191 |
self.cli.query("CREATE USER test WITH PASSWORD 'test'") |
| 192 |
rsp = self.cli.get_list_users() |
| 193 |
|
| 194 |
self.assertEqual( |
| 195 |
[{'user': 'test', 'admin': False}], |
| 196 |
rsp |
| 197 |
) |
| 198 |
|
| 199 |
def test_create_user_blank_username(self): |
| 200 |
"""Test create blank username.""" |
| 201 |
with self.assertRaises(InfluxDBClientError) as ctx: |
| 202 |
self.cli.create_user('', 'secret_password') |
| 203 |
self.assertIn('username required', |
| 204 |
ctx.exception.content) |
| 205 |
rsp = list(self.cli.query("SHOW USERS")['results']) |
| 206 |
self.assertEqual(rsp, []) |
| 207 |
|
| 208 |
def test_drop_user(self): |
| 209 |
"""Test drop a user.""" |
| 210 |
self.cli.query("CREATE USER test WITH PASSWORD 'test'") |
| 211 |
self.cli.drop_user('test') |
| 212 |
users = list(self.cli.query("SHOW USERS")['results']) |
| 213 |
self.assertEqual(users, []) |
| 214 |
|
| 215 |
def test_drop_user_nonexisting(self): |
| 216 |
"""Test dropping a nonexistent user.""" |
| 217 |
with self.assertRaises(InfluxDBClientError) as ctx: |
| 218 |
self.cli.drop_user('test') |
| 219 |
self.assertIn('user not found', |
| 220 |
ctx.exception.content) |
| 221 |
|
| 222 |
@unittest.skip("Broken as of 0.9.0") |
| 223 |
def test_revoke_admin_privileges(self): |
| 224 |
"""Test revoking admin privs, deprecated as of v0.9.0.""" |
| 225 |
self.cli.create_user('test', 'test', admin=True) |
| 226 |
self.assertEqual([{'user': 'test', 'admin': True}], |
| 227 |
self.cli.get_list_users()) |
| 228 |
self.cli.revoke_admin_privileges('test') |
| 229 |
self.assertEqual([{'user': 'test', 'admin': False}], |
| 230 |
self.cli.get_list_users()) |
| 231 |
|
| 232 |
def test_grant_privilege(self): |
| 233 |
"""Test grant privs to user.""" |
| 234 |
self.cli.create_user('test', 'test') |
| 235 |
self.cli.create_database('testdb') |
| 236 |
self.cli.grant_privilege('all', 'testdb', 'test') |
| 237 |
# TODO: when supported by InfluxDB, check if privileges are granted |
| 238 |
|
| 239 |
def test_grant_privilege_invalid(self): |
| 240 |
"""Test grant invalid privs to user.""" |
| 241 |
self.cli.create_user('test', 'test') |
| 242 |
self.cli.create_database('testdb') |
| 243 |
with self.assertRaises(InfluxDBClientError) as ctx: |
| 244 |
self.cli.grant_privilege('', 'testdb', 'test') |
| 245 |
self.assertEqual(400, ctx.exception.code) |
| 246 |
self.assertIn('{"error":"error parsing query: ', |
| 247 |
ctx.exception.content) |
| 248 |
|
| 249 |
def test_revoke_privilege(self): |
| 250 |
"""Test revoke privs from user.""" |
| 251 |
self.cli.create_user('test', 'test') |
| 252 |
self.cli.create_database('testdb') |
| 253 |
self.cli.revoke_privilege('all', 'testdb', 'test') |
| 254 |
# TODO: when supported by InfluxDB, check if privileges are revoked |
| 255 |
|
| 256 |
def test_revoke_privilege_invalid(self): |
| 257 |
"""Test revoke invalid privs from user.""" |
| 258 |
self.cli.create_user('test', 'test') |
| 259 |
self.cli.create_database('testdb') |
| 260 |
with self.assertRaises(InfluxDBClientError) as ctx: |
| 261 |
self.cli.revoke_privilege('', 'testdb', 'test') |
| 262 |
self.assertEqual(400, ctx.exception.code) |
| 263 |
self.assertIn('{"error":"error parsing query: ', |
| 264 |
ctx.exception.content) |
| 265 |
|
| 266 |
def test_invalid_port_fails(self): |
| 267 |
"""Test invalid port access fails.""" |
| 268 |
with self.assertRaises(ValueError): |
| 269 |
InfluxDBClient('host', '80/redir', 'username', 'password') |
| 270 |
|
| 271 |
|
| 272 |
@skip_server_tests |
| 273 |
class CommonTests(ManyTestCasesWithServerMixin, unittest.TestCase): |
| 274 |
"""Define a class to handle common tests for the server.""" |
| 275 |
|
| 276 |
influxdb_template_conf = os.path.join(THIS_DIR, 'influxdb.conf.template') |
| 277 |
|
| 278 |
def test_write(self): |
| 279 |
"""Test write to the server.""" |
| 280 |
self.assertIs(True, self.cli.write( |
| 281 |
{'points': dummy_point}, |
| 282 |
params={'db': 'db'}, |
| 283 |
)) |
| 284 |
|
| 285 |
def test_write_check_read(self): |
| 286 |
"""Test write and check read of data to server.""" |
| 287 |
self.test_write() |
| 288 |
time.sleep(1) |
| 289 |
rsp = self.cli.query('SELECT * FROM cpu_load_short', database='db') |
| 290 |
self.assertListEqual([{'value': 0.64, 'time': '2009-11-10T23:00:00Z', |
| 291 |
"host": "server01", "region": "us-west"}], |
| 292 |
list(rsp.get_points())) |
| 293 |
|
| 294 |
def test_write_points(self): |
| 295 |
"""Test writing points to the server.""" |
| 296 |
self.assertIs(True, self.cli.write_points(dummy_point)) |
| 297 |
|
| 298 |
@skip_if_pypy |
| 299 |
def test_write_points_DF(self): |
| 300 |
"""Test writing points with dataframe.""" |
| 301 |
self.assertIs( |
| 302 |
True, |
| 303 |
self.cliDF.write_points( |
| 304 |
dummy_point_df['dataframe'], |
| 305 |
dummy_point_df['measurement'], |
| 306 |
dummy_point_df['tags'] |
| 307 |
) |
| 308 |
) |
| 309 |
|
| 310 |
def test_write_points_check_read(self): |
| 311 |
"""Test writing points and check read back.""" |
| 312 |
self.test_write_points() |
| 313 |
time.sleep(1) # same as test_write_check_read() |
| 314 |
rsp = self.cli.query('SELECT * FROM cpu_load_short') |
| 315 |
|
| 316 |
self.assertEqual( |
| 317 |
list(rsp), |
| 318 |
[[ |
| 319 |
{'value': 0.64, |
| 320 |
'time': '2009-11-10T23:00:00Z', |
| 321 |
"host": "server01", |
| 322 |
"region": "us-west"} |
| 323 |
]] |
| 324 |
) |
| 325 |
|
| 326 |
rsp2 = list(rsp.get_points()) |
| 327 |
self.assertEqual(len(rsp2), 1) |
| 328 |
pt = rsp2[0] |
| 329 |
|
| 330 |
self.assertEqual( |
| 331 |
pt, |
| 332 |
{'time': '2009-11-10T23:00:00Z', |
| 333 |
'value': 0.64, |
| 334 |
"host": "server01", |
| 335 |
"region": "us-west"} |
| 336 |
) |
| 337 |
|
| 338 |
@unittest.skip("Broken as of 0.9.0") |
| 339 |
def test_write_points_check_read_DF(self): |
| 340 |
"""Test write points and check back with dataframe.""" |
| 341 |
self.test_write_points_DF() |
| 342 |
time.sleep(1) # same as test_write_check_read() |
| 343 |
|
| 344 |
rsp = self.cliDF.query('SELECT * FROM cpu_load_short') |
| 345 |
assert_frame_equal( |
| 346 |
rsp['cpu_load_short'], |
| 347 |
dummy_point_df['dataframe'] |
| 348 |
) |
| 349 |
|
| 350 |
# Query with Tags |
| 351 |
rsp = self.cliDF.query( |
| 352 |
"SELECT * FROM cpu_load_short GROUP BY *") |
| 353 |
assert_frame_equal( |
| 354 |
rsp[('cpu_load_short', |
| 355 |
(('host', 'server01'), ('region', 'us-west')))], |
| 356 |
dummy_point_df['dataframe'] |
| 357 |
) |
| 358 |
|
| 359 |
def test_write_multiple_points_different_series(self): |
| 360 |
"""Test write multiple points to different series.""" |
| 361 |
self.assertIs(True, self.cli.write_points(dummy_points)) |
| 362 |
time.sleep(1) |
| 363 |
rsp = self.cli.query('SELECT * FROM cpu_load_short') |
| 364 |
lrsp = list(rsp) |
| 365 |
|
| 366 |
self.assertEqual( |
| 367 |
[[ |
| 368 |
{'value': 0.64, |
| 369 |
'time': '2009-11-10T23:00:00Z', |
| 370 |
"host": "server01", |
| 371 |
"region": "us-west"} |
| 372 |
]], |
| 373 |
lrsp |
| 374 |
) |
| 375 |
|
| 376 |
rsp = list(self.cli.query('SELECT * FROM memory')) |
| 377 |
|
| 378 |
self.assertEqual( |
| 379 |
rsp, |
| 380 |
[[ |
| 381 |
{'value': 33, |
| 382 |
'time': '2009-11-10T23:01:35Z', |
| 383 |
"host": "server01", |
| 384 |
"region": "us-west"} |
| 385 |
]] |
| 386 |
) |
| 387 |
|
| 388 |
def test_select_into_as_post(self): |
| 389 |
"""Test SELECT INTO is POSTed.""" |
| 390 |
self.assertIs(True, self.cli.write_points(dummy_points)) |
| 391 |
time.sleep(1) |
| 392 |
rsp = self.cli.query('SELECT * INTO "newmeas" FROM "memory"') |
| 393 |
rsp = self.cli.query('SELECT * FROM "newmeas"') |
| 394 |
lrsp = list(rsp) |
| 395 |
|
| 396 |
self.assertEqual( |
| 397 |
lrsp, |
| 398 |
[[ |
| 399 |
{'value': 33, |
| 400 |
'time': '2009-11-10T23:01:35Z', |
| 401 |
"host": "server01", |
| 402 |
"region": "us-west"} |
| 403 |
]] |
| 404 |
) |
| 405 |
|
| 406 |
@unittest.skip("Broken as of 0.9.0") |
| 407 |
def test_write_multiple_points_different_series_DF(self): |
| 408 |
"""Test write multiple points using dataframe to different series.""" |
| 409 |
for i in range(2): |
| 410 |
self.assertIs( |
| 411 |
True, self.cliDF.write_points( |
| 412 |
dummy_points_df[i]['dataframe'], |
| 413 |
dummy_points_df[i]['measurement'], |
| 414 |
dummy_points_df[i]['tags'])) |
| 415 |
time.sleep(1) |
| 416 |
rsp = self.cliDF.query('SELECT * FROM cpu_load_short') |
| 417 |
|
| 418 |
assert_frame_equal( |
| 419 |
rsp['cpu_load_short'], |
| 420 |
dummy_points_df[0]['dataframe'] |
| 421 |
) |
| 422 |
|
| 423 |
rsp = self.cliDF.query('SELECT * FROM memory') |
| 424 |
assert_frame_equal( |
| 425 |
rsp['memory'], |
| 426 |
dummy_points_df[1]['dataframe'] |
| 427 |
) |
| 428 |
|
| 429 |
def test_write_points_batch(self): |
| 430 |
"""Test writing points in a batch.""" |
| 431 |
dummy_points = [ |
| 432 |
{"measurement": "cpu_usage", "tags": {"unit": "percent"}, |
| 433 |
"time": "2009-11-10T23:00:00Z", "fields": {"value": 12.34}}, |
| 434 |
{"measurement": "network", "tags": {"direction": "in"}, |
| 435 |
"time": "2009-11-10T23:00:00Z", "fields": {"value": 123.00}}, |
| 436 |
{"measurement": "network", "tags": {"direction": "out"}, |
| 437 |
"time": "2009-11-10T23:00:00Z", "fields": {"value": 12.00}} |
| 438 |
] |
| 439 |
self.cli.write_points(points=dummy_points, |
| 440 |
tags={"host": "server01", |
| 441 |
"region": "us-west"}, |
| 442 |
batch_size=2) |
| 443 |
time.sleep(5) |
| 444 |
net_in = self.cli.query("SELECT value FROM network " |
| 445 |
"WHERE direction=$dir", |
| 446 |
bind_params={'dir': 'in'} |
| 447 |
).raw |
| 448 |
net_out = self.cli.query("SELECT value FROM network " |
| 449 |
"WHERE direction='out'").raw |
| 450 |
cpu = self.cli.query("SELECT value FROM cpu_usage").raw |
| 451 |
self.assertIn(123, net_in['series'][0]['values'][0]) |
| 452 |
self.assertIn(12, net_out['series'][0]['values'][0]) |
| 453 |
self.assertIn(12.34, cpu['series'][0]['values'][0]) |
| 454 |
|
| 455 |
def test_write_points_batch_generator(self): |
| 456 |
"""Test writing points in a batch from a generator.""" |
| 457 |
dummy_points = [ |
| 458 |
{"measurement": "cpu_usage", "tags": {"unit": "percent"}, |
| 459 |
"time": "2009-11-10T23:00:00Z", "fields": {"value": 12.34}}, |
| 460 |
{"measurement": "network", "tags": {"direction": "in"}, |
| 461 |
"time": "2009-11-10T23:00:00Z", "fields": {"value": 123.00}}, |
| 462 |
{"measurement": "network", "tags": {"direction": "out"}, |
| 463 |
"time": "2009-11-10T23:00:00Z", "fields": {"value": 12.00}} |
| 464 |
] |
| 465 |
dummy_points_generator = (point for point in dummy_points) |
| 466 |
self.cli.write_points(points=dummy_points_generator, |
| 467 |
tags={"host": "server01", |
| 468 |
"region": "us-west"}, |
| 469 |
batch_size=2) |
| 470 |
time.sleep(5) |
| 471 |
net_in = self.cli.query("SELECT value FROM network " |
| 472 |
"WHERE direction=$dir", |
| 473 |
bind_params={'dir': 'in'} |
| 474 |
).raw |
| 475 |
net_out = self.cli.query("SELECT value FROM network " |
| 476 |
"WHERE direction='out'").raw |
| 477 |
cpu = self.cli.query("SELECT value FROM cpu_usage").raw |
| 478 |
self.assertIn(123, net_in['series'][0]['values'][0]) |
| 479 |
self.assertIn(12, net_out['series'][0]['values'][0]) |
| 480 |
self.assertIn(12.34, cpu['series'][0]['values'][0]) |
| 481 |
|
| 482 |
def test_query(self): |
| 483 |
"""Test querying data back from server.""" |
| 484 |
self.assertIs(True, self.cli.write_points(dummy_point)) |
| 485 |
|
| 486 |
@unittest.skip('Not implemented for 0.9') |
| 487 |
def test_query_chunked(self): |
| 488 |
"""Test query for chunked response from server.""" |
| 489 |
cli = InfluxDBClient(database='db') |
| 490 |
example_object = { |
| 491 |
'points': [ |
| 492 |
[1415206250119, 40001, 667], |
| 493 |
[1415206244555, 30001, 7], |
| 494 |
[1415206228241, 20001, 788], |
| 495 |
[1415206212980, 10001, 555], |
| 496 |
[1415197271586, 10001, 23] |
| 497 |
], |
| 498 |
'name': 'foo', |
| 499 |
'columns': [ |
| 500 |
'time', |
| 501 |
'sequence_number', |
| 502 |
'val' |
| 503 |
] |
| 504 |
} |
| 505 |
del cli |
| 506 |
del example_object |
| 507 |
# TODO ? |
| 508 |
|
| 509 |
def test_delete_series_invalid(self): |
| 510 |
"""Test delete invalid series.""" |
| 511 |
with self.assertRaises(InfluxDBClientError): |
| 512 |
self.cli.delete_series() |
| 513 |
|
| 514 |
def test_default_retention_policy(self): |
| 515 |
"""Test add default retention policy.""" |
| 516 |
rsp = self.cli.get_list_retention_policies() |
| 517 |
self.assertEqual( |
| 518 |
[ |
| 519 |
{'name': 'autogen', |
| 520 |
'duration': '0s', |
| 521 |
'replicaN': 1, |
| 522 |
'shardGroupDuration': u'168h0m0s', |
| 523 |
'default': True} |
| 524 |
], |
| 525 |
rsp |
| 526 |
) |
| 527 |
|
| 528 |
def test_create_retention_policy_default(self): |
| 529 |
"""Test create a new default retention policy.""" |
| 530 |
self.cli.create_retention_policy('somename', '1d', 1, default=True) |
| 531 |
self.cli.create_retention_policy('another', '2d', 1, default=False) |
| 532 |
rsp = self.cli.get_list_retention_policies() |
| 533 |
|
| 534 |
self.assertEqual( |
| 535 |
[ |
| 536 |
{'duration': '0s', |
| 537 |
'default': False, |
| 538 |
'replicaN': 1, |
| 539 |
'shardGroupDuration': u'168h0m0s', |
| 540 |
'name': 'autogen'}, |
| 541 |
{'duration': '24h0m0s', |
| 542 |
'default': True, |
| 543 |
'replicaN': 1, |
| 544 |
'shardGroupDuration': u'1h0m0s', |
| 545 |
'name': 'somename'}, |
| 546 |
{'duration': '48h0m0s', |
| 547 |
'default': False, |
| 548 |
'replicaN': 1, |
| 549 |
'shardGroupDuration': u'24h0m0s', |
| 550 |
'name': 'another'} |
| 551 |
], |
| 552 |
rsp |
| 553 |
) |
| 554 |
|
| 555 |
def test_create_retention_policy(self): |
| 556 |
"""Test creating a new retention policy, not default.""" |
| 557 |
self.cli.create_retention_policy('somename', '1d', 1) |
| 558 |
# NB: creating a retention policy without specifying |
| 559 |
# shard group duration |
| 560 |
# leads to a shard group duration of 1 hour |
| 561 |
rsp = self.cli.get_list_retention_policies() |
| 562 |
self.assertEqual( |
| 563 |
[ |
| 564 |
{'duration': '0s', |
| 565 |
'default': True, |
| 566 |
'replicaN': 1, |
| 567 |
'shardGroupDuration': u'168h0m0s', |
| 568 |
'name': 'autogen'}, |
| 569 |
{'duration': '24h0m0s', |
| 570 |
'default': False, |
| 571 |
'replicaN': 1, |
| 572 |
'shardGroupDuration': u'1h0m0s', |
| 573 |
'name': 'somename'} |
| 574 |
], |
| 575 |
rsp |
| 576 |
) |
| 577 |
|
| 578 |
self.cli.drop_retention_policy('somename', 'db') |
| 579 |
# recreate the RP |
| 580 |
self.cli.create_retention_policy('somename', '1w', 1, |
| 581 |
shard_duration='1h') |
| 582 |
|
| 583 |
rsp = self.cli.get_list_retention_policies() |
| 584 |
self.assertEqual( |
| 585 |
[ |
| 586 |
{'duration': '0s', |
| 587 |
'default': True, |
| 588 |
'replicaN': 1, |
| 589 |
'shardGroupDuration': u'168h0m0s', |
| 590 |
'name': 'autogen'}, |
| 591 |
{'duration': '168h0m0s', |
| 592 |
'default': False, |
| 593 |
'replicaN': 1, |
| 594 |
'shardGroupDuration': u'1h0m0s', |
| 595 |
'name': 'somename'} |
| 596 |
], |
| 597 |
rsp |
| 598 |
) |
| 599 |
|
| 600 |
self.cli.drop_retention_policy('somename', 'db') |
| 601 |
# recreate the RP |
| 602 |
self.cli.create_retention_policy('somename', '1w', 1) |
| 603 |
|
| 604 |
rsp = self.cli.get_list_retention_policies() |
| 605 |
self.assertEqual( |
| 606 |
[ |
| 607 |
{'duration': '0s', |
| 608 |
'default': True, |
| 609 |
'replicaN': 1, |
| 610 |
'shardGroupDuration': u'168h0m0s', |
| 611 |
'name': 'autogen'}, |
| 612 |
{'duration': '168h0m0s', |
| 613 |
'default': False, |
| 614 |
'replicaN': 1, |
| 615 |
'shardGroupDuration': u'24h0m0s', |
| 616 |
'name': 'somename'} |
| 617 |
], |
| 618 |
rsp |
| 619 |
) |
| 620 |
|
| 621 |
def test_alter_retention_policy(self): |
| 622 |
"""Test alter a retention policy, not default.""" |
| 623 |
self.cli.create_retention_policy('somename', '1d', 1) |
| 624 |
|
| 625 |
# Test alter duration |
| 626 |
self.cli.alter_retention_policy('somename', 'db', |
| 627 |
duration='4d', |
| 628 |
shard_duration='2h') |
| 629 |
# NB: altering retention policy doesn't change shard group duration |
| 630 |
rsp = self.cli.get_list_retention_policies() |
| 631 |
self.assertEqual( |
| 632 |
[ |
| 633 |
{'duration': '0s', |
| 634 |
'default': True, |
| 635 |
'replicaN': 1, |
| 636 |
'shardGroupDuration': u'168h0m0s', |
| 637 |
'name': 'autogen'}, |
| 638 |
{'duration': '96h0m0s', |
| 639 |
'default': False, |
| 640 |
'replicaN': 1, |
| 641 |
'shardGroupDuration': u'2h0m0s', |
| 642 |
'name': 'somename'} |
| 643 |
], |
| 644 |
rsp |
| 645 |
) |
| 646 |
|
| 647 |
# Test alter replication |
| 648 |
self.cli.alter_retention_policy('somename', 'db', |
| 649 |
replication=4) |
| 650 |
|
| 651 |
# NB: altering retention policy doesn't change shard group duration |
| 652 |
rsp = self.cli.get_list_retention_policies() |
| 653 |
self.assertEqual( |
| 654 |
[ |
| 655 |
{'duration': '0s', |
| 656 |
'default': True, |
| 657 |
'replicaN': 1, |
| 658 |
'shardGroupDuration': u'168h0m0s', |
| 659 |
'name': 'autogen'}, |
| 660 |
{'duration': '96h0m0s', |
| 661 |
'default': False, |
| 662 |
'replicaN': 4, |
| 663 |
'shardGroupDuration': u'2h0m0s', |
| 664 |
'name': 'somename'} |
| 665 |
], |
| 666 |
rsp |
| 667 |
) |
| 668 |
|
| 669 |
# Test alter default |
| 670 |
self.cli.alter_retention_policy('somename', 'db', |
| 671 |
default=True) |
| 672 |
# NB: altering retention policy doesn't change shard group duration |
| 673 |
rsp = self.cli.get_list_retention_policies() |
| 674 |
self.assertEqual( |
| 675 |
[ |
| 676 |
{'duration': '0s', |
| 677 |
'default': False, |
| 678 |
'replicaN': 1, |
| 679 |
'shardGroupDuration': u'168h0m0s', |
| 680 |
'name': 'autogen'}, |
| 681 |
{'duration': '96h0m0s', |
| 682 |
'default': True, |
| 683 |
'replicaN': 4, |
| 684 |
'shardGroupDuration': u'2h0m0s', |
| 685 |
'name': 'somename'} |
| 686 |
], |
| 687 |
rsp |
| 688 |
) |
| 689 |
|
| 690 |
# Test alter shard_duration |
| 691 |
self.cli.alter_retention_policy('somename', 'db', |
| 692 |
shard_duration='4h') |
| 693 |
|
| 694 |
rsp = self.cli.get_list_retention_policies() |
| 695 |
self.assertEqual( |
| 696 |
[ |
| 697 |
{'duration': '0s', |
| 698 |
'default': False, |
| 699 |
'replicaN': 1, |
| 700 |
'shardGroupDuration': u'168h0m0s', |
| 701 |
'name': 'autogen'}, |
| 702 |
{'duration': '96h0m0s', |
| 703 |
'default': True, |
| 704 |
'replicaN': 4, |
| 705 |
'shardGroupDuration': u'4h0m0s', |
| 706 |
'name': 'somename'} |
| 707 |
], |
| 708 |
rsp |
| 709 |
) |
| 710 |
|
| 711 |
def test_alter_retention_policy_invalid(self): |
| 712 |
"""Test invalid alter retention policy.""" |
| 713 |
self.cli.create_retention_policy('somename', '1d', 1) |
| 714 |
with self.assertRaises(InfluxDBClientError) as ctx: |
| 715 |
self.cli.alter_retention_policy('somename', 'db') |
| 716 |
self.assertEqual(400, ctx.exception.code) |
| 717 |
self.assertIn('{"error":"error parsing query: ', |
| 718 |
ctx.exception.content) |
| 719 |
rsp = self.cli.get_list_retention_policies() |
| 720 |
self.assertEqual( |
| 721 |
[ |
| 722 |
{'duration': '0s', |
| 723 |
'default': True, |
| 724 |
'replicaN': 1, |
| 725 |
'shardGroupDuration': u'168h0m0s', |
| 726 |
'name': 'autogen'}, |
| 727 |
{'duration': '24h0m0s', |
| 728 |
'default': False, |
| 729 |
'replicaN': 1, |
| 730 |
'shardGroupDuration': u'1h0m0s', |
| 731 |
'name': 'somename'} |
| 732 |
], |
| 733 |
rsp |
| 734 |
) |
| 735 |
|
| 736 |
def test_drop_retention_policy(self): |
| 737 |
"""Test drop a retention policy.""" |
| 738 |
self.cli.create_retention_policy('somename', '1d', 1) |
| 739 |
|
| 740 |
# Test drop retention |
| 741 |
self.cli.drop_retention_policy('somename', 'db') |
| 742 |
rsp = self.cli.get_list_retention_policies() |
| 743 |
self.assertEqual( |
| 744 |
[ |
| 745 |
{'duration': '0s', |
| 746 |
'default': True, |
| 747 |
'replicaN': 1, |
| 748 |
'shardGroupDuration': u'168h0m0s', |
| 749 |
'name': 'autogen'} |
| 750 |
], |
| 751 |
rsp |
| 752 |
) |
| 753 |
|
| 754 |
def test_create_continuous_query(self): |
| 755 |
"""Test continuous query creation.""" |
| 756 |
self.cli.create_retention_policy('some_rp', '1d', 1) |
| 757 |
query = 'select count("value") into "some_rp"."events" from ' \ |
| 758 |
'"events" group by time(10m)' |
| 759 |
self.cli.create_continuous_query('test_cq', query, 'db') |
| 760 |
cqs = self.cli.get_list_continuous_queries() |
| 761 |
expected_cqs = [ |
| 762 |
{ |
| 763 |
'db': [ |
| 764 |
{ |
| 765 |
'name': 'test_cq', |
| 766 |
'query': 'CREATE CONTINUOUS QUERY test_cq ON db ' |
| 767 |
'BEGIN SELECT count(value) INTO ' |
| 768 |
'db.some_rp.events FROM db.autogen.events ' |
| 769 |
'GROUP BY time(10m) END' |
| 770 |
} |
| 771 |
] |
| 772 |
} |
| 773 |
] |
| 774 |
self.assertEqual(cqs, expected_cqs) |
| 775 |
|
| 776 |
def test_drop_continuous_query(self): |
| 777 |
"""Test continuous query drop.""" |
| 778 |
self.test_create_continuous_query() |
| 779 |
self.cli.drop_continuous_query('test_cq', 'db') |
| 780 |
cqs = self.cli.get_list_continuous_queries() |
| 781 |
expected_cqs = [{'db': []}] |
| 782 |
self.assertEqual(cqs, expected_cqs) |
| 783 |
|
| 784 |
def test_issue_143(self): |
| 785 |
"""Test for PR#143 from repo.""" |
| 786 |
pt = partial(point, 'a_series_name', timestamp='2015-03-30T16:16:37Z') |
| 787 |
pts = [ |
| 788 |
pt(value=15), |
| 789 |
pt(tags={'tag_1': 'value1'}, value=5), |
| 790 |
pt(tags={'tag_1': 'value2'}, value=10), |
| 791 |
] |
| 792 |
self.cli.write_points(pts) |
| 793 |
time.sleep(1) |
| 794 |
rsp = list(self.cli.query('SELECT * FROM a_series_name \ |
| 795 |
GROUP BY tag_1').get_points()) |
| 796 |
|
| 797 |
self.assertEqual( |
| 798 |
[ |
| 799 |
{'time': '2015-03-30T16:16:37Z', 'value': 15}, |
| 800 |
{'time': '2015-03-30T16:16:37Z', 'value': 5}, |
| 801 |
{'time': '2015-03-30T16:16:37Z', 'value': 10} |
| 802 |
], |
| 803 |
rsp |
| 804 |
) |
| 805 |
|
| 806 |
# a slightly more complex one with 2 tags values: |
| 807 |
pt = partial(point, 'series2', timestamp='2015-03-30T16:16:37Z') |
| 808 |
pts = [ |
| 809 |
pt(tags={'tag1': 'value1', 'tag2': 'v1'}, value=0), |
| 810 |
pt(tags={'tag1': 'value1', 'tag2': 'v2'}, value=5), |
| 811 |
pt(tags={'tag1': 'value2', 'tag2': 'v1'}, value=10), |
| 812 |
] |
| 813 |
self.cli.write_points(pts) |
| 814 |
time.sleep(1) |
| 815 |
rsp = self.cli.query('SELECT * FROM series2 GROUP BY tag1,tag2') |
| 816 |
|
| 817 |
self.assertEqual( |
| 818 |
[ |
| 819 |
{'value': 0, 'time': '2015-03-30T16:16:37Z'}, |
| 820 |
{'value': 5, 'time': '2015-03-30T16:16:37Z'}, |
| 821 |
{'value': 10, 'time': '2015-03-30T16:16:37Z'} |
| 822 |
], |
| 823 |
list(rsp['series2']) |
| 824 |
) |
| 825 |
|
| 826 |
all_tag2_equal_v1 = list(rsp.get_points(tags={'tag2': 'v1'})) |
| 827 |
|
| 828 |
self.assertEqual( |
| 829 |
[{'value': 0, 'time': '2015-03-30T16:16:37Z'}, |
| 830 |
{'value': 10, 'time': '2015-03-30T16:16:37Z'}], |
| 831 |
all_tag2_equal_v1, |
| 832 |
) |
| 833 |
|
| 834 |
def test_query_multiple_series(self): |
| 835 |
"""Test query for multiple series.""" |
| 836 |
pt = partial(point, 'series1', timestamp='2015-03-30T16:16:37Z') |
| 837 |
pts = [ |
| 838 |
pt(tags={'tag1': 'value1', 'tag2': 'v1'}, value=0), |
| 839 |
] |
| 840 |
self.cli.write_points(pts) |
| 841 |
|
| 842 |
pt = partial(point, 'series2', timestamp='1970-03-30T16:16:37Z') |
| 843 |
pts = [ |
| 844 |
pt(tags={'tag1': 'value1', 'tag2': 'v1'}, |
| 845 |
value=0, data1=33, data2="bla"), |
| 846 |
] |
| 847 |
self.cli.write_points(pts) |
| 848 |
|
| 849 |
def test_get_list_series(self): |
| 850 |
"""Test get a list of series from the database.""" |
| 851 |
dummy_points = [ |
| 852 |
{ |
| 853 |
"measurement": "cpu_load_short", |
| 854 |
"tags": { |
| 855 |
"host": "server01", |
| 856 |
"region": "us-west" |
| 857 |
}, |
| 858 |
"time": "2009-11-10T23:00:00.123456Z", |
| 859 |
"fields": { |
| 860 |
"value": 0.64 |
| 861 |
} |
| 862 |
} |
| 863 |
] |
| 864 |
|
| 865 |
dummy_points_2 = [ |
| 866 |
{ |
| 867 |
"measurement": "memory_usage", |
| 868 |
"tags": { |
| 869 |
"host": "server02", |
| 870 |
"region": "us-east" |
| 871 |
}, |
| 872 |
"time": "2009-11-10T23:00:00.123456Z", |
| 873 |
"fields": { |
| 874 |
"value": 80 |
| 875 |
} |
| 876 |
} |
| 877 |
] |
| 878 |
|
| 879 |
self.cli.write_points(dummy_points) |
| 880 |
self.cli.write_points(dummy_points_2) |
| 881 |
|
| •882 |
self.assertEqual( |
| 883 |
self.cli.get_list_series(), |
| 884 |
['cpu_load_short,host=server01,region=us-west', |
| 885 |
'memory_usage,host=server02,region=us-east'] |
| 886 |
) |
| 887 |
|
| •888 |
self.assertEqual( |
| 889 |
self.cli.get_list_series(measurement='memory_usage'), |
| 890 |
['memory_usage,host=server02,region=us-east'] |
| 891 |
) |
| 892 |
|
| •893 |
self.assertEqual( |
| 894 |
self.cli.get_list_series(measurement='memory_usage'), |
| 895 |
['memory_usage,host=server02,region=us-east'] |
| 896 |
) |
| 897 |
|
| •898 |
self.assertEqual( |
| 899 |
self.cli.get_list_series(tags={'host': 'server02'}), |
| 900 |
['memory_usage,host=server02,region=us-east']) |
| 901 |
|
| •902 |
self.assertEqual( |
| 903 |
self.cli.get_list_series( |
| 904 |
measurement='cpu_load_short', tags={'host': 'server02'}), |
| 905 |
[]) |
| 906 |
|
| 907 |
|
| 908 |
@skip_server_tests |
| 909 |
class UdpTests(ManyTestCasesWithServerMixin, unittest.TestCase): |
| 910 |
"""Define a class to test UDP series.""" |
| 911 |
|
| 912 |
influxdb_udp_enabled = True |
| 913 |
influxdb_template_conf = os.path.join(THIS_DIR, |
| 914 |
'influxdb.conf.template') |
| 915 |
|
| 916 |
def test_write_points_udp(self): |
| 917 |
"""Test write points UDP.""" |
| 918 |
cli = InfluxDBClient( |
| 919 |
'localhost', |
| 920 |
self.influxd_inst.http_port, |
| 921 |
'root', |
| 922 |
'', |
| 923 |
database='db', |
| 924 |
use_udp=True, |
| 925 |
udp_port=self.influxd_inst.udp_port |
| 926 |
) |
| 927 |
cli.write_points(dummy_point) |
| 928 |
|
| 929 |
# The points are not immediately available after write_points. |
| 930 |
# This is to be expected because we are using udp (no response !). |
| 931 |
# So we have to wait some time, |
| 932 |
time.sleep(3) # 3 sec seems to be a good choice. |
| 933 |
rsp = self.cli.query('SELECT * FROM cpu_load_short') |
| 934 |
|
| 935 |
self.assertEqual( |
| 936 |
# this is dummy_points : |
| 937 |
[ |
| 938 |
{'value': 0.64, |
| 939 |
'time': '2009-11-10T23:00:00Z', |
| 940 |
"host": "server01", |
| 941 |
"region": "us-west"} |
| 942 |
], |
| 943 |
list(rsp['cpu_load_short']) |
| 944 |
) |
| 945 |
|
| 946 |
|
| 947 |
# Run the tests again, but with gzip enabled this time |
| 948 |
@skip_server_tests |
| 949 |
class GzipSimpleTests(SimpleTests, SingleTestCaseWithServerGzipMixin): |
| 950 |
"""Repeat the simple tests with InfluxDBClient where gzip=True.""" |
| 951 |
|
| 952 |
pass |
| 953 |
|
| 954 |
|
| 955 |
@skip_server_tests |
| 956 |
class GzipCommonTests(CommonTests, ManyTestCasesWithServerGzipMixin): |
| 957 |
"""Repeat the common tests with InfluxDBClient where gzip=True.""" |
| 958 |
|
| 959 |
pass |
| 960 |
|
| 961 |
|
| 962 |
@skip_server_tests |
| 963 |
class GzipUdpTests(UdpTests, ManyTestCasesWithServerGzipMixin): |
| 964 |
"""Repeat the UDP tests with InfluxDBClient where gzip=True.""" |
| 965 |
|
| 966 |
pass |