[control] Recovery: record the outcome before clearing the journal; a failed revert pauses changes until a Restore, and a restart never loads the unverified head

This commit is contained in:
2026-11-07 18:26:03 +01:00
parent 7cc92ec42d
commit c5380c65a6
2 changed files with 167 additions and 12 deletions
+122
View File
@@ -436,6 +436,128 @@ mod unix {
out
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_queued_apply_can_be_cancelled() {
let good = Echo::start("good").await;
let mut env = Env::new(EnvSpec {
watch_secs: 3,
..Default::default()
})
.await;
env.start_control(None);
let a = env
.call(
"POST",
"/api/v1/hosts",
Some(json!({ "host": host("a.test", &good.url(), true) })),
)
.await;
let b = env
.call(
"POST",
"/api/v1/hosts",
Some(json!({ "host": host("b.test", &good.url(), true) })),
)
.await;
let bid = b.json["apply"]["id"].as_str().unwrap().to_owned();
let r = env
.call("DELETE", &format!("/api/v1/applies/{bid}"), None)
.await;
assert_eq!(r.status, 204, "{}", r.text);
let rb = env.wait_apply(&bid).await;
assert_eq!(rb["state"], "failed");
assert_eq!(rb["reason"]["code"], "apply.cancelled");
let ra = env
.wait_apply(a.json["apply"]["id"].as_str().unwrap())
.await;
assert_eq!(ra["state"], "kept");
// A running or finished apply cannot be cancelled.
let r = env
.call(
"DELETE",
&format!("/api/v1/applies/{}", ra["id"].as_str().unwrap()),
None,
)
.await;
assert_eq!(r.status, 409);
assert!(
!env.head_files()
.iter()
.any(|(p, _)| p == "hosts/b.test.toml")
);
env.assert_consistent().await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_revert_that_fails_pauses_changes_until_a_restore() {
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(None);
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();
// While it is being watched, lock the branch so the revert commit cannot land.
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;
std::fs::remove_file(&lock).unwrap();
assert_eq!(rec["state"], "needs_attention", "{rec}");
// Edge was still told to drop the trial: visitors get the last good config.
let st = env.edge_status().await;
assert_eq!(st["config_sha256"], sha256(&good_edge));
assert_eq!(env.file("edge/edge.json"), good_edge);
let o = env.call("GET", "/api/v1/overview", None).await;
assert_eq!(o.json["paused"]["code"], "control.paused", "{}", o.text);
assert_eq!(o.json["undo"]["blocked"]["code"], "undo.paused");
let r = env
.call(
"POST",
"/api/v1/hosts",
Some(json!({ "host": host("c.test", &good.url(), false) })),
)
.await;
assert_eq!(r.status, 409);
// A control restart does not push the unverified head to edge.
env.kill_control();
env.start_control(None);
assert_eq!(env.edge_status().await["config_sha256"], sha256(&good_edge));
// Restore the last kept version: the way out.
let r = env
.apply(
"POST",
"/api/v1/restore",
Some(json!({ "change_id": kept["id"] })),
)
.await;
assert_eq!(r["state"], "kept", "{r}");
let o = env.call("GET", "/api/v1/overview", None).await;
assert!(o.json["paused"].is_null(), "{}", o.text);
env.assert_consistent().await;
}
// ------------------------------------------------------------ undo, restore, conflicts
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]