[tests] Ports and Caddy: test ports come from outside the ephemeral range, and a Caddy start that does not come up is retried

This commit is contained in:
2026-11-07 19:45:18 +01:00
parent 83b7b3b7d6
commit 432ae0f803
2 changed files with 68 additions and 10 deletions
+30 -7
View File
@@ -224,8 +224,28 @@ pub fn start_edge(env: &Env, name: &'static str, config_text: &str) -> Target {
}
}
/// Start `caddy run` on a generated Caddy JSON.
/// Start `caddy run` on a generated Caddy JSON; a start that does not come up is retried
/// (twice), since a port can be briefly unavailable right after the previous server on it.
pub fn start_caddy(env: &Env, name: &'static str, caddy: &std::path::Path, json: &str) -> Target {
let mut last = String::new();
for _ in 0..3 {
match try_start_caddy(env, name, caddy, json) {
Ok(t) => return t,
Err(e) => {
last = e;
std::thread::sleep(Duration::from_millis(500));
}
}
}
panic!("{last}")
}
fn try_start_caddy(
env: &Env,
name: &'static str,
caddy: &std::path::Path,
json: &str,
) -> Result<Target, String> {
let v: serde_json::Value = serde_json::from_str(json).unwrap();
let servers = &v["apps"]["http"]["servers"];
let https: SocketAddr = servers["ianitor_https"]["listen"][0]
@@ -252,18 +272,21 @@ pub fn start_caddy(env: &Env, name: &'static str, caddy: &std::path::Path, json:
.stderr(Stdio::from(log))
.spawn()
.expect("spawn caddy");
let guard = ChildGuard(child);
if !wait_for_port(https, Duration::from_secs(20)) {
let mut guard = ChildGuard(child);
if !wait_for_port(https, Duration::from_secs(20))
|| !wait_for_port(http, Duration::from_secs(5))
{
let _ = guard.0.kill();
let _ = guard.0.wait();
let out = std::fs::read_to_string(env.dir.join(&format!("{name}.log"))).unwrap_or_default();
panic!("caddy did not listen on {https}:\n{out}");
return Err(format!("caddy did not listen on {https} / {http}:\n{out}"));
}
assert!(wait_for_port(http, Duration::from_secs(5)));
Target {
Ok(Target {
name,
http,
https,
_guard: Guard::Caddy(guard),
}
})
}
pub enum Action {