From 460a955eca3d49c626547ea03e966b8424522862 Mon Sep 17 00:00:00 2001 From: ivanvs Date: Sat, 14 Dec 2024 18:16:43 +0100 Subject: [PATCH 1/2] feat: Add Valkey module --- Cargo.toml | 1 + src/lib.rs | 4 +++ src/valkey/mod.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/valkey/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 1026948..3e3d2a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ solr = [] surrealdb = [] trufflesuite_ganachecli = [] victoria_metrics = [] +valkey = [] zookeeper = [] cockroach_db = [] kwok = [] diff --git a/src/lib.rs b/src/lib.rs index 61a0056..02dd7ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -152,6 +152,10 @@ pub mod trufflesuite_ganachecli; #[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; #[cfg(feature = "zookeeper")] #[cfg_attr(docsrs, doc(cfg(feature = "zookeeper")))] /// **Apache ZooKeeper** (locking and configuratin management) testcontainer diff --git a/src/valkey/mod.rs b/src/valkey/mod.rs new file mode 100644 index 0000000..d06765f --- /dev/null +++ b/src/valkey/mod.rs @@ -0,0 +1,84 @@ +use testcontainers::core::{ContainerPort, WaitFor}; +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 +/// ``` +/// 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 { + vec![WaitFor::message_on_stdout("Ready to accept connections")] + } +} + +#[cfg(test)] +mod tests { + use crate::{valkey::Valkey, testcontainers::runners::SyncRunner}; + use redis::Commands; + + #[test] + fn valkey_fetch_an_integer() -> Result<(), Box> { + 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(()) + } +} \ No newline at end of file From 68de271d79937a1eb752f0c780ea2095f0cee62f Mon Sep 17 00:00:00 2001 From: ivanvs Date: Mon, 16 Dec 2024 21:12:29 +0100 Subject: [PATCH 2/2] fix: format code properly --- src/lib.rs | 8 ++++---- src/valkey/mod.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 02dd7ef..fd02157 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,14 +148,14 @@ pub mod surrealdb; #[cfg_attr(docsrs, doc(cfg(feature = "trufflesuite_ganachecli")))] /// **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; +#[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 = "zookeeper")] #[cfg_attr(docsrs, doc(cfg(feature = "zookeeper")))] /// **Apache ZooKeeper** (locking and configuratin management) testcontainer diff --git a/src/valkey/mod.rs b/src/valkey/mod.rs index d06765f..6fc35b9 100644 --- a/src/valkey/mod.rs +++ b/src/valkey/mod.rs @@ -62,7 +62,7 @@ impl Image for Valkey { #[cfg(test)] mod tests { - use crate::{valkey::Valkey, testcontainers::runners::SyncRunner}; + use crate::{testcontainers::runners::SyncRunner, valkey::Valkey}; use redis::Commands; #[test] @@ -81,4 +81,4 @@ mod tests { assert_eq!(42, result); Ok(()) } -} \ No newline at end of file +}