Provide helpers for running tests against elasticsearch
This commit is contained in:
@@ -0,0 +1,58 @@
|
|||||||
|
import time
|
||||||
|
import os
|
||||||
|
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
|
||||||
|
|
||||||
|
def get_test_client():
|
||||||
|
# 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'])
|
||||||
|
|
||||||
|
client = Elasticsearch([os.environ.get('TEST_ES_SERVER', {})], **kw)
|
||||||
|
|
||||||
|
# wait for yellow status
|
||||||
|
for _ in range(100):
|
||||||
|
time.sleep(.1)
|
||||||
|
try:
|
||||||
|
client.cluster.health(wait_for_status='yellow')
|
||||||
|
return client
|
||||||
|
except ConnectionError:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
# timeout
|
||||||
|
raise SkipTest("Elasticsearch failed to start.")
|
||||||
|
|
||||||
|
def _get_version(version_string):
|
||||||
|
version = version_string.strip().split('.')
|
||||||
|
return tuple(int(v) if v.isdigit() else 999 for v in version)
|
||||||
|
|
||||||
|
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()
|
||||||
|
self.client.indices.delete(index='*')
|
||||||
|
self.client.indices.delete_template(name='*', ignore=404)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def es_version(self):
|
||||||
|
if not hasattr(self, '_es_version'):
|
||||||
|
version_string = self.client.info()['version']['number']
|
||||||
|
self._es_version = _get_version(version_string)
|
||||||
|
return self._es_version
|
||||||
|
|
||||||
@@ -43,12 +43,10 @@ Customizing the tests
|
|||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
You can create a `local.py` file in the `test_elasticsearch` directory which
|
You can create a `local.py` file in the `test_elasticsearch` directory which
|
||||||
should contain a `get_client` function:
|
should contain a `get_client` function.
|
||||||
|
|
||||||
def get_client(hosts, ** kwargs):
|
If this file exists the function will be used instead of
|
||||||
...
|
`elasticsearch.helpers.test.get_test_client` to construct the client used for
|
||||||
|
any integration tests. You can use this to make sure your plugins and
|
||||||
If this file exists the function will be used instead of the built in one to
|
extensions work with `elasticsearch-py`.
|
||||||
construct the client used for any integration tests. You can use this to make
|
|
||||||
sure your plugins and extensions work with `elasticsearch-py`.
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,4 @@
|
|||||||
import time
|
from elasticsearch.helpers.test import get_test_client, ElasticsearchTestCase as BaseTestCase
|
||||||
import os
|
|
||||||
|
|
||||||
from elasticsearch import Elasticsearch
|
|
||||||
from elasticsearch.exceptions import ConnectionError, NotFoundError
|
|
||||||
|
|
||||||
from ..test_cases import TestCase, SkipTest
|
|
||||||
|
|
||||||
client = None
|
client = None
|
||||||
|
|
||||||
@@ -13,54 +7,21 @@ def get_client():
|
|||||||
if client is not None:
|
if client is not None:
|
||||||
return client
|
return client
|
||||||
|
|
||||||
# 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 and locate manual override in the local environment
|
||||||
try:
|
try:
|
||||||
from test_elasticsearch.local import get_client as local_get_client
|
from test_elasticsearch.local import get_client as local_get_client
|
||||||
client = local_get_client([os.environ.get('TEST_ES_SERVER', {})], **kw)
|
client = local_get_client()
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# fallback to using vanilla client
|
# fallback to using vanilla client
|
||||||
client = Elasticsearch([os.environ.get('TEST_ES_SERVER', {})], **kw)
|
client = get_test_client()
|
||||||
|
|
||||||
# wait for yellow status
|
|
||||||
for _ in range(100):
|
|
||||||
time.sleep(.1)
|
|
||||||
try:
|
|
||||||
client.cluster.health(wait_for_status='yellow')
|
|
||||||
return client
|
return client
|
||||||
except ConnectionError:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
# timeout
|
|
||||||
raise SkipTest("Elasticsearch failed to start.")
|
|
||||||
|
|
||||||
def setup():
|
def setup():
|
||||||
get_client()
|
get_client()
|
||||||
|
|
||||||
ES_VERSION = None
|
class ElasticsearchTestCase(BaseTestCase):
|
||||||
|
@staticmethod
|
||||||
def _get_version(version_string):
|
def _get_client():
|
||||||
version = version_string.strip().split('.')
|
return get_client()
|
||||||
return tuple(int(v) if v.isdigit() else 999 for v in version)
|
|
||||||
|
|
||||||
class ElasticTestCase(TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
self.client = get_client()
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.client.indices.delete(index='*')
|
|
||||||
self.client.indices.delete_template(name='*', ignore=404)
|
|
||||||
|
|
||||||
@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
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,10 @@ import yaml
|
|||||||
|
|
||||||
from elasticsearch import TransportError
|
from elasticsearch import TransportError
|
||||||
from elasticsearch.compat import string_types
|
from elasticsearch.compat import string_types
|
||||||
|
from elasticsearch.helpers.test import _get_version
|
||||||
|
|
||||||
from ..test_cases import SkipTest
|
from ..test_cases import SkipTest
|
||||||
from . import ElasticTestCase, _get_version
|
from . import ElasticsearchTestCase
|
||||||
|
|
||||||
# some params had to be changed in python, keep track of them so we can rename
|
# some params had to be changed in python, keep track of them so we can rename
|
||||||
# those in the tests accordingly
|
# those in the tests accordingly
|
||||||
@@ -33,7 +34,7 @@ IMPLEMENTED_FEATURES = ('regex', 'gtelte')
|
|||||||
class InvalidActionType(Exception):
|
class InvalidActionType(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class YamlTestCase(ElasticTestCase):
|
class YamlTestCase(ElasticsearchTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(YamlTestCase, self).setUp()
|
super(YamlTestCase, self).setUp()
|
||||||
if hasattr(self, '_setup_code'):
|
if hasattr(self, '_setup_code'):
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
from elasticsearch import helpers
|
from elasticsearch import helpers
|
||||||
|
|
||||||
from . import ElasticTestCase
|
from . import ElasticsearchTestCase
|
||||||
from ..test_cases import SkipTest
|
from ..test_cases import SkipTest
|
||||||
|
|
||||||
class TestStreamingBulk(ElasticTestCase):
|
class TestStreamingBulk(ElasticsearchTestCase):
|
||||||
def test_actions_remain_unchanged(self):
|
def test_actions_remain_unchanged(self):
|
||||||
actions = [{'_id': 1}, {'_id': 2}]
|
actions = [{'_id': 1}, {'_id': 2}]
|
||||||
for ok, item in helpers.streaming_bulk(self.client, actions, index='test-index', doc_type='answers'):
|
for ok, item in helpers.streaming_bulk(self.client, actions, index='test-index', doc_type='answers'):
|
||||||
@@ -53,7 +53,7 @@ class TestStreamingBulk(ElasticTestCase):
|
|||||||
self.assertEquals({'f': 'v'}, self.client.get(index='i', id=47)['_source'])
|
self.assertEquals({'f': 'v'}, self.client.get(index='i', id=47)['_source'])
|
||||||
|
|
||||||
|
|
||||||
class TestBulk(ElasticTestCase):
|
class TestBulk(ElasticsearchTestCase):
|
||||||
def test_all_documents_get_inserted(self):
|
def test_all_documents_get_inserted(self):
|
||||||
docs = [{"answer": x, '_id': x} for x in range(100)]
|
docs = [{"answer": x, '_id': x} for x in range(100)]
|
||||||
success, failed = helpers.bulk(self.client, docs, index='test-index', doc_type='answers', refresh=True)
|
success, failed = helpers.bulk(self.client, docs, index='test-index', doc_type='answers', refresh=True)
|
||||||
@@ -128,7 +128,7 @@ class TestBulk(ElasticTestCase):
|
|||||||
self.assertEquals(1, failed)
|
self.assertEquals(1, failed)
|
||||||
|
|
||||||
|
|
||||||
class TestScan(ElasticTestCase):
|
class TestScan(ElasticsearchTestCase):
|
||||||
def test_all_documents_are_read(self):
|
def test_all_documents_are_read(self):
|
||||||
bulk = []
|
bulk = []
|
||||||
for x in range(100):
|
for x in range(100):
|
||||||
@@ -142,7 +142,7 @@ class TestScan(ElasticTestCase):
|
|||||||
self.assertEquals(set(map(str, range(100))), set(d['_id'] for d in docs))
|
self.assertEquals(set(map(str, range(100))), set(d['_id'] for d in docs))
|
||||||
self.assertEquals(set(range(100)), set(d['_source']['answer'] for d in docs))
|
self.assertEquals(set(range(100)), set(d['_source']['answer'] for d in docs))
|
||||||
|
|
||||||
class TestReindex(ElasticTestCase):
|
class TestReindex(ElasticsearchTestCase):
|
||||||
def test_all_documents_get_moved(self):
|
def test_all_documents_get_moved(self):
|
||||||
bulk = []
|
bulk = []
|
||||||
for x in range(100):
|
for x in range(100):
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ from elasticsearch import Elasticsearch, MemcachedConnection, NotFoundError
|
|||||||
from elasticsearch.transport import ADDRESS_RE
|
from elasticsearch.transport import ADDRESS_RE
|
||||||
from elasticsearch.compat import u
|
from elasticsearch.compat import u
|
||||||
|
|
||||||
from . import ElasticTestCase
|
from . import ElasticsearchTestCase
|
||||||
from ..test_cases import SkipTest
|
from ..test_cases import SkipTest
|
||||||
|
|
||||||
class TestMemcachedConnection(ElasticTestCase):
|
class TestMemcachedConnection(ElasticsearchTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
try:
|
try:
|
||||||
import pylibmc
|
import pylibmc
|
||||||
|
|||||||
Reference in New Issue
Block a user