Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: put reqwest behind http_wait feature #705

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion testcontainers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand Down
6 changes: 4 additions & 2 deletions testcontainers/src/core/error.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, TestcontainersError>;

Expand Down Expand Up @@ -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")]
Expand Down
10 changes: 10 additions & 0 deletions testcontainers/src/core/wait/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
DDtKey marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(docsrs, doc(cfg(feature = "http_wait")))]
pub use http_strategy::HttpWaitStrategy;
pub use log_strategy::LogWaitStrategy;

Expand All @@ -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;

Expand All @@ -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")]
DDtKey marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(docsrs, doc(cfg(feature = "http_wait")))]
Http(HttpWaitStrategy),
/// Wait for the container to exit.
Exit(ExitWaitStrategy),
Expand Down Expand Up @@ -66,6 +71,8 @@ impl WaitFor {
}

/// Wait for a certain HTTP response.
#[cfg(feature = "http_wait")]
DDtKey marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(docsrs, doc(cfg(feature = "http_wait")))]
pub fn http(http_strategy: HttpWaitStrategy) -> WaitFor {
WaitFor::Http(http_strategy)
}
Expand Down Expand Up @@ -110,6 +117,8 @@ impl WaitFor {
}
}

#[cfg(feature = "http_wait")]
#[cfg_attr(docsrs, doc(cfg(feature = "http_wait")))]
impl From<HttpWaitStrategy> for WaitFor {
fn from(value: HttpWaitStrategy) -> Self {
Self::Http(value)
Expand All @@ -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?;
}
Expand Down
9 changes: 6 additions & 3 deletions testcontainers/tests/async_runner.rs
Original file line number Diff line number Diff line change
@@ -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, *,
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions testcontainers/tests/sync_runner.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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())
Expand Down
Loading