Skip to content

Commit dfb939d

Browse files
committed
Major test refactor.
- removed the git submodule, relying on the elasticsearch repo existing outside of this one - modified the run_tests.py script to inspect currently running elasticsearch and try and checkout the exact SHA used to build the server - removed the option of starting out own elasticsearch server - fixed travis.yml to reflect the changed situation - added a README file to the test suite explaining all the options
1 parent 5aecde0 commit dfb939d

File tree

7 files changed

+123
-87
lines changed

7 files changed

+123
-87
lines changed

.travis.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ matrix:
2121
env: TEST_ES_CONNECTION=ThriftConnection
2222

2323
install:
24-
- wget -O - http://s3-us-west-2.amazonaws.com/build.elasticsearch.org/origin/master/nightly/JDK6/elasticsearch-latest-SNAPSHOT.tar.gz | tar xz -C /tmp
25-
- /tmp/elasticsearch-1.0.0.RC1-SNAPSHOT/bin/plugin -install elasticsearch/elasticsearch-transport-memcached/1.6.0
26-
- /tmp/elasticsearch-1.0.0.RC1-SNAPSHOT/bin/plugin -install elasticsearch/elasticsearch-transport-thrift/1.6.0
27-
- git submodule init
24+
- mkdir /tmp/elasticsearch
25+
- wget -O - http://s3-us-west-2.amazonaws.com/build.elasticsearch.org/origin/master/nightly/JDK6/elasticsearch-latest-SNAPSHOT.tar.gz | tar xz --directory=/tmp/elasticsearch --strip-components=1
26+
- /tmp/elasticsearch/bin/plugin -install elasticsearch/elasticsearch-transport-memcached/2.0.0.RC1
27+
- /tmp/elasticsearch/bin/plugin -install elasticsearch/elasticsearch-transport-thrift/2.0.0.RC1
28+
- git clone https://github.com/elasticsearch/elasticsearch.git ../elasticsearch
2829
- pip install coveralls
2930
- pip install .
3031

3132
before_script:
32-
- /tmp/elasticsearch-1.0.0.RC1-SNAPSHOT/bin/elasticsearch -d -D es.path.data=/tmp -D es.gateway.type=none -D es.index.store.type=memory -D es.discovery.zen.ping.multicast.enabled=false
33+
- /tmp/elasticsearch/bin/elasticsearch -d -D es.path.data=/tmp -D es.gateway.type=none -D es.index.store.type=memory -D es.discovery.zen.ping.multicast.enabled=false
3334

3435
script:
35-
- TEST_ES_SERVER=localhost python setup.py test
36+
- python setup.py test
3637

3738
after_success:
3839
- coveralls

CONTRIBUTING.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@ The process for contributing to any of the Elasticsearch repositories is similar
2020
2. Run the test suite to ensure your changes do not break existing code:
2121

2222
````
23-
git submodules init
2423
python setup.py test
2524
````
2625
27-
If your changes require special configuration of the client you can create a
28-
`test_elasticsearch/local.py` file containing a `get_client` with the same
29-
signature as the client itself (`hosts, **kwargs` will do) and returns a client
30-
instance. This function will be used for any tests running against ES and will
31-
be passed all the parameters that would otherwise be used to construct the
32-
client instance directly.
26+
See the README file in `test_elasticsearch` dirctory for more information on
27+
running the test suite.
3328
3429
3. Rebase your changes.
3530
Update your local repository with the most recent code from the main

rest-api-spec

Submodule rest-api-spec deleted from 2e4b70d

test_elasticsearch/README.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
elasticsearch-py test suite
2+
===========================
3+
4+
Warning - by default the tests will try and connect to `localhost:9200` and
5+
will destroy all contents of given cluster!
6+
7+
Running the tests
8+
-----------------
9+
10+
To simply run the tests just execute the `run_tests.py` script or invoke
11+
`python setup.py test`. The behavior is driven by environmental variables:
12+
13+
* `TEST_ES_SERVER` - can contain "hostname[:port]" of running es cluster
14+
15+
* `TEST_ES_CONNECTION` - name of the connection class to use from
16+
`elasticsearch.connection` module. If you want to run completely with your
17+
own see section on customizing tests.
18+
19+
* `TEST_ES_YAML_DIR` - path to the yaml test suite contained in the
20+
elasticsearch repo. Defaults to `$TEST_ES_REPO/rest-api-spec/test`
21+
22+
* `TEST_ES_REPO` - path to the elasticsearch repo, by default it will look in
23+
the same directory as `elasticsearch-py` is in. It will not be used if
24+
`TEST_ES_YAML_DIR` is specified directly.
25+
26+
* `TEST_ES_NOFETCH` - controls if we should fetch new updates to elasticsearch
27+
repo and reset it's version to the sha used to build the current es server.
28+
Defaults to `False` which means we will fetch the elasticsearch repo and
29+
`git checkout` the sha used to build the server.
30+
31+
Alternatively, if you wish to control what you are doing you have several additional options:
32+
33+
* `run_tests.py` will pass any parameters specified to `nosetests`
34+
35+
* you can just run your favorite runner in the `test_elasticsearch` directory
36+
(verified to work with nose and py.test) and bypass the fetch logic entirely.
37+
38+
Customizing the tests
39+
---------------------
40+
41+
You can create a `local.py` file in the `test_elasticsearch` directory which
42+
should contain a `get_client` function:
43+
44+
def get_client(hosts, ** kwargs):
45+
...
46+
47+
If this file exists the function will be used instead of the built in one to
48+
construct the client used for any integration tests. You can use this to make
49+
sure your plugins and extensions work with `elasticsearch-py`.
50+

test_elasticsearch/run_tests.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
11
#!/usr/bin/env python
2+
from __future__ import print_function
3+
24
import sys
3-
from os import path
5+
from os import environ
6+
from os.path import dirname, join, pardir, abspath, exists
7+
import subprocess
48

59
import nose
610

11+
from test_elasticsearch.test_server import get_client
12+
from test_elasticsearch.test_cases import SkipTest
13+
14+
def fetch_es_repo():
15+
# user is manually setting YAML dir, don't tamper with it
16+
if 'TEST_ES_YAML_DIR' in environ:
17+
return
18+
19+
repo_path = environ.get(
20+
'TEST_ES_REPO',
21+
abspath(join(dirname(__file__), pardir, pardir, 'elasticsearch'))
22+
)
23+
24+
# no repo
25+
if not exists(repo_path) or not exists(join(repo_path, '.git')):
26+
print('No elasticsearch repo found...')
27+
# set YAML DIR to empty to skip yaml tests
28+
environ['TEST_ES_YAML_DIR'] = ''
29+
return
30+
31+
# set YAML test dir
32+
environ['TEST_ES_YAML_DIR'] = join(repo_path, 'rest-api-spec', 'test')
33+
34+
# fetching of yaml tests disabled, we'll run with what's there
35+
if environ.get('TEST_ES_NOFETCH', False):
36+
return
37+
38+
# find out the sha of the running es
39+
try:
40+
es = get_client()
41+
sha = es.info()['version']['build_hash']
42+
except (SkipTest, KeyError):
43+
print('No running elasticsearch >1.X server...')
44+
return
45+
46+
# fetch new commits to be sure...
47+
print('Fetching elasticsearch repo...')
48+
subprocess.check_call('cd %s && git fetch https://github.com/elasticsearch/elasticsearch.git' % repo_path, shell=True)
49+
# reset to the version fron info()
50+
subprocess.check_call('cd %s && git checkout %s' % (repo_path, sha), shell=True)
51+
752
def run_all(argv=None):
853
sys.exitfunc = lambda: sys.stderr.write('Shutting down....\n')
54+
55+
# fetch yaml tests
56+
fetch_es_repo()
57+
958
# always insert coverage when running tests
1059
if argv is None:
1160
argv = [
@@ -21,7 +70,7 @@ def run_all(argv=None):
2170

2271
nose.run_exit(
2372
argv=argv,
24-
defaultTest=path.abspath(path.dirname(__file__))
73+
defaultTest=abspath(dirname(__file__))
2574
)
2675

2776
if __name__ == '__main__':

test_elasticsearch/test_server/__init__.py

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,46 @@
11
import time
2-
import subprocess
3-
import tempfile
42
import os
53

6-
import requests
7-
84
from elasticsearch import Elasticsearch
95
from elasticsearch.exceptions import ConnectionError, NotFoundError
106

117
from ..test_cases import TestCase, SkipTest
128

13-
data_dir = None
14-
15-
CMD = """
16-
elasticsearch \
17-
-f \
18-
-D es.cluster.name=%(cluster_name)s \
19-
-D es.node.name=test_name \
20-
-D es.http.port=%(port)s \
21-
-D es.gateway.type=none \
22-
-D es.index.store.type=memory \
23-
-D es.discovery.zen.ping.multicast.enabled=false \
24-
-D es.path.data=%(data_dir)s \
25-
-D es.pidfile=%(pidfile)s \
26-
>/dev/null 2>&1
27-
"""
9+
client = None
2810

29-
server = None
30-
pidfile = tempfile.mktemp()
11+
def get_client():
12+
global client
13+
if client is not None:
14+
return client
3115

32-
def get_client(**kwargs):
3316
# construct kwargs from the environment
3417
kw = {}
3518
if 'TEST_ES_CONNECTION' in os.environ:
3619
from elasticsearch import connection
3720
kw['connection_class'] = getattr(connection, os.environ['TEST_ES_CONNECTION'])
38-
# update them with params
39-
kw.update(kwargs)
4021

4122
# try and locate manual override in the local environment
4223
try:
4324
from test_elasticsearch.local import get_client as local_get_client
44-
return local_get_client([os.environ['TEST_ES_SERVER']], **kw)
25+
client = local_get_client([os.environ.get('TEST_ES_SERVER', {})], **kw)
4526
except ImportError:
4627
# fallback to using vanilla client
47-
return Elasticsearch([os.environ['TEST_ES_SERVER']], **kw)
48-
49-
50-
def setup():
51-
global server
52-
53-
# if use running ES instance, don't attempt to start our own
54-
if 'TEST_ES_SERVER' not in os.environ:
55-
# check installed
56-
if subprocess.call('which elasticsearch >/dev/null 2>&1', shell=True) != 0:
57-
raise SkipTest("No Elasticsearch server, skipping integration tests.")
58-
59-
args = {
60-
'cluster_name': 'es_client_test',
61-
'port': 9900,
62-
'data_dir': tempfile.tempdir,
63-
'pidfile': pidfile
64-
}
65-
66-
# check running
67-
try:
68-
requests.get('http://localhost:%(port)s' % args)
69-
except requests.ConnectionError:
70-
pass
71-
else:
72-
raise SkipTest('Elasticsearch already running!')
73-
74-
75-
cmd = CMD % args
76-
77-
server = subprocess.Popen(cmd, shell=True)
78-
os.environ['TEST_ES_SERVER'] = 'localhost:%(port)s' % args
79-
80-
client = get_client()
28+
client = Elasticsearch([os.environ.get('TEST_ES_SERVER', {})], **kw)
8129

8230
# wait for yellow status
8331
for _ in range(100):
8432
time.sleep(.1)
8533
try:
8634
client.cluster.health(wait_for_status='yellow')
87-
break
35+
return client
8836
except ConnectionError:
8937
continue
90-
9138
else:
9239
# timeout
9340
raise SkipTest("Elasticsearch failed to start.")
9441

95-
96-
def teardown():
97-
if server is not None:
98-
with open(pidfile) as pidf:
99-
pid = pidf.read()
100-
os.kill(int(pid), 15)
101-
server.wait()
42+
def setup():
43+
get_client()
10244

10345
ES_VERSION = None
10446

test_elasticsearch/test_server/test_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ def m(self):
195195
return type(name, (YamlTestCase, ), attrs)
196196

197197
YAML_DIR = environ.get(
198-
'YAML_TEST_DIR',
198+
'TEST_ES_YAML_DIR',
199199
join(
200-
dirname(__file__), pardir, pardir,
201-
'rest-api-spec', 'rest-api-spec', 'test'
200+
dirname(__file__), pardir, pardir, pardir,
201+
'elasticsearch', 'rest-api-spec', 'test'
202202
)
203203
)
204204

0 commit comments

Comments
 (0)