Skip to content

Commit 2b238a2

Browse files
port cassandra from tc-java, fix small closing bug in both test
1 parent 1114f4d commit 2b238a2

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

modules/cassandra/testcontainers/cassandra/__init__.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from testcontainers.core.config import MAX_TRIES
22
from testcontainers.core.generic import DockerContainer
3-
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
3+
from testcontainers.core.waiting_utils import wait_for_logs
44

55

66
class CassandraContainer(DockerContainer):
@@ -21,26 +21,30 @@ class CassandraContainer(DockerContainer):
2121
... "{'class': 'SimpleStrategy', 'replication_factor': '1'};")
2222
"""
2323

24-
def __init__(self, image="rinscy/cassandra:latest", ports_to_expose=(9042,)):
24+
def __init__(self, image="cassandra:latest", ports_to_expose=(9042,)):
2525
super().__init__(image)
2626
self.ports_to_expose = ports_to_expose
2727
self.with_exposed_ports(*self.ports_to_expose)
2828

29-
@wait_container_is_ready()
30-
def _connect(self):
31-
wait_for_logs(self, predicate="Starting listening for CQL clients", timeout=MAX_TRIES)
32-
cluster = self.get_cluster()
33-
cluster.connect()
29+
self.with_env("CASSANDRA_SNITCH", "GossipingPropertyFileSnitch")
30+
self.with_env("CASSANDRA_ENDPOINT_SNITCH", "GossipingPropertyFileSnitch")
31+
self.with_env("JVM_OPTS",
32+
"-Dcassandra.skip_wait_for_gossip_to_settle=0 "
33+
"-Dcassandra.initial_token=0")
34+
self.with_env("HEAP_NEWSIZE", "128M")
35+
self.with_env("MAX_HEAP_SIZE", "1024M")
36+
self.with_env("CASSANDRA_DC", "datacenter1")
3437

3538
def start(self):
3639
super().start()
37-
self._connect()
40+
wait_for_logs(self, predicate="Starting listening for CQL clients", timeout=MAX_TRIES)
3841
return self
3942

4043
def get_cluster(self, **kwargs):
4144
from cassandra.cluster import Cluster
4245

4346
container = self.get_wrapped_container()
4447
container.reload()
45-
hostname = container.attrs["NetworkSettings"]["IPAddress"]
46-
return Cluster(contact_points=[hostname], **kwargs)
48+
hostname = self.get_container_host_ip()
49+
port = self.get_exposed_port(9042)
50+
return Cluster(contact_points=[hostname], port=port, **kwargs)

modules/cassandra/tests/test_cassandra.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import pytest
2+
13
from testcontainers.cassandra import CassandraContainer
24

35

4-
def test_docker_run_cassandra():
5-
with CassandraContainer() as cassandra:
6-
cluster = cassandra.get_cluster()
7-
with cluster.connect() as session:
6+
@pytest.mark.parametrize("version", ["3.11.6", "4.1.4"], ids=["3.11.6", "4.1.4"])
7+
def test_docker_run_cassandra(version):
8+
with CassandraContainer(image=f"cassandra:{version}") as cassandra:
9+
with cassandra.get_cluster() as cluster, cluster.connect() as session:
810
session.execute(
911
"CREATE KEYSPACE keyspace1 WITH replication = "
1012
"{'class': 'SimpleStrategy', 'replication_factor': '1'};"

modules/scylla/tests/test_scylla.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
def test_docker_run_scylla():
55
with ScyllaContainer() as scylla:
6-
cluster = scylla.get_cluster()
7-
with cluster.connect() as session:
6+
with scylla.get_cluster() as cluster, cluster.connect() as session:
87
session.execute(
98
"CREATE KEYSPACE keyspace1 WITH replication = "
109
"{'class': 'SimpleStrategy', 'replication_factor': '1'};"

0 commit comments

Comments
 (0)