Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ watchdog = ["testcontainers/watchdog"]
json = ["serde", "serde_json"]
anvil = []
arrow_flightsql = []
cassandra = []
clickhouse = ["http_wait"]
cratedb = []
cncf_distribution = []
Expand Down
85 changes: 85 additions & 0 deletions src/cassandra/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use testcontainers::{core::WaitFor, Image};

const NAME: &str = "cassandra";
const TAG: &str = "5.0.6";

/// Module to work with [`Cassandra`] inside of tests.
///
/// This module is based on the official [`Cassandra docker image`].
///
/// # Example
/// ```
/// use scylla::client::{session::Session, session_builder::SessionBuilder};
/// use std::time::Duration;
/// use testcontainers::{runners::AsyncRunner, ImageExt};
///
/// #[tokio::test]
/// async fn default_cassandra() -> Result<(), Box<dyn std::error::Error + 'static>> {
/// let image = ScyllaDB::default();
/// let instance = image.start().await?;
/// let host = instance.get_host().await?;
/// let port = instance.get_host_port_ipv4(9042).await?;
/// let hostname = format!("{host}:{port}");
/// let session: Session = SessionBuilder::new().known_node(hostname).build().await?;
///
/// let prepared_statement = session
/// .prepare("SELECT release_version FROM system.local")
/// .await?;
/// let rows = session
/// .execute_unpaged(&prepared_statement, &[])
/// .await?
/// .into_rows_result()?;
/// let (version,) = rows.single_row::<(String,)>()?;
/// assert_eq!(version, "5.0.6");
/// Ok(())
/// }
/// ```
///
/// [`Cassandra`]: https://cassandra.apache.org
/// [`Cassandra docker image`]: https://hub.docker.com/_/cassandra
#[derive(Default, Clone, Debug)]
pub struct Cassandra {}

impl Image for Cassandra {
fn name(&self) -> &str {
NAME
}

fn tag(&self) -> &str {
TAG
}

fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_either_std("Startup complete")]
}
}

#[cfg(test)]
mod tests {
use scylla::client::{session::Session, session_builder::SessionBuilder};
use std::time::Duration;
use testcontainers::{runners::AsyncRunner, ImageExt};

use super::*;

#[tokio::test]
async fn cassandra_select_version() -> Result<(), Box<dyn std::error::Error + 'static>> {
let image = Cassandra::default().with_startup_timeout(Duration::from_secs(240));
let instance = image.start().await?;
let host = instance.get_host().await?;
let port = instance.get_host_port_ipv4(9042).await?;
let hostname = format!("{host}:{port}");
let session: Session = SessionBuilder::new().known_node(hostname).build().await?;

let prepared_statement = session
.prepare("SELECT release_version FROM system.local")
.await?;
let rows = session
.execute_unpaged(&prepared_statement, &[])
.await?
.into_rows_result()?;
let (version,) = rows.single_row::<(String,)>()?;
assert_eq!(version, "5.0.6");
Ok(())
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub mod arrow_flightsql;
#[cfg_attr(docsrs, doc(cfg(feature = "azurite")))]
/// **Azurite** (azure storage emulator) testcontainer
pub mod azurite;
#[cfg(feature = "cassandra")]
#[cfg_attr(docsrs, doc(cfg(feature = "cassandra")))]
/// **Cassandra** (distributed NoSQL wide-column data store) testcontainer
pub mod cassandra;
#[cfg(feature = "clickhouse")]
#[cfg_attr(docsrs, doc(cfg(feature = "clickhouse")))]
/// **Clickhouse** (analytics database) testcontainer
Expand Down