|
10 | 10 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
11 | 11 | # License for the specific language governing permissions and limitations |
12 | 12 | # under the License. |
13 | | - |
14 | 13 | """ |
15 | | -testcontainers/influxdb provides means to spawn an InfluxDB instance within a Docker container. |
| 14 | +testcontainers.community.influxdb provides means to spawn an InfluxDB instance within a Docker container. |
16 | 15 |
|
17 | | -- this influxdb.py module provides the common mechanism to spawn an InfluxDB container. |
| 16 | +- The InfluxDbContainer provides the common mechanism to spawn an InfluxDB container. |
18 | 17 | You are not likely to use this module directly. |
19 | | -- import the InfluxDb1Container class from the influxdb1/__init__.py module to spawn |
20 | | - a container for an InfluxDB 1.x instance |
21 | | -- import the InfluxDb2Container class from the influxdb2/__init__.py module to spawn |
22 | | - a container for an InfluxDB 2.x instance |
| 18 | +- Import the InfluxDb1Container class to spawn a container for an InfluxDB 1.x instance |
| 19 | +- Import the InfluxDb2Container class to spawn a container for an InfluxDB 2.x instance |
23 | 20 |
|
24 | 21 | The 2 containers are separated in different modules for 2 reasons: |
25 | 22 | - because the Docker images are not designed to be used in the same way |
26 | 23 | - because the InfluxDB clients are different for 1.x and 2.x versions, |
27 | 24 | so you won't have to install dependencies that you do not need |
28 | 25 | """ |
29 | 26 |
|
30 | | -from typing import Optional |
31 | | - |
32 | | -from requests import get |
33 | | -from requests.exceptions import ConnectionError, ReadTimeout |
34 | | - |
35 | | -from testcontainers.core.container import DockerContainer |
36 | | -from testcontainers.core.waiting_utils import wait_container_is_ready |
37 | | - |
38 | | - |
39 | | -class InfluxDbContainer(DockerContainer): |
40 | | - """ |
41 | | - Abstract class for Docker containers of InfluxDB v1 and v2. |
42 | | -
|
43 | | - Concrete implementations for InfluxDB 1.x and 2.x are separated iun different packages |
44 | | - because their respective clients rely on different Python libraries which we don't want |
45 | | - to import at the same time. |
46 | | - """ |
47 | | - |
48 | | - def __init__( |
49 | | - self, |
50 | | - # Docker image name |
51 | | - image: str, |
52 | | - # in the container, the default port for influxdb is often 8086 and not likely to change |
53 | | - container_port: int = 8086, |
54 | | - # specifies the port on the host machine where influxdb is exposed; a random available port otherwise |
55 | | - host_port: Optional[int] = None, |
56 | | - **docker_client_kw, |
57 | | - ) -> None: |
58 | | - super().__init__(image=image, **docker_client_kw) |
59 | | - self.container_port = container_port |
60 | | - self.host_port = host_port |
61 | | - self.with_bind_ports(self.container_port, self.host_port) |
62 | | - |
63 | | - def get_url(self) -> str: |
64 | | - """ |
65 | | - Returns the url to interact with the InfluxDB container (health check, REST API, etc.) |
66 | | - """ |
67 | | - host = self.get_container_host_ip() |
68 | | - port = self.get_exposed_port(self.container_port) |
69 | | - |
70 | | - return f"http://{host}:{port}" |
71 | | - |
72 | | - @wait_container_is_ready(ConnectionError, ReadTimeout) |
73 | | - def _health_check(self) -> dict: |
74 | | - """ |
75 | | - Performs a health check on the running InfluxDB container. |
76 | | - The call is retried until it works thanks to the @wait_container_is_ready decorator. |
77 | | - See its documentation for the max number of retries or the timeout. |
78 | | - """ |
79 | | - |
80 | | - url = self.get_url() |
81 | | - response = get(f"{url}/health", timeout=1) |
82 | | - response.raise_for_status() |
83 | | - |
84 | | - return response.json() |
85 | | - |
86 | | - def get_influxdb_version(self) -> str: |
87 | | - """ |
88 | | - Returns the version of the InfluxDB service, as returned by the healthcheck. |
89 | | - """ |
90 | | - |
91 | | - return self._health_check().get("version") |
92 | | - |
93 | | - def start(self) -> "InfluxDbContainer": |
94 | | - """ |
95 | | - Spawns a container of the InfluxDB Docker image, ready to be used. |
96 | | - """ |
97 | | - super().start() |
98 | | - self._health_check() |
| 27 | +from .base import InfluxDbContainer |
| 28 | +from .version1 import InfluxDb1Container |
| 29 | +from .version2 import InfluxDb2Container |
99 | 30 |
|
100 | | - return self |
| 31 | +__all__ = ["InfluxDb1Container", "InfluxDb2Container", "InfluxDbContainer"] |
0 commit comments