feat: undo a restore from the safety copy it took
- POST /refresh-runs accepts undo_of: restores the safety copy of a completed restore back onto the same target through the normal run engine (checks, countdown, progress, and a new safety copy so the undo can be undone); refused when something was restored after it or the copy is gone - a safety copy is accepted as a source only for the target it was taken from (hub and agent both check) - run views carry undo and undo_available; audited as refresh.undo_started - the modal offers Undo last restore and, after a restore, Undo this restore - proven with a real agent and PostgreSQL: restore, undo back to the original contents, undo of the undo Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2188,3 +2188,180 @@ async fn the_screens_get_what_they_need_to_offer_restore_for_an_orbit() {
|
||||
"{kinds:?}"
|
||||
);
|
||||
}
|
||||
|
||||
// ---- undoing a restore: put the target back from the safety copy the restore took ----
|
||||
|
||||
impl Lab {
|
||||
async fn start_undo(&self, caller: &Caller, key: &str, run: &str) -> (StatusCode, Value) {
|
||||
self.send(
|
||||
caller,
|
||||
"POST",
|
||||
"/api/v1/refresh-runs",
|
||||
Some(json!({"relation_id": self.relation, "confirmation": self.confirmation(), "idempotency_key": key, "undo_of": run})),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// A completed restore of a fresh capture, with its safety copy in the vault.
|
||||
async fn restored_once(&self, key: &str) -> String {
|
||||
let (_, run) = self.start(&self.admin(), key).await;
|
||||
let run = run["id"].as_str().unwrap().to_string();
|
||||
self.capture(&Lab::production_identity(), false)
|
||||
.await
|
||||
.unwrap();
|
||||
self.tick().await;
|
||||
self.finish_restore().await;
|
||||
assert_eq!(self.run(&run).status, "completed");
|
||||
run
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_completed_restore_can_be_undone_from_its_safety_copy_and_the_undo_can_be_undone() {
|
||||
let lab = fresh().await;
|
||||
let (qa, _) = lab.refresher("qa");
|
||||
let first = lab.restored_once("first").await;
|
||||
let (_, view) = lab
|
||||
.send(&qa, "GET", &format!("/api/v1/refresh-runs/{first}"), None)
|
||||
.await;
|
||||
assert_eq!(view["undo_available"], true, "{view}");
|
||||
assert!(
|
||||
view.get("safety_snapshot_id").is_none(),
|
||||
"no snapshot id reaches a non-administrator: {view}"
|
||||
);
|
||||
|
||||
let (status, undo) = lab.start_undo(&qa, "undo-1", &first).await;
|
||||
assert_eq!(status, StatusCode::ACCEPTED, "{undo}");
|
||||
assert_eq!(undo["undo"], true);
|
||||
let undo_id = undo["id"].as_str().unwrap().to_string();
|
||||
let captures = lab.capture_jobs();
|
||||
lab.tick().await;
|
||||
// the restore job takes the first restore's safety copy back onto the same target
|
||||
let (job, _) = lab
|
||||
.restore_until_ready()
|
||||
.await
|
||||
.expect("an undo job is waiting");
|
||||
let artifact: Value = serde_json::from_str(&lab.scalar::<String>(&format!(
|
||||
"SELECT artifact FROM restore_jobs WHERE id='{job}'"
|
||||
)))
|
||||
.unwrap();
|
||||
assert_eq!(artifact["rollback"], true, "{artifact}");
|
||||
assert_eq!(lab.capture_jobs(), captures, "an undo captures nothing");
|
||||
let (_, token) = (
|
||||
job.clone(),
|
||||
lab.scalar::<String>(&format!(
|
||||
"SELECT lease_token FROM restore_jobs WHERE id='{job}'"
|
||||
)),
|
||||
);
|
||||
assert_eq!(lab.apply(&job, &token).await, StatusCode::NO_CONTENT);
|
||||
lab.report_applied(&job, &token).await;
|
||||
lab.tick().await;
|
||||
assert_eq!(lab.run(&undo_id).status, "completed");
|
||||
|
||||
// only the newest restore of a target can be undone: the first one no longer can, the undo can
|
||||
let (_, old) = lab
|
||||
.send(&qa, "GET", &format!("/api/v1/refresh-runs/{first}"), None)
|
||||
.await;
|
||||
let (_, new) = lab
|
||||
.send(&qa, "GET", &format!("/api/v1/refresh-runs/{undo_id}"), None)
|
||||
.await;
|
||||
assert_eq!(
|
||||
(old["undo_available"].clone(), new["undo_available"].clone()),
|
||||
(json!(false), json!(true))
|
||||
);
|
||||
assert!(lab
|
||||
.audit_log()
|
||||
.iter()
|
||||
.any(|(event, _)| event == "restore.queued"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_undo_is_refused_when_there_is_nothing_sound_to_go_back_to() {
|
||||
let lab = fresh().await;
|
||||
let admin = lab.admin();
|
||||
// a run that has not finished, and one that does not exist
|
||||
let (_, running) = lab.start(&admin, "running").await;
|
||||
let running = running["id"].as_str().unwrap().to_string();
|
||||
for (what, id) in [
|
||||
("unfinished", running.clone()),
|
||||
("unknown", Uuid::new_v4().to_string()),
|
||||
] {
|
||||
let (status, body) = lab.start_undo(&admin, &format!("u-{what}"), &id).await;
|
||||
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{what}: {body}");
|
||||
assert_eq!(body["reason"], "nothing_to_undo", "{what}");
|
||||
}
|
||||
lab.capture(&Lab::production_identity(), false)
|
||||
.await
|
||||
.unwrap();
|
||||
lab.tick().await;
|
||||
lab.finish_restore().await;
|
||||
|
||||
// the safety copy has been removed from the vault
|
||||
let safety = lab.scalar::<String>("SELECT safety_id FROM restore_jobs LIMIT 1");
|
||||
lab.state
|
||||
.vault
|
||||
.write()
|
||||
.await
|
||||
.forget(&[Uuid::parse_str(&safety).unwrap()])
|
||||
.unwrap();
|
||||
let (status, body) = lab.start_undo(&admin, "gone", &running).await;
|
||||
assert_eq!(
|
||||
(status, body["reason"].clone()),
|
||||
(StatusCode::UNPROCESSABLE_ENTITY, json!("nothing_to_undo")),
|
||||
"{body}"
|
||||
);
|
||||
assert_eq!(
|
||||
lab.scalar::<i64>("SELECT COUNT(*) FROM refresh_runs"),
|
||||
1,
|
||||
"nothing was started"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_safety_copy_is_only_ever_restored_onto_the_target_it_came_from() {
|
||||
let lab = fresh().await;
|
||||
let admin = lab.admin();
|
||||
let first = lab.restored_once("first").await;
|
||||
let own_safety =
|
||||
Uuid::parse_str(&lab.scalar::<String>("SELECT safety_id FROM restore_jobs LIMIT 1"))
|
||||
.unwrap();
|
||||
let hints = lab
|
||||
.state
|
||||
.vault
|
||||
.read()
|
||||
.await
|
||||
.get_snapshot(own_safety)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.manifest
|
||||
.restore_hints;
|
||||
// the same kind of copy, but taken from some other target
|
||||
let mut foreign_hints = hints.clone();
|
||||
foreign_hints["target_id"] = json!(Uuid::new_v4());
|
||||
let foreign = lab
|
||||
.plant_snapshot(lab.orbit, &["restore-safety"], foreign_hints)
|
||||
.await;
|
||||
let (status, body) = lab.start_snapshot(&admin, "foreign", foreign).await;
|
||||
assert_eq!(
|
||||
(status, body["reason"].clone()),
|
||||
(StatusCode::UNPROCESSABLE_ENTITY, json!("snapshot_unusable")),
|
||||
"{body}"
|
||||
);
|
||||
// its own copy is fine, through the plain snapshot route as well
|
||||
let (status, body) = lab.start_snapshot(&admin, "own", own_safety).await;
|
||||
assert_eq!(status, StatusCode::ACCEPTED, "{body}");
|
||||
let _ = first;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn only_someone_who_may_use_the_relation_can_undo_through_it() {
|
||||
let lab = fresh().await;
|
||||
let first = lab.restored_once("first").await;
|
||||
let stranger = lab.person("stranger");
|
||||
let status = lab.start_undo(&stranger, "s", &first).await.0;
|
||||
assert!(
|
||||
status == StatusCode::FORBIDDEN || status == StatusCode::NOT_FOUND,
|
||||
"{status}"
|
||||
);
|
||||
assert_eq!(lab.scalar::<i64>("SELECT COUNT(*) FROM refresh_runs"), 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user