testcontainers_modules/scylladb/
mod.rs

1use testcontainers::{core::WaitFor, Image};
2
3const NAME: &str = "scylladb/scylla";
4const TAG: &str = "2025.1.0";
5
6/// Module to work with [`ScyllaDB`] inside of tests.
7///
8/// This module is based on the official [`ScyllaDB docker image`].
9///
10/// # Example
11/// ```
12/// use scylla::client::{session::Session, session_builder::SessionBuilder};
13/// use testcontainers_modules::{scylladb::ScyllaDB, testcontainers::runners::AsyncRunner};
14///
15/// #[tokio::test]
16/// async fn default_scylladb() -> Result<(), Box<dyn std::error::Error + 'static>> {
17///     let image = ScyllaDB::default();
18///     let instance = image.start().await?;
19///     let host = instance.get_host().await?;
20///     let port = instance.get_host_port_ipv4(9042).await?;
21///     let hostname = format!("{host}:{port}");
22///     let session: Session = SessionBuilder::new().known_node(hostname).build().await?;
23///
24///     let prepared_statement = session
25///         .prepare("SELECT release_version FROM system.local")
26///         .await?;
27///     let rows = session
28///         .execute_unpaged(&prepared_statement, &[])
29///         .await?
30///         .into_rows_result()?;
31///     let (version,) = rows.single_row::<(String,)>()?;
32///     assert_eq!(version, "3.0.8");
33///     Ok(())
34/// }
35/// ```
36///
37/// [`ScyllaDB`]: https://www.scylladb.com/
38/// [`ScyllaDB docker image`]: https://hub.docker.com/r/scylladb/scylla
39#[derive(Clone, Debug, Default)]
40pub struct ScyllaDB {}
41
42impl Image for ScyllaDB {
43    fn name(&self) -> &str {
44        NAME
45    }
46
47    fn tag(&self) -> &str {
48        TAG
49    }
50
51    fn ready_conditions(&self) -> Vec<WaitFor> {
52        vec![WaitFor::message_on_stderr("init - serving")]
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use scylla::client::{session::Session, session_builder::SessionBuilder};
59    use testcontainers::runners::AsyncRunner;
60
61    use super::*;
62
63    #[tokio::test]
64    async fn scylladb_select_version() -> Result<(), Box<dyn std::error::Error + 'static>> {
65        let image = ScyllaDB::default();
66        let instance = image.start().await?;
67        let host = instance.get_host().await?;
68        let port = instance.get_host_port_ipv4(9042).await?;
69        let hostname = format!("{host}:{port}");
70        let session: Session = SessionBuilder::new().known_node(hostname).build().await?;
71
72        let prepared_statement = session
73            .prepare("SELECT release_version FROM system.local")
74            .await?;
75        let rows = session
76            .execute_unpaged(&prepared_statement, &[])
77            .await?
78            .into_rows_result()?;
79        let (version,) = rows.single_row::<(String,)>()?;
80        assert_eq!(version, "3.0.8");
81        Ok(())
82    }
83}