Skip to main content

Start Elasticsearch with Python (for testing or other purposes)

Project description

Start Elasticsearch using Python.

Settings

elasticsearch_test.ElasticsearchTest(
  port=1234
  es_path='/home/maria/elasticsearch-5.2.4'
  data_dir='/home/maria/es-data'
  es_java_opts='-Xms512m -Xmx512m'
)

port

Default: 9200

es_path

Path to the Elasticsearch directory (home of /bin). Default: uses $ES_HOME

es_java_opts

Sets $ES_JAVA_OPTS for process to this value if present.

data_dir

Directory to place /data and /logs in. Default: uses tempfile.

Running

import time
import elasticsearch_test
instance = elasticsearch_test.ElasticsearchTest()

# This blocks until either an error is found
# or Elasticsearch has been initialized.

instance.start()

# or

instance.start(block=False)

while True:
    if instance.is_started():
        break
    time.sleep(1)

Testing

Use it as a resource::

import elasticsearch_test
import elasticsearch

with elasticsearch_test.ElasticsearchTest() as instance:
    connection_info = instance.get_connection_info()
    client = elasticsearch.Elasticsearch(connection_info)

Use it as a Pytest fixture::

import elasticsearch_test

@pytest.fixture(scope="session")
def es():
    instance = elasticsearch_test.ElasticsearchTest()
    instance.start()
    yield instance
    instance.stop()

When using data_dir, if there are preexisting data in the directory, you should also wait for the data to be initialized::

with elasticsearch_test.ElasticsearchTest(data_dir='my_data') as instance:
    while not instance.is_data_initialized():
        time.sleep(1)
    # do something!

Test this package

Needed: pyenv and the plugin pyenv-virtualenv.

  1. Create a normal virtualenv and activate it

  2. Install extras pip install .[testing]

  3. Install the needed Python versions using pyenv: 3.8, 3.9, 3.10, 3.11

  4. Setup virtualenvs for all but the Python version you are using, for example if you use 3.8

    $ pyenv virtualenv -p python3.9 3.9.8 py39
    $ pyenv virtualenv -p python3.10 3.10.8 py310
    $ pyenv virtualenv -p python3.11 3.11.0 py311
  5. Setup path to Elasticsearch

    $ export ES_HOME=/opt/elasticsearch-6.8.23/
  6. Activate everything and run tox:

    $ pyenv shell py39 py310 py311
    $ source ./venv/bin/activate
    $ tox

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

elasticsearch-test-py-1.2.0.tar.gz (4.9 kB view details)

Uploaded Source

File details

Details for the file elasticsearch-test-py-1.2.0.tar.gz.

File metadata

  • Download URL: elasticsearch-test-py-1.2.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for elasticsearch-test-py-1.2.0.tar.gz
Algorithm Hash digest
SHA256 ca1ccf6206fec245da2c77fa1ad865c3a423b235929f216f9f8f62fe84556bf9
MD5 8b555170cbb32b53b53bdd9389aa7319
BLAKE2b-256 ce175e5cced1bb8c5242b9bdfdb7c965f3a918bbc2635bae9dab7c72ed36a76c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page