1212# under the License.
1313import logging
1414import re
15- import urllib
16- from urllib .error import URLError
1715
1816from testcontainers .core .container import DockerContainer
1917from testcontainers .core .utils import raise_for_deprecated_parameter
20- from testcontainers .core .waiting_utils import wait_container_is_ready
18+ from testcontainers .core .wait_strategies import HttpWaitStrategy
2119
2220_FALLBACK_VERSION = 8
2321"""This version is used when no version could be detected from the image name."""
@@ -69,14 +67,14 @@ class ElasticSearchContainer(DockerContainer):
6967 >>> from testcontainers.elasticsearch import ElasticSearchContainer
7068
7169 >>> with ElasticSearchContainer(f'elasticsearch:8.3.3', mem_limit='3G') as es:
72- ... resp = urllib.request.urlopen(es.get_url() )
70+ ... resp = urllib.request.urlopen(f'http://{ es.get_container_host_ip()}:{es.get_exposed_port(es.port)}' )
7371 ... json.loads(resp.read().decode())['version']['number']
7472 '8.3.3'
7573 """
7674
7775 def __init__ (self , image : str = "elasticsearch" , port : int = 9200 , ** kwargs ) -> None :
7876 raise_for_deprecated_parameter (kwargs , "port_to_expose" , "port" )
79- super ().__init__ (image , ** kwargs )
77+ super ().__init__ (image , _wait_strategy = HttpWaitStrategy ( port ), ** kwargs )
8078 self .port = port
8179 self .with_exposed_ports (self .port )
8280 self .with_env ("transport.host" , "127.0.0.1" )
@@ -85,19 +83,3 @@ def __init__(self, image: str = "elasticsearch", port: int = 9200, **kwargs) ->
8583 major_version = _major_version_from_image_name (image )
8684 for key , value in _environment_by_version (major_version ).items ():
8785 self .with_env (key , value )
88-
89- @wait_container_is_ready (URLError )
90- def _connect (self ) -> None :
91- res = urllib .request .urlopen (self .get_url ())
92- if res .status != 200 :
93- raise Exception ()
94-
95- def get_url (self ) -> str :
96- host = self .get_container_host_ip ()
97- port = self .get_exposed_port (self .port )
98- return f"http://{ host } :{ port } "
99-
100- def start (self ) -> "ElasticSearchContainer" :
101- super ().start ()
102- self ._connect ()
103- return self
0 commit comments