base.py 4.1 KB

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