diff --git a/Cargo.toml b/Cargo.toml index 612b672..7f84c41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ postgres = [] rabbitmq = [] redis = [] trufflesuite_ganachecli = [] +victoria_metrics = [] zookeeper = [] [dependencies] @@ -52,7 +53,7 @@ postgres = "0.19.7" pretty_env_logger = "0.5.0" rdkafka = "0.36.0" redis = "0.24.0" -reqwest = { version = "0.11.20", features = ["blocking"] } +reqwest = { version = "0.11.20", features = ["blocking", "json"] } serde = { version = "1.0.188", features = [ "derive" ] } serde_json = "1.0.107" tokio = { version = "1", features = ["macros"] } diff --git a/src/lib.rs b/src/lib.rs index 8601817..98475c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,9 @@ pub mod redis; #[cfg(feature = "trufflesuite_ganachecli")] #[cfg_attr(docsrs, doc(cfg(feature = "trufflesuite_ganachecli")))] pub mod trufflesuite_ganachecli; +#[cfg(feature = "victoria_metrics")] +#[cfg_attr(docsrs, doc(cfg(feature = "victoria_metrics")))] +pub mod victoria_metrics; #[cfg(feature = "zookeeper")] #[cfg_attr(docsrs, doc(cfg(feature = "zookeeper")))] pub mod zookeeper; diff --git a/src/victoria_metrics/mod.rs b/src/victoria_metrics/mod.rs new file mode 100644 index 0000000..16e0573 --- /dev/null +++ b/src/victoria_metrics/mod.rs @@ -0,0 +1,73 @@ +use testcontainers::{core::WaitFor, Image}; + +const NAME: &str = "victoriametrics/victoria-metrics"; +const TAG: &str = "v1.96.0"; + +/// Module to work with [`VictoriaMetrics`] inside of tests. +/// +/// Starts an instance of single-node VictoriaMetrics. +/// +/// This module is based on the official [`VictoriaMetrics docker image`]. +/// +/// # Example +/// ``` +/// use testcontainers::clients; +/// use testcontainers_modules::victoria_metrics; +/// +/// let docker = clients::Cli::default(); +/// let victoria_metrics_instance = docker.run(victoria_metrics::VictoriaMetrics); +/// +/// let import_url = format!("http://127.0.0.1:{}/api/v1/import", victoria_metrics_instance.get_host_port_ipv4(8428)); +/// let export_url = format!("http://127.0.0.1:{}/api/v1/export", victoria_metrics_instance.get_host_port_ipv4(8428)); +/// +/// // operate on the import and export URLs.. +/// ``` +/// +/// [`VictoriaMetrics`]: https://docs.victoriametrics.com/ +/// [`VictoriaMetrics API examples`]: https://docs.victoriametrics.com/url-examples.html#victoriametrics-api-examples +/// [`VictoriaMetrics Docker image`]: https://hub.docker.com/r/victoriametrics/victoria-metrics +#[derive(Debug, Default)] +pub struct VictoriaMetrics; + +impl Image for VictoriaMetrics { + type Args = (); + + fn name(&self) -> String { + NAME.to_owned() + } + + fn tag(&self) -> String { + TAG.to_owned() + } + + fn ready_conditions(&self) -> Vec { + vec![ + WaitFor::message_on_stderr("started VictoriaMetrics"), + WaitFor::message_on_stderr( + "pprof handlers are exposed at http://127.0.0.1:8428/debug/pprof/", + ), + ] + } +} + +#[cfg(test)] +mod tests { + use crate::victoria_metrics::VictoriaMetrics as VictoriaMetricsImage; + use testcontainers::clients; + + #[test] + fn query_buildinfo() { + let docker = clients::Cli::default(); + let node = docker.run(VictoriaMetricsImage); + let host_port = node.get_host_port_ipv4(8428); + let url = format!("http://127.0.0.1:{}/api/v1/status/buildinfo", host_port); + + let response = reqwest::blocking::get(url) + .unwrap() + .json::() + .unwrap(); + let version = response["data"]["version"].as_str().unwrap(); + + assert_eq!(version, "2.24.0"); + } +}