Skip to content

Commit

Permalink
test_proxy_connection: make test more robust (#390)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
tsnoam authored Jan 3, 2025
1 parent 57788a6 commit 92ec4a9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
53 changes: 47 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ rstest = "0.23.0"
serial_test = "3.2.0"
derive_more = { version = "1.0.0", features = ["from"] }

[target.'cfg(target_os = "macos")'.dev-dependencies]
get_if_addrs = "0.5.3"


[profile.release]
lto = "fat"
panic = "abort"
Expand Down
29 changes: 22 additions & 7 deletions src/protocols/tcp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ mod tests {
use super::*;
use futures_util::pin_mut;
use std::borrow::Cow;
use std::net::SocketAddr;
use std::net::IpAddr;
use testcontainers::core::WaitFor;
use testcontainers::runners::AsyncRunner;
use testcontainers::{ContainerAsync, Image, ImageExt};
Expand Down Expand Up @@ -263,15 +263,30 @@ mod tests {

#[tokio::test]
async fn test_proxy_connection() {
let server_addr: SocketAddr = "[::1]:1236".parse().unwrap();
let server = TcpListener::bind(server_addr).await.unwrap();
let (network_name, host) = if cfg!(not(target_os = "macos")) {
("host", "127.0.0.1".parse::<IpAddr>().unwrap())
} else {
let host = get_if_addrs::get_if_addrs()
.unwrap()
.into_iter()
.map(|iface| iface.addr.ip())
.find(|ip| ip.is_ipv4() && !ip.is_loopback())
.unwrap();
("wstunnel_test_proxy_connection", host)
};

let mitm_proxy: ContainerAsync<MitmProxy> = MitmProxy.with_network(network_name).start().await.unwrap();

let proxy_port = mitm_proxy.get_host_port_ipv4(8080).await.unwrap();

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

let mut client = connect_with_http_proxy(
&"http://localhost:8080".parse().unwrap(),
&Host::Domain("[::1]".to_string()),
1236,
&Url::parse(&format!("http://127.0.0.1:{proxy_port}")).unwrap(),
&Host::Domain(host.to_string()),
server_port,
None,
Duration::from_secs(1),
&DnsResolver::System,
Expand Down

0 comments on commit 92ec4a9

Please sign in to comment.