testcontainers_modules/victoria_metrics/
mod.rs

1use testcontainers::{core::WaitFor, Image};
2
3const NAME: &str = "victoriametrics/victoria-metrics";
4const TAG: &str = "v1.96.0";
5
6/// Module to work with [`VictoriaMetrics`] inside of tests.
7///
8/// Starts an instance of single-node VictoriaMetrics.
9///
10/// This module is based on the official [`VictoriaMetrics docker image`].
11///
12/// # Example
13/// ```
14/// use testcontainers_modules::{testcontainers::runners::SyncRunner, victoria_metrics};
15///
16/// let victoria_metrics_instance = victoria_metrics::VictoriaMetrics::default()
17///     .start()
18///     .unwrap();
19///
20/// let import_url = format!(
21///     "http://127.0.0.1:{}/api/v1/import",
22///     victoria_metrics_instance.get_host_port_ipv4(8428).unwrap()
23/// );
24/// let export_url = format!(
25///     "http://127.0.0.1:{}/api/v1/export",
26///     victoria_metrics_instance.get_host_port_ipv4(8428).unwrap()
27/// );
28///
29/// // operate on the import and export URLs..
30/// ```
31///
32/// [`VictoriaMetrics`]: https://docs.victoriametrics.com/
33/// [`VictoriaMetrics API examples`]: https://docs.victoriametrics.com/url-examples.html#victoriametrics-api-examples
34/// [`VictoriaMetrics Docker image`]: https://hub.docker.com/r/victoriametrics/victoria-metrics
35#[derive(Debug, Default, Clone)]
36pub struct VictoriaMetrics {
37    /// (remove if there is another variable)
38    /// Field is included to prevent this struct to be a unit struct.
39    /// This allows extending functionality (and thus further variables) without breaking changes
40    _priv: (),
41}
42
43impl Image for VictoriaMetrics {
44    fn name(&self) -> &str {
45        NAME
46    }
47
48    fn tag(&self) -> &str {
49        TAG
50    }
51
52    fn ready_conditions(&self) -> Vec<WaitFor> {
53        vec![
54            WaitFor::message_on_stderr("started VictoriaMetrics"),
55            WaitFor::message_on_stderr(
56                "pprof handlers are exposed at http://127.0.0.1:8428/debug/pprof/",
57            ),
58        ]
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use crate::{
65        testcontainers::runners::SyncRunner,
66        victoria_metrics::VictoriaMetrics as VictoriaMetricsImage,
67    };
68
69    #[test]
70    fn query_buildinfo() -> Result<(), Box<dyn std::error::Error + 'static>> {
71        let node = VictoriaMetricsImage::default().start()?;
72        let host_ip = node.get_host()?;
73        let host_port = node.get_host_port_ipv4(8428)?;
74        let url = format!("http://{host_ip}:{host_port}/api/v1/status/buildinfo");
75
76        let response = reqwest::blocking::get(url)
77            .unwrap()
78            .json::<serde_json::Value>()
79            .unwrap();
80        let version = response["data"]["version"].as_str().unwrap();
81
82        assert_eq!(version, "2.24.0");
83        Ok(())
84    }
85}