testcontainers_modules/weaviate/
mod.rs

1use std::time::Duration;
2
3use testcontainers::{
4    core::{wait::HttpWaitStrategy, ContainerPort, WaitFor},
5    Image,
6};
7
8const NAME: &str = "semitechnologies/weaviate";
9
10const TAG: &str = "1.28.2";
11
12const HTTP_PORT: u16 = 8080;
13const GRPC_PORT: u16 = 50051;
14
15const PORTS: [ContainerPort; 2] = [ContainerPort::Tcp(HTTP_PORT), ContainerPort::Tcp(GRPC_PORT)];
16
17/// Module to work with [`Weaviate`] inside of tests.
18///
19/// Starts an instance of Weaviate based on the official
20/// [Docker image](https://hub.docker.com/r/semitechnologies/weaviate)
21#[derive(Default)]
22pub struct Weaviate {
23    _priv: (),
24}
25
26impl Image for Weaviate {
27    fn name(&self) -> &str {
28        NAME
29    }
30
31    fn tag(&self) -> &str {
32        TAG
33    }
34
35    fn ready_conditions(&self) -> Vec<WaitFor> {
36        vec![WaitFor::http(
37            HttpWaitStrategy::new("/v1/.well-known/ready")
38                .with_poll_interval(Duration::from_millis(100))
39                .with_response_matcher(|resp| resp.status().is_success()),
40        )]
41    }
42
43    fn expose_ports(&self) -> &[ContainerPort] {
44        &PORTS
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use std::error::Error;
51
52    use reqwest::blocking::Client;
53
54    use super::*;
55    use crate::testcontainers::runners::SyncRunner;
56
57    #[test]
58    fn test_connect_simple() -> Result<(), Box<dyn Error>> {
59        let container = Weaviate::default().start()?;
60        let client = Client::new();
61
62        let host = container.get_host()?.to_string();
63        let port = container.get_host_port_ipv4(8080)?;
64        let base_url = format!("http://{host}:{port}");
65
66        let response = client.get(&base_url).send()?;
67
68        assert!(response.status().is_success());
69
70        let schema_url = format!("{base_url}/v1/schema");
71        let schema_response: String = client.get(&schema_url).send()?.text()?;
72
73        assert_eq!(&schema_response, "{\"classes\":[]}\n");
74
75        Ok(())
76    }
77}