|
| 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 | + |
| 14 | +import requests |
| 15 | + |
| 16 | +from testcontainers.core.config import testcontainers_config as c |
| 17 | +from testcontainers.core.generic import DockerContainer |
| 18 | +from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs |
| 19 | + |
| 20 | + |
| 21 | +class MilvusContainer(DockerContainer): |
| 22 | + """ |
| 23 | + Milvus database container. |
| 24 | +
|
| 25 | + Read mode about Milvus: https://milvus.io/docs |
| 26 | +
|
| 27 | + Example: |
| 28 | +
|
| 29 | + The example spins up a Milvus database and connects to it client using MilvisClient. |
| 30 | +
|
| 31 | + .. doctest:: |
| 32 | +
|
| 33 | + >>> from testcontainers.milvus import MilvusContainer |
| 34 | + >>> with MilvusContainer("milvusdb/milvus:v2.4.4") as milvus_container: |
| 35 | + ... milvus_container.get_exposed_port(milvus_container.port) in milvus_container.get_connection_url() |
| 36 | + True |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + image: str = "milvusdb/milvus:latest", |
| 42 | + port: int = 19530, |
| 43 | + **kwargs, |
| 44 | + ) -> None: |
| 45 | + super().__init__(image=image, **kwargs) |
| 46 | + self.port = port |
| 47 | + self.healthcheck_port = 9091 |
| 48 | + self.with_exposed_ports(self.port, self.healthcheck_port) |
| 49 | + self.cmd = "milvus run standalone" |
| 50 | + |
| 51 | + envs = {"ETCD_USE_EMBED": "true", "ETCD_DATA_DIR": "/var/lib/milvus/etcd", "COMMON_STORAGETYPE": "local"} |
| 52 | + |
| 53 | + for env, value in envs.items(): |
| 54 | + self.with_env(env, value) |
| 55 | + |
| 56 | + def get_connection_url(self) -> str: |
| 57 | + ip = self.get_container_host_ip() |
| 58 | + port = self.get_exposed_port(self.port) |
| 59 | + return f"http://{ip}:{port}" |
| 60 | + |
| 61 | + @wait_container_is_ready() |
| 62 | + def _connect(self) -> None: |
| 63 | + msg = "Welcome to use Milvus!" |
| 64 | + wait_for_logs(self, f".*{msg}.*", c.max_tries, c.sleep_time) |
| 65 | + self._healthcheck() |
| 66 | + |
| 67 | + def _get_healthcheck_url(self) -> str: |
| 68 | + ip = self.get_container_host_ip() |
| 69 | + port = self.get_exposed_port(self.healthcheck_port) |
| 70 | + return f"http://{ip}:{port}" |
| 71 | + |
| 72 | + @wait_container_is_ready(requests.exceptions.HTTPError) |
| 73 | + def _healthcheck(self) -> None: |
| 74 | + healthcheck_url = self._get_healthcheck_url() |
| 75 | + response = requests.get(f"{healthcheck_url}/healthz", timeout=1) |
| 76 | + response.raise_for_status() |
| 77 | + |
| 78 | + def start(self) -> "MilvusContainer": |
| 79 | + """This method starts the Milvus container and runs the healthcheck |
| 80 | + to verify that the container is ready to use.""" |
| 81 | + self.with_command(self.cmd) |
| 82 | + super().start() |
| 83 | + self._connect() |
| 84 | + self._healthcheck() |
| 85 | + return self |
0 commit comments