Skip to content

Commit

Permalink
Merge 3503540 into 6778f23
Browse files Browse the repository at this point in the history
  • Loading branch information
dlcron committed Apr 4, 2018
2 parents 6778f23 + 3503540 commit 1be157a
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 29 deletions.
9 changes: 9 additions & 0 deletions .travis.install-elasticsearch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
function install_from_zip {
wget $1 -O out
unzip out
sudo mv $2 /opt/$2
}

install_from_zip https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.5.2.zip elasticsearch-1.5.2
install_from_zip https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.zip elasticsearch-6.2.3
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sudo: true
language: python
python:
- '2.7'
Expand All @@ -15,10 +16,12 @@ branches:
except:
- requires-io-master
install:
- pip install "setuptools>=18.0.0,<30.0.0" # pypy support
- pip install "setuptools>=18.5.0" # pypy support
- "pip install -r requirements-test.txt"
- python setup.py $INSTALL
- pip install pytest_elasticsearch[tests] coveralls wheel
before_script:
- bash .travis.install-elasticsearch.sh
script:
- py.test -n $XDIST --cov src/pytest_elasticsearch
- pylama
Expand Down
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ of pytest-elasticsearch along its history.
* Paweł Wilczyński
* Tomasz Karbownicki
* Karolina Blümke
* Marcin Maślany
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

Unreleased
-------

- [feature] - Support for elasticsearch version 6.2.3


1.2.1
-------

Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ You can pick which you prefer, but remember that these settings are handled in t
+----------------------+--------------------------------------+------------------------------------------------------+----------------------------------------------------+------------------------------+
| enable zen discovery | discovery_zen_ping_multicast_enabled | --elasticsearch-discovery-zen-ping-multicast-enabled | elasticsearch_discovery_zen_ping_multicast_enabled | False |
+----------------------+--------------------------------------+------------------------------------------------------+----------------------------------------------------+------------------------------+
| transport tcp port | transport_tcp_port | --elasticsearch-transport-tcp-port | elasticsearch_transport_tcp_port | random |
+----------------------+--------------------------------------+------------------------------------------------------+----------------------------------------------------+------------------------------+
| configuration path | configuration_path | --elasticsearch-configuration-path | elasticsearch_configuration_path | /etc/elasticsearch |
+----------------------+--------------------------------------+------------------------------------------------------+----------------------------------------------------+------------------------------+

Example usage:

Expand Down
90 changes: 73 additions & 17 deletions src/pytest_elasticsearch/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
# You should have received a copy of the GNU Lesser General Public License
# along with pytest-elasticsearch. If not, see <http://www.gnu.org/licenses/>.
"""Fixture factories."""
import re
import os.path
import shutil
from tempfile import gettempdir
import subprocess

import pytest

Expand All @@ -31,9 +33,9 @@ def return_config(request):
"""Return a dictionary with config options."""
config = {}
options = [
'port', 'host', 'cluster_name', 'network_publish_host',
'discovery_zen_ping_multicast_enabled', 'index_store_type',
'logs_prefix', 'logsdir'
'port', 'transport_tcp_port', 'host', 'cluster_name',
'network_publish_host', 'discovery_zen_ping_multicast_enabled',
'index_store_type', 'logs_prefix', 'logsdir', 'configuration_path'
]
for option in options:
option_name = 'elasticsearch_' + option
Expand All @@ -43,12 +45,30 @@ def return_config(request):
return config


def get_version_parts(executable):
"""Get the given elasticsearch executable version parts."""
try:
output = subprocess.check_output([executable, '-Vv']).decode('utf-8')
match = re.match(
'Version: (?P<major>\d)\.(?P<minor>\d)\.(?P<patch>\d)', output
)
if not match:
raise RuntimeError("Elasticsearch version is not recognized. "
"It is probably not supported.")
return match.groupdict()
except OSError:
raise RuntimeError(
"'%s' does not point to elasticsearch." % executable
)


def elasticsearch_proc(executable='/usr/share/elasticsearch/bin/elasticsearch',
host=None, port=-1, cluster_name=None,
network_publish_host=None,
host=None, port=-1, transport_tcp_port=None,
cluster_name=None, network_publish_host=None,
discovery_zen_ping_multicast_enabled=None,
index_store_type=None, logs_prefix=None,
elasticsearch_logsdir=None):
elasticsearch_logsdir=None,
configuration_path='/etc/elasticsearch'):
"""
Create elasticsearch process fixture.
Expand Down Expand Up @@ -77,15 +97,44 @@ def elasticsearch_proc(executable='/usr/share/elasticsearch/bin/elasticsearch',
:param str elasticsearch_logsdir: path for logs.
:param elasticsearch_logsdir: path for elasticsearch logs
"""
commands_exec = {}
commands_exec['1.5'] = '''
{deamon} -p {pidfile}
--http.port={port}
--path.home={home_path}
--transport.tcp.port={transport_tcp_port}
--default.path.logs={logs_path}
--default.path.work={work_path}
--default.path.data={work_path}
--default.path.conf={conf_path}
--cluster.name={cluster}
--network.publish_host='{network_publish_host}'
--discovery.zen.ping.multicast.enabled={multicast_enabled}
--index.store.type={index_store_type}
'''
commands_exec['6.2'] = '''
{deamon} -p {pidfile}
-E http.port={port}
-E transport.tcp.port={transport_tcp_port}
-E path.logs={logs_path}
-E path.data={work_path}
-E cluster.name={cluster}
-E network.host='{network_publish_host}'
-E index.store.type={index_store_type}
'''

@pytest.fixture(scope='session')
def elasticsearch_proc_fixture(request):
"""Elasticsearch process starting fixture."""
tmpdir = gettempdir()
config = return_config(request)
version_parts = get_version_parts(executable)

elasticsearch_host = host or config['host']

elasticsearch_port = get_port(port) or get_port(config['port'])
elasticsearch_transport_port = get_port(transport_tcp_port) or \
get_port(config['transport_tcp_port'])

elasticsearch_cluster_name = \
cluster_name or config['cluster_name'] or \
Expand All @@ -108,26 +157,32 @@ def elasticsearch_proc_fixture(request):
home_path = os.path.join(
tmpdir, 'elasticsearch_{0}'.format(elasticsearch_port))
work_path = '{0}_tmp'.format(home_path)
conf_path = configuration_path or config['configuration_path']

if discovery_zen_ping_multicast_enabled is not None:
multicast_enabled = str(
discovery_zen_ping_multicast_enabled).lower()
else:
multicast_enabled = config['discovery_zen_ping_multicast_enabled']

command_exec = '''
{deamon} -p {pidfile} --http.port={port}
--path.home={home_path} --default.path.logs={logs_path}
--default.path.work={work_path}
--default.path.conf=/etc/elasticsearch
--cluster.name={cluster}
--network.publish_host='{network_publish_host}'
--discovery.zen.ping.multicast.enabled={multicast_enabled}
--index.store.type={index_store_type}
'''.format(
exact = '{major}.{minor}.{patch}'.format(**version_parts)
major_minor = '{major}.{minor}'.format(**version_parts)
major = '{major}'.format(**version_parts)

# try exact version if there is such; otherwise fallback to minor;
# as final step fallback to major
command = commands_exec.get(exact) or \
commands_exec.get(major_minor) or \
commands_exec.get(major)
if not command:
raise RuntimeError("Elasticsearch could not be started.")

command_exec = command.format(
deamon=executable,
pidfile=pidfile,
port=elasticsearch_port,
transport_tcp_port=elasticsearch_transport_port,
conf_path=conf_path,
home_path=home_path,
logs_path=logs_path,
work_path=work_path,
Expand All @@ -149,7 +204,8 @@ def elasticsearch_proc_fixture(request):

def finalize_elasticsearch():
elasticsearch_executor.stop()
shutil.rmtree(home_path)
shutil.rmtree(work_path)
shutil.rmtree(logs_path)

request.addfinalizer(finalize_elasticsearch)
return elasticsearch_executor
Expand Down
27 changes: 27 additions & 0 deletions src/pytest_elasticsearch/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
'network host to which elasticsearch publish to connect to cluseter'
_help_logs_prefix = 'prefix for the logs file'
_help_discovery_zen_ping_multicast_enabled = 'Use zen discovery'
_help_elasticsearch_transport_tcp_port = 'The tcp ansport port used \
for internal communication between nodes within the cluster'
_help_configuration_path = 'Config file location'


def pytest_addoption(parser):
Expand Down Expand Up @@ -84,6 +87,18 @@ def pytest_addoption(parser):
default=None,
)

parser.addini(
'elasticsearch_transport_tcp_port',
help=_help_elasticsearch_transport_tcp_port,
default=None
)

parser.addini(
'elasticsearch_configuration_path',
help=_help_configuration_path,
default=None
)

parser.addoption(
'--elasticsearch-logsdir',
action='store',
Expand Down Expand Up @@ -137,6 +152,18 @@ def pytest_addoption(parser):
help=_help_port
)

parser.addoption(
'--elasticsearch-transport-tcp-port',
action='store',
dest='elasticsearch_transport_tcp_port',
)

parser.addoption(
'--elasticsearch-configuration-path',
action='store',
dest='elasticsearch_configuration_path'
)


elasticsearch_proc = factories.elasticsearch_proc()
elasticsearch = factories.elasticsearch('elasticsearch_proc')
65 changes: 54 additions & 11 deletions tests/test_elastic.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,69 @@
"""Pytest-elasticsearch tests."""
import pytest
from tempfile import gettempdir

from mock import patch

from pytest_elasticsearch import factories

ELASTICSEARCH_PATH_1_5_2 = '/opt/elasticsearch-1.5.2/'
ELASTICSEARCH_CONF_PATH_1_5_2 = ELASTICSEARCH_PATH_1_5_2 + 'config'
ELASTICSEARCH_EXECUTABLE_1_5_2 = ELASTICSEARCH_PATH_1_5_2 + 'bin/elasticsearch'
ELASTICSEARCH_EXECUTABLE_6_2_3 = '/opt/elasticsearch-6.2.3/bin/elasticsearch'

def test_elastic_process(elasticsearch_proc):
elasticsearch_proc_1_5_2 = factories.elasticsearch_proc(
ELASTICSEARCH_EXECUTABLE_1_5_2, port=None,
configuration_path=ELASTICSEARCH_CONF_PATH_1_5_2
)
elasticsearch_1_5_2 = factories.elasticsearch('elasticsearch_proc_1_5_2')

elasticsearch_proc_6_2_3 = factories.elasticsearch_proc(
ELASTICSEARCH_EXECUTABLE_6_2_3, port=None)
elasticsearch_6_2_3 = factories.elasticsearch('elasticsearch_proc_6_2_3')


@pytest.mark.parametrize('elasticsearch_proc_name', (
'elasticsearch_proc_1_5_2',
'elasticsearch_proc_6_2_3'
))
def test_elastic_process(request, elasticsearch_proc_name):
"""Simple test for starting elasticsearch_proc."""
elasticsearch_proc = request.getfixturevalue(elasticsearch_proc_name)
assert elasticsearch_proc.running() is True


def test_elasticsarch(elasticsearch):
@pytest.mark.parametrize('elasticsearch_name', (
'elasticsearch_1_5_2',
'elasticsearch_6_2_3'
))
def test_elasticsarch(request, elasticsearch_name):
"""Test if elasticsearch fixtures connects to process."""
info = elasticsearch.info()
assert info['status'] == 200


elasticsearch_proc_random = factories.elasticsearch_proc(port=None)
elasticsearch = request.getfixturevalue(elasticsearch_name)
info = elasticsearch.cluster.health()
assert info['status'] == 'green'


@pytest.mark.parametrize('executable, expected_version', (
(ELASTICSEARCH_EXECUTABLE_1_5_2, '1.5.2'),
(ELASTICSEARCH_EXECUTABLE_6_2_3, '6.2.3')
))
def test_version_extraction(executable, expected_version):
"""Verfiy if we can properly extract elasticsearch version."""
assert '{major}.{minor}.{patch}'.format(
**factories.get_version_parts(executable)
) == expected_version


elasticsearch_proc_random = factories.elasticsearch_proc(
ELASTICSEARCH_EXECUTABLE_1_5_2, port=None,
configuration_path=ELASTICSEARCH_CONF_PATH_1_5_2
)
elasticsearch_random = factories.elasticsearch('elasticsearch_proc_random')


def test_random_port(elasticsearch_random):
"""Test if elasticsearch fixture can be started on random port."""
assert elasticsearch_random.info()['status'] == 200
assert elasticsearch_random.cluster.health()['status'] == 'green'


def test_default_configuration(request):
Expand Down Expand Up @@ -61,7 +101,10 @@ def test_ini_option_configuration(request):


elasticsearch_proc_args = factories.elasticsearch_proc(
port=None, elasticsearch_logsdir='/tmp')
ELASTICSEARCH_EXECUTABLE_1_5_2,
configuration_path=ELASTICSEARCH_CONF_PATH_1_5_2,
port=None, elasticsearch_logsdir='/tmp'
)


@patch('pytest_elasticsearch.plugin.pytest.config')
Expand All @@ -72,8 +115,8 @@ def test_fixture_arg_is_first(request, elasticsearch_proc_args):
conf_dict = factories.return_config(request)

port = elasticsearch_proc_args.port
command = elasticsearch_proc_args.command_parts
path_logs = '--default.path.logs=/tmp/elasticsearch_{}_logs'.format(port)
command = ' '.join(elasticsearch_proc_args.command_parts)
path_logs = 'path.logs=/tmp/elasticsearch_{}_logs'.format(port)

assert conf_dict['logsdir'] == '/test1'
assert path_logs in command

0 comments on commit 1be157a

Please sign in to comment.