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

nordisk/gunnar

deploy/upcloud/tests/machine_decides_the_attach.rs

//! **The attach strategy is DERIVED from the declared machine, not remembered.**
//!
//! The re-image used to create two storages and attach one `cdrom` plus one
//! virtio `disk` because a person had learned that by hand, at the cost of an
//! hour against the live box. The fact was already written down FIVE times —
//! draupnir's `MediumAttach::{Cdrom,Virtio,Both}`, `installtest/Cargo.toml`'s
//! MEASURED 2026-08-15 note quoting the same `CD001` line,
//! `installtest/src/mother_kvm.rs`, this crate's own header, and the roster's
//! `MediumBring::IsoCdrom` — and only the sixth copy, the one in the cloud
//! path, was load-bearing.
//!
//! So the door reads the roster instead. What that buys, and what these tests
//! assert:
//!
//! * `upcloud` says `MediumBring::IsoCdrom`, so the medium rides twice.
//! * `scaleway-instance` says `ImportQcow2` — there is no ISO-attach door on
//!   that platform at all — so this verb is REFUSED BY NAME instead of
//!   attempted and mysteriously failing three API calls in.
//! * The refusal lands BEFORE the live appliance is stopped. A machine check
//!   that ran after the soft stop would leave a serving box down.

use gunnar_upcloud::api::DeviceKind;
use gunnar_upcloud::machine::{attach_for, Attach, MACHINE};

/// The one machine this door has ever run against, and the reason it needs two
/// faces of one medium.
#[test]
fn upcloud_takes_an_attached_iso_so_the_medium_rides_twice() {
    let attach = attach_for(MACHINE).expect("`upcloud` is in the roster and takes an attached ISO");
    assert_eq!(attach, Attach::CdromAndVirtio);

    let faces = attach.faces();
    assert_eq!(
        faces[0].kind,
        DeviceKind::Cdrom,
        "the firmware boots a cdrom"
    );
    assert_eq!(faces[0].suffix, "cdrom");
    assert_eq!(
        faces[1].kind,
        DeviceKind::Disk,
        "korp-installer probes ONLY virtio for the CD001 magic"
    );
    assert_eq!(faces[1].suffix, "virtio");
    assert_ne!(
        faces[0].suffix, faces[1].suffix,
        "two faces of one ISO need two storage titles"
    );
}

/// **The refusal this whole derivation exists for.**
///
/// `scaleway-instance` presents the identical device set to `upcloud` —
/// virtio-blk + virtio-net — so nothing about the SHAPE could have told the two
/// apart. The roster is what knows that one takes an attached ISO and the other
/// a qcow2 snapshot import.
#[test]
fn a_machine_that_imports_a_qcow2_is_refused_by_name() {
    let why = attach_for("scaleway-instance")
        .expect_err("there is no ISO-attach door on scaleway-instance");
    assert!(
        why.contains("scaleway-instance"),
        "names the machine: {why}"
    );
    assert!(why.contains("qcow2"), "names how the medium arrives: {why}");
    assert!(
        why.contains("INSTALLER path does not exist") || why.contains("no ISO attach"),
        "says WHY this verb cannot be the door: {why}"
    );
}

/// Every other roster machine that does not take an attached ISO is refused by
/// the same rule, not by a hand-kept list of four names.
#[test]
fn every_import_machine_in_the_roster_is_refused_and_every_iso_one_is_not() {
    let mut refused = Vec::new();
    let mut allowed = Vec::new();
    for p in nornir_build_thing::target::presets::PRESETS {
        match attach_for(p.name) {
            Ok(_) => allowed.push(p.name),
            Err(_) => refused.push(p.name),
        }
    }
    assert!(
        allowed.contains(&"upcloud") && allowed.contains(&"hetzner-cloud"),
        "the ISO-attach machines are the ones this door serves: {allowed:?}"
    );
    assert!(
        refused.contains(&"aws-nitro") && refused.contains(&"azure-gen2"),
        "an imported-disk machine has no ISO to attach: {refused:?}"
    );
    assert!(
        !allowed.is_empty() && !refused.is_empty(),
        "a rule that answers the same for every machine is not a rule"
    );
    for name in &allowed {
        let p = nornir_build_thing::target::presets::preset(name).unwrap();
        assert!(
            p.medium.is_iso_attach(),
            "{name} was allowed but the roster says {}",
            p.medium.describe()
        );
    }
}

/// A name nobody declared is a typo, and the refusal hands over the roster.
#[test]
fn a_machine_that_is_not_in_the_roster_says_so_and_lists_the_ones_that_are() {
    let why = attach_for("upcloud-2").expect_err("not a machine");
    assert!(why.contains("upcloud-2"), "{why}");
    assert!(why.contains("upcloud"), "lists the roster: {why}");
}