Package: pytest-elasticsearch
pytest-elasticsearchWhat is this?This is a pytest plugin that enables you to test your code that relies on a running Elasticsearch search engine.
It allows you to specify fixtures for Elasticsearch process and client.How to useWarningThis plugin requires at least version 6.0 of elasticsearch to work.The plugin contains two fixtures:elasticsearch- a client fixture that has functional scope, and which
cleans Elasticsearch at the end of each test.elasticsearch_proc- a session scoped fixture, that starts Elasticsearch
instance at its first use and stops at the end of the tests.elasticsearch_nooproc- a nooprocess fixture, that’s holds connection data
to already running elasticsearchSimply include one of these fixtures into your tests fixture list.You can also create additional elasticsearch client and process fixtures if you’d need to:frompytest_elasticsearchimportfactorieselasticsearch_my_proc=factories.elasticsearch_proc(port=None)elasticsearch_my=factories.elasticsearch('elasticsearch_my_proc')NoteEach elasticsearch process fixture can be configured in a different way than the others through the fixture factory arguments.Connecting to already existing Elasticsearch serviceSome projects are using already running Elasticsearch servers
(ie on docker instances). In order to connect to them, one would be using theelasticsearch_nooprocfixture.es_external=factories.elasticsearch('elasticsearch_nooproc')By default theelasticsearch_nooprocfixture would connect to elasticsearch
instance using9300port.ConfigurationYou can define your settings in three ways, it’s fixture factory argument, command line option and pytest.ini configuration option.
You can pick which you prefer, but remember that these settings are handled in the following order:Fixture factory argumentCommand line optionConfiguration option in your pytest.ini fileConfiguration optionsElasticSearch optionFixture factory argumentCommand line optionpytest.ini optionNoop process fixtureDefaultElasticsearch executableexecutable–elasticsearch-executableelasticsearch_executable/usr/share/elasticsearch/bin/elasticsearchhosthost–elasticsearch-hostelasticsearch_hosthost127.0.0.1portport–elasticsearch-portelasticsearch_port6300randomElasticsearch cluster namecluster_name–elasticsearch-cluster-nameelasticsearch_cluster_nameelasticsearch_cluster_<port>index storage typeindex_store_type–elasticsearch-index-store-typeelasticsearch_index_store_typemmapfsnetwork publish hostnetwork_publish_host–elasticsearch-network-publish-hostelasticsearch_network_publish_host127.0.0.1transport tcp porttransport_tcp_port–elasticsearch-transport-tcp-portelasticsearch_transport_tcp_portrandomExample usage:pass it as an argument in your own fixtureelasticsearch_proc=factories.elasticsearch_proc(cluster_name='awsome_cluster)specify your directory aselasticsearch_cluster_namein yourpytest.inifile.To do so, put a line like the following under the[pytest]section of yourpytest.ini:[pytest]elasticsearch_cluster_name=awsome_clusterKnown issuesIt might happen, that the process can’t be started due to lack of permissions.
The files that user running tests has to have access to are:/etc/default/elasticsearchMake sure that you either run tests as a user that has access to these files,
or you give user proper permissions or add it to proper user groups.In CI at the moment, we install elasticsearch from tar/zip archives,
which do not set up additional permission restrictions, so it’s not a problem on the CI/CD.Package resourcesBug tracker:https://github.com/ClearcodeHQ/pytest-elasticsearch/issues