From e354b7532b5d448ab37c9f95dab6c68c592cb62b Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Tue, 23 Jul 2024 21:24:45 +1000 Subject: [PATCH] refactor!: put `reqwest` behind `http_wait` feature (#705) closes https://github.com/testcontainers/testcontainers-rs/issues/704 I have no particular attachment to the `http_wait` feature name but it seems short and clear to me. --- testcontainers/Cargo.toml | 3 ++- testcontainers/src/core/error.rs | 6 ++++-- testcontainers/src/core/wait/mod.rs | 10 ++++++++++ testcontainers/tests/async_runner.rs | 9 ++++++--- testcontainers/tests/sync_runner.rs | 7 +++++-- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/testcontainers/Cargo.toml b/testcontainers/Cargo.toml index 0ec555a5..cc488ecd 100644 --- a/testcontainers/Cargo.toml +++ b/testcontainers/Cargo.toml @@ -29,7 +29,7 @@ log = "0.4" memchr = "2.7.2" parse-display = "0.9.0" pin-project-lite = "0.2.14" -reqwest = { version = "0.12.5", features = ["rustls-tls", "rustls-tls-native-roots", "hickory-dns", "json", "charset", "http2"], default-features = false } +reqwest = { version = "0.12.5", features = ["rustls-tls", "rustls-tls-native-roots", "hickory-dns", "json", "charset", "http2"], default-features = false, optional = true } serde = { version = "1", features = ["derive"] } serde-java-properties = { version = "0.2.0", optional = true } serde_json = "1" @@ -45,6 +45,7 @@ url = { version = "2", features = ["serde"] } default = [] blocking = [] watchdog = ["signal-hook", "conquer-once"] +http_wait = ["reqwest"] properties-config = ["serde-java-properties"] [dev-dependencies] diff --git a/testcontainers/src/core/error.rs b/testcontainers/src/core/error.rs index 1957b53c..70b5eade 100644 --- a/testcontainers/src/core/error.rs +++ b/testcontainers/src/core/error.rs @@ -1,7 +1,7 @@ use std::error::Error; +use crate::core::logs::WaitLogError; pub use crate::core::{client::ClientError, env::ConfigurationError, ContainerPort}; -use crate::core::{logs::WaitLogError, wait::http_strategy::HttpWaitError}; pub type Result = std::result::Result; @@ -55,7 +55,9 @@ pub enum WaitContainerError { #[error("container state is unavailable")] StateUnavailable, #[error("container is not ready: {0}")] - HttpWait(#[from] HttpWaitError), + #[cfg(feature = "http_wait")] + #[cfg_attr(docsrs, doc(cfg(feature = "http_wait")))] + HttpWait(#[from] crate::core::wait::http_strategy::HttpWaitError), #[error("healthcheck is not configured for container: {0}")] HealthCheckNotConfigured(String), #[error("container is unhealthy")] diff --git a/testcontainers/src/core/wait/mod.rs b/testcontainers/src/core/wait/mod.rs index 77eb5c00..afb258d7 100644 --- a/testcontainers/src/core/wait/mod.rs +++ b/testcontainers/src/core/wait/mod.rs @@ -2,6 +2,8 @@ use std::{env::var, fmt::Debug, time::Duration}; pub use exit_strategy::ExitWaitStrategy; pub use health_strategy::HealthWaitStrategy; +#[cfg(feature = "http_wait")] +#[cfg_attr(docsrs, doc(cfg(feature = "http_wait")))] pub use http_strategy::HttpWaitStrategy; pub use log_strategy::LogWaitStrategy; @@ -13,6 +15,7 @@ use crate::{ pub(crate) mod cmd_wait; pub(crate) mod exit_strategy; pub(crate) mod health_strategy; +#[cfg(feature = "http_wait")] pub(crate) mod http_strategy; pub(crate) mod log_strategy; @@ -36,6 +39,8 @@ pub enum WaitFor { /// Wait for the container's status to become `healthy`. Healthcheck(HealthWaitStrategy), /// Wait for a certain HTTP response. + #[cfg(feature = "http_wait")] + #[cfg_attr(docsrs, doc(cfg(feature = "http_wait")))] Http(HttpWaitStrategy), /// Wait for the container to exit. Exit(ExitWaitStrategy), @@ -66,6 +71,8 @@ impl WaitFor { } /// Wait for a certain HTTP response. + #[cfg(feature = "http_wait")] + #[cfg_attr(docsrs, doc(cfg(feature = "http_wait")))] pub fn http(http_strategy: HttpWaitStrategy) -> WaitFor { WaitFor::Http(http_strategy) } @@ -110,6 +117,8 @@ impl WaitFor { } } +#[cfg(feature = "http_wait")] +#[cfg_attr(docsrs, doc(cfg(feature = "http_wait")))] impl From for WaitFor { fn from(value: HttpWaitStrategy) -> Self { Self::Http(value) @@ -130,6 +139,7 @@ impl WaitStrategy for WaitFor { WaitFor::Healthcheck(strategy) => { strategy.wait_until_ready(client, container).await?; } + #[cfg(feature = "http_wait")] WaitFor::Http(strategy) => { strategy.wait_until_ready(client, container).await?; } diff --git a/testcontainers/tests/async_runner.rs b/testcontainers/tests/async_runner.rs index 59d8f4a3..c3bc8f86 100644 --- a/testcontainers/tests/async_runner.rs +++ b/testcontainers/tests/async_runner.rs @@ -1,12 +1,11 @@ use std::time::Duration; use bollard::Docker; -use reqwest::StatusCode; use testcontainers::{ core::{ logs::{consumer::logging_consumer::LoggingConsumer, LogFrame}, - wait::{ExitWaitStrategy, HttpWaitStrategy, LogWaitStrategy}, - CmdWaitFor, ExecCommand, IntoContainerPort, WaitFor, + wait::{ExitWaitStrategy, LogWaitStrategy}, + CmdWaitFor, ExecCommand, WaitFor, }, runners::AsyncRunner, GenericImage, *, @@ -144,8 +143,12 @@ async fn async_run_exec() -> anyhow::Result<()> { Ok(()) } +#[cfg(feature = "http_wait")] #[tokio::test] async fn async_wait_for_http() -> anyhow::Result<()> { + use reqwest::StatusCode; + use testcontainers::core::{wait::HttpWaitStrategy, IntoContainerPort}; + let _ = pretty_env_logger::try_init(); let image = GenericImage::new("simple_web_server", "latest") diff --git a/testcontainers/tests/sync_runner.rs b/testcontainers/tests/sync_runner.rs index 75b71d8b..025cda06 100644 --- a/testcontainers/tests/sync_runner.rs +++ b/testcontainers/tests/sync_runner.rs @@ -1,10 +1,9 @@ #![cfg(feature = "blocking")] -use reqwest::StatusCode; use testcontainers::{ core::{ logs::{consumer::logging_consumer::LoggingConsumer, LogFrame}, - wait::{HttpWaitStrategy, LogWaitStrategy}, + wait::LogWaitStrategy, CmdWaitFor, ExecCommand, Host, IntoContainerPort, WaitFor, }, runners::SyncRunner, @@ -40,9 +39,13 @@ fn sync_can_run_hello_world() -> anyhow::Result<()> { Ok(()) } +#[cfg(feature = "http_wait")] #[test] fn sync_wait_for_http() -> anyhow::Result<()> { + use crate::core::wait::HttpWaitStrategy; + let _ = pretty_env_logger::try_init(); + use reqwest::StatusCode; let image = GenericImage::new("simple_web_server", "latest") .with_exposed_port(80.tcp())