|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | +from testcontainers.core.container import DockerContainer |
| 14 | +from testcontainers.core.waiting_utils import wait_container_is_ready |
| 15 | +import urllib |
| 16 | + |
| 17 | + |
| 18 | +class ElasticsearchContainer(DockerContainer): |
| 19 | + def __init__(self, image="elasticsearch:latest", port_to_expose=9200): |
| 20 | + super(ElasticsearchContainer, self).__init__(image) |
| 21 | + self.port_to_expose = port_to_expose |
| 22 | + 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) |
| 30 | + |
| 31 | + @wait_container_is_ready() |
| 32 | + 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)) |
| 35 | + if res.status != 200: |
| 36 | + raise Exception() |
| 37 | + |
| 38 | + def start(self): |
| 39 | + super().start() |
| 40 | + self._connect() |
| 41 | + return self |
0 commit comments