Files
opensearch-pyd/elasticsearch/helpers/test.py
T

65 lines
1.8 KiB
Python
Raw Normal View History

import time
import os
2019-05-10 09:16:33 -06:00
try:
# python 2.6
from unittest2 import TestCase, SkipTest
except ImportError:
from unittest import TestCase, SkipTest
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import ConnectionError
2019-05-10 09:16:33 -06:00
def get_test_client(nowait=False, **kwargs):
# construct kwargs from the environment
2019-05-10 09:16:33 -06:00
kw = {"timeout": 30}
if "TEST_ES_CONNECTION" in os.environ:
from elasticsearch import connection
2019-05-10 09:16:33 -06:00
kw["connection_class"] = getattr(connection, os.environ["TEST_ES_CONNECTION"])
kw.update(kwargs)
2019-05-10 09:16:33 -06:00
client = Elasticsearch([os.environ.get("TEST_ES_SERVER", {})], **kw)
# wait for yellow status
for _ in range(1 if nowait else 100):
try:
2019-05-10 09:16:33 -06:00
client.cluster.health(wait_for_status="yellow")
return client
except ConnectionError:
2019-05-10 09:16:33 -06:00
time.sleep(0.1)
else:
# timeout
raise SkipTest("Elasticsearch failed to start.")
2019-05-10 09:16:33 -06:00
def _get_version(version_string):
2019-05-10 09:16:33 -06:00
if "." not in version_string:
2015-04-22 21:50:19 +02:00
return ()
2019-05-10 09:16:33 -06:00
version = version_string.strip().split(".")
return tuple(int(v) if v.isdigit() else 999 for v in version)
2019-05-10 09:16:33 -06:00
class ElasticsearchTestCase(TestCase):
@staticmethod
def _get_client():
return get_test_client()
@classmethod
def setUpClass(cls):
super(ElasticsearchTestCase, cls).setUpClass()
cls.client = cls._get_client()
def tearDown(self):
super(ElasticsearchTestCase, self).tearDown()
2019-05-10 09:16:33 -06:00
self.client.indices.delete(index="*", ignore=404)
self.client.indices.delete_template(name="*", ignore=404)
@property
def es_version(self):
2019-05-10 09:16:33 -06:00
if not hasattr(self, "_es_version"):
version_string = self.client.info()["version"]["number"]
self._es_version = _get_version(version_string)
return self._es_version