Skip to content

Commit

Permalink
Testcontainer is not limited to single port
Browse files Browse the repository at this point in the history
  • Loading branch information
milenkovicm committed Aug 10, 2024
1 parent a362b53 commit 13941b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
45 changes: 27 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,9 +22,9 @@ impl Redpanda {
/// creates test container for specified tag
pub fn for_tag(tag: String) -> ContainerRequest<Self> {
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()"]
Expand Down Expand Up @@ -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<Item = impl Into<std::borrow::Cow<'_, str>>> {
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 {
Expand All @@ -98,27 +103,31 @@ impl Image for Redpanda {

fn ready_conditions(&self) -> Vec<testcontainers::core::WaitFor> {
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),
// },
]
}

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<Vec<ExecCommand>, TestcontainersError> {
Ok(vec![])
fn exec_after_start(&self, state: ContainerState) -> Result<Vec<ExecCommand>, 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])
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 13941b8

Please sign in to comment.