From 339882ec96df30336b5310fa159d05ff0c896a86 Mon Sep 17 00:00:00 2001 From: mnovak Date: Sat, 7 Nov 2026 19:14:29 +0100 Subject: [PATCH] [control] Tests first: a Restore never drops an unsaved pause, a revert that cannot see edge is not called reverted (red) --- crates/control/tests/apply.rs | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/crates/control/tests/apply.rs b/crates/control/tests/apply.rs index 1a0d7b8..01c74a4 100644 --- a/crates/control/tests/apply.rs +++ b/crates/control/tests/apply.rs @@ -568,6 +568,79 @@ mod unix { env.assert_consistent().await; } + /// QC fix round 2 (M3 reopened): a Restore while the pause is not saved must not take the + /// `reverting` journal with it, or a restart loads the unverified head. + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn a_restore_while_the_pause_is_unsaved_never_drops_it() { + let good = Echo::start("good").await; + let sick = Echo::start("sick").await; + sick.health.store(500, std::sync::atomic::Ordering::SeqCst); + let mut env = Env::new(EnvSpec { watch_secs: 3, ..Default::default() }).await; + env.start_control_env(None, &[("IANITOR_TEST_FAIL_WRITE", "paused.json")]); + let kept = env.add_host(host("a.test", &good.url(), true)).await; + let good_edge = env.file("edge/edge.json"); + let r = env + .call("POST", "/api/v1/hosts", Some(json!({ "host": host("b.test", &sick.url(), true) }))) + .await; + let id = r.json["apply"]["id"].as_str().unwrap().to_owned(); + let deadline = std::time::Instant::now() + Duration::from_secs(10); + while env.call("GET", &format!("/api/v1/applies/{id}"), None).await.json["commit"].is_null() { + assert!(std::time::Instant::now() < deadline); + tokio::time::sleep(Duration::from_millis(20)).await; + } + let lock = env.repo().join("refs/heads/main.lock"); + std::fs::write(&lock, "").unwrap(); + let rec = env.wait_apply(&id).await; + assert_eq!(rec["state"], "needs_attention", "{rec}"); + // Restore (the way out) while the pause still cannot be saved: refused, journal kept. + let r = env + .call("POST", "/api/v1/restore", Some(json!({ "change_id": kept["id"] }))) + .await; + if r.status == 202 { + env.wait_apply(r.json["apply"]["id"].as_str().unwrap()).await; + } else { + assert_eq!(r.status, 409, "{}", r.text); + assert_eq!(r.json["error"]["code"], "control.paused", "{}", r.text); + } + let j: serde_json::Value = + serde_json::from_str(&env.file("state/journal.json")).expect("the reverting journal is kept"); + assert_eq!(j["stage"], "reverting", "{j}"); + // The cause goes away; control restarts and finishes the revert, never loading the + // unverified head. + env.kill_control(); + std::fs::remove_file(&lock).unwrap(); + env.start_control(None); + tokio::time::sleep(Duration::from_secs(1)).await; + assert_eq!(env.edge_status().await["config_sha256"], sha256(&good_edge)); + env.assert_consistent().await; + } + + /// 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)] + async fn a_revert_that_cannot_see_edge_ends_as_needs_attention() { + let good = Echo::start("good").await; + let mut env = Env::new(EnvSpec { watch_secs: 3, ..Default::default() }).await; + env.start_control(None); + assert_eq!(env.add_host(host("a.test", &good.url(), true)).await["state"], "kept"); + let good_edge = env.file("edge/edge.json"); + let r = env + .call("POST", "/api/v1/hosts", Some(json!({ "host": host("b.test", &good.url(), true) }))) + .await; + let id = r.json["apply"]["id"].as_str().unwrap().to_owned(); + tokio::time::sleep(Duration::from_millis(1500)).await; + env.stop_edge(); + let rec = env.wait_apply(&id).await; + assert_eq!(rec["state"], "needs_attention", "{rec}"); + env.start_edge(); + env.kill_control(); + env.start_control(None); + let rec = env.call("GET", &format!("/api/v1/applies/{id}"), None).await; + assert_eq!(rec.json["state"], "reverted", "{}", rec.text); + assert_eq!(env.edge_status().await["config_sha256"], sha256(&good_edge)); + env.assert_consistent().await; + } + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] async fn a_pause_that_cannot_be_saved_keeps_the_journal_and_a_restart_finishes_the_revert() { let good = Echo::start("good").await;