-
Notifications
You must be signed in to change notification settings - Fork 373
feat(valkey): add Valkey module #947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexanderankin
merged 8 commits into
testcontainers:main
from
daric93:valkey_test_container
Apr 9, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3ec8609
valkey test container
daric93 4b73002
address comments
daric93 21ec9f8
address code review issues
daric93 a0cd2a0
fix formatting in valkey module
daric93 4c9f94d
Merge branch 'main' into valkey_test_container
Tranquility2 8a895cf
address ci failures
daric93 570c6cf
Merge branch 'valkey_test_container' of github.com:daric93/testcontai…
daric93 2108a20
update uv.lock for version 4.15.0-rc.1
daric93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Valkey | ||
|
|
||
| Since testcontainers-python <a href="https://github.com/testcontainers/testcontainers-python/releases/tag/v4.14.3"><span class="tc-version">:material-tag: v4.14.3</span></a> | ||
|
|
||
| ## Introduction | ||
|
|
||
| The Testcontainers module for Valkey. | ||
|
|
||
| ## Adding this module to your project dependencies | ||
|
|
||
| Please run the following command to add the Valkey module to your python dependencies: | ||
|
|
||
| ```bash | ||
| pip install testcontainers[valkey] | ||
| ``` | ||
|
|
||
| ## Usage example | ||
|
|
||
| <!--codeinclude--> | ||
|
|
||
| [Creating a Valkey container](../../modules/valkey/example_basic.py) | ||
|
|
||
| <!--/codeinclude--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .. autoclass:: testcontainers.valkey.ValkeyContainer | ||
| .. title:: testcontainers.valkey.ValkeyContainer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """ | ||
| Valkey container usage examples with valkey-glide sync client. | ||
|
|
||
| Requires: pip install valkey-glide-sync | ||
| """ | ||
|
|
||
| from glide_sync import GlideClient, GlideClientConfiguration, NodeAddress, ServerCredentials | ||
|
|
||
| from testcontainers.valkey import ValkeyContainer | ||
|
|
||
|
|
||
| def basic_example(): | ||
| with ValkeyContainer() as valkey_container: | ||
| host = valkey_container.get_host() | ||
| port = valkey_container.get_exposed_port() | ||
| connection_url = valkey_container.get_connection_url() | ||
|
|
||
| print(f"Valkey connection URL: {connection_url}") | ||
| print(f"Host: {host}, Port: {port}") | ||
|
|
||
| config = GlideClientConfiguration([NodeAddress(host, port)]) | ||
| client = GlideClient.create(config) | ||
|
|
||
| pong = client.ping() | ||
| print(f"PING response: {pong}") | ||
|
|
||
| client.set("key", "value") | ||
| print("SET response: OK") | ||
|
|
||
| value = client.get("key") | ||
| print(f"GET response: {value}") | ||
|
|
||
| client.close() | ||
|
|
||
|
|
||
| def password_example(): | ||
| with ValkeyContainer().with_password("mypassword") as valkey_container: | ||
| host = valkey_container.get_host() | ||
| port = valkey_container.get_exposed_port() | ||
| connection_url = valkey_container.get_connection_url() | ||
|
|
||
| print(f"\nValkey with password connection URL: {connection_url}") | ||
|
|
||
| config = GlideClientConfiguration( | ||
| [NodeAddress(host, port)], | ||
| credentials=ServerCredentials(password="mypassword"), | ||
| ) | ||
| client = GlideClient.create(config) | ||
|
|
||
| pong = client.ping() | ||
| print(f"PING response: {pong}") | ||
|
|
||
| client.close() | ||
|
|
||
|
|
||
| def version_example(): | ||
| with ValkeyContainer().with_image_tag("8.0") as valkey_container: | ||
| print(f"\nUsing image: {valkey_container.image}") | ||
| connection_url = valkey_container.get_connection_url() | ||
| print(f"Connection URL: {connection_url}") | ||
|
|
||
|
|
||
| def bundle_example(): | ||
| with ValkeyContainer().with_bundle() as valkey_container: | ||
| print(f"\nUsing bundle image: {valkey_container.image}") | ||
| host = valkey_container.get_host() | ||
| port = valkey_container.get_exposed_port() | ||
|
|
||
| config = GlideClientConfiguration([NodeAddress(host, port)]) | ||
| client = GlideClient.create(config) | ||
|
|
||
| pong = client.ping() | ||
| print(f"PING response: {pong}") | ||
|
|
||
| client.close() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| basic_example() | ||
| password_example() | ||
| version_example() | ||
| bundle_example() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. You may obtain | ||
| # a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from testcontainers.core.container import DockerContainer | ||
| from testcontainers.core.wait_strategies import ExecWaitStrategy | ||
|
|
||
| _BASE_IMAGE = "valkey/valkey" | ||
| _BUNDLE_IMAGE = "valkey/valkey-bundle" | ||
|
|
||
|
|
||
| class ValkeyContainer(DockerContainer): | ||
| """ | ||
| Valkey container. | ||
|
|
||
| """ | ||
|
|
||
| def __init__(self, image: str = f"{_BASE_IMAGE}:latest", port: int = 6379, **kwargs) -> None: | ||
| super().__init__(image, **kwargs) | ||
| self.port = port | ||
| self.password: str | None = None | ||
| self.with_exposed_ports(self.port) | ||
| self.waiting_for(ExecWaitStrategy(["valkey-cli", "ping"])) | ||
|
|
||
| def with_password(self, password: str) -> "ValkeyContainer": | ||
| """ | ||
| Configure authentication for Valkey. | ||
|
|
||
| Args: | ||
| password: Password for Valkey authentication. | ||
|
|
||
| Returns: | ||
| self: Container instance for method chaining. | ||
| """ | ||
| self.password = password | ||
| self.with_command(["valkey-server", "--requirepass", password]) | ||
| self.waiting_for(ExecWaitStrategy(["valkey-cli", "-a", password, "ping"])) | ||
| return self | ||
|
|
||
| def with_image_tag(self, tag: str) -> "ValkeyContainer": | ||
| """ | ||
| Specify Valkey version. | ||
|
|
||
| Args: | ||
| tag: Image tag (e.g., '8.0', 'latest'). | ||
|
|
||
| Returns: | ||
| self: Container instance for method chaining. | ||
| """ | ||
| base_image = self.image.rsplit(":", 1)[0] | ||
| self.image = f"{base_image}:{tag}" | ||
| return self | ||
|
|
||
| def with_bundle(self) -> "ValkeyContainer": | ||
| """ | ||
| Enable all modules by switching to valkey-bundle image. | ||
|
|
||
| Returns: | ||
| self: Container instance for method chaining. | ||
| """ | ||
| tag = self.image.rsplit(":", 1)[-1] | ||
| self.image = f"{_BUNDLE_IMAGE}:{tag}" | ||
| return self | ||
|
|
||
| def get_connection_url(self) -> str: | ||
| """ | ||
| Get connection URL for Valkey. | ||
|
|
||
| Returns: | ||
| url: Connection URL in format valkey://[:password@]host:port | ||
| """ | ||
| host = self.get_host() | ||
| port = self.get_exposed_port() | ||
| if self.password: | ||
| return f"valkey://:{self.password}@{host}:{port}" | ||
| return f"valkey://{host}:{port}" | ||
|
|
||
| def get_host(self) -> str: | ||
| """ | ||
| Get container host. | ||
|
|
||
| Returns: | ||
| host: Container host IP. | ||
| """ | ||
| return self.get_container_host_ip() | ||
|
|
||
| def get_exposed_port(self) -> int: | ||
| """ | ||
| Get mapped port. | ||
|
|
||
| Returns: | ||
| port: Exposed port number. | ||
| """ | ||
| return int(super().get_exposed_port(self.port)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import socket | ||
|
|
||
| from testcontainers.valkey import ValkeyContainer | ||
|
|
||
|
|
||
| def test_docker_run_valkey(): | ||
| with ValkeyContainer() as valkey: | ||
| host = valkey.get_host() | ||
| port = valkey.get_exposed_port() | ||
|
|
||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
| s.connect((host, port)) | ||
| s.sendall(b"*1\r\n$4\r\nPING\r\n") | ||
| response = s.recv(1024) | ||
| assert b"+PONG" in response | ||
|
|
||
|
|
||
| def test_docker_run_valkey_with_password(): | ||
| with ValkeyContainer().with_password("mypass") as valkey: | ||
| host = valkey.get_host() | ||
| port = valkey.get_exposed_port() | ||
|
|
||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
| s.connect((host, port)) | ||
| # Authenticate | ||
| s.sendall(b"*2\r\n$4\r\nAUTH\r\n$6\r\nmypass\r\n") | ||
| auth_response = s.recv(1024) | ||
| assert b"+OK" in auth_response | ||
|
|
||
| # Test SET command | ||
| s.sendall(b"*3\r\n$3\r\nSET\r\n$5\r\nhello\r\n$5\r\nworld\r\n") | ||
| set_response = s.recv(1024) | ||
| assert b"+OK" in set_response | ||
|
|
||
| # Test GET command | ||
| s.sendall(b"*2\r\n$3\r\nGET\r\n$5\r\nhello\r\n") | ||
| get_response = s.recv(1024) | ||
| assert b"world" in get_response | ||
|
|
||
|
|
||
| def test_get_connection_url(): | ||
| with ValkeyContainer() as valkey: | ||
| url = valkey.get_connection_url() | ||
| assert url.startswith("valkey://") | ||
| assert str(valkey.get_exposed_port()) in url | ||
|
|
||
|
|
||
| def test_get_connection_url_with_password(): | ||
| with ValkeyContainer().with_password("secret") as valkey: | ||
| url = valkey.get_connection_url() | ||
| assert url.startswith("valkey://:secret@") | ||
| assert str(valkey.get_exposed_port()) in url | ||
|
|
||
|
|
||
| def test_with_image_tag(): | ||
| container = ValkeyContainer().with_image_tag("8.0") | ||
| assert container.image == "valkey/valkey:8.0" | ||
|
|
||
|
|
||
| def test_with_bundle(): | ||
| container = ValkeyContainer().with_bundle() | ||
| assert container.image == "valkey/valkey-bundle:latest" | ||
|
|
||
|
|
||
| def test_with_bundle_and_tag(): | ||
| container = ValkeyContainer().with_bundle().with_image_tag("9.0") | ||
| assert container.image == "valkey/valkey-bundle:9.0" | ||
|
|
||
|
|
||
| def test_with_tag_and_bundle(): | ||
| container = ValkeyContainer().with_image_tag("8.0").with_bundle() | ||
| assert container.image == "valkey/valkey-bundle:8.0" | ||
|
|
||
|
|
||
| def test_bundle_starts(): | ||
| with ValkeyContainer().with_bundle() as valkey: | ||
| host = valkey.get_host() | ||
| port = valkey.get_exposed_port() | ||
|
|
||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
| s.connect((host, port)) | ||
| s.sendall(b"*1\r\n$4\r\nPING\r\n") | ||
| response = s.recv(1024) | ||
| assert b"+PONG" in response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
even if this code gets merged now, which im inclined to do to just clear the backlog, this will not survive into v5 which i want to do asap. this is a method that belongs on the docker image datastructure not on container (or even base container)