[tests] Tests first, fix round 1: proof only on the forward-auth UI host, revocations that cannot be written, root key files by content, intermediates that cannot issue, stale SNI after a failed or resumed handshake, edge re-reads the allow list, changes apply while Caddy serves, Hostname focus (red)

This commit is contained in:
2026-11-08 10:26:31 +01:00
parent e2a777882c
commit 0749119f19
6 changed files with 690 additions and 11 deletions
+178 -10
View File
@@ -116,19 +116,11 @@ esac
let caddy = std::fs::read_to_string(env.dir.join("caddy/caddy.json")).unwrap();
assert!(caddy.contains("a.test"));
// While Caddy serves: changes and share links wait, the overview says so.
// While Caddy serves: changes go on (changes_apply_while_caddy_serves), share links wait.
let ov = env.call("GET", "/api/v1/overview", None).await;
assert_eq!(ov.json["engine"]["running"], "caddy", "{}", ov.text);
assert_eq!(ov.json["paused"]["code"], "engine.caddy", "{}", ov.text);
assert!(ov.json["paused"].is_null(), "{}", ov.text);
assert_eq!(ov.json["site"]["share_links_available"], false);
let add = env
.call(
"POST",
"/api/v1/hosts",
Some(json!({"host": {"hostname": "b.test", "upstreams": ["http://127.0.0.1:9"], "tls": "domi"}})),
)
.await;
assert_eq!(add.status, 409, "{}", add.text);
let share = env
.call(
"POST",
@@ -224,4 +216,180 @@ esac
let e = env.call("GET", "/api/v1/engine", None).await;
assert_eq!(e.json["switchable"], false);
}
/// The fake systemctl that really runs Caddy for caddy-fallback.service (edge runs in this
/// process and keeps its own ports; Caddy gets others through [compile_caddy]).
fn fake_with_caddy(dir: &std::path::Path, caddy: &std::path::Path) -> String {
format!(
r#"#!/bin/sh
dir="{d}/fake/units"
mkdir -p "$dir"
[ "$1" = "--no-ask-password" ] && shift
echo "$*" >> "{d}/fake/calls.log"
case "$1:$2" in
start:caddy-fallback.service)
XDG_DATA_HOME="{d}/caddy-state" XDG_CONFIG_HOME="{d}/caddy-state" HOME="{d}/caddy-state" \
nohup "{c}" run --config "{d}/caddy/caddy.json" >> "{d}/logs/caddy.log" 2>&1 &
echo $! > "{d}/fake/caddy.pid"; touch "$dir/$2" ;;
stop:caddy-fallback.service)
[ -f "{d}/fake/caddy.pid" ] && kill "$(cat "{d}/fake/caddy.pid")" 2>/dev/null; rm -f "$dir/$2" "{d}/fake/caddy.pid" ;;
start:*) touch "$dir/$2" ;;
stop:*) rm -f "$dir/$2" ;;
is-active:*) [ "$2" = "--quiet" ] && shift; [ -e "$dir/$2" ] ;;
*) exit 2 ;;
esac
"#,
d = dir.display(),
c = caddy.display()
)
}
struct CaddyEnv {
env: Env,
https: std::net::SocketAddr,
}
impl Drop for CaddyEnv {
fn drop(&mut self) {
if let Ok(p) = std::fs::read_to_string(self.env.dir.join("fake/caddy.pid")) {
let _ = std::process::Command::new("kill").arg(p.trim()).status();
}
}
}
async fn env_with_caddy() -> Option<CaddyEnv> {
let caddy = ianitor_testkit::proc::require_binary("caddy")?;
let mut env = Env::new(EnvSpec::default()).await;
let fake = env.dir.join("fake/systemctl");
std::fs::create_dir_all(env.dir.join("fake/units")).unwrap();
std::fs::create_dir_all(env.dir.join("caddy-state")).unwrap();
std::fs::write(&fake, fake_with_caddy(&env.dir.join("."), &caddy)).unwrap();
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&fake, std::fs::Permissions::from_mode(0o755)).unwrap();
}
std::fs::write(env.dir.join("fake/units/ianitor-edge.service"), "").unwrap();
let http = ianitor_testkit::proc::local(ianitor_testkit::proc::free_port());
let https = ianitor_testkit::proc::local(ianitor_testkit::proc::free_port());
let d = |p: &str| env.dir.join(p).display().to_string();
let text = std::fs::read_to_string(env.config_path()).unwrap()
+ &format!(
"\n[engine]\nfile = \"{}\"\nsystemctl = \"{}\"\n\n[compile_caddy]\nhttp_listen = \"{http}\"\nhttps_listen = \"{https}\"\ncert_dir = \"{}\"\nstate_dir = \"{}\"\naccess_log = \"{}\"\ncaddy_credentials_dir = \"{}\"\ncaddy_admin_socket = \"{}\"\n",
d("engine"),
fake.display(),
d("certs"),
d("caddy-state"),
d("logs/caddy-access.log"),
d("creds"),
d("caddy/admin.sock"),
);
std::fs::write(env.config_path(), text).unwrap();
env.start_control(None);
Some(CaddyEnv { env, https })
}
async fn visit(env: &Env, at: std::net::SocketAddr, host: &str) -> u16 {
match ianitor_testkit::client::https(
at,
host,
env.ca.roots(),
&ianitor_testkit::client::Req::get(host, "/"),
)
.await
{
Ok(r) => r.status,
Err(_) => 0,
}
}
/// Mark, 2026-11-08: changes keep working while Caddy serves. An apply is loaded into
/// Caddy, watched through it, and kept, or reverted by loading the previous caddy.json;
/// Undo works the same way; share links stay off. Switching back brings edge up to date.
#[tokio::test(flavor = "multi_thread")]
async fn changes_apply_while_caddy_serves() {
let Some(ce) = env_with_caddy().await else {
return;
};
let env = &ce.env;
let echo = ianitor_testkit::upstream::Echo::start("app").await;
let host = |h: &str, up: String| json!({"host": {"hostname": h, "upstreams": [up], "tls": "domi", "auth": "none", "share_links": true}});
assert_eq!(
env.apply("POST", "/api/v1/hosts", Some(host("a.test", echo.url())))
.await["state"],
"kept"
);
let r = env
.call("POST", "/api/v1/engine", Some(json!({"target": "caddy"})))
.await;
assert_eq!(r.status, 202, "{}", r.text);
wait_engine(env, "caddy").await;
assert_eq!(
visit(env, ce.https, "a.test").await,
200,
"{}",
env.control_log()
);
// No pause while Caddy serves.
let ov = env.call("GET", "/api/v1/overview", None).await;
assert!(ov.json["paused"].is_null(), "{}", ov.text);
// Kept through Caddy.
let rec = env
.apply("POST", "/api/v1/hosts", Some(host("b.test", echo.url())))
.await;
assert_eq!(rec["state"], "kept", "{rec}\n{}", env.control_log());
assert_eq!(visit(env, ce.https, "b.test").await, 200);
// A broken change is reverted by loading the previous caddy.json.
let broken = json!({"host": {"hostname": "c.test", "upstreams": [format!("http://127.0.0.1:{}", ianitor_testkit::upstream::closed_port())],
"tls": "domi", "auth": "none", "health_check": {"path": "/", "interval_secs": 1}}});
let rec = env.apply("POST", "/api/v1/hosts", Some(broken)).await;
assert_eq!(rec["state"], "reverted", "{rec}\n{}", env.control_log());
let caddy_json = std::fs::read_to_string(env.dir.join("caddy/caddy.json")).unwrap();
assert!(!caddy_json.contains("c.test"));
assert_eq!(visit(env, ce.https, "a.test").await, 200);
assert_eq!(visit(env, ce.https, "b.test").await, 200);
// Undo (removes b.test) works under Caddy too.
let u = env.call("GET", "/api/v1/undo", None).await;
let target = u.json["target"]["change_id"].as_str().unwrap().to_owned();
let rec = env
.apply(
"POST",
"/api/v1/undo",
Some(json!({"target_change_id": target})),
)
.await;
assert_eq!(rec["state"], "kept", "{rec}");
assert_ne!(visit(env, ce.https, "b.test").await, 200);
// Share links stay off.
let s = env
.call(
"POST",
"/api/v1/hosts/a.test/share-links",
Some(json!({"path_prefix": "/", "expires_in_s": 86400})),
)
.await;
assert_eq!(s.status, 409, "{}", s.text);
assert_eq!(s.json["error"]["code"], "share.engine_caddy");
// Back to Pingora: edge serves what was kept under Caddy.
let r = env
.call("POST", "/api/v1/engine", Some(json!({"target": "pingora"})))
.await;
assert_eq!(r.status, 202, "{}", r.text);
wait_engine(env, "pingora").await;
let edge = env.edge.as_ref().unwrap();
assert_eq!(visit(env, edge.https, "a.test").await, 200);
assert_ne!(visit(env, edge.https, "b.test").await, 200);
let edge_json = std::fs::read_to_string(env.dir.join("edge/edge.json")).unwrap();
assert!(
edge_json.contains("a.test")
&& !edge_json.contains("b.test")
&& !edge_json.contains("c.test")
);
}
}