test: cover the agent restore sequence with fakes
Safety failure never applies, identity and source guards, rollback exception, per-step timeouts, lost apply permit, crash after permit, poll lease checks, and the heartbeat-failure behaviour. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -322,3 +322,6 @@ async fn report<H: RestoreHub>(
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|||||||
792
crates/tessera-agent/src/restore/tests.rs
Normal file
792
crates/tessera-agent/src/restore/tests.rs
Normal file
@@ -0,0 +1,792 @@
|
|||||||
|
//! The restore sequence is destructive, so these tests drive `run` and `poll` with in-process fakes and
|
||||||
|
//! assert what the agent does (and above all never does) at each step. Every fake appends to one shared
|
||||||
|
//! event log so tests can assert on ordering and absence.
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use std::{
|
||||||
|
collections::VecDeque,
|
||||||
|
io::{Read, Write},
|
||||||
|
sync::Mutex,
|
||||||
|
};
|
||||||
|
use tessera_core::restore::{RestoreArtifact, RestoreJob, RestoreTarget as JobTarget};
|
||||||
|
|
||||||
|
type Log = Arc<Mutex<Vec<String>>>;
|
||||||
|
|
||||||
|
fn identity(instance: &str, database: &str) -> DatabaseIdentity {
|
||||||
|
DatabaseIdentity {
|
||||||
|
instance: instance.into(),
|
||||||
|
database: database.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn target_identity() -> DatabaseIdentity {
|
||||||
|
identity("1", "target")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn source_identity() -> DatabaseIdentity {
|
||||||
|
identity("2", "source")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lease_for(target: Uuid, application: Uuid, rollback: bool) -> RestoreLease {
|
||||||
|
RestoreLease {
|
||||||
|
job: RestoreJob {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
snapshot_id: Uuid::new_v4(),
|
||||||
|
target: JobTarget {
|
||||||
|
id: target,
|
||||||
|
application_id: application,
|
||||||
|
outpost_id: Uuid::new_v4(),
|
||||||
|
name: "target".into(),
|
||||||
|
engine: tessera_core::restore::Engine::Postgres,
|
||||||
|
identity: target_identity(),
|
||||||
|
},
|
||||||
|
actor: "admin".into(),
|
||||||
|
status: "running".into(),
|
||||||
|
phase: "queued".into(),
|
||||||
|
safety_snapshot_id: None,
|
||||||
|
created_at: 0,
|
||||||
|
updated_at: 0,
|
||||||
|
failure_reason: None,
|
||||||
|
},
|
||||||
|
token: Uuid::new_v4(),
|
||||||
|
artifact: RestoreArtifact {
|
||||||
|
format: CaptureFormat::PostgresCustom,
|
||||||
|
// A normal restore comes from another database; a rollback comes from the target itself.
|
||||||
|
identity: if rollback {
|
||||||
|
target_identity()
|
||||||
|
} else {
|
||||||
|
source_identity()
|
||||||
|
},
|
||||||
|
sha256: "00".into(),
|
||||||
|
rollback,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lease() -> RestoreLease {
|
||||||
|
lease_for(Uuid::new_v4(), Uuid::new_v4(), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
|
enum Step {
|
||||||
|
Ok,
|
||||||
|
Fail,
|
||||||
|
Hang,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Step {
|
||||||
|
async fn run(self, what: &str) -> Result<()> {
|
||||||
|
match self {
|
||||||
|
Step::Ok => Ok(()),
|
||||||
|
Step::Fail => anyhow::bail!("{what} exploded"),
|
||||||
|
Step::Hang => std::future::pending().await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Answer = Result<DatabaseIdentity, &'static str>;
|
||||||
|
|
||||||
|
struct FakeTarget {
|
||||||
|
log: Log,
|
||||||
|
/// Answers for successive `identity` calls; the last one repeats.
|
||||||
|
identities: Mutex<VecDeque<Answer>>,
|
||||||
|
hang_identity: bool,
|
||||||
|
prepare: Step,
|
||||||
|
safety: Step,
|
||||||
|
apply: Step,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FakeTarget {
|
||||||
|
fn new(log: &Log) -> Self {
|
||||||
|
Self {
|
||||||
|
log: log.clone(),
|
||||||
|
identities: Mutex::new(VecDeque::from([Ok(target_identity())])),
|
||||||
|
hang_identity: false,
|
||||||
|
prepare: Step::Ok,
|
||||||
|
safety: Step::Ok,
|
||||||
|
apply: Step::Ok,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn identities(self, answers: Vec<Answer>) -> Self {
|
||||||
|
*self.identities.lock().unwrap() = answers.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl RestoreTarget for FakeTarget {
|
||||||
|
async fn identity(&self) -> Result<DatabaseIdentity> {
|
||||||
|
self.log.lock().unwrap().push("target.identity".into());
|
||||||
|
if self.hang_identity {
|
||||||
|
std::future::pending::<()>().await;
|
||||||
|
}
|
||||||
|
let answer = {
|
||||||
|
let mut answers = self.identities.lock().unwrap();
|
||||||
|
if answers.len() > 1 {
|
||||||
|
answers.pop_front().unwrap()
|
||||||
|
} else {
|
||||||
|
answers.front().cloned().unwrap()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
answer.map_err(|why| anyhow::anyhow!(why))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn prepare(&self, mut input: File, _: &RestoreLease) -> Result<File> {
|
||||||
|
self.log.lock().unwrap().push("target.prepare".into());
|
||||||
|
self.prepare.run("prepare").await?;
|
||||||
|
let mut script = tempfile::tempfile()?;
|
||||||
|
let mut body = Vec::new();
|
||||||
|
input.read_to_end(&mut body)?;
|
||||||
|
script.write_all(&body)?;
|
||||||
|
Ok(script)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn safety(&self, _: File) -> Result<()> {
|
||||||
|
self.log.lock().unwrap().push("target.safety".into());
|
||||||
|
self.safety.run("safety dump").await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn apply(&self, _: File) -> Result<()> {
|
||||||
|
self.log.lock().unwrap().push("target.apply".into());
|
||||||
|
self.apply.run("apply").await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FakeSource(Answer);
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl BackupSource for FakeSource {
|
||||||
|
async fn identity(&self) -> Result<DatabaseIdentity> {
|
||||||
|
self.0.clone().map_err(|why| anyhow::anyhow!(why))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FakeHub {
|
||||||
|
log: Log,
|
||||||
|
/// Queue of claim replies; empty means "nothing to restore".
|
||||||
|
claims: Mutex<VecDeque<String>>,
|
||||||
|
download: Step,
|
||||||
|
upload: Step,
|
||||||
|
/// A request whose path ends with this fails (the response is "lost").
|
||||||
|
fail_path: Option<&'static str>,
|
||||||
|
/// Reports with this progress fail (used to break heartbeats).
|
||||||
|
fail_progress: Option<&'static str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FakeHub {
|
||||||
|
fn new(log: &Log) -> Self {
|
||||||
|
Self {
|
||||||
|
log: log.clone(),
|
||||||
|
claims: Mutex::default(),
|
||||||
|
download: Step::Ok,
|
||||||
|
upload: Step::Ok,
|
||||||
|
fail_path: None,
|
||||||
|
fail_progress: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl RestoreHub for FakeHub {
|
||||||
|
async fn send(
|
||||||
|
&self,
|
||||||
|
path: String,
|
||||||
|
body: serde_json::Value,
|
||||||
|
restore: Option<Uuid>,
|
||||||
|
) -> Result<String> {
|
||||||
|
if path.ends_with("/claim") {
|
||||||
|
return Ok(self
|
||||||
|
.claims
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.pop_front()
|
||||||
|
.unwrap_or_else(|| "null".into()));
|
||||||
|
}
|
||||||
|
if path.ends_with("/apply") {
|
||||||
|
assert!(restore.is_some(), "the apply permit needs the lease token");
|
||||||
|
self.log.lock().unwrap().push("hub.apply-permit".into());
|
||||||
|
} else {
|
||||||
|
let progress = body.as_str().unwrap_or("?").to_owned();
|
||||||
|
self.log.lock().unwrap().push(format!("report:{progress}"));
|
||||||
|
if self.fail_progress == Some(progress.as_str()) {
|
||||||
|
anyhow::bail!("hub unreachable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.fail_path.is_some_and(|suffix| path.ends_with(suffix)) {
|
||||||
|
anyhow::bail!("response lost");
|
||||||
|
}
|
||||||
|
Ok(String::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn send_with_reason(
|
||||||
|
&self,
|
||||||
|
_: String,
|
||||||
|
body: serde_json::Value,
|
||||||
|
_: Uuid,
|
||||||
|
reason: Option<String>,
|
||||||
|
) -> Result<String> {
|
||||||
|
self.log.lock().unwrap().push(format!(
|
||||||
|
"failed:{}:{}",
|
||||||
|
body.as_str().unwrap_or("?"),
|
||||||
|
reason.unwrap_or_default()
|
||||||
|
));
|
||||||
|
Ok(String::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn upload_safety(&self, _: &RestoreLease, _: File, _: Duration) -> Result<()> {
|
||||||
|
self.log.lock().unwrap().push("hub.safety-upload".into());
|
||||||
|
self.upload.run("upload").await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn download(&self, _: &RestoreLease, _: Duration) -> Result<File> {
|
||||||
|
self.log.lock().unwrap().push("hub.download".into());
|
||||||
|
self.download.run("download").await?;
|
||||||
|
Ok(tempfile::tempfile()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const SHORT: Duration = Duration::from_millis(60);
|
||||||
|
|
||||||
|
struct Outcome {
|
||||||
|
result: Result<()>,
|
||||||
|
log: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Outcome {
|
||||||
|
fn did(&self, event: &str) -> bool {
|
||||||
|
self.log.iter().any(|entry| entry == event)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn failed(&self, progress: &str) -> Option<&str> {
|
||||||
|
self.log
|
||||||
|
.iter()
|
||||||
|
.find_map(|entry| entry.strip_prefix(&format!("failed:{progress}:")))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The agent never reached the destructive step.
|
||||||
|
fn never_applied(&self) -> bool {
|
||||||
|
!self.did("hub.apply-permit") && !self.did("target.apply")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn drive(
|
||||||
|
lease: &RestoreLease,
|
||||||
|
log: &Log,
|
||||||
|
hub: FakeHub,
|
||||||
|
target: FakeTarget,
|
||||||
|
source: Option<FakeSource>,
|
||||||
|
) -> Outcome {
|
||||||
|
let result = run(&hub, lease, &target, source.as_ref(), SHORT).await;
|
||||||
|
let log = log.lock().unwrap().clone();
|
||||||
|
Outcome { result, log }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn log() -> Log {
|
||||||
|
Log::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn full_restore_runs_in_order_and_reports_applied() {
|
||||||
|
let log = log();
|
||||||
|
let out = drive(
|
||||||
|
&lease(),
|
||||||
|
&log,
|
||||||
|
FakeHub::new(&log),
|
||||||
|
FakeTarget::new(&log),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
let steps: Vec<&str> = out
|
||||||
|
.log
|
||||||
|
.iter()
|
||||||
|
.map(String::as_str)
|
||||||
|
.filter(|entry| *entry != "target.identity")
|
||||||
|
.collect();
|
||||||
|
assert_eq!(
|
||||||
|
steps,
|
||||||
|
[
|
||||||
|
"report:checking",
|
||||||
|
"hub.download",
|
||||||
|
"target.prepare",
|
||||||
|
"report:safety",
|
||||||
|
"target.safety",
|
||||||
|
"hub.safety-upload",
|
||||||
|
"hub.apply-permit",
|
||||||
|
"target.apply",
|
||||||
|
"report:applied",
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- safety capture: nothing destructive may happen unless the safety copy is stored ----
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn safety_dump_failure_never_applies() {
|
||||||
|
let log = log();
|
||||||
|
let mut target = FakeTarget::new(&log);
|
||||||
|
target.safety = Step::Fail;
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out.never_applied());
|
||||||
|
assert!(out
|
||||||
|
.failed("safety_failed")
|
||||||
|
.unwrap()
|
||||||
|
.contains("safety dump exploded"));
|
||||||
|
assert!(!out.did("hub.safety-upload"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn safety_upload_failure_never_applies() {
|
||||||
|
let log = log();
|
||||||
|
let mut hub = FakeHub::new(&log);
|
||||||
|
hub.upload = Step::Fail;
|
||||||
|
let out = drive(&lease(), &log, hub, FakeTarget::new(&log), None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out.never_applied());
|
||||||
|
assert!(out
|
||||||
|
.failed("safety_failed")
|
||||||
|
.unwrap()
|
||||||
|
.contains("upload exploded"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn target_changed_during_safety_capture_never_applies() {
|
||||||
|
let log = log();
|
||||||
|
// Verified as the expected target, then a different database answers after the dump.
|
||||||
|
let target =
|
||||||
|
FakeTarget::new(&log).identities(vec![Ok(target_identity()), Ok(source_identity())]);
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out.never_applied());
|
||||||
|
assert!(out
|
||||||
|
.failed("safety_failed")
|
||||||
|
.unwrap()
|
||||||
|
.contains("target changed during safety capture"));
|
||||||
|
assert!(
|
||||||
|
!out.did("hub.safety-upload"),
|
||||||
|
"a dump of the wrong database must not be stored"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn target_unreachable_after_safety_capture_never_applies() {
|
||||||
|
let log = log();
|
||||||
|
let target = FakeTarget::new(&log).identities(vec![Ok(target_identity()), Err("gone")]);
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out.never_applied());
|
||||||
|
assert!(out.failed("safety_failed").is_some());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- identity checks ----
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn identity_mismatch_refuses_before_any_dump() {
|
||||||
|
let log = log();
|
||||||
|
let target = FakeTarget::new(&log).identities(vec![Ok(identity("1", "other"))]);
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out
|
||||||
|
.failed("wrong_target")
|
||||||
|
.unwrap()
|
||||||
|
.contains("identity does not match"));
|
||||||
|
assert!(out.never_applied());
|
||||||
|
assert!(
|
||||||
|
!out.did("target.safety"),
|
||||||
|
"must refuse before touching the target"
|
||||||
|
);
|
||||||
|
assert!(!out.did("report:safety"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn identity_probe_error_is_a_wrong_target() {
|
||||||
|
let log = log();
|
||||||
|
let target = FakeTarget::new(&log).identities(vec![Err("connection refused")]);
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out
|
||||||
|
.failed("wrong_target")
|
||||||
|
.unwrap()
|
||||||
|
.contains("connection refused"));
|
||||||
|
assert!(out.never_applied());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn restoring_a_backup_onto_its_own_source_database_is_refused() {
|
||||||
|
let log = log();
|
||||||
|
let mut lease = lease();
|
||||||
|
lease.artifact.identity = target_identity(); // artifact was taken from the target itself
|
||||||
|
let out = drive(
|
||||||
|
&lease,
|
||||||
|
&log,
|
||||||
|
FakeHub::new(&log),
|
||||||
|
FakeTarget::new(&log),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out.failed("wrong_target").is_some());
|
||||||
|
assert!(out.never_applied());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn rollback_may_restore_onto_the_database_the_copy_came_from() {
|
||||||
|
let log = log();
|
||||||
|
let lease = lease_for(Uuid::new_v4(), Uuid::new_v4(), true);
|
||||||
|
assert_eq!(lease.artifact.identity, lease.job.target.identity);
|
||||||
|
let out = drive(
|
||||||
|
&lease,
|
||||||
|
&log,
|
||||||
|
FakeHub::new(&log),
|
||||||
|
FakeTarget::new(&log),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out.did("report:applied"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn rollback_still_requires_the_target_identity_to_match() {
|
||||||
|
let log = log();
|
||||||
|
let lease = lease_for(Uuid::new_v4(), Uuid::new_v4(), true);
|
||||||
|
let target = FakeTarget::new(&log).identities(vec![Ok(identity("1", "impostor"))]);
|
||||||
|
let out = drive(&lease, &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out.failed("wrong_target").is_some());
|
||||||
|
assert!(out.never_applied());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn target_that_is_the_local_backup_source_is_refused() {
|
||||||
|
let log = log();
|
||||||
|
let source = FakeSource(Ok(target_identity()));
|
||||||
|
let out = drive(
|
||||||
|
&lease(),
|
||||||
|
&log,
|
||||||
|
FakeHub::new(&log),
|
||||||
|
FakeTarget::new(&log),
|
||||||
|
Some(source),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out
|
||||||
|
.failed("wrong_target")
|
||||||
|
.unwrap()
|
||||||
|
.contains("target resolves to the local backup source"));
|
||||||
|
assert!(out.never_applied());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_different_local_source_does_not_block_a_restore() {
|
||||||
|
let log = log();
|
||||||
|
let source = FakeSource(Ok(source_identity()));
|
||||||
|
let out = drive(
|
||||||
|
&lease(),
|
||||||
|
&log,
|
||||||
|
FakeHub::new(&log),
|
||||||
|
FakeTarget::new(&log),
|
||||||
|
Some(source),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out.did("report:applied"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Documented as designed: the source guard is best effort. If the local source cannot be identified (its
|
||||||
|
/// probe errors), the restore proceeds on the strength of the target identity checks alone.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn unidentifiable_local_source_does_not_block_a_restore() {
|
||||||
|
let log = log();
|
||||||
|
let source = FakeSource(Err("source down"));
|
||||||
|
let out = drive(
|
||||||
|
&lease(),
|
||||||
|
&log,
|
||||||
|
FakeHub::new(&log),
|
||||||
|
FakeTarget::new(&log),
|
||||||
|
Some(source),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out.did("report:applied"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- per-step timeouts map to their own failure state, and never reach apply ----
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn download_timeout_is_invalid_payload() {
|
||||||
|
let log = log();
|
||||||
|
let mut hub = FakeHub::new(&log);
|
||||||
|
hub.download = Step::Hang;
|
||||||
|
let out = drive(&lease(), &log, hub, FakeTarget::new(&log), None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert_eq!(out.failed("invalid_payload"), Some("timed out"));
|
||||||
|
assert!(out.never_applied());
|
||||||
|
assert!(!out.did("target.safety"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn prepare_timeout_is_invalid_payload() {
|
||||||
|
let log = log();
|
||||||
|
let mut target = FakeTarget::new(&log);
|
||||||
|
target.prepare = Step::Hang;
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert_eq!(out.failed("invalid_payload"), Some("timed out"));
|
||||||
|
assert!(out.never_applied());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn prepare_error_is_invalid_payload_with_its_reason() {
|
||||||
|
let log = log();
|
||||||
|
let mut target = FakeTarget::new(&log);
|
||||||
|
target.prepare = Step::Fail;
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out
|
||||||
|
.failed("invalid_payload")
|
||||||
|
.unwrap()
|
||||||
|
.contains("prepare exploded"));
|
||||||
|
assert!(out.never_applied());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn identity_check_timeout_is_wrong_target() {
|
||||||
|
let log = log();
|
||||||
|
let mut target = FakeTarget::new(&log);
|
||||||
|
target.hang_identity = true;
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert_eq!(out.failed("wrong_target"), Some("timed out"));
|
||||||
|
assert!(out.never_applied());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn safety_dump_timeout_is_safety_failed() {
|
||||||
|
let log = log();
|
||||||
|
let mut target = FakeTarget::new(&log);
|
||||||
|
target.safety = Step::Hang;
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert_eq!(out.failed("safety_failed"), Some("timed out"));
|
||||||
|
assert!(out.never_applied());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn safety_upload_timeout_is_safety_failed() {
|
||||||
|
let log = log();
|
||||||
|
let mut hub = FakeHub::new(&log);
|
||||||
|
hub.upload = Step::Hang;
|
||||||
|
let out = drive(&lease(), &log, hub, FakeTarget::new(&log), None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert_eq!(out.failed("safety_failed"), Some("timed out"));
|
||||||
|
assert!(out.never_applied());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn apply_timeout_is_apply_failed_after_the_permit() {
|
||||||
|
let log = log();
|
||||||
|
let mut target = FakeTarget::new(&log);
|
||||||
|
target.apply = Step::Hang;
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert_eq!(out.failed("apply_failed"), Some("timed out"));
|
||||||
|
assert!(out.did("hub.apply-permit") && out.did("target.apply"));
|
||||||
|
assert!(!out.did("report:applied"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn apply_error_is_apply_failed_with_its_reason() {
|
||||||
|
let log = log();
|
||||||
|
let mut target = FakeTarget::new(&log);
|
||||||
|
target.apply = Step::Fail;
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert!(out
|
||||||
|
.failed("apply_failed")
|
||||||
|
.unwrap()
|
||||||
|
.contains("apply exploded"));
|
||||||
|
assert!(!out.did("report:applied"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- the permit ----
|
||||||
|
|
||||||
|
/// The permit is single-use and the agent does not retry it. If the response is lost (or the hub refuses),
|
||||||
|
/// `run` returns the error WITHOUT applying and without reporting anything further: the hub has consumed
|
||||||
|
/// (or may have consumed) the permit, so the target stays locked until the hub reconciles the lease.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn lost_apply_permit_response_does_not_apply_and_leaves_the_lease_for_reconciliation() {
|
||||||
|
let log = log();
|
||||||
|
let mut hub = FakeHub::new(&log);
|
||||||
|
hub.fail_path = Some("/apply");
|
||||||
|
let out = drive(&lease(), &log, hub, FakeTarget::new(&log), None).await;
|
||||||
|
assert!(out
|
||||||
|
.result
|
||||||
|
.as_ref()
|
||||||
|
.unwrap_err()
|
||||||
|
.to_string()
|
||||||
|
.contains("response lost"));
|
||||||
|
assert!(out.did("hub.apply-permit"));
|
||||||
|
assert!(
|
||||||
|
!out.did("target.apply"),
|
||||||
|
"no permit response, no destructive step"
|
||||||
|
);
|
||||||
|
assert_eq!(out.log.last().map(String::as_str), Some("hub.apply-permit"));
|
||||||
|
assert!(out
|
||||||
|
.log
|
||||||
|
.iter()
|
||||||
|
.all(|e| !e.starts_with("failed:") && e != "report:applied"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Agent dies (or is cancelled) after the permit but before the restore finishes. The hub has seen the
|
||||||
|
/// permit but never a terminal report, so the lease is left mid-flight for the hub to expire.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn crash_between_permit_and_apply_leaves_no_terminal_report() {
|
||||||
|
let log = log();
|
||||||
|
let hub = FakeHub::new(&log);
|
||||||
|
let mut target = FakeTarget::new(&log);
|
||||||
|
target.apply = Step::Hang;
|
||||||
|
let lease = lease();
|
||||||
|
let crashed = tokio::time::timeout(
|
||||||
|
Duration::from_millis(200),
|
||||||
|
run(
|
||||||
|
&hub,
|
||||||
|
&lease,
|
||||||
|
&target,
|
||||||
|
None::<&FakeSource>,
|
||||||
|
Duration::from_secs(30),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
assert!(crashed.is_err(), "the run was dropped mid-apply");
|
||||||
|
let log = log.lock().unwrap().clone();
|
||||||
|
assert!(log.contains(&"hub.apply-permit".to_owned()));
|
||||||
|
assert!(log.contains(&"target.apply".to_owned()));
|
||||||
|
assert!(log
|
||||||
|
.iter()
|
||||||
|
.all(|e| e != "report:applied" && !e.starts_with("failed:")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn failure_reasons_are_cleaned_before_they_are_reported() {
|
||||||
|
let log = log();
|
||||||
|
let target = FakeTarget::new(&log).identities(vec![Err("line one\nline\ttwo \u{1F4A5}")]);
|
||||||
|
let out = drive(&lease(), &log, FakeHub::new(&log), target, None).await;
|
||||||
|
out.result.as_ref().unwrap();
|
||||||
|
assert_eq!(out.failed("wrong_target"), Some("line one line two ?"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- poll ----
|
||||||
|
|
||||||
|
fn poll_fixture(
|
||||||
|
log: &Log,
|
||||||
|
lease: &RestoreLease,
|
||||||
|
target_id: Uuid,
|
||||||
|
) -> (FakeHub, HashMap<Uuid, FakeTarget>) {
|
||||||
|
let hub = FakeHub::new(log);
|
||||||
|
hub.claims
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.push_back(serde_json::to_string(lease).unwrap());
|
||||||
|
let targets = HashMap::from([(target_id, FakeTarget::new(log))]);
|
||||||
|
(hub, targets)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn poll_with_nothing_to_claim_does_nothing() {
|
||||||
|
let log = log();
|
||||||
|
let hub = FakeHub::new(&log);
|
||||||
|
let targets = HashMap::from([(Uuid::new_v4(), FakeTarget::new(&log))]);
|
||||||
|
let did = poll(
|
||||||
|
&hub,
|
||||||
|
Uuid::new_v4(),
|
||||||
|
None::<&FakeSource>,
|
||||||
|
&targets,
|
||||||
|
1,
|
||||||
|
SHORT,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert!(!did);
|
||||||
|
assert!(log.lock().unwrap().is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn poll_refuses_a_lease_for_another_target_or_application() {
|
||||||
|
for swap_target in [true, false] {
|
||||||
|
let log = log();
|
||||||
|
let (target_id, application) = (Uuid::new_v4(), Uuid::new_v4());
|
||||||
|
let lease = if swap_target {
|
||||||
|
lease_for(Uuid::new_v4(), application, false)
|
||||||
|
} else {
|
||||||
|
lease_for(target_id, Uuid::new_v4(), false)
|
||||||
|
};
|
||||||
|
let (hub, targets) = poll_fixture(&log, &lease, target_id);
|
||||||
|
let err = poll(&hub, application, None::<&FakeSource>, &targets, 1, SHORT)
|
||||||
|
.await
|
||||||
|
.unwrap_err();
|
||||||
|
assert!(err.to_string().contains("unexpected restore target"));
|
||||||
|
assert!(
|
||||||
|
log.lock().unwrap().is_empty(),
|
||||||
|
"nothing may run for a mismatched lease"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn poll_runs_a_matching_lease() {
|
||||||
|
let log = log();
|
||||||
|
let (target_id, application) = (Uuid::new_v4(), Uuid::new_v4());
|
||||||
|
let lease = lease_for(target_id, application, false);
|
||||||
|
let (hub, targets) = poll_fixture(&log, &lease, target_id);
|
||||||
|
let did = poll(
|
||||||
|
&hub,
|
||||||
|
application,
|
||||||
|
None::<&FakeSource>,
|
||||||
|
&targets,
|
||||||
|
5,
|
||||||
|
Duration::from_secs(60),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert!(did);
|
||||||
|
assert!(log.lock().unwrap().contains(&"report:applied".to_owned()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Current behaviour, flagged in the report as a risk: a failed heartbeat report abandons the in-flight
|
||||||
|
/// restore future (and with it the apply, whose child processes are `kill_on_drop`), so a transient hub
|
||||||
|
/// blip mid-apply can kill a restore half way with no terminal report. Pinned so a deliberate change shows.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn failed_heartbeat_abandons_the_in_flight_restore() {
|
||||||
|
let log = log();
|
||||||
|
let (target_id, application) = (Uuid::new_v4(), Uuid::new_v4());
|
||||||
|
let lease = lease_for(target_id, application, false);
|
||||||
|
let (mut hub, mut targets) = poll_fixture(&log, &lease, target_id);
|
||||||
|
hub.fail_progress = Some("heartbeat");
|
||||||
|
targets.get_mut(&target_id).unwrap().apply = Step::Hang;
|
||||||
|
let err = tokio::time::timeout(
|
||||||
|
Duration::from_secs(5),
|
||||||
|
poll(
|
||||||
|
&hub,
|
||||||
|
application,
|
||||||
|
None::<&FakeSource>,
|
||||||
|
&targets,
|
||||||
|
30,
|
||||||
|
Duration::from_millis(50),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("poll returns once the heartbeat fails")
|
||||||
|
.unwrap_err();
|
||||||
|
assert!(err.to_string().contains("hub unreachable"));
|
||||||
|
let log = log.lock().unwrap().clone();
|
||||||
|
assert!(
|
||||||
|
log.contains(&"target.apply".to_owned()),
|
||||||
|
"apply had started"
|
||||||
|
);
|
||||||
|
assert!(log
|
||||||
|
.iter()
|
||||||
|
.all(|e| e != "report:applied" && !e.starts_with("failed:")));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user