//! The cases of docs/migrations.md, as far as the first version goes: a renamed slot //! and a rollback across it, a type that changes without a converter, and what is //! kept. G1 and G2 are generations, S1 and S2 their schemas. mod common; use common::Host; use enstate::view::Change; use serde_json::{Value, json}; fn secrets(slots: Value, views: Value) -> Value { json!({ "schema": 1, "channels": { "secrets": { "slots": slots } }, "views": views }) } fn rotate(host: &Host, name: &str, slot: &str, bytes: &[u8]) -> std::path::PathBuf { let address = format!("secrets/{slot}"); host.submission( name, json!({ "channel": "secrets", "items": [{ "address": address, "op": "set" }] }), &[(address.as_str(), bytes)], ) } /// Case 1: S2 renames `tinc.key` to `retiolum.key`. Switching to G2 needs no /// submission; a rotation under G2 stores the state under S2; rolling back to G1 /// gives it the rotated key, by S2's rename read backwards. #[test] fn a_renamed_slot_and_a_rollback() { let host = Host::new("rename"); let s1 = host.schema(secrets( json!({ "tinc.key": { "type": "application/octet-stream", "secret": true } }), json!({ "tinc.key": { "from": ["secrets/tinc.key"], "reactions": { "restart": ["tinc.service"] } } }), )); let s2_value = secrets( json!({ "retiolum.key": { "type": "application/octet-stream", "secret": true, "renamedFrom": "tinc.key" } }), json!({ "retiolum.key": { "from": ["secrets/retiolum.key"], "reactions": { "restart": ["tincr.service"] } } }), ); // G1 runs, K submitted. assert!( host.apply(&s1, &rotate(&host, "k", "tinc.key", b"K")) .accepted ); let first = host.snapshot("secrets").unwrap(); // Before switching to G2: it would see K, under its new name. let s2 = host.schema(s2_value.clone()); let migration = host.migrate(&s2, true); assert!(migration.accepted, "{migration:?}"); assert!( migration.channels["secrets"].seen.notes[0] .contains("secrets/retiolum.key: from secrets/tinc.key, renamed"), "{migration:?}" ); assert!(host.view("tinc.key", "tinc.key").is_some()); // Switching: the views follow, the snapshot isn't rewritten. let migration = host.migrate(&s2, false); assert!( migration.accepted && migration.failed.is_empty(), "{migration:?}" ); assert_eq!(host.view("retiolum.key", "retiolum.key").unwrap(), b"K"); assert!(host.view("tinc.key", "tinc.key").is_none()); assert!( host.reactions() .contains("--no-block try-restart tincr.service") ); let stored = host.store().current("secrets").unwrap().unwrap(); assert_eq!(stored.id, first); assert_eq!(stored.schema, s1.1); // Rotating under G2 stores the state under S2, and nothing is kept: tinc.key went // into retiolum.key. let plan = host.apply(&s2, &rotate(&host, "k2", "retiolum.key", b"K'")); assert!(plan.accepted && plan.warnings.is_empty(), "{plan:?}"); let stored = host.store().current("secrets").unwrap().unwrap(); assert_eq!(stored.schema, s2.1); assert_eq!(stored.content.keys().collect::>(), ["retiolum.key"]); assert!(stored.kept.is_empty()); // Rolling back to G1: tinc.key is the rotated key, from S2's rename. let s1 = host.schema(serde_json::from_slice(&s1.1).unwrap()); let migration = host.migrate(&s1, false); assert!(migration.accepted, "{migration:?}"); assert!( migration.channels["secrets"].seen.notes[0].contains( "secrets/tinc.key: from secrets/retiolum.key, which the schema it was accepted under renamed from it" ), "{migration:?}" ); assert_eq!(host.view("tinc.key", "tinc.key").unwrap(), b"K'"); assert!(host.view("retiolum.key", "retiolum.key").is_none()); } /// Trying an older generation and back costs nothing: the snapshot stays as it was. #[test] fn switching_back_and_forth_rewrites_nothing() { let host = Host::new("back-and-forth"); let s1 = host.schema(secrets( json!({ "a": { "type": "application/octet-stream" } }), json!({ "a": { "from": ["secrets/a"] } }), )); assert!(host.apply(&s1, &rotate(&host, "a", "a", b"A")).accepted); let id = host.snapshot("secrets").unwrap(); let s2 = host.schema(secrets( json!({ "b": { "type": "application/octet-stream", "renamedFrom": "a" } }), json!({ "b": { "from": ["secrets/b"] } }), )); assert!(host.migrate(&s2, false).accepted); let s1 = host.schema(serde_json::from_slice(&s1.1).unwrap()); assert!(host.migrate(&s1, false).accepted); assert!(host.migrate(&s2, false).accepted); assert_eq!(host.snapshot("secrets").unwrap(), id); assert_eq!(host.view("b", "b").unwrap(), b"A"); } /// Case 2, without converters (not in the first version): a slot whose type changes /// can't be filled, so the switch is refused before switching, naming the slot; after /// a switch anyway (a boot), the views made from it aren't there. #[test] fn a_type_without_a_converter() { let host = Host::new("type"); let fleet = |media_type: &str| { json!({ "schema": 1, "channels": { "fleet": { "slots": { "hosts": { "type": media_type } } } }, "views": { "hosts": { "from": ["fleet/hosts"] } } }) }; let s1 = host.schema(fleet("application/x-hosts1+json")); let first = host.submission( "first", json!({ "channel": "fleet", "items": [{ "address": "fleet/hosts/ne", "op": "set" }] }), &[("fleet/hosts", br#"{"ne": {"addr": "a"}}"#)], ); assert!(host.apply(&s1, &first).accepted); let s2 = host.schema(fleet("application/x-hosts2+json")); let migration = host.migrate(&s2, true); assert!(!migration.accepted); assert_eq!( migration.refused, ["required slot fleet/hosts can't be filled"] ); assert!( migration.channels["fleet"].seen.notes[0].contains( "fleet/hosts: type application/x-hosts1+json is now application/x-hosts2+json, and there is no converter" ), "{migration:?}" ); // The dry run changed nothing; a switch anyway takes the view away. assert!(host.view("hosts", "hosts").is_some()); let migration = host.migrate(&s2, false); assert!(!migration.accepted); assert!( migration .views .iter() .any(|view| view.name == "hosts" && view.change == Change::Removed) ); assert!(host.view("hosts", "hosts").is_none()); // A submission made for S2 fills it, and the old entry goes: it is older. let fresh = host.submission( "fresh", json!({ "channel": "fleet", "items": [{ "address": "fleet/hosts/ne", "op": "set" }] }), &[("fleet/hosts", br#"{"ne": {"addrs": ["a", "b"]}}"#)], ); let plan = host.apply(&s2, &fresh); assert!(plan.accepted, "{plan:?}"); assert_eq!( plan.warnings, [ "goes, as fleet/hosts is filled anew: the entry of type application/x-hosts1+json kept from before" ] ); assert!(host.view("hosts", "hosts").is_some()); // Back to G1 is refused before switching: nothing in S2 makes S1's type. let s1 = host.schema(serde_json::from_slice(&s1.1).unwrap()); let migration = host.migrate(&s1, true); assert_eq!( migration.refused, ["required slot fleet/hosts can't be filled"] ); assert!(migration.channels["fleet"].seen.notes[0].contains("is now application/x-hosts1+json")); } /// What no slot wants is kept, and fills its slot again after a rollback; a complete /// submission lets it go. #[test] fn what_is_kept() { let host = Host::new("kept"); let both = secrets( json!({ "a": { "type": "application/octet-stream" }, "extra": { "type": "application/octet-stream", "required": false } }), json!({ "extra": { "from": ["secrets/extra"] } }), ); let s1 = host.schema(both.clone()); let first = host.submission( "first", json!({ "channel": "secrets", "complete": true, "items": [ { "address": "secrets/a", "op": "set" }, { "address": "secrets/extra", "op": "set" } ] }), &[("secrets/a", b"A"), ("secrets/extra", b"E")], ); assert!(host.apply(&s1, &first).accepted); let s2 = host.schema(secrets( json!({ "a": { "type": "application/octet-stream" } }), json!({}), )); let migration = host.migrate(&s2, false); assert_eq!(migration.channels["secrets"].kept, ["extra"]); assert!(host.view("extra", "extra").is_none()); let plan = host.apply(&s2, &rotate(&host, "a2", "a", b"A2")); assert_eq!( plan.warnings, ["kept, in no slot of the schema: secrets/extra"] ); let stored = host.store().current("secrets").unwrap().unwrap(); assert_eq!(stored.kept["extra"].1, b"E"); // Back to G1: extra is there again. let s1 = host.schema(both); let migration = host.migrate(&s1, false); assert!(migration.channels["secrets"].seen.notes[0].contains("from what was kept")); assert_eq!(host.view("extra", "extra").unwrap(), b"E"); // Under G2 again, a complete submission lets it go. let s2 = host.schema(secrets( json!({ "a": { "type": "application/octet-stream" } }), json!({}), )); let complete = host.submission( "complete", json!({ "channel": "secrets", "complete": true, "items": [{ "address": "secrets/a", "op": "set" }] }), &[("secrets/a", b"A3")], ); let plan = host.apply(&s2, &complete); assert_eq!( plan.warnings, ["goes, as the submission is complete: secrets/extra, which was kept"] ); assert!( host.store() .current("secrets") .unwrap() .unwrap() .kept .is_empty() ); } /// A generation that adds a required slot: the switch is refused until a submission /// for it is staged, which changes nothing the running generation serves; a submission /// under the running generation keeps the staged entry; the switch brings it. And a /// first deployment, without any state. #[test] fn staging_for_the_next_generation() { let host = Host::new("stage"); let s1_value = secrets( json!({ "a": { "type": "application/octet-stream" } }), json!({ "a": { "from": ["secrets/a"] } }), ); let s1 = host.schema(s1_value.clone()); assert!(host.apply(&s1, &rotate(&host, "a", "a", b"A")).accepted); let reactions_before = host.reactions(); let s2_value = secrets( json!({ "a": { "type": "application/octet-stream" }, "b.key": { "type": "application/octet-stream", "secret": true } }), json!({ "a": { "from": ["secrets/a"] }, "b.key": { "from": ["secrets/b.key"], "reactions": { "restart": ["b.service"] } } }), ); let s2 = host.schema(s2_value.clone()); assert_eq!( host.migrate(&s2, true).refused, ["required slot secrets/b.key can't be filled"] ); // Staged: the snapshot, under S2; no views, no reactions. let plan = host.stage(&s2, &rotate(&host, "b", "b.key", b"B")); assert!( plan.accepted && plan.staged && plan.snapshot.is_some(), "{plan:?}" ); assert!(host.view("b.key", "b.key").is_none()); assert_eq!(host.view("a", "a").unwrap(), b"A"); assert_eq!(host.reactions(), reactions_before); assert_eq!( host.store().current("secrets").unwrap().unwrap().schema, 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); let plan = host.apply(&s1, &rotate(&host, "a2", "a", b"A2")); assert_eq!( plan.warnings, ["kept, in no slot of the schema: secrets/b.key"] ); // The switch: the staged entry is there, with its view and reaction. let s2 = host.schema(s2_value.clone()); let migration = host.migrate(&s2, false); assert!(migration.accepted, "{migration:?}"); assert_eq!(host.view("b.key", "b.key").unwrap(), b"B"); assert_eq!(host.view("a", "a").unwrap(), b"A2"); assert!( 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"); let s2 = fresh.schema(s2_value); let first = fresh.submission( "first", json!({ "channel": "secrets", "complete": true, "items": [ { "address": "secrets/a", "op": "set" }, { "address": "secrets/b.key", "op": "set" } ] }), &[("secrets/a", b"A"), ("secrets/b.key", b"B")], ); let plan = fresh.stage(&s2, &first); assert!(plan.accepted && plan.snapshot.is_some(), "{plan:?}"); assert!(fresh.view("a", "a").is_none()); 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"); }