Skip to content

Commit

Permalink
use random forward port for redis
Browse files Browse the repository at this point in the history
Signed-off-by: Rajat Jindal <rajatjindal83@gmail.com>
  • Loading branch information
rajatjindal committed Apr 23, 2024
1 parent 242a2e9 commit f8e0b07
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions tests/src/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ mod test {
#[tokio::test]
async fn spin_inbound_redis_outbound_redis_test() -> Result<()> {
let host_port = 8082;
let forward_port = 6380;
let redis_port = 6379;

// Ensure kubectl is in PATH
if !is_kubectl_installed().await? {
anyhow::bail!("kubectl is not installed");
}

port_forward_redis(forward_port, redis_port).await?;
let forward_port = port_forward_redis(redis_port).await?;

let client = redis::Client::open(format!("redis://localhost:{}", forward_port))?;
let mut con = client.get_multiplexed_async_connection().await?;
Expand Down Expand Up @@ -100,15 +99,14 @@ mod test {
"Hello world from multi trigger Spin!"
);

let forward_port = 6380;
let redis_port = 6379;

// Ensure kubectl is in PATH
if !is_kubectl_installed().await? {
anyhow::bail!("kubectl is not installed");
}

port_forward_redis(forward_port, redis_port).await?;
let forward_port = port_forward_redis(redis_port).await?;

let client = redis::Client::open(format!("redis://localhost:{}", forward_port))
.context("connecting to redis")?;
Expand Down Expand Up @@ -143,17 +141,24 @@ mod test {
}
}

async fn port_forward_redis(forward_port: u16, redis_port: u16) -> Result<()> {
println!(
" >>> kubectl portforward redis {}:{} ",
forward_port, redis_port
);
async fn port_forward_redis(redis_port: u16) -> Result<u16> {
let port = get_random_port()?;

println!(" >>> kubectl portforward redis {}:{} ", port, redis_port);

Command::new("kubectl")
.arg("port-forward")
.arg("redis")
.arg(format!("{}:{}", forward_port, redis_port))
.arg(format!("{}:{}", port, redis_port))
.spawn()?;
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
Ok(())
Ok(port)
}

/// Uses a track to get a random unused port
fn get_random_port() -> anyhow::Result<u16> {
Ok(std::net::TcpListener::bind("localhost:0")?
.local_addr()?
.port())
}
}

0 comments on commit f8e0b07

Please sign in to comment.