[control] Pause fixes: Restore waits until an unsaved pause is on disk, unwritable config files pause with a way out, a failed promote at start starts paused, a revert that cannot see edge is not reverted

This commit is contained in:
2026-11-07 19:17:16 +01:00
parent 339882ec96
commit dd10557b87
4 changed files with 212 additions and 38 deletions
+74 -11
View File
@@ -575,16 +575,29 @@ mod unix {
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;
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) })))
.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() {
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;
}
@@ -594,16 +607,21 @@ mod unix {
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"] })))
.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;
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");
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.
@@ -615,17 +633,60 @@ mod unix {
env.assert_consistent().await;
}
/// QC fix round 2: when the config files cannot be written after a confirmed apply,
/// control pauses with the journal kept, still starts (paused) if they cannot be written
/// at the next start either, and resumes once they can.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn unwritable_config_files_pause_and_never_stop_control_from_starting() {
let good = Echo::start("good").await;
let mut env = Env::new(EnvSpec::default()).await;
env.start_control(None);
assert_eq!(env.add_host(host("a.test", &good.url(), false)).await["state"], "kept");
env.kill_control();
env.start_control_env(None, &[("IANITOR_TEST_FAIL_WRITE", "edge.json")]);
let r = env.add_host(host("b.test", &good.url(), false)).await;
assert_eq!(r["state"], "kept", "{r}");
let j: serde_json::Value = serde_json::from_str(&env.file("state/journal.json")).expect("journal kept");
assert_eq!(j["stage"], "confirmed", "{j}");
let o = env.call("GET", "/api/v1/overview", None).await;
assert_eq!(o.json["paused"]["code"], "control.paused", "{}", o.text);
// Restart while the file still cannot be written: control starts, paused.
env.kill_control();
env.start_control_env(None, &[("IANITOR_TEST_FAIL_WRITE", "edge.json")]);
let o = env.call("GET", "/api/v1/overview", None).await;
assert_eq!(o.status, 200);
assert_eq!(o.json["paused"]["code"], "control.paused", "{}", o.text);
// Once it can be written, a restart finishes the promotion and changes resume.
env.kill_control();
env.start_control(None);
let o = env.call("GET", "/api/v1/overview", None).await;
assert!(o.json["paused"].is_null(), "{}", o.text);
assert!(env.head_files().iter().any(|(p, _)| p == "hosts/b.test.toml"));
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;
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");
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) })))
.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;
@@ -635,7 +696,9 @@ mod unix {
env.start_edge();
env.kill_control();
env.start_control(None);
let rec = env.call("GET", &format!("/api/v1/applies/{id}"), None).await;
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;