Skip to content
New issue

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

feat: add Valkey module #252

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -52,6 +52,7 @@ solr = []
surrealdb = []
trufflesuite_ganachecli = []
victoria_metrics = []
valkey = []
zookeeper = []
cockroach_db = []
kwok = []
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,17 @@
/// **surrealdb** (mutli model database) testcontainer
pub mod surrealdb;
#[cfg(feature = "trufflesuite_ganachecli")]
#[cfg_attr(docsrs, doc(cfg(feature = "trufflesuite_ganachecli")))]

Check warning on line 148 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt check

Diff in /home/runner/work/testcontainers-rs-modules-community/testcontainers-rs-modules-community/src/lib.rs
/// **Trufflesuite Ganache CLI** (etherium simulator) testcontainer
pub mod trufflesuite_ganachecli;
#[cfg(feature = "victoria_metrics")]
#[cfg_attr(docsrs, doc(cfg(feature = "victoria_metrics")))]
/// **VictoriaMetrics** (monitoring and time series metrics database) testcontainer
pub mod victoria_metrics;
#[cfg(feature = "valkey")]
#[cfg_attr(docsrs, doc(cfg(feature = "valkey")))]
/// **Valkey** (in memory nosql database) testcontainer
pub mod valkey;

Check warning on line 158 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Rustfmt check

Diff in /home/runner/work/testcontainers-rs-modules-community/testcontainers-rs-modules-community/src/lib.rs
#[cfg(feature = "zookeeper")]
#[cfg_attr(docsrs, doc(cfg(feature = "zookeeper")))]
/// **Apache ZooKeeper** (locking and configuratin management) testcontainer
Expand Down
84 changes: 84 additions & 0 deletions src/valkey/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use testcontainers::core::{ContainerPort, WaitFor};

Check warning on line 1 in src/valkey/mod.rs

View workflow job for this annotation

GitHub Actions / Rustfmt check

Diff in /home/runner/work/testcontainers-rs-modules-community/testcontainers-rs-modules-community/src/valkey/mod.rs
use testcontainers::Image;

const NAME: &str = "valkey/valkey";
const TAG: &str = "8.0.1-alpine";

/// Default port (6379) on which Valkey is exposed
pub const VALKEY_PORT: ContainerPort = ContainerPort::Tcp(6379);

/// Module to work with [`Valkey`] inside of tests.
/// Valkey is a high-performance data structure server that primarily serves key/value workloads.
///
/// Starts an instance of Valkey based on the official [`Valkey docker image`].
///
/// By default, Valkey is exposed on Port 6379 ([`VALKEY_PORT`]), just like Redis, and has no access control.
/// Currently, for communication with Valkey we can still use redis library.
///
/// # Example
/// ```

Check warning on line 19 in src/valkey/mod.rs

View workflow job for this annotation

GitHub Actions / Rustfmt check

Diff in /home/runner/work/testcontainers-rs-modules-community/testcontainers-rs-modules-community/src/valkey/mod.rs
/// use redis::Commands;
/// use testcontainers_modules::{
/// valkey::{Valkey, VALKEY_PORT},
/// testcontainers::runners::SyncRunner,
/// };
///
/// let valkey_instance = Valkey::default().start().unwrap();
/// let host_ip = valkey_instance.get_host().unwrap();
/// let host_port = valkey_instance.get_host_port_ipv4(VALKEY_PORT).unwrap();
///
/// let url = format!("redis://{host_ip}:{host_port}");
/// let client = redis::Client::open(url.as_ref()).unwrap();
/// let mut con = client.get_connection().unwrap();
///
/// con.set::<_, _, ()>("my_key", 42).unwrap();
/// let result: i64 = con.get("my_key").unwrap();
/// ```
///
/// [`Valkey`]: https://valkey.io/
/// [`Valeky docker image`]: https://hub.docker.com/r/valkey/valkey
/// [`VALKEY_PORT`]: super::VALKEY_PORT
#[derive(Debug, Default, Clone)]
pub struct Valkey {
/// (remove if there is another variable)
/// Field is included to prevent this struct to be a unit struct.
/// This allows extending functionality (and thus further variables) without breaking changes
_priv: (),
}

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

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

fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stdout("Ready to accept connections")]
}
}

Check warning on line 62 in src/valkey/mod.rs

View workflow job for this annotation

GitHub Actions / Rustfmt check

Diff in /home/runner/work/testcontainers-rs-modules-community/testcontainers-rs-modules-community/src/valkey/mod.rs
#[cfg(test)]
mod tests {
use crate::{valkey::Valkey, testcontainers::runners::SyncRunner};
use redis::Commands;

#[test]
fn valkey_fetch_an_integer() -> Result<(), Box<dyn std::error::Error + 'static>> {
let _ = pretty_env_logger::try_init();
let node = Valkey::default().start()?;
let host_ip = node.get_host()?;
let host_port = node.get_host_port_ipv4(6379)?;
let url = format!("redis://{host_ip}:{host_port}");

let client = redis::Client::open(url.as_ref()).unwrap();
let mut con = client.get_connection().unwrap();

con.set::<_, _, ()>("my_key", 42).unwrap();
let result: i64 = con.get("my_key").unwrap();
assert_eq!(42, result);
Ok(())

Check warning on line 82 in src/valkey/mod.rs

View workflow job for this annotation

GitHub Actions / Rustfmt check

Diff in /home/runner/work/testcontainers-rs-modules-community/testcontainers-rs-modules-community/src/valkey/mod.rs
}
}
Loading