Skip to content

Commit 21ec9f8

Browse files
committed
address code review issues
Signed-off-by: Daria Korenieva <daric2612@gmail.com>
1 parent 4b73002 commit 21ec9f8

5 files changed

Lines changed: 43 additions & 38 deletions

File tree

docs/modules/valkey.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Valkey
22

3-
Since testcontainers-python <a href="https://github.com/testcontainers/testcontainers-python/releases/tag/v4.14.0"><span class="tc-version">:material-tag: v4.14.0</span></a>
3+
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>
44

55
## Introduction
66

modules/valkey/example_basic.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
from glide import GlideClient, NodeAddress
1+
"""
2+
Valkey container usage examples with valkey-glide sync client.
3+
4+
Requires: pip install valkey-glide-sync
5+
"""
6+
7+
from glide_sync import GlideClient, GlideClientConfiguration, NodeAddress, ServerCredentials
28

39
from testcontainers.valkey import ValkeyContainer
410

511

612
def basic_example():
713
with ValkeyContainer() as valkey_container:
8-
# Get connection parameters
914
host = valkey_container.get_host()
1015
port = valkey_container.get_exposed_port()
1116
connection_url = valkey_container.get_connection_url()
1217

1318
print(f"Valkey connection URL: {connection_url}")
1419
print(f"Host: {host}, Port: {port}")
1520

16-
# Connect using Glide client
17-
client = GlideClient([NodeAddress(host, port)])
21+
config = GlideClientConfiguration([NodeAddress(host, port)])
22+
client = GlideClient.create(config)
1823

19-
# PING command
2024
pong = client.ping()
2125
print(f"PING response: {pong}")
2226

23-
# SET command
2427
client.set("key", "value")
2528
print("SET response: OK")
2629

27-
# GET command
2830
value = client.get("key")
2931
print(f"GET response: {value}")
3032

@@ -39,35 +41,37 @@ def password_example():
3941

4042
print(f"\nValkey with password connection URL: {connection_url}")
4143

42-
# Connect using Glide client with password
43-
client = GlideClient([NodeAddress(host, port)], password="mypassword")
44+
config = GlideClientConfiguration(
45+
[NodeAddress(host, port)],
46+
credentials=ServerCredentials(password="mypassword"),
47+
)
48+
client = GlideClient.create(config)
4449

45-
# PING after auth
4650
pong = client.ping()
4751
print(f"PING response: {pong}")
4852

4953
client.close()
5054

5155

5256
def version_example():
53-
# Using specific version
5457
with ValkeyContainer().with_image_tag("8.0") as valkey_container:
5558
print(f"\nUsing image: {valkey_container.image}")
5659
connection_url = valkey_container.get_connection_url()
5760
print(f"Connection URL: {connection_url}")
5861

5962

6063
def bundle_example():
61-
# Using bundle with all modules (JSON, Bloom, Search, etc.)
6264
with ValkeyContainer().with_bundle() as valkey_container:
6365
print(f"\nUsing bundle image: {valkey_container.image}")
6466
host = valkey_container.get_host()
6567
port = valkey_container.get_exposed_port()
6668

67-
# Connect using Glide client
68-
client = GlideClient([NodeAddress(host, port)])
69+
config = GlideClientConfiguration([NodeAddress(host, port)])
70+
client = GlideClient.create(config)
71+
6972
pong = client.ping()
7073
print(f"PING response: {pong}")
74+
7175
client.close()
7276

7377

modules/valkey/testcontainers/valkey/__init__.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
1313

14-
from typing import Optional
15-
1614
from testcontainers.core.container import DockerContainer
1715
from testcontainers.core.wait_strategies import ExecWaitStrategy
1816

17+
_BASE_IMAGE = "valkey/valkey"
18+
_BUNDLE_IMAGE = "valkey/valkey-bundle"
19+
1920

2021
class ValkeyContainer(DockerContainer):
2122
"""
2223
Valkey container.
2324
2425
"""
2526

26-
def __init__(self, image: str = "valkey/valkey:latest", port: int = 6379, **kwargs) -> None:
27+
def __init__(self, image: str = f"{_BASE_IMAGE}:latest", port: int = 6379, **kwargs) -> None:
2728
super().__init__(image, **kwargs)
2829
self.port = port
29-
self.password: Optional[str] = None
30+
self.password: str | None = None
3031
self.with_exposed_ports(self.port)
32+
self.waiting_for(ExecWaitStrategy(["valkey-cli", "ping"]))
3133

3234
def with_password(self, password: str) -> "ValkeyContainer":
3335
"""
@@ -41,19 +43,20 @@ def with_password(self, password: str) -> "ValkeyContainer":
4143
"""
4244
self.password = password
4345
self.with_command(["valkey-server", "--requirepass", password])
46+
self.waiting_for(ExecWaitStrategy(["valkey-cli", "-a", password, "ping"]))
4447
return self
4548

4649
def with_image_tag(self, tag: str) -> "ValkeyContainer":
4750
"""
4851
Specify Valkey version.
4952
5053
Args:
51-
tag: Image tag (e.g., '8.0', 'latest', 'bundle:latest').
54+
tag: Image tag (e.g., '8.0', 'latest').
5255
5356
Returns:
5457
self: Container instance for method chaining.
5558
"""
56-
base_image = self.image.split(":")[0]
59+
base_image = self.image.rsplit(":", 1)[0]
5760
self.image = f"{base_image}:{tag}"
5861
return self
5962

@@ -64,7 +67,8 @@ def with_bundle(self) -> "ValkeyContainer":
6467
Returns:
6568
self: Container instance for method chaining.
6669
"""
67-
self.image = self.image.replace("valkey/valkey", "valkey/valkey-bundle")
70+
tag = self.image.rsplit(":", 1)[-1]
71+
self.image = f"{_BUNDLE_IMAGE}:{tag}"
6872
return self
6973

7074
def get_connection_url(self) -> str:
@@ -98,17 +102,3 @@ def get_exposed_port(self) -> int:
98102
"""
99103
return int(super().get_exposed_port(self.port))
100104

101-
def start(self) -> "ValkeyContainer":
102-
"""
103-
Start the container and wait for it to be ready.
104-
105-
Returns:
106-
self: Started container instance.
107-
"""
108-
if self.password:
109-
self.waiting_for(ExecWaitStrategy(["valkey-cli", "-a", self.password, "ping"]))
110-
else:
111-
self.waiting_for(ExecWaitStrategy(["valkey-cli", "ping"]))
112-
113-
super().start()
114-
return self

modules/valkey/tests/test_valkey.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_get_connection_url_with_password():
5454

5555
def test_with_image_tag():
5656
container = ValkeyContainer().with_image_tag("8.0")
57-
assert "valkey/valkey:8.0" in container.image
57+
assert container.image == "valkey/valkey:8.0"
5858

5959

6060
def test_with_bundle():
@@ -67,6 +67,11 @@ def test_with_bundle_and_tag():
6767
assert container.image == "valkey/valkey-bundle:9.0"
6868

6969

70+
def test_with_tag_and_bundle():
71+
container = ValkeyContainer().with_image_tag("8.0").with_bundle()
72+
assert container.image == "valkey/valkey-bundle:8.0"
73+
74+
7075
def test_bundle_starts():
7176
with ValkeyContainer().with_bundle() as valkey:
7277
host = valkey.get_host()

0 commit comments

Comments
 (0)