|
| 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 typing import TYPE_CHECKING, Optional, override |
| 14 | + |
| 15 | +import requests |
| 16 | + |
| 17 | +from testcontainers.core.container import DockerContainer |
| 18 | +from testcontainers.core.wait_strategies import HttpWaitStrategy |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + from influxdb_client_3 import InfluxDBClient3 |
| 22 | + |
| 23 | + |
| 24 | +class InfluxDb3Container(DockerContainer): |
| 25 | + """ |
| 26 | + Docker container for InfluxDB 3 Core. |
| 27 | + Official Docker images for InfluxDB are hosted at https://hub.docker.com/_/influxdb/. |
| 28 | +
|
| 29 | + Example: |
| 30 | +
|
| 31 | + .. doctest:: |
| 32 | +
|
| 33 | + >>> from testcontainers.influxdb3 import InfluxDb3Container |
| 34 | +
|
| 35 | + >>> with InfluxDb3Container() as influxdb3: |
| 36 | + ... url = influxdb3.get_url() |
| 37 | + """ |
| 38 | + |
| 39 | + INFLUXDB3_PORT = 8181 |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + image: str = "influxdb:3-core", |
| 44 | + container_port: int = INFLUXDB3_PORT, |
| 45 | + host_port: Optional[int] = None, |
| 46 | + is_auth_disabled: bool = False, |
| 47 | + **kw, |
| 48 | + ): |
| 49 | + """ |
| 50 | + Initialize InfluxDB 3 container. |
| 51 | +
|
| 52 | + Args: |
| 53 | + image: Docker image to use. Defaults to ``influxdb:3-core``. |
| 54 | + container_port: Port inside the container. Defaults to ``8181``. |
| 55 | + host_port: Port on the host to bind to. If ``None``, a random port is assigned. |
| 56 | + is_auth_disabled: If ``True``, disables authentication. Defaults to ``False``. |
| 57 | + **kw: Additional keyword arguments passed to ``DockerContainer``. |
| 58 | + """ |
| 59 | + super().__init__(image=image, **kw) |
| 60 | + |
| 61 | + self.container_port: int = container_port |
| 62 | + self.host_port: Optional[int] = host_port |
| 63 | + _ = self.with_bind_ports(self.container_port, self.host_port) |
| 64 | + |
| 65 | + self._is_auth_disabled: bool = is_auth_disabled |
| 66 | + self.token: Optional[str] = None |
| 67 | + |
| 68 | + _ = self.with_command( |
| 69 | + "influxdb3 serve --node-id local01 --object-store file --data-dir /home/influxdb3/.influxdb3" |
| 70 | + ) |
| 71 | + |
| 72 | + if self._is_auth_disabled: |
| 73 | + healthy_status_code = 200 |
| 74 | + _ = self.with_env("INFLUXDB3_START_WITHOUT_AUTH", str(self._is_auth_disabled).lower()) |
| 75 | + else: |
| 76 | + healthy_status_code = 401 |
| 77 | + |
| 78 | + _ = self.waiting_for(HttpWaitStrategy(self.container_port, "/health").for_status_code(healthy_status_code)) |
| 79 | + |
| 80 | + def get_url(self) -> str: |
| 81 | + """ |
| 82 | + Get the URL to connect to the InfluxDB 3 instance. |
| 83 | +
|
| 84 | + Returns: |
| 85 | + The HTTP URL of the running InfluxDB 3 container. |
| 86 | + """ |
| 87 | + host = self.get_container_host_ip() |
| 88 | + port = self.get_exposed_port(self.container_port) |
| 89 | + return f"http://{host}:{port}" |
| 90 | + |
| 91 | + def _create_token(self) -> str: |
| 92 | + url = f"{self.get_url()}/api/v3/configure/token/admin" |
| 93 | + response = requests.post( |
| 94 | + url, headers={"Accept": "application/json", "Content-Type": "application/json"}, timeout=10 |
| 95 | + ) |
| 96 | + response.raise_for_status() |
| 97 | + return response.json()["token"] |
| 98 | + |
| 99 | + def get_client(self, database: str, **kw) -> "InfluxDBClient3": |
| 100 | + """ |
| 101 | + Get an InfluxDB 3 client connected to this container. |
| 102 | +
|
| 103 | + Args: |
| 104 | + database: Database name to connect to. |
| 105 | + **kw: Additional keyword arguments passed to ``InfluxDBClient3``. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + An ``InfluxDBClient3`` instance connected to this container. |
| 109 | + """ |
| 110 | + from influxdb_client_3 import InfluxDBClient3 |
| 111 | + |
| 112 | + return InfluxDBClient3( |
| 113 | + host=self.get_url(), |
| 114 | + token=self.token, |
| 115 | + database=database, |
| 116 | + **kw, |
| 117 | + ) |
| 118 | + |
| 119 | + @override |
| 120 | + def start(self) -> "InfluxDb3Container": |
| 121 | + """ |
| 122 | + Start the InfluxDB 3 container and initialize authentication. |
| 123 | +
|
| 124 | + Returns: |
| 125 | + The started ``InfluxDb3Container`` instance. |
| 126 | + """ |
| 127 | + _ = super().start() |
| 128 | + if not self._is_auth_disabled: |
| 129 | + self.token = self._create_token() |
| 130 | + return self |
0 commit comments