Skip to content

Commit

Permalink
Merge 6ca6871 into 7abd442
Browse files Browse the repository at this point in the history
  • Loading branch information
kblumke committed Jul 26, 2016
2 parents 7abd442 + 6ca6871 commit df3ed2c
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 92 deletions.
34 changes: 29 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,20 @@ To check if everything is ready to go, you can always test both fixtures:
Configuration
=============

You can define your own path for elasticsearch logs directory. There are three ways to achieve this:
You can define your own paths and/or variables for elasticsearch:

* logs directory (logsdir, elasticsearch_logsdir, --elasticsearch-logsdir),
* host (host, elasticsearch_host, --elasticsearch-host),
* port (port, elasticsearch_port, --elasticsearch-port),
* cluster name (cluster_name, elasticsearch_cluster_name, --elasticsearch-cluster-name),
* index store type (index_store_type, elasticsearch_index_store_type, --elasticsearch-index-store-type),
* network publish host (network_publish_host, elasticsearch_network_publish_host, --elasticsearch-network-publish-host),
* logs prefix (logs_prefix, elasticsearch_logs_prefix, --elasticsearch-logs-prefix),
* elasticsearch discovery zen ping multicast enabled (discovery_zen_ping_multicast_enabled,
elasticsearch_discovery_zen_ping_multicast_enabled, --elasticsearch-discovery-zen-ping-multicast-enabled),


There are three ways to achieve this (example of use: ``logs directory``):

* pass it as an argument in your own fixture

Expand All @@ -95,12 +108,23 @@ You can define your own path for elasticsearch logs directory. There are three w
elasticsearch_logsdir =
/tmp/elasticsearch/logs
If you don't want to define your own directory path in any given way, you can always use a default value,
which is simply ``/tmp``. If you do, you have to remember about the order of priority:
If you don't want to define your own directory path and/or variables in any given way, you can always use a default value.
If you do, you have to remember about the order of priority:

* ``fixture argument``
* ``--elasticsearch-logsdir``
* ``logsdir in pytest.ini``
* ``commandline option``
* ``var in pytest.ini``

List of default values (defined in .ini):

* logs directory - ``/tmp``
* host - ``'127.0.0.1'``
* port - ``9201``
* cluster_name - ``'elasticsearch_cluster_9201'``
* index store type - ``'memory'``
* network publish host - ``'127.0.0.1'``
* logs prefix - ``''``
* elasticsearch discovery zen ping multicast enabled - ``'false'``


Package resources
Expand Down
11 changes: 10 additions & 1 deletion pylama.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# D100: Missing docstring in public module [pep257]
# D102: Missing docstring in public method [pep257]
# D105: Missing docstring in magic method [pep257]

[pylama]
linters = pep8,pyflakes,pep257
skip = docs/*,\
build/*
ignore = D203,\
D204
D204

[pylama:*/port.py]
ignore = D100,\
D102,\
D105
3 changes: 2 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ mccabe==0.5.0
pyroma==2.0.2
pytest==2.8.7
path.py==8.2.1
port-for==0.3.1
port-for==0.3.1
mock==2.0.0
74 changes: 46 additions & 28 deletions src/pytest_elasticsearch/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,26 @@
def return_config(request):
"""Return a dictionary with config options."""
config = {}
logsdir = request.config.getoption('logsdir') or \
request.config.getini('elasticsearch_logsdir')
config['logsdir'] = logsdir
options = [
'port', 'host', 'cluster_name',
'network_publish_host', 'discovery_zen_ping_multicast_enabled',
'index_store_type', 'logs_prefix', 'logsdir'
]
for option in options:
conf = request.config.getoption(option) or \
request.config.getini('elasticsearch_{0}'.format(option))
if option == 'cluster_name' and conf in('', None):
port = config['port']
conf = 'elasticsearch_cluster_{0}'.format(port)
config[option] = conf
return config


def elasticsearch_proc(executable='/usr/share/elasticsearch/bin/elasticsearch',
host='127.0.0.1', port=9201, cluster_name=None,
network_publish_host='127.0.0.1',
discovery_zen_ping_multicast_enabled=False,
index_store_type='memory', logs_prefix='',
host=None, port=-1, cluster_name=None,
network_publish_host=None,
discovery_zen_ping_multicast_enabled=None,
index_store_type=None, logs_prefix=None,
elasticsearch_logsdir=None):
"""
Create elasticsearch process fixture.
Expand All @@ -50,11 +59,12 @@ def elasticsearch_proc(executable='/usr/share/elasticsearch/bin/elasticsearch',
:param str executable: elasticsearch's executable
:param str host: host that the instance listens on
:param int|str port: exact port that the instance listens on (e.g. 8000),
or randomly selected port:
'?' - any random available port
'2000-3000' - random available port from a given range
'4002,4003' - random of 4002 or 4003 ports
:param str|int|tuple|set|list port:
exact port (e.g. '8000', 8000)
randomly selected port (None) - any random available port
[(2000,3000)] or (2000,3000) - random available port from a given range
[{4002,4003}] or {4002,4003} - random of 4002 or 4003 ports
[(2000,3000), {4002,4003}] -random of given orange and set
:param str cluster_name: name of a cluser this node should work on.
Used for autodiscovery. By default each node is in it's own cluser.
:param str network_publish_host: host to publish itself within cluser
Expand All @@ -66,29 +76,37 @@ def elasticsearch_proc(executable='/usr/share/elasticsearch/bin/elasticsearch',
to speed up tests
:param str logs_prefix: prefix for log filename
:param str elasticsearch_logsdir: path for logs.
You can also use:
'--elasticsearch_logsdir' - command line option
'logsdir' var in your pytest.ini file
to set your own logs path.
:param elasticsearch_logsdir: path for elasticsearch logs
"""
@pytest.fixture(scope='session')
def elasticsearch_proc_fixture(request):
"""Elasticsearch process starting fixture."""
config = return_config(request)
elasticsearch_port = get_port(port)

pidfile = '/tmp/elasticsearch.{0}.pid'.format(elasticsearch_port)
home_path = '/tmp/elasticsearch_{0}'.format(elasticsearch_port)
elasticsearch_host = host or config['host']
elasticsearch_port = get_port(port) or get_port(config['port'])
elasticsearch_cluster_name = cluster_name or config['cluster_name']
elasticsearch_logs_prefix = logs_prefix or config['logs_prefix']
elasticsearch_index_store_type = index_store_type or \
config['index_store_type']
elasticsearch_network_publish_host = network_publish_host or \
config['network_publish_host']

logsdir = elasticsearch_logsdir or config['logsdir']

logs_path = path(logsdir) / '{prefix}elasticsearch_{port}_logs'.format(
prefix=logs_prefix,
prefix=elasticsearch_logs_prefix,
port=elasticsearch_port
)
work_path = '/tmp/elasticsearch_{0}_tmp'.format(elasticsearch_port)
cluster = cluster_name or 'dbfixtures.{0}'.format(elasticsearch_port)
multicast_enabled = str(discovery_zen_ping_multicast_enabled).lower()

home_path = '/tmp/elasticsearch_{0}'.format(elasticsearch_port)
pidfile = '{0}.pid'.format(home_path.replace('_', '.'))
work_path = '{0}_tmp'.format(home_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}
Expand All @@ -106,15 +124,15 @@ def elasticsearch_proc_fixture(request):
home_path=home_path,
logs_path=logs_path,
work_path=work_path,
cluster=cluster,
network_publish_host=network_publish_host,
cluster=elasticsearch_cluster_name,
network_publish_host=elasticsearch_network_publish_host,
multicast_enabled=multicast_enabled,
index_store_type=index_store_type
index_store_type=elasticsearch_index_store_type
)

elasticsearch_executor = HTTPExecutor(
command_exec, 'http://{host}:{port}'.format(
host=host,
host=elasticsearch_host,
port=elasticsearch_port
),
timeout=60,
Expand Down
85 changes: 84 additions & 1 deletion src/pytest_elasticsearch/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,48 @@ def pytest_addoption(parser):
parser.addini(
name='elasticsearch_logsdir',
help='',
default='/tmp',
default='/tmp'
)

parser.addini(
name='elasticsearch_host',
help='',
default='127.0.0.1'
)

parser.addini(
name='elasticsearch_cluster_name',
help='',
default=''
)

parser.addini(
name='elasticsearch_index_store_type',
help='',
default='memory'
)

parser.addini(
name='elasticsearch_network_publish_host',
help='',
default='127.0.0.1')

parser.addini(
name='elasticsearch_logs_prefix',
help='',
default=''
)

parser.addini(
name='elasticsearch_discovery_zen_ping_multicast_enabled',
help='',
default='false',
)

parser.addini(
name='elasticsearch_port',
help='',
default=9201,
)

parser.addoption(
Expand All @@ -37,5 +78,47 @@ def pytest_addoption(parser):
dest='logsdir',
)

parser.addoption(
'--elasticsearch-host',
action='store',
dest='host',
)

parser.addoption(
'--elasticsearch-cluster-name',
action='store',
dest='cluster_name',
)

parser.addoption(
'--elasticsearch-index-store-type',
action='store',
dest='index_store_type',
)

parser.addoption(
'--elasticsearch-network-publish-host)',
action='store',
dest='network_publish_host',
)

parser.addoption(
'--elasticsearch-logs-prefix',
action='store',
dest='logs_prefix',
)

parser.addoption(
'--elasticsearch-discovery-zen-ping-multicast-enabled',
action='store',
dest='discovery_zen_ping_multicast_enabled',
)

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

elasticsearch_proc = factories.elasticsearch_proc()
elasticsearch = factories.elasticsearch('elasticsearch_proc')
Loading

0 comments on commit df3ed2c

Please sign in to comment.