| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Define the base module for server test.""" |
| 3 |
|
| 4 |
from __future__ import absolute_import |
| 5 |
from __future__ import division |
| 6 |
from __future__ import print_function |
| 7 |
from __future__ import unicode_literals |
| 8 |
|
| 9 |
import sys |
| 10 |
|
| 11 |
from influxdb.tests import using_pypy |
| 12 |
from influxdb.tests.server_tests.influxdb_instance import InfluxDbInstance |
| 13 |
|
| 14 |
from influxdb.client import InfluxDBClient |
| 15 |
|
| 16 |
if not using_pypy: |
| 17 |
from influxdb.dataframe_client import DataFrameClient |
| 18 |
|
| 19 |
|
| 20 |
def _setup_influxdb_server(inst): |
| 21 |
inst.influxd_inst = InfluxDbInstance( |
| 22 |
inst.influxdb_template_conf, |
| 23 |
udp_enabled=getattr(inst, 'influxdb_udp_enabled', False), |
| 24 |
) |
| 25 |
|
| 26 |
inst.cli = InfluxDBClient('localhost', |
| 27 |
inst.influxd_inst.http_port, |
| 28 |
'root', |
| 29 |
'', |
| 30 |
database='db') |
| 31 |
if not using_pypy: |
| 32 |
inst.cliDF = DataFrameClient('localhost', |
| 33 |
inst.influxd_inst.http_port, |
| 34 |
'root', |
| 35 |
'', |
| 36 |
database='db') |
| 37 |
|
| 38 |
|
| 39 |
def _setup_gzip_client(inst): |
| 40 |
inst.cli = InfluxDBClient('localhost', |
| 41 |
inst.influxd_inst.http_port, |
| 42 |
'root', |
| 43 |
'', |
| 44 |
database='db', |
| 45 |
gzip=True) |
| 46 |
|
| 47 |
|
| 48 |
def _teardown_influxdb_server(inst): |
| 49 |
remove_tree = sys.exc_info() == (None, None, None) |
| 50 |
inst.influxd_inst.close(remove_tree=remove_tree) |
| 51 |
|
| 52 |
|
| 53 |
class SingleTestCaseWithServerMixin(object): |
| 54 |
"""Define the single testcase with server mixin. |
| 55 |
|
| 56 |
A mixin for unittest.TestCase to start an influxdb server instance |
| 57 |
in a temporary directory **for each test function/case** |
| 58 |
""" |
| 59 |
|
| 60 |
# 'influxdb_template_conf' attribute must be set |
| 61 |
# on the TestCase class or instance. |
| 62 |
|
| 63 |
@classmethod |
| 64 |
def setUp(cls): |
| 65 |
"""Set up an instance of the SingleTestCaseWithServerMixin.""" |
| 66 |
_setup_influxdb_server(cls) |
| 67 |
|
| 68 |
@classmethod |
| 69 |
def tearDown(cls): |
| 70 |
"""Tear down an instance of the SingleTestCaseWithServerMixin.""" |
| 71 |
_teardown_influxdb_server(cls) |
| 72 |
|
| 73 |
|
| 74 |
class ManyTestCasesWithServerMixin(object): |
| 75 |
"""Define the many testcase with server mixin. |
| 76 |
|
| 77 |
Same as the SingleTestCaseWithServerMixin but this module creates |
| 78 |
a single instance for the whole class. Also pre-creates a fresh |
| 79 |
database: 'db'. |
| 80 |
""" |
| 81 |
|
| 82 |
# 'influxdb_template_conf' attribute must be set on the class itself ! |
| 83 |
|
| 84 |
@classmethod |
| 85 |
def setUpClass(cls): |
| 86 |
"""Set up an instance of the ManyTestCasesWithServerMixin.""" |
| 87 |
_setup_influxdb_server(cls) |
| 88 |
|
| 89 |
def setUp(self): |
| 90 |
"""Set up an instance of the ManyTestCasesWithServerMixin.""" |
| 91 |
self.cli.create_database('db') |
| 92 |
|
| 93 |
@classmethod |
| 94 |
def tearDownClass(cls): |
| 95 |
"""Deconstruct an instance of ManyTestCasesWithServerMixin.""" |
| 96 |
_teardown_influxdb_server(cls) |
| 97 |
|
| 98 |
def tearDown(self): |
| 99 |
"""Deconstruct an instance of ManyTestCasesWithServerMixin.""" |
| 100 |
self.cli.drop_database('db') |
| 101 |
|
| 102 |
|
| 103 |
class SingleTestCaseWithServerGzipMixin(object): |
| 104 |
"""Define the single testcase with server with gzip client mixin. |
| 105 |
|
| 106 |
Same as the SingleTestCaseWithServerGzipMixin but the InfluxDBClient has |
| 107 |
gzip=True |
| 108 |
""" |
| 109 |
|
| 110 |
@classmethod |
| 111 |
def setUp(cls): |
| 112 |
"""Set up an instance of the SingleTestCaseWithServerGzipMixin.""" |
| 113 |
_setup_influxdb_server(cls) |
| 114 |
_setup_gzip_client(cls) |
| 115 |
|
| 116 |
@classmethod |
| 117 |
def tearDown(cls): |
| 118 |
"""Tear down an instance of the SingleTestCaseWithServerMixin.""" |
| 119 |
_teardown_influxdb_server(cls) |
| 120 |
|
| 121 |
|
| 122 |
class ManyTestCasesWithServerGzipMixin(object): |
| 123 |
"""Define the many testcase with server with gzip client mixin. |
| 124 |
|
| 125 |
Same as the ManyTestCasesWithServerMixin but the InfluxDBClient has |
| 126 |
gzip=True. |
| 127 |
""" |
| 128 |
|
| 129 |
@classmethod |
| 130 |
def setUpClass(cls): |
| 131 |
"""Set up an instance of the ManyTestCasesWithServerGzipMixin.""" |
| 132 |
_setup_influxdb_server(cls) |
| 133 |
_setup_gzip_client(cls) |
| 134 |
|
| 135 |
@classmethod |
| 136 |
def tearDown(cls): |
| 137 |
"""Tear down an instance of the SingleTestCaseWithServerMixin.""" |
| 138 |
_teardown_influxdb_server(cls) |