From b74359e97643be0f43034c3aeab533a2389a939f Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 30 Nov 2026 18:21:16 +0000 Subject: enstate: submissions waiting for the next switch --migrate first takes the submissions in STATE/incoming, in the order of their names: planned against the schema, stored if accepted (their views follow with the rest), removed, and moved to STATE/refused if refused, which refuses the migration. A dry run (the check before a switch) does so on a copy of the snapshots, changing nothing. --stage leaves a marker, STATE/migrate-due, for the next switch to migrate even if the schema doesn't change; --migrate removes it. Scratch directories are cleaned up without following the symbolic links in them. --- tests/migrations.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'tests/migrations.rs') diff --git a/tests/migrations.rs b/tests/migrations.rs index 1856b38..05e3099 100644 --- a/tests/migrations.rs +++ b/tests/migrations.rs @@ -293,6 +293,8 @@ fn staging_for_the_next_generation() { s2.1 ); assert!(host.migrate(&s2, true).accepted); + // A marker, for the next switch to serve the views; a dry run leaves it. + assert!(host.root.join("state/migrate-due").exists()); // The running generation goes on: what it has no slot for is kept. let s1 = host.schema(s1_value); @@ -312,6 +314,7 @@ fn staging_for_the_next_generation() { host.reactions() .contains("--no-block try-restart b.service") ); + assert!(!host.root.join("state/migrate-due").exists()); // A first deployment: no state, no views; staged, then switched to. let fresh = Host::new("stage-first"); @@ -330,3 +333,70 @@ fn staging_for_the_next_generation() { assert!(fresh.migrate(&s2, false).accepted); assert_eq!(fresh.view("b.key", "b.key").unwrap(), b"B"); } + +/// Submissions waiting in the incoming directory: planned against the next +/// generation's schema by the check before the switch (on a copy, changing nothing), +/// and taken by the switch, before the views; one refused refuses the switch, and is +/// moved aside. +#[test] +fn incoming_submissions() { + let host = Host::new("incoming"); + let s2 = host.schema(secrets( + json!({ "a": { "type": "application/octet-stream" } }), + json!({ "a": { "from": ["secrets/a"], "reactions": { "restart": ["a.service"] } } }), + )); + let put = |name: &str, dir: &std::path::Path| { + let incoming = host.root.join("state/incoming"); + std::fs::create_dir_all(&incoming).unwrap(); + std::fs::rename(dir, incoming.join(name)).unwrap(); + }; + // A first deployment: nothing but a waiting submission. + put("1", &rotate(&host, "first", "a", b"A")); + let check = host.migrate(&s2, true); + assert!(check.accepted, "{check:?}"); + assert!(check.incoming[0].plan.as_ref().unwrap().accepted); + assert!(host.snapshot("secrets").is_none()); + assert!(host.root.join("state/incoming/1").exists()); + // The dry run's copy of the snapshots is gone. + assert_eq!( + std::fs::read_dir(host.root.join("run/tmp")) + .unwrap() + .count(), + 0 + ); + // The switch. + let migration = host.migrate(&s2, false); + assert!(migration.accepted, "{migration:?}"); + assert_eq!(host.view("a", "a").unwrap(), b"A"); + assert!(host.snapshot("secrets").is_some()); + assert!(!host.root.join("state/incoming/1").exists()); + + // Two in order, the second building on what the first makes; then one refused, + // which refuses the switch, and is moved aside by it. + put("2", &rotate(&host, "second", "a", b"B")); + put("3", &rotate(&host, "third", "a", b"C")); + let migration = host.migrate(&s2, false); + assert!( + migration.accepted && migration.incoming.len() == 2, + "{migration:?}" + ); + assert_eq!(host.view("a", "a").unwrap(), b"C"); + let bad = host.submission( + "bad", + json!({ "channel": "secrets", "items": [{ "address": "secrets/nope", "op": "set" }] }), + &[("secrets/nope", b"x")], + ); + put("4", &bad); + let check = host.migrate(&s2, true); + assert!(!check.accepted); + assert!( + check.refused[0].starts_with("incoming 4: required item secrets/nope"), + "{check:?}" + ); + assert!(host.root.join("state/incoming/4").exists()); + let migration = host.migrate(&s2, false); + assert!(!migration.accepted); + assert!(host.root.join("state/refused/4").exists()); + assert!(!host.root.join("state/incoming/4").exists()); + assert_eq!(host.view("a", "a").unwrap(), b"C"); +} -- cgit v1.3.1