| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .. autoclass:: testcontainers.cassandra.CassandraContainer | ||
| .. title:: testcontainers.cassandra.CassandraContainer |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # | ||
| # 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.waiting_utils import wait_for_logs | ||
|
|
||
|
|
||
| class CassandraContainer(DockerContainer): | ||
| """ | ||
| Cassandra database container. | ||
|
|
||
| Example: | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> from testcontainers.cassandra import CassandraContainer | ||
| >>> from cassandra.cluster import Cluster, DCAwareRoundRobinPolicy | ||
|
|
||
| >>> with CassandraContainer("cassandra:4.1.4") as cassandra, Cluster( | ||
| ... cassandra.get_contact_points(), | ||
| ... load_balancing_policy=DCAwareRoundRobinPolicy(cassandra.get_local_datacenter()), | ||
| ... ) as cluster: | ||
| ... session = cluster.connect() | ||
| ... result = session.execute("SELECT release_version FROM system.local;") | ||
| ... result.one().release_version | ||
| '4.1.4' | ||
| """ | ||
|
|
||
| CQL_PORT = 9042 | ||
| DEFAULT_LOCAL_DATACENTER = "datacenter1" | ||
|
|
||
| def __init__(self, image: str = "cassandra:latest", **kwargs) -> None: | ||
| super().__init__(image=image, **kwargs) | ||
| self.with_exposed_ports(self.CQL_PORT) | ||
| self.with_env("JVM_OPTS", "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0") | ||
| self.with_env("HEAP_NEWSIZE", "128M") | ||
| self.with_env("MAX_HEAP_SIZE", "1024M") | ||
| self.with_env("CASSANDRA_ENDPOINT_SNITCH", "GossipingPropertyFileSnitch") | ||
| self.with_env("CASSANDRA_DC", self.DEFAULT_LOCAL_DATACENTER) | ||
|
|
||
| def _connect(self): | ||
| wait_for_logs(self, "Startup complete") | ||
|
|
||
| def start(self) -> "CassandraContainer": | ||
| super().start() | ||
| self._connect() | ||
| return self | ||
|
|
||
| def get_contact_points(self) -> list[tuple[str, int]]: | ||
| return [(self.get_container_host_ip(), int(self.get_exposed_port(self.CQL_PORT)))] | ||
|
|
||
| def get_local_datacenter(self) -> str: | ||
| return self.env.get("CASSANDRA_DC", self.DEFAULT_LOCAL_DATACENTER) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from cassandra.cluster import Cluster, DCAwareRoundRobinPolicy | ||
|
|
||
| from testcontainers.cassandra import CassandraContainer | ||
|
|
||
|
|
||
| def test_docker_run_cassandra(): | ||
| with CassandraContainer("cassandra:4.1.4") as cassandra: | ||
| cluster = Cluster( | ||
| cassandra.get_contact_points(), | ||
| load_balancing_policy=DCAwareRoundRobinPolicy(cassandra.get_local_datacenter()), | ||
| ) | ||
| session = cluster.connect() | ||
| result = session.execute("SELECT release_version FROM system.local;") | ||
| assert result.one().release_version == "4.1.4" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWhat is the preferred strategy for the readiness probe ?
I have used wait_for_logs here because it doesn't need to depend on the cassandra driver.
But I see that the Java implementation uses the driver and it gives the opportunity to provide an init script.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.