How to use _major_version_from_image_name method in testcontainers-python

Best Python code snippet using testcontainers-python_python

elasticsearch.py

Source:elasticsearch.py Github

copy

Full Screen

...18from testcontainers.core.container import DockerContainer19from testcontainers.core.waiting_utils import wait_container_is_ready20_FALLBACK_VERSION = 821"""This version is used when no version could be detected from the image name."""22def _major_version_from_image_name(image_name: str) -> int:23 """Returns the major version from a container name like 'elasticsearch:8.1.0'24 If the major version could not be determined, it will use the most recent25 one (8 at the time of writing 2022-08-11).26 """27 version_string = image_name.split(":")[-1]28 regex_match = re.compile(r"(\d+)\.\d+\.\d+").match(version_string)29 if not regex_match:30 logging.warning("Could not determine major version from image name '%s'. Will use %s",31 image_name, _FALLBACK_VERSION)32 return _FALLBACK_VERSION33 else:34 return int(regex_match.group(1))35def _environment_by_version(version: int) -> Dict[str, str]:36 """Returns environment variables required for each major version to work."""37 if version == 6:38 # This setting is needed to avoid the check for the kernel parameter39 # vm.max_map_count in the BootstrapChecks40 return {"discovery.zen.minimum_master_nodes": "1"}41 elif version == 7:42 return {}43 elif version == 8:44 # Elasticsearch uses https now by default. However, our readiness45 # check uses http, which does not work. Hence we disable security46 # which should not be an issue for our context47 return {"xpack.security.enabled": "false"}48 else:49 raise ValueError(f"Unknown elasticsearch version given: {version}")50class ElasticSearchContainer(DockerContainer):51 """52 ElasticSearch container.53 Example54 -------55 ::56 with ElasticSearchContainer() as es:57 connection_url = es.get_url()58 """59 def __init__(self, image="elasticsearch", port_to_expose=9200, **kwargs):60 super(ElasticSearchContainer, self).__init__(image, **kwargs)61 self.port_to_expose = port_to_expose62 self.with_exposed_ports(self.port_to_expose)63 self.with_env('transport.host', '127.0.0.1')64 self.with_env('http.host', '0.0.0.0')65 major_version = _major_version_from_image_name(image)66 for key, value in _environment_by_version(major_version).items():67 self.with_env(key, value)68 @wait_container_is_ready()69 def _connect(self):70 res = urllib.request.urlopen(self.get_url())71 if res.status != 200:72 raise Exception()73 def get_url(self):74 host = self.get_container_host_ip()75 port = self.get_exposed_port(self.port_to_expose)76 return 'http://{}:{}'.format(host, port)77 def start(self):78 super().start()79 self._connect()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run testcontainers-python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful