From 13941b8bdf90972905d34fdd0aadf3fa749cb93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Milenkovi=C4=87?= Date: Sat, 10 Aug 2024 19:34:45 +0100 Subject: [PATCH] Testcontainer is not limited to single port --- README.md | 4 ---- src/lib.rs | 45 +++++++++++++++++++++++++++------------------ tests/common/mod.rs | 6 +++++- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 15b636b..760f546 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,6 @@ async fn main() { } ``` -Limitations: - -- It will use default kafka ports and only single test can run on given host. - Note about version compatibility: - `0.7.x` supports `testcontainers` `0.21` diff --git a/src/lib.rs b/src/lib.rs index 140bb8d..86ce2bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ use testcontainers::{ core::{wait::LogWaitStrategy, CmdWaitFor, ContainerPort, ContainerState, ExecCommand, WaitFor}, - ContainerRequest, Image, ImageExt, TestcontainersError, + ContainerRequest, Image, TestcontainersError, }; pub use testcontainers::runners::AsyncRunner; @@ -22,9 +22,9 @@ impl Redpanda { /// creates test container for specified tag pub fn for_tag(tag: String) -> ContainerRequest { ContainerRequest::from(Self { tag }) - .with_mapped_port(REDPANDA_PORT, ContainerPort::Tcp(REDPANDA_PORT)) - .with_mapped_port(SCHEMA_REGISTRY_PORT, ContainerPort::Tcp(SCHEMA_REGISTRY_PORT)) - .with_mapped_port(ADMIN_PORT, ContainerPort::Tcp(ADMIN_PORT)) + //.with_mapped_port(REDPANDA_PORT, ContainerPort::Tcp(REDPANDA_PORT)) + //.with_mapped_port(SCHEMA_REGISTRY_PORT, ContainerPort::Tcp(SCHEMA_REGISTRY_PORT)) + //.with_mapped_port(ADMIN_PORT, ContainerPort::Tcp(ADMIN_PORT)) } #[deprecated = "Use Self::latest()"] @@ -84,12 +84,17 @@ impl Image for Redpanda { "docker.redpanda.com/redpandadata/redpanda" } + //withCommand("sh -c while [ ! -f /start-panda.sh ]; do sleep 0.1; done; /start-panda.sh); + fn entrypoint(&self) -> Option<&str> { + Some("sh") + } + fn cmd(&self) -> impl IntoIterator>> { vec![ - "-c", - "/usr/bin/rpk redpanda start --mode dev-container --node-id 0 --set redpanda.auto_create_topics_enabled=true" - ] - .into_iter() + "-c", + "while [ ! -f /tmp/start-panda.sh ]; do sleep 0.1; done; sh /tmp/start-panda.sh", + ] + .into_iter() } fn tag(&self) -> &str { @@ -98,7 +103,9 @@ impl Image for Redpanda { fn ready_conditions(&self) -> Vec { vec![ - WaitFor::Log(LogWaitStrategy::stderr("Initialized cluster_id to ")), + // this has been moved to exec_after_start + // + // WaitFor::Log(LogWaitStrategy::stderr("Initialized cluster_id to ")), // No need to wait for cluster to settle down if we get `Initialized cluster_id to` message // WaitFor::Duration { // length: std::time::Duration::from_secs(1), @@ -106,19 +113,21 @@ impl Image for Redpanda { ] } - fn entrypoint(&self) -> Option<&str> { - Some("sh") - } - fn expose_ports(&self) -> &[ContainerPort] { - // this is not needed as we map it explicitly - // and testcontainer gets confused and re-map it - // vec![REDPANDA_PORT, SCHEMA_REGISTRY_PORT, ADMIN_PORT] &[] } - fn exec_after_start(&self, _: ContainerState) -> Result, TestcontainersError> { - Ok(vec![]) + fn exec_after_start(&self, state: ContainerState) -> Result, TestcontainersError> { + let c = ExecCommand::new(vec![ + "sh", "-c", + format!("echo '/usr/bin/rpk redpanda start --mode dev-container --node-id 0 --set redpanda.auto_create_topics_enabled=true --kafka-addr INTERNAL://0.0.0.0:29092,EXTERNAL://0.0.0.0:9092 --advertise-kafka-addr INTERNAL://localhost:29092,EXTERNAL://localhost:{}' > /tmp/start-panda.sh", state.host_port_ipv4(ContainerPort::Tcp(REDPANDA_PORT)).unwrap()).as_str() + ]).with_container_ready_conditions( + vec![ + WaitFor::Log(LogWaitStrategy::stderr("Initialized cluster_id to ")) + ] + ); + + Ok(vec![c]) } } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 7447f67..14fb986 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -87,5 +87,9 @@ pub fn random_topic_name() -> String { #[ctor::ctor] fn init() { // Enable RUST_LOG logging configuration for test - let _ = env_logger::builder().is_test(true).try_init(); + let _ = env_logger::builder() + .filter_level(log::LevelFilter::Info) + .parse_filters("testcontainers-redpanda-rs=debug") + .is_test(true) + .try_init(); }