Skip to content

Commit 19ae2b7

Browse files
authored
test(cli): retry forward tests when ssh loses the port after the check (#4799)
Forward tests retried on a new port only when the CLI's port check found the port in use. Another test can also take the port after that check passes and before the fake ssh binds it. The fake then panicked with exit status 101, which the retry did not recognize, so the test failed. The fake ssh now exits 255 when its port is taken, as ssh -o ExitOnForwardFailure=yes does, and the tests retry on that exit status too. Signed-off-by: Kris Hicks <khicks@nvidia.com>
1 parent a4c27f9 commit 19ae2b7

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

‎crates/openshell-cli/tests/fixtures/fake_forward.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ fn reexec_as_ssh() -> ! {
3333

3434
fn run_listener() {
3535
let port = forward_port().expect("fake forward must receive an SSH -L argument");
36-
let listener = TcpListener::bind(("127.0.0.1", port)).expect("fake forward must bind");
36+
// Like `ssh -o ExitOnForwardFailure=yes`, exit 255 when another process
37+
// holds the forward port.
38+
let listener = match TcpListener::bind(("127.0.0.1", port)) {
39+
Ok(listener) => listener,
40+
Err(error) if error.kind() == std::io::ErrorKind::AddrInUse => {
41+
eprintln!("bind [127.0.0.1]:{port}: Address already in use");
42+
std::process::exit(255);
43+
}
44+
Err(error) => panic!("fake forward must bind: {error}"),
45+
};
3746
for stream in listener.incoming() {
3847
let _ = stream;
3948
}

‎crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3930,7 +3930,8 @@ fn reserve_local_port() -> u16 {
39303930

39313931
/// Run a CLI forward on a free loopback port, retrying on a new port if another
39323932
/// process takes it first. The CLI checks the port before it binds it, which can
3933-
/// be seconds later for `sandbox create`, so a concurrent test can claim it.
3933+
/// be seconds later for `sandbox create`, so a concurrent test can claim it
3934+
/// either before the check or between the check and the fake `ssh`'s bind.
39343935
async fn with_free_forward_port<F, Fut, T>(mut forward: F) -> (u16, miette::Result<T>)
39353936
where
39363937
F: FnMut(openshell_core::forward::ForwardSpec) -> Fut,
@@ -3939,13 +3940,72 @@ where
39393940
for _ in 0..10 {
39403941
let port = reserve_local_port();
39413942
let result = forward(openshell_core::forward::ForwardSpec::new(port)).await;
3942-
if !matches!(&result, Err(err) if err.to_string().contains("is already in use")) {
3943+
if !matches!(&result, Err(err) if lost_forward_port(err)) {
39433944
return (port, result);
39443945
}
39453946
}
39463947
panic!("another process took the forward port on every attempt");
39473948
}
39483949

3950+
/// Whether a forward failed because another process holds its port: the CLI's
3951+
/// check found it in use, or the fake `ssh` exited 255 when it could not bind.
3952+
fn lost_forward_port(err: &miette::Report) -> bool {
3953+
err.chain().any(|cause| {
3954+
let cause = cause.to_string();
3955+
cause.contains("is already in use")
3956+
|| (cause.starts_with("ssh exited with status") && cause.contains("exit status: 255"))
3957+
})
3958+
}
3959+
3960+
#[test]
3961+
fn fake_forward_exits_like_ssh_when_its_port_is_taken() {
3962+
let dir = helpers::tempdir().unwrap();
3963+
let fake_forward = install_fake_forward_process_helper(&dir);
3964+
let taken = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
3965+
let port = taken.local_addr().unwrap().port();
3966+
3967+
let status = std::process::Command::new(fake_forward)
3968+
.env("OPENSHELL_FAKE_FORWARD_MODE", "listen")
3969+
.args(["-N", "-L", &format!("{port}:127.0.0.1:22"), "sandbox"])
3970+
.stderr(std::process::Stdio::null())
3971+
.status()
3972+
.unwrap();
3973+
3974+
assert_eq!(
3975+
status.code(),
3976+
Some(255),
3977+
"ssh -o ExitOnForwardFailure=yes exits 255 when it cannot bind"
3978+
);
3979+
}
3980+
3981+
#[tokio::test]
3982+
async fn forward_retries_when_ssh_loses_the_port_after_the_check() {
3983+
use miette::WrapErr;
3984+
use std::os::unix::process::ExitStatusExt;
3985+
3986+
let mut attempts = 0;
3987+
let (_, result) = with_free_forward_port(|spec| {
3988+
attempts += 1;
3989+
let first = attempts == 1;
3990+
async move {
3991+
if first {
3992+
let status = std::process::ExitStatus::from_raw(255 << 8);
3993+
return Err(miette::miette!(
3994+
"ssh exited with status {status} before local forward listener opened on \
3995+
127.0.0.1:{}",
3996+
spec.port
3997+
))
3998+
.wrap_err("ssh process started but local forward listener was not reachable");
3999+
}
4000+
Ok(())
4001+
}
4002+
})
4003+
.await;
4004+
4005+
result.expect("the second port should succeed");
4006+
assert_eq!(attempts, 2);
4007+
}
4008+
39494009
fn spawn_service_forward(
39504010
server: &TestServer,
39514011
sandbox: &str,

0 commit comments

Comments
 (0)