Skip to content

Commit f76891a

Browse files
alexpdp7SergeyPirogov
authored andcommitted
Update Elasticsearch support (#46)
* elasticsearch:latest no longer exists * Entrypoint has changed too; pass stuff as environment variables instead * Refactor method to calculate URL * Add some testing * Add some docs, reference in index.rst
1 parent 53a905b commit f76891a

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

docs/database.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,12 @@ Connection detail for Oracle DB.
6868
sid: xe
6969
username: system
7070
password: oracle
71+
72+
Elasticsearch
73+
-------------
74+
75+
::
76+
77+
es = ElasticSearchContainer()
78+
with es:
79+
es.get_url() # gives you the http URL to connect to Elasticsearch

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Currently available features:
1414
- MySql db container
1515
- PostgreSQL db container
1616
- Google Cloud PubSub emulator container
17+
- Elasticsearch container
1718
- Generic Docker containers
1819

1920
Installation

testcontainers/elasticsearch.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,24 @@
1616

1717

1818
class ElasticsearchContainer(DockerContainer):
19-
def __init__(self, image="elasticsearch:latest", port_to_expose=9200):
19+
def __init__(self, image="elasticsearch:7.5.0", port_to_expose=9200):
2020
super(ElasticsearchContainer, self).__init__(image)
2121
self.port_to_expose = port_to_expose
2222
self.with_exposed_ports(self.port_to_expose)
23-
cmd_dict = {
24-
'transport.host': '127.0.0.1',
25-
'http.host': '0.0.0.0',
26-
'discovery.zen.minimum_master_nodes': '1'
27-
}
28-
command = ' '.join(['-E{0}={1}'.format(k, v) for k, v in cmd_dict.items()])
29-
self.with_command(command)
23+
self.with_env('transport.host', '127.0.0.1')
24+
self.with_env('http.host', '0.0.0.0')
25+
self.with_env('discovery.zen.minimum_master_nodes', '1')
3026

3127
@wait_container_is_ready()
3228
def _connect(self):
33-
port = self.get_exposed_port(self.port_to_expose)
34-
res = urllib.request.urlopen('http://127.0.0.1:{}'.format(port))
29+
res = urllib.request.urlopen(self.get_url())
3530
if res.status != 200:
3631
raise Exception()
3732

33+
def get_url(self):
34+
port = self.get_exposed_port(self.port_to_expose)
35+
return 'http://127.0.0.1:{}'.format(port)
36+
3837
def start(self):
3938
super().start()
4039
self._connect()

tests/test_elasticsearch.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import json
2+
import urllib
3+
4+
from testcontainers.elasticsearch import ElasticsearchContainer
5+
6+
7+
def test_docker_run_elasticsearch():
8+
config = ElasticsearchContainer()
9+
with config as es:
10+
resp = urllib.request.urlopen(es.get_url())
11+
assert json.loads(resp.read().decode())['version']['number'] == '7.5.0'

0 commit comments

Comments
 (0)