|
| 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 socket |
| 15 | +from typing import Optional |
| 16 | + |
| 17 | +from testcontainers.core.container import DockerContainer |
| 18 | +from testcontainers.core.waiting_utils import wait_container_is_ready |
| 19 | + |
| 20 | + |
| 21 | +class ValkeyNotReady(Exception): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +class ValkeyContainer(DockerContainer): |
| 26 | + """ |
| 27 | + Valkey container. |
| 28 | +
|
| 29 | + Example: |
| 30 | +
|
| 31 | + .. doctest:: |
| 32 | +
|
| 33 | + >>> from testcontainers.valkey import ValkeyContainer |
| 34 | +
|
| 35 | + >>> with ValkeyContainer() as valkey_container: |
| 36 | + ... connection_url = valkey_container.get_connection_url() |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, image: str = "valkey/valkey:latest", port: int = 6379, **kwargs) -> None: |
| 40 | + super().__init__(image, **kwargs) |
| 41 | + self.port = port |
| 42 | + self.password: Optional[str] = None |
| 43 | + self.with_exposed_ports(self.port) |
| 44 | + |
| 45 | + def with_password(self, password: str) -> "ValkeyContainer": |
| 46 | + """ |
| 47 | + Configure authentication for Valkey. |
| 48 | +
|
| 49 | + Args: |
| 50 | + password: Password for Valkey authentication. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + self: Container instance for method chaining. |
| 54 | + """ |
| 55 | + self.password = password |
| 56 | + self.with_command(f"valkey-server --requirepass {password}") |
| 57 | + return self |
| 58 | + |
| 59 | + def with_image_tag(self, tag: str) -> "ValkeyContainer": |
| 60 | + """ |
| 61 | + Specify Valkey version. |
| 62 | +
|
| 63 | + Args: |
| 64 | + tag: Image tag (e.g., '8.0', 'latest', 'bundle:latest'). |
| 65 | +
|
| 66 | + Returns: |
| 67 | + self: Container instance for method chaining. |
| 68 | + """ |
| 69 | + base_image = self.image.split(":")[0] |
| 70 | + self.image = f"{base_image}:{tag}" |
| 71 | + return self |
| 72 | + |
| 73 | + def with_bundle(self) -> "ValkeyContainer": |
| 74 | + """ |
| 75 | + Enable all modules by switching to valkey-bundle image. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + self: Container instance for method chaining. |
| 79 | + """ |
| 80 | + self.image = self.image.replace("valkey/valkey", "valkey/valkey-bundle") |
| 81 | + return self |
| 82 | + |
| 83 | + def get_connection_url(self) -> str: |
| 84 | + """ |
| 85 | + Get connection URL for Valkey. |
| 86 | +
|
| 87 | + Returns: |
| 88 | + url: Connection URL in format valkey://[:password@]host:port |
| 89 | + """ |
| 90 | + host = self.get_host() |
| 91 | + port = self.get_exposed_port() |
| 92 | + if self.password: |
| 93 | + return f"valkey://:{self.password}@{host}:{port}" |
| 94 | + return f"valkey://{host}:{port}" |
| 95 | + |
| 96 | + def get_host(self) -> str: |
| 97 | + """ |
| 98 | + Get container host. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + host: Container host IP. |
| 102 | + """ |
| 103 | + return self.get_container_host_ip() |
| 104 | + |
| 105 | + def get_exposed_port(self) -> int: |
| 106 | + """ |
| 107 | + Get mapped port. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + port: Exposed port number. |
| 111 | + """ |
| 112 | + return int(super().get_exposed_port(self.port)) |
| 113 | + |
| 114 | + @wait_container_is_ready(ValkeyNotReady) |
| 115 | + def _connect(self) -> None: |
| 116 | + """Wait for Valkey to be ready by sending PING command.""" |
| 117 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 118 | + s.connect((self.get_host(), self.get_exposed_port())) |
| 119 | + if self.password: |
| 120 | + s.sendall(f"*2\r\n$4\r\nAUTH\r\n${len(self.password)}\r\n{self.password}\r\n".encode()) |
| 121 | + auth_response = s.recv(1024) |
| 122 | + if b"+OK" not in auth_response: |
| 123 | + raise ValkeyNotReady("Authentication failed") |
| 124 | + s.sendall(b"*1\r\n$4\r\nPING\r\n") |
| 125 | + response = s.recv(1024) |
| 126 | + if b"+PONG" not in response: |
| 127 | + raise ValkeyNotReady("Valkey not ready yet") |
| 128 | + |
| 129 | + def start(self) -> "ValkeyContainer": |
| 130 | + """ |
| 131 | + Start the container and wait for it to be ready. |
| 132 | +
|
| 133 | + Returns: |
| 134 | + self: Started container instance. |
| 135 | + """ |
| 136 | + super().start() |
| 137 | + self._connect() |
| 138 | + return self |
0 commit comments