|
| 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 | +from testcontainers.core.container import DockerContainer |
| 14 | +from testcontainers.core.waiting_utils import wait_for_logs |
| 15 | + |
| 16 | + |
| 17 | +class CassandraContainer(DockerContainer): |
| 18 | + """ |
| 19 | + Cassandra database container. |
| 20 | +
|
| 21 | + Example: |
| 22 | +
|
| 23 | + .. doctest:: |
| 24 | +
|
| 25 | + >>> from testcontainers.cassandra import CassandraContainer |
| 26 | + >>> from cassandra.cluster import Cluster, DCAwareRoundRobinPolicy |
| 27 | +
|
| 28 | + >>> with CassandraContainer("cassandra:4.1.4") as cassandra, Cluster( |
| 29 | + ... cassandra.get_contact_points(), |
| 30 | + ... load_balancing_policy=DCAwareRoundRobinPolicy(cassandra.get_local_datacenter()), |
| 31 | + ... ) as cluster: |
| 32 | + ... session = cluster.connect() |
| 33 | + ... result = session.execute("SELECT release_version FROM system.local;") |
| 34 | + ... result.one().release_version |
| 35 | + '4.1.4' |
| 36 | + """ |
| 37 | + |
| 38 | + CQL_PORT = 9042 |
| 39 | + DEFAULT_LOCAL_DATACENTER = "datacenter1" |
| 40 | + |
| 41 | + def __init__(self, image: str = "cassandra:latest", **kwargs) -> None: |
| 42 | + super().__init__(image=image, **kwargs) |
| 43 | + self.with_exposed_ports(self.CQL_PORT) |
| 44 | + self.with_env("JVM_OPTS", "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0") |
| 45 | + self.with_env("HEAP_NEWSIZE", "128M") |
| 46 | + self.with_env("MAX_HEAP_SIZE", "1024M") |
| 47 | + self.with_env("CASSANDRA_ENDPOINT_SNITCH", "GossipingPropertyFileSnitch") |
| 48 | + self.with_env("CASSANDRA_DC", self.DEFAULT_LOCAL_DATACENTER) |
| 49 | + |
| 50 | + def _connect(self): |
| 51 | + wait_for_logs(self, "Startup complete") |
| 52 | + |
| 53 | + def start(self) -> "CassandraContainer": |
| 54 | + super().start() |
| 55 | + self._connect() |
| 56 | + return self |
| 57 | + |
| 58 | + def get_contact_points(self) -> list[tuple[str, int]]: |
| 59 | + return [(self.get_container_host_ip(), int(self.get_exposed_port(self.CQL_PORT)))] |
| 60 | + |
| 61 | + def get_local_datacenter(self) -> str: |
| 62 | + return self.env.get("CASSANDRA_DC", self.DEFAULT_LOCAL_DATACENTER) |
0 commit comments