|
| 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 | +from testcontainers.core.container import DockerContainer |
| 15 | +from testcontainers.core.wait_strategies import ExecWaitStrategy |
| 16 | + |
| 17 | +_BASE_IMAGE = "valkey/valkey" |
| 18 | +_BUNDLE_IMAGE = "valkey/valkey-bundle" |
| 19 | + |
| 20 | + |
| 21 | +class ValkeyContainer(DockerContainer): |
| 22 | + """ |
| 23 | + Valkey container. |
| 24 | +
|
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, image: str = f"{_BASE_IMAGE}:latest", port: int = 6379, **kwargs) -> None: |
| 28 | + super().__init__(image, **kwargs) |
| 29 | + self.port = port |
| 30 | + self.password: str | None = None |
| 31 | + self.with_exposed_ports(self.port) |
| 32 | + self.waiting_for(ExecWaitStrategy(["valkey-cli", "ping"])) |
| 33 | + |
| 34 | + def with_password(self, password: str) -> "ValkeyContainer": |
| 35 | + """ |
| 36 | + Configure authentication for Valkey. |
| 37 | +
|
| 38 | + Args: |
| 39 | + password: Password for Valkey authentication. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + self: Container instance for method chaining. |
| 43 | + """ |
| 44 | + self.password = password |
| 45 | + self.with_command(["valkey-server", "--requirepass", password]) |
| 46 | + self.waiting_for(ExecWaitStrategy(["valkey-cli", "-a", password, "ping"])) |
| 47 | + return self |
| 48 | + |
| 49 | + def with_image_tag(self, tag: str) -> "ValkeyContainer": |
| 50 | + """ |
| 51 | + Specify Valkey version. |
| 52 | +
|
| 53 | + Args: |
| 54 | + tag: Image tag (e.g., '8.0', 'latest'). |
| 55 | +
|
| 56 | + Returns: |
| 57 | + self: Container instance for method chaining. |
| 58 | + """ |
| 59 | + base_image = self.image.rsplit(":", 1)[0] |
| 60 | + self.image = f"{base_image}:{tag}" |
| 61 | + return self |
| 62 | + |
| 63 | + def with_bundle(self) -> "ValkeyContainer": |
| 64 | + """ |
| 65 | + Enable all modules by switching to valkey-bundle image. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + self: Container instance for method chaining. |
| 69 | + """ |
| 70 | + tag = self.image.rsplit(":", 1)[-1] |
| 71 | + self.image = f"{_BUNDLE_IMAGE}:{tag}" |
| 72 | + return self |
| 73 | + |
| 74 | + def get_connection_url(self) -> str: |
| 75 | + """ |
| 76 | + Get connection URL for Valkey. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + url: Connection URL in format valkey://[:password@]host:port |
| 80 | + """ |
| 81 | + host = self.get_host() |
| 82 | + port = self.get_exposed_port() |
| 83 | + if self.password: |
| 84 | + return f"valkey://:{self.password}@{host}:{port}" |
| 85 | + return f"valkey://{host}:{port}" |
| 86 | + |
| 87 | + def get_host(self) -> str: |
| 88 | + """ |
| 89 | + Get container host. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + host: Container host IP. |
| 93 | + """ |
| 94 | + return self.get_container_host_ip() |
| 95 | + |
| 96 | + def get_exposed_port(self) -> int: |
| 97 | + """ |
| 98 | + Get mapped port. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + port: Exposed port number. |
| 102 | + """ |
| 103 | + return int(super().get_exposed_port(self.port)) |
0 commit comments