Skip to content

Commit 92ec4a9

Browse files
authored
test_proxy_connection: make test more robust (#390)
- Support running on macos with podman (this probably also fixes it to work with docker as well). This is done by using a dedicated network name and by dynamically finding the host ip. In macos, both podman & docker are running inside a Linux VM, so it is essential to find a real IP associated with the macos itself. - Run the mock TCP server on a dynamic port. This avoid flakiness in case the port is already in use.
1 parent 57788a6 commit 92ec4a9

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

Cargo.lock

+47-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ rstest = "0.23.0"
7777
serial_test = "3.2.0"
7878
derive_more = { version = "1.0.0", features = ["from"] }
7979

80+
[target.'cfg(target_os = "macos")'.dev-dependencies]
81+
get_if_addrs = "0.5.3"
82+
83+
8084
[profile.release]
8185
lto = "fat"
8286
panic = "abort"

src/protocols/tcp/server.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ mod tests {
233233
use super::*;
234234
use futures_util::pin_mut;
235235
use std::borrow::Cow;
236-
use std::net::SocketAddr;
236+
use std::net::IpAddr;
237237
use testcontainers::core::WaitFor;
238238
use testcontainers::runners::AsyncRunner;
239239
use testcontainers::{ContainerAsync, Image, ImageExt};
@@ -263,15 +263,30 @@ mod tests {
263263

264264
#[tokio::test]
265265
async fn test_proxy_connection() {
266-
let server_addr: SocketAddr = "[::1]:1236".parse().unwrap();
267-
let server = TcpListener::bind(server_addr).await.unwrap();
266+
let (network_name, host) = if cfg!(not(target_os = "macos")) {
267+
("host", "127.0.0.1".parse::<IpAddr>().unwrap())
268+
} else {
269+
let host = get_if_addrs::get_if_addrs()
270+
.unwrap()
271+
.into_iter()
272+
.map(|iface| iface.addr.ip())
273+
.find(|ip| ip.is_ipv4() && !ip.is_loopback())
274+
.unwrap();
275+
("wstunnel_test_proxy_connection", host)
276+
};
277+
278+
let mitm_proxy: ContainerAsync<MitmProxy> = MitmProxy.with_network(network_name).start().await.unwrap();
279+
280+
let proxy_port = mitm_proxy.get_host_port_ipv4(8080).await.unwrap();
268281

269-
let _mitm_proxy: ContainerAsync<MitmProxy> = MitmProxy.with_network("host".to_string()).start().await.unwrap();
282+
// bind to a dynamic port - avoid conflicts
283+
let server = TcpListener::bind((host, 0)).await.unwrap();
284+
let server_port = server.local_addr().unwrap().port();
270285

271286
let mut client = connect_with_http_proxy(
272-
&"http://localhost:8080".parse().unwrap(),
273-
&Host::Domain("[::1]".to_string()),
274-
1236,
287+
&Url::parse(&format!("http://127.0.0.1:{proxy_port}")).unwrap(),
288+
&Host::Domain(host.to_string()),
289+
server_port,
275290
None,
276291
Duration::from_secs(1),
277292
&DnsResolver::System,

0 commit comments

Comments
 (0)