Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/holger

server/lib/tests/the_role_comes_only_from_the_pairing.rs

//! ★ **The role comes only from the pairing** (stinky SB #36): a config that
//! names `role:` — either role — is refused at wiring, by name
//! (`holger-twin-21`); the box's role is the installer's election, recorded in
//! `holger-role.ron`, and nothing else.

use std::io::Write as _;

use holger_server_lib::{read_ron_config, wire_holger, Holger};

fn config(data: &std::path::Path, role: &str) -> tempfile::NamedTempFile {
    let ron = format!(
        r#"(
    repositories: [
        (ron_name: "crates", ron_repo_type: "raw", ron_upstreams: [], ron_in: None,
         ron_out: Some((ron_storage_endpoint: "storage", ron_exposed_endpoint: "default")),
         ron_data_dir: Some("{data}/crates")),
    ],
    exposed_endpoints: [
        (ron_name: "default", ron_url: "127.0.0.1:1", ron_http_url: Some("127.0.0.1:2")),
    ],
    storage_endpoints: [ (ron_name: "storage", ron_storage_type: "znippy", ron_path: "{data}") ],
    audit: ( dir: "{data}/audit", enabled: true ),
    {role}
)"#,
        data = data.display(),
    );
    let mut cfg = tempfile::NamedTempFile::new().unwrap();
    cfg.write_all(ron.as_bytes()).unwrap();
    cfg
}

#[test]
fn a_config_that_names_a_role_is_refused_and_the_election_decides() {
    for role in [
        "role: Primary,",
        "role: Twin(primary: \"10.0.0.1:50051\", poll_seconds: 1),",
    ] {
        let data = tempfile::tempdir().unwrap();
        let cfg = config(data.path(), role);
        let mut holger: Holger =
            read_ron_config(cfg.path()).expect("the key still parses, so the refusal can name it");
        let e = wire_holger(&mut holger).expect_err("a config role is refused");
        assert!(format!("{e:#}").contains("holger-twin-21"), "{role}: {e:#}");
    }

    // No role named: the box's recorded election decides.
    let data = tempfile::tempdir().unwrap();
    std::fs::write(
        holger_server_lib::twin::role_record_path(data.path()),
        r#"(role: Twin(primary: "10.0.0.1:50051", poll_seconds: 1), elected_at: 1, elected_by: "installer", reason: "test")"#,
    )
    .unwrap();
    let cfg = config(data.path(), "");
    let mut holger: Holger = read_ron_config(cfg.path()).unwrap();
    wire_holger(&mut holger).expect("an elected box wires");
    assert!(holger.role.is_twin(), "the election made it a twin");
    assert_eq!(holger.role.primary(), Some("10.0.0.1:50051"));
}