omry_integration/
flora_stack.rs

1use anyhow::{Context, Result};
2use omry_service::tarpc::client;
3use omry_service::tarpc::tokio_serde::formats::Bincode;
4use omry_service::{FloraServiceClient, tarpc};
5use std::env::consts;
6use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
7use std::time::Duration;
8use testcontainers::Image;
9use tracing::{Level, instrument};
10
11use crate::containers::{ContainerConfig, ImageConfig, ImageExtConfig, get_bridge_gateway_ip};
12use crate::flora_image::{FloraConfig, FloraContainer, start_flora_container};
13use crate::typesense::TestTypesense;
14
15/// Default tracing level for the flora test server.
16const DEFAULT_TRACING_LEVEL: Level = Level::DEBUG;
17
18/// Default interval at which the flora test server will sync new records to Typesense.
19const DEFAULT_SYNC_INTERVAL: Duration = Duration::from_secs(10);
20
21/// Value that corresponds to the host machine in Docker.
22const DOCKER_HOST_INTERNAL: &str = "host.docker.internal";
23
24/// Optional parameters for [`FloraStack`].
25#[derive(Debug, Clone)]
26pub struct FloraStackOptions {
27    /// The tracing level to set for the test flora server.
28    ///
29    /// Corresponds to `FLORA_TRACING_LEVEL`.
30    pub tracing_level: Level,
31
32    /// The interval at which the test flora server will sync
33    /// new records to Typesense.
34    ///
35    /// Corresponds to `FLORA_SEARCH_SYNC_INTERVAL_SECS`.
36    pub search_sync_interval: Duration,
37}
38
39impl Default for FloraStackOptions {
40    fn default() -> Self {
41        Self {
42            tracing_level: DEFAULT_TRACING_LEVEL,
43            search_sync_interval: DEFAULT_SYNC_INTERVAL,
44        }
45    }
46}
47
48/// Flora stack (flora server and Typesense), running in testcontainers, for integration testing.
49#[derive(Debug)]
50pub struct FloraStack {
51    flora_server: FloraContainer,
52    typesense: TestTypesense,
53}
54
55impl FloraStack {
56    /// Creates a new instance of the Omry stack.
57    ///
58    /// ## Errors
59    /// Returns [`anyhow::Error`] if we can't:
60    ///
61    /// - Start the Typesense container
62    /// - Get the client config to connect to Typesense
63    /// - Map the Typesense host to Docker's platform-specific localhost address (this differs between Linux and macOS)
64    /// - Start the Omry container
65    #[instrument]
66    pub async fn new(stack_options: Option<FloraStackOptions>) -> Result<Self> {
67        let typesense_img_ext_conf = ImageExtConfig::default();
68        let typesense_con_conf =
69            ContainerConfig::new(ImageConfig::default(), typesense_img_ext_conf);
70        let typesense = TestTypesense::new(typesense_con_conf)
71            .await
72            .map_err(|error| {
73                anyhow::anyhow!("Couldn't start the test Typesense container: {error}")
74            })?;
75
76        let client_config = typesense
77            .make_client_config()
78            .await
79            .map_err(|error| anyhow::anyhow!("{error}"))?;
80        let Some(mut typesense_url) = client_config.url else {
81            anyhow::bail!("Test Typesense client config doesn't have a URL value.");
82        };
83
84        // Workaround for connecting within docker.
85        let remapped_host = match typesense_url.host_str() {
86            Some("127.0.0.1" | "localhost") => match consts::OS {
87                "macos" => Some(DOCKER_HOST_INTERNAL.to_string()),
88                "linux" => Some(get_bridge_gateway_ip().await?),
89                os => {
90                    tracing::warn!("your OS ({os}) may be unsupported for docker tests");
91                    None
92                }
93            },
94            _ => None,
95        };
96
97        if let Some(host) = remapped_host {
98            typesense_url.set_host(Some(&host))?;
99        }
100
101        let options = stack_options.unwrap_or_default();
102        let flora_conf = FloraConfig::new(&typesense_url, client_config.api_key)
103            .with_tracing_level(options.tracing_level);
104
105        let flora_image_conf = ImageConfig::default();
106        let flora_image_ext_conf = ImageExtConfig::default();
107        let flora_con_conf = ContainerConfig::new(flora_image_conf, flora_image_ext_conf);
108
109        let flora_server = start_flora_container(flora_conf, flora_con_conf)
110            .await
111            .map_err(|error| anyhow::anyhow!("{error}"))?;
112
113        Ok(FloraStack {
114            flora_server,
115            typesense,
116        })
117    }
118
119    /// Stops the Docker containers in this Omry stack.
120    ///
121    /// ## Errors
122    /// Returns [`anyhow::Result`] if we can't:
123    ///
124    /// - Stop the Omry container
125    /// - Stop the Typesense container
126    #[instrument]
127    pub async fn stop(&self) -> Result<()> {
128        self.flora_server.stop().await?;
129        self.typesense.container().stop().await?;
130        Ok(())
131    }
132
133    /// Returns a reference to the flora server test container
134    /// running as part of this stack.
135    #[must_use]
136    pub fn flora_container(&self) -> &FloraContainer {
137        &self.flora_server
138    }
139}
140
141/// Spawns a new [`FloraServiceClient`] connected to the given socket.
142///
143/// ## Errors
144///
145/// Returns [`anyhow::Error`] if we can't initialize the [`tarpc`] transport.
146#[instrument(skip(socket))]
147pub async fn spawn_client(socket: impl tokio::net::ToSocketAddrs) -> Result<FloraServiceClient> {
148    let mut transport = tarpc::serde_transport::tcp::connect(socket, Bincode::default);
149    transport.config_mut().max_frame_length(usize::MAX);
150
151    let client = FloraServiceClient::new(client::Config::default(), transport.await?).spawn();
152    Ok(client)
153}
154
155/// Attempts to use [`ContainerAsync`] APIs to get the host and host-side port
156/// for the given container.
157///
158/// This may not work if the container is configured to use host networking.
159async fn get_host_port(container: &FloraContainer) -> Result<(Ipv4Addr, u16)> {
160    let container_host = container
161        .get_host()
162        .await
163        .context("couldn't get the flora container host")?
164        .to_string();
165    let host_ip = if container_host == "localhost" {
166        Ipv4Addr::LOCALHOST
167    } else {
168        anyhow::bail!("Non-localhost host is not supported yet: {container_host}")
169    };
170
171    let container_port = container
172        .image()
173        .expose_ports()
174        .first()
175        .ok_or_else(|| anyhow::anyhow!("flora container doesn't expose any ports"))?;
176    let host_port = container
177        .get_host_port_ipv4(*container_port)
178        .await
179        .context("couldn't get the host-side port for the flora container")?;
180    Ok((host_ip, host_port))
181}
182
183/// Spawns a new [`FloraServiceClient`] connected to the Omry server
184/// running in the given Docker container.
185///
186/// ## Errors
187///
188/// Returns [`anyhow::Error`] if we can't spawn the Omry service client.
189#[instrument]
190pub async fn flora_client(container: &FloraContainer) -> Result<FloraServiceClient> {
191    let (host_ip, host_port) = match get_host_port(container).await {
192        Ok(result) => result,
193        Err(error) => {
194            tracing::warn!(
195                "couldn't get the host port using the testcontainers APIs; trying workaround\
196                \noriginal error: {error}"
197            );
198
199            // If the host port is not `None`, that means we're running on localhost.
200            let Some(port) = container.image().host_port() else {
201                anyhow::bail!("couldn't get the host-side port for the flora container")
202            };
203            (Ipv4Addr::LOCALHOST, port)
204        }
205    };
206
207    let socket_addr = SocketAddr::V4(SocketAddrV4::new(host_ip, host_port));
208    spawn_client(socket_addr).await
209}
210
211#[cfg(test)]
212mod tests {
213    use super::*;
214    use crate::helpers;
215    use omry_service::PONG;
216    use omry_service::tarpc::context;
217    use pretty_assertions::assert_eq;
218    use std::time::Duration;
219    use tokio::time;
220    use url::Url;
221
222    #[test]
223    fn can_create_flora_config_with_options() -> Result<()> {
224        let options = FloraStackOptions {
225            tracing_level: Level::WARN,
226            search_sync_interval: Duration::from_secs(37),
227        };
228        let url: Url = "https://my.test.typesense".try_into()?;
229        let api_key = "very-secret".into();
230
231        let flora_config =
232            FloraConfig::new(&url, api_key).with_tracing_level(options.tracing_level);
233        insta::assert_debug_snapshot!(flora_config);
234
235        Ok(())
236    }
237
238    #[tokio::test]
239    async fn can_start_flora_stack() {
240        assert!(FloraStack::new(None).await.is_ok());
241    }
242
243    #[tokio::test]
244    async fn can_spawn_flora_client_connected_to_test_server() -> Result<()> {
245        helpers::init_tracing_subscriber()?;
246
247        let flora_stack = FloraStack::new(None).await?;
248
249        // Give the flora server time to start.
250        time::sleep(Duration::from_secs(5)).await;
251
252        let flora_container = flora_stack.flora_container();
253
254        let flora_client = flora_client(flora_container)
255            .await
256            .context("couldn't spawn the test client")?;
257
258        let ping_response = flora_client
259            .ping(context::current())
260            .await
261            .context("couldn't send the ping request")?;
262
263        assert_eq!(ping_response, PONG);
264
265        Ok(())
266    }
267
268    #[tokio::test]
269    async fn can_configure_flora_stack() -> Result<()> {
270        helpers::init_tracing_subscriber()?;
271
272        let options = FloraStackOptions::default();
273        let flora_stack = FloraStack::new(Some(options)).await?;
274
275        // Give the flora server time to start.
276        time::sleep(Duration::from_secs(5)).await;
277
278        let flora_container = flora_stack.flora_container();
279
280        let flora_client = flora_client(flora_container)
281            .await
282            .context("couldn't spawn the test client")?;
283
284        let ping_response = flora_client
285            .ping(context::current())
286            .await
287            .context("couldn't send the ping request")?;
288
289        insta::assert_snapshot!(ping_response, PONG);
290
291        Ok(())
292    }
293}