Files
opensearch-pyd/test_elasticsearch/test_server/__init__.py
T

67 lines
1.8 KiB
Python
Raw Normal View History

2013-05-27 23:02:38 +02:00
import time
import os
from elasticsearch import Elasticsearch
2013-09-23 14:20:16 +02:00
from elasticsearch.exceptions import ConnectionError, NotFoundError
2013-08-28 19:11:28 +02:00
from ..test_cases import TestCase, SkipTest
2013-05-27 23:02:38 +02:00
2014-01-18 21:08:15 +01:00
client = None
2013-05-27 23:02:38 +02:00
2014-01-18 21:08:15 +01:00
def get_client():
global client
if client is not None:
return client
2013-05-27 23:02:38 +02:00
# construct kwargs from the environment
kw = {}
if 'TEST_ES_CONNECTION' in os.environ:
from elasticsearch import connection
kw['connection_class'] = getattr(connection, os.environ['TEST_ES_CONNECTION'])
# try and locate manual override in the local environment
try:
from test_elasticsearch.local import get_client as local_get_client
2014-01-18 21:08:15 +01:00
client = local_get_client([os.environ.get('TEST_ES_SERVER', {})], **kw)
except ImportError:
# fallback to using vanilla client
2014-01-18 21:08:15 +01:00
client = Elasticsearch([os.environ.get('TEST_ES_SERVER', {})], **kw)
# wait for yellow status
for _ in range(100):
2013-05-27 23:02:38 +02:00
time.sleep(.1)
try:
client.cluster.health(wait_for_status='yellow')
2014-01-18 21:08:15 +01:00
return client
except ConnectionError:
continue
2013-05-27 23:02:38 +02:00
else:
# timeout
raise SkipTest("Elasticsearch failed to start.")
2014-01-18 21:08:15 +01:00
def setup():
get_client()
2013-08-01 14:47:34 +02:00
ES_VERSION = None
def _get_version(version_string):
version = version_string.strip().split('.')
return tuple(int(v) if v.isdigit() else 999 for v in version)
2013-08-01 14:47:34 +02:00
class ElasticTestCase(TestCase):
def setUp(self):
self.client = get_client()
2013-08-01 14:47:34 +02:00
def tearDown(self):
2014-01-18 01:54:30 +01:00
self.client.indices.delete('*')
self.client.indices.delete_template('*', ignore=404)
2013-08-01 14:47:34 +02:00
@property
def es_version(self):
global ES_VERSION
if ES_VERSION is None:
version_string = self.client.info()['version']['number']
ES_VERSION = _get_version(version_string)
return ES_VERSION