Cover package, kargs and staging contracts with Rust tests (D-098)
This commit is contained in:
parent
022cd34416
commit
8556954e7c
4 changed files with 130 additions and 12 deletions
|
|
@ -387,3 +387,27 @@ fn karg_edits(args: &[&str]) -> Result<Vec<Dictionary>, String> {
|
|||
if result.is_empty() { return Err("No kernel argument edits".into()); }
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod interface_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn kargs_cli_forms_roundtrip_through_typed_wire_values() {
|
||||
let edits = karg_edits(&["--append=console=tty0", "--append", "example=two words", "--delete-if-present=quiet"]).unwrap();
|
||||
let decoded = Value::Array(edits.into_iter().map(|row| map(row).unwrap()).collect());
|
||||
assert_eq!(decoded, json!([
|
||||
{"op":"append","argument":"console=tty0"},
|
||||
{"op":"append","argument":"example=two words"},
|
||||
{"op":"delete-if-present","argument":"quiet"}]));
|
||||
super::super::kargs::validate(&decoded).unwrap();
|
||||
for args in [vec![], vec!["--append"], vec!["--execute=quiet"], vec!["append=quiet"]] {
|
||||
assert!(karg_edits(&args).is_err());
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn global_flags_preserve_host_command_routing() {
|
||||
let args = ["--json", "--request-id", "12345678-1234-1234-1234-123456789abc", "--offline", "extension", "status"].map(str::to_owned);
|
||||
assert_eq!(command(&args), Some("extension"));
|
||||
assert_eq!(error_exit_code(r#"{"code":"PlanStale"}"#), archipelago_contracts::system_bus::ErrorCode::PlanStale.exit_code());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,19 @@ mod tests {
|
|||
assert!(validate(&json!([{"op":"append","argument":"rd.driver.blacklist=nouveau"}])).is_ok());
|
||||
assert!(validate(&json!([{"op":"execute","argument":"quiet"}])).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn stage_inherits_pending_arguments_with_duplicates_and_spaces() {
|
||||
let rows = json!([
|
||||
{"booted":true,"arguments":["quiet"]},
|
||||
{"staged":true,"arguments":["console=tty0","console=ttyS0,115200","example=two words"]}]);
|
||||
let args = deploy_args(&"a".repeat(64), &selected(&rows).unwrap()["arguments"]).unwrap();
|
||||
assert_eq!(&args[6..9], ["--karg-append=console=tty0", "--karg-append=console=ttyS0,115200", "--karg-append=example=two words"]);
|
||||
assert!(args.contains(&"--stage".into()) && args.contains(&"--retain".into()));
|
||||
assert!(!args.iter().any(|a| a == "--karg-append=quiet"));
|
||||
assert!(deploy_args("stable/kde", &json!([])).is_err());
|
||||
assert!(deploy_args(&"a".repeat(64), &json!([false])).is_err());
|
||||
assert!(selected(&json!([])).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inspect() -> Result<Value> {
|
||||
|
|
|
|||
|
|
@ -306,19 +306,73 @@ fn retained_requests(worker: &Worker, state: &Value, catalog: &Value) -> Result<
|
|||
}).collect()
|
||||
}
|
||||
|
||||
pub fn prepare_change(worker: &Worker, changes: Vec<Change>, expected_context: &str, uid: u32) -> Result<Value> {
|
||||
let _lock = crate::updates::lock().map_err(|_| Failure::new(ErrorCode::Busy, "Sysroot unavailable or provisioning incomplete"))?;
|
||||
let (context, state) = current(worker)?;
|
||||
crate::updates::managed::ensure_pending_owned(worker, &state)?;
|
||||
let id = context_id(&context)?;
|
||||
if id != expected_context { return Err(Failure::new(ErrorCode::PlanStale, "Host context changed")); }
|
||||
if feature_policy()?["enabled"] != true { return Err(Failure::new(ErrorCode::Conflict, "Explicitly enable Host Extensions before changing requests")); }
|
||||
let catalog: Value = storage::json(&Path::new(ROOT).join("package-catalog.json"))?;
|
||||
if catalog["context_id"] != id { return Err(Failure::new(ErrorCode::PlanStale, "Refresh the selected snapshot catalog")); }
|
||||
let before = retained_requests(worker, &state, &catalog)?;
|
||||
let mut requested = before.clone();
|
||||
#[cfg(test)]
|
||||
mod acceptance_tests {
|
||||
use super::*;
|
||||
fn catalog() -> Value {
|
||||
json!({"context_id":"snapshot-a", "packages":[
|
||||
{"package_id":"nano-a","name":"nano","source":"repository","checksum":"a"},
|
||||
{"package_id":"tree-a","name":"tree","source":"repository","checksum":"b"},
|
||||
{"package_id":"linux-a","name":"linux","source":"repository","checksum":"c"}],
|
||||
"state":{"desired":{"managed_inventory":{"linux":"1"}}}})
|
||||
}
|
||||
fn add(id: &str) -> Change { Change::AddRepository { package_id: id.into() } }
|
||||
|
||||
#[test]
|
||||
fn cumulative_add_duplicate_remove_and_failed_request_preserve_intent() {
|
||||
let catalog = catalog();
|
||||
let nano = edit_requests(&[], &[add("nano-a")], &catalog, "snapshot-a", 1000).unwrap();
|
||||
let both = edit_requests(&nano, &[add("tree-a")], &catalog, "snapshot-a", 1000).unwrap();
|
||||
assert_eq!(both.iter().map(|r| r["name"].as_str().unwrap()).collect::<Vec<_>>(), ["nano", "tree"]);
|
||||
assert_eq!(edit_requests(&both, &[add("nano-a")], &catalog, "snapshot-a", 1000).unwrap(), both);
|
||||
let retained = both.clone();
|
||||
assert!(edit_requests(&both, &[Change::Remove { request_id: both[0]["request_id"].as_str().unwrap().into() }, add("missing")], &catalog, "snapshot-a", 1000).is_err());
|
||||
assert_eq!(both, retained);
|
||||
let remaining = edit_requests(&both, &[Change::Remove { request_id: both[0]["request_id"].as_str().unwrap().into() }], &catalog, "snapshot-a", 1000).unwrap();
|
||||
assert_eq!(remaining, vec![both[1].clone()]);
|
||||
assert_eq!(nano[0]["name"], "nano");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn managed_packages_stale_catalog_and_conflicting_batch_are_rejected() {
|
||||
let catalog = catalog();
|
||||
assert_eq!(edit_requests(&[], &[add("linux-a")], &catalog, "snapshot-a", 1000).unwrap_err().code, ErrorCode::Conflict);
|
||||
assert_eq!(edit_requests(&[], &[add("nano-a")], &catalog, "snapshot-b", 1000).unwrap_err().code, ErrorCode::PlanStale);
|
||||
assert!(edit_requests(&[], &[add("nano-a"), add("nano-a")], &catalog, "snapshot-a", 1000).is_err());
|
||||
let overrides = edit_requests(&[], &[Change::OverrideRepository { package_id:"linux-a".into(), replaces:"linux".into(), reason:"explicit test".into() }], &catalog, "snapshot-a", 1000).unwrap();
|
||||
let id = overrides[0]["request_id"].as_str().unwrap().to_owned();
|
||||
assert!(edit_requests(&overrides, &[Change::Remove { request_id:id.clone() }], &catalog, "snapshot-a", 1000).is_err());
|
||||
assert!(edit_requests(&overrides, &[Change::ResetOverride { request_id:id }], &catalog, "snapshot-a", 1000).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_inputs_are_never_silently_discarded() {
|
||||
let retained = vec![json!({"request_id":"legacy","name":"custom","source":"unknown"})];
|
||||
assert!(edit_requests(&retained, &[add("nano-a")], &catalog(), "snapshot-a", 1000).is_err());
|
||||
let result = edit_requests(&retained, &[Change::Remove { request_id:"legacy".into() }, add("nano-a")], &catalog(), "snapshot-a", 1000).unwrap();
|
||||
assert_eq!(result.len(), 1);
|
||||
assert_eq!(result[0]["name"], "nano");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_selection_ignores_other_stateroot_and_blocks_finalized_pending() {
|
||||
let boot = "a".repeat(64); let pending = "b".repeat(64);
|
||||
let mut state = json!({"deployments":[
|
||||
{"stateroot":"other","checksum":"c".repeat(64),"staged":true},
|
||||
{"stateroot":"archipelago","checksum":boot,"booted":true},
|
||||
{"stateroot":"archipelago","checksum":pending,"staged":true,"pending":true}]});
|
||||
assert_eq!(selected_checksum(&state).unwrap(), pending);
|
||||
state["deployments"][2]["staged"] = json!(false);
|
||||
assert_eq!(selected_checksum(&state).unwrap_err().code, ErrorCode::Conflict);
|
||||
state["deployments"].as_array_mut().unwrap().pop();
|
||||
assert_eq!(selected_checksum(&state).unwrap(), boot);
|
||||
}
|
||||
}
|
||||
|
||||
fn edit_requests(before: &[Value], changes: &[Change], catalog: &Value, id: &str, uid: u32) -> Result<Vec<Value>> {
|
||||
let mut requested = before.to_vec();
|
||||
let mut touched = std::collections::BTreeSet::new();
|
||||
for change in &changes {
|
||||
for change in changes {
|
||||
let (mut row, replacement) = match change {
|
||||
Change::Remove { request_id } | Change::ResetOverride { request_id } => {
|
||||
let index = requested.iter().position(|p| p["request_id"] == *request_id)
|
||||
|
|
@ -368,6 +422,20 @@ pub fn prepare_change(worker: &Worker, changes: Vec<Change>, expected_context: &
|
|||
if requested.iter().any(|p| p["source"] == "unknown") {
|
||||
return Err(Failure::new(ErrorCode::Unsupported, "Unknown legacy inputs require explicit removal or a new protected import"));
|
||||
}
|
||||
Ok(requested)
|
||||
}
|
||||
|
||||
pub fn prepare_change(worker: &Worker, changes: Vec<Change>, expected_context: &str, uid: u32) -> Result<Value> {
|
||||
let _lock = crate::updates::lock().map_err(|_| Failure::new(ErrorCode::Busy, "Sysroot unavailable or provisioning incomplete"))?;
|
||||
let (context, state) = current(worker)?;
|
||||
crate::updates::managed::ensure_pending_owned(worker, &state)?;
|
||||
let id = context_id(&context)?;
|
||||
if id != expected_context { return Err(Failure::new(ErrorCode::PlanStale, "Host context changed")); }
|
||||
if feature_policy()?["enabled"] != true { return Err(Failure::new(ErrorCode::Conflict, "Explicitly enable Host Extensions before changing requests")); }
|
||||
let catalog: Value = storage::json(&Path::new(ROOT).join("package-catalog.json"))?;
|
||||
if catalog["context_id"] != id { return Err(Failure::new(ErrorCode::PlanStale, "Refresh the selected snapshot catalog")); }
|
||||
let before = retained_requests(worker, &state, &catalog)?;
|
||||
let requested = edit_requests(&before, &changes, &catalog, &id, uid)?;
|
||||
worker.events.checkpoint(Phase::Planning, true, "Resolving the complete desired extension set against its managed base")?;
|
||||
let sandbox = Sandbox::create(worker, &context.managed_base)?;
|
||||
for row in requested.iter().filter(|r| r["source"] == "local") {
|
||||
|
|
|
|||
|
|
@ -360,6 +360,19 @@ pub(crate) fn staged(state: &Value, target: &str) -> Result<Value> {
|
|||
mod compatibility_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn staging_requires_one_exact_target_and_preserves_serial_identity() {
|
||||
let target = "b".repeat(64);
|
||||
let mut state = json!({"deployments":[
|
||||
{"stateroot":"archipelago","checksum":"a".repeat(64),"booted":true,"serial":0},
|
||||
{"stateroot":"archipelago","checksum":target,"staged":true,"serial":3}]});
|
||||
assert_eq!(staged(&state, &target).unwrap()["deployment_id"], format!("archipelago/{target}.3"));
|
||||
assert!(staged(&state, &"c".repeat(64)).is_err());
|
||||
let duplicate = state["deployments"][1].clone();
|
||||
state["deployments"].as_array_mut().unwrap().push(duplicate);
|
||||
assert!(staged(&state, &target).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn published_hosts_without_overlay_metadata_keep_the_existing_base() {
|
||||
let published = "a".repeat(64);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue