From e28b084a19f32750daaf757defa15f93c024af8a Mon Sep 17 00:00:00 2001 From: mnovak Date: Sat, 7 Nov 2026 19:37:04 +0100 Subject: [PATCH] [control] Tests first: a request during a Restore's watch never writes the unverified head or drops its journal (red) --- crates/control/tests/apply.rs | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/crates/control/tests/apply.rs b/crates/control/tests/apply.rs index 65eb16f..c195b3e 100644 --- a/crates/control/tests/apply.rs +++ b/crates/control/tests/apply.rs @@ -673,6 +673,61 @@ mod unix { env.assert_consistent().await; } + /// QC fix round 3: a request during a Restore's watch must not write the unverified head + /// to edge.json (or drop the Restore's journal); after the Restore is reverted, edge serves + /// what it served before. + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn a_request_during_a_restore_watch_never_writes_the_unverified_head() { + async fn upstream_of(env: &Env) -> String { + let r = client::https(env.opts.https_listen, "a.test", env.roots(), &Req::get("a.test", "/")) + .await + .unwrap(); + let v: serde_json::Value = serde_json::from_slice(&r.body).unwrap_or_default(); + format!("{} {}", r.status, v["upstream"].as_str().unwrap_or("?")) + } + let one = Echo::start("one").await; + let two = Echo::start("two").await; + let mut env = Env::new(EnvSpec { watch_secs: 4, ..Default::default() }).await; + env.start_control(None); + let kept1 = env.add_host(host("a.test", &one.url(), true)).await; + assert_eq!(kept1["state"], "kept"); + // edge.json cannot be replaced (a non-empty directory in its place): promote fails. + let ej = env.dir.join("edge/edge.json"); + std::fs::remove_file(&ej).unwrap(); + std::fs::create_dir(&ej).unwrap(); + std::fs::write(ej.join("x"), "x").unwrap(); + let r = env + .apply("PUT", "/api/v1/hosts/a.test", Some(json!({ "host": host("a.test", &two.url(), true) }))) + .await; + assert_eq!(r["state"], "kept", "{r}"); + assert_eq!(upstream_of(&env).await, "200 two"); + // Restore the first version; its upstream is sick now, so its watch will fail. + one.health.store(500, std::sync::atomic::Ordering::SeqCst); + let r = env.call("POST", "/api/v1/restore", Some(json!({ "change_id": kept1["id"] }))).await; + assert_eq!(r.status, 202, "{}", r.text); + let id = r.json["apply"]["id"].as_str().unwrap().to_owned(); + let deadline = std::time::Instant::now() + Duration::from_secs(20); + loop { + let s = env.call("GET", &format!("/api/v1/applies/{id}"), None).await; + if s.json["state"] == "watching" { + break; + } + assert!(std::time::Instant::now() < deadline, "never watched: {}", s.text); + tokio::time::sleep(Duration::from_millis(50)).await; + } + // The disk problem goes away; a write request arrives during the watch. + std::fs::remove_dir_all(&ej).unwrap(); + let _ = env + .call("POST", "/api/v1/hosts", Some(json!({ "host": host("a.test", &two.url(), true) }))) + .await; + assert!(env.dir.join("state/journal.json").exists(), "the Restore's journal is still there"); + assert!(!ej.exists() || env.file("edge/edge.json").contains(&two.url()), "edge.json not written from the head under watch"); + let rec = env.wait_apply(&id).await; + assert_ne!(rec["state"], "kept", "{rec}"); + let now = upstream_of(&env).await; + assert_eq!(now, "200 two", "after the Restore was undone, edge serves the failed Restore's config ({now})"); + } + /// Edge gone while a revert runs: control cannot see what edge serves, so it does not /// claim `reverted`; it pauses, and a restart with edge back finishes the job. #[tokio::test(flavor = "multi_thread", worker_threads = 4)]