use std::net::{Ipv6Addr, TcpListener};
use testcontainers::{clients, core::WaitFor, GenericImage};
#[tokio::test]
async fn test_ipv4_ipv6_host_ports() {
let _ = pretty_env_logger::try_init();
let docker = clients::Cli::default();
let wait_for = WaitFor::message_on_stdout("server is ready");
let image = GenericImage::new("simple_web_server", "latest").with_wait_for(wait_for.clone());
let first_container = docker.run(image.clone());
let first_ipv4_port = first_container.get_host_port_ipv4(80);
let first_ipv6_port = first_container.get_host_port_ipv6(80);
assert_eq!(
"foo",
reqwest::get(&format!("http://127.0.0.1:{first_ipv4_port}"))
.await
.unwrap()
.text()
.await
.unwrap(),
);
assert_eq!(
"foo",
reqwest::get(&format!("http://[::1]:{first_ipv6_port}"))
.await
.unwrap()
.text()
.await
.unwrap(),
);
let mut sockets = Vec::new();
for port in first_ipv6_port + 1..first_ipv6_port + 9 {
if let Ok(socket) = TcpListener::bind((Ipv6Addr::LOCALHOST, port)) {
sockets.push(socket);
}
}
let second_container = docker.run(image);
let second_ipv4_port = second_container.get_host_port_ipv4(80);
let second_ipv6_port = second_container.get_host_port_ipv6(80);
assert_eq!(
"foo",
reqwest::get(&format!("http://127.0.0.1:{second_ipv4_port}"))
.await
.unwrap()
.text()
.await
.unwrap(),
);
assert_eq!(
"foo",
reqwest::get(&format!("http://[::1]:{second_ipv6_port}"))
.await
.unwrap()
.text()
.await
.unwrap(),
);
}