use std::path::PathBuf;
use isothrum_runner::{ErrorReport, RunOptions, Runner, SecretValue, Secrets};
use super::{Course, Failure, Step, TestVm};
const MODULE: &str = include_str!("proxmox.tf");
const MACHINE: &str = "selftest_vm";
const IMAGE: &str = "_download_file.selftest_image";
const NETWORK: &str = "_sdn_applier.selftest_net";
const VM: &str = "_virtual_environment_vm.selftest_vm";
const NO_ADDRESS: &str = "got no IPv4 address on bridge";
const API_PORT: u16 = 8006;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum SignIn {
Token(SecretValue),
Password {
user: String,
password: SecretValue,
},
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Key {
pub private_file: PathBuf,
pub public: String,
}
impl Key {
#[must_use]
pub fn new(private_file: impl Into<PathBuf>, public: impl Into<String>) -> Key {
Key {
private_file: private_file.into(),
public: public.into(),
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Proxmox {
pub endpoint: String,
pub insecure: bool,
pub sign_in: SignIn,
pub node: String,
pub bridge: String,
pub datastore: Option<String>,
pub image_datastore: Option<String>,
pub snippets_datastore: Option<String>,
pub node_ssh_user: Option<String>,
pub node_ssh_key_file: Option<PathBuf>,
pub node_address: Option<String>,
pub key: Option<Key>,
pub jump: Option<String>,
pub jump_known_hosts: Option<PathBuf>,
}
impl Proxmox {
#[must_use]
pub fn new(endpoint: impl Into<String>, sign_in: SignIn, node: impl Into<String>) -> Proxmox {
Proxmox {
endpoint: endpoint.into(),
insecure: false,
sign_in,
node: node.into(),
bridge: "vmbr0".to_owned(),
datastore: None,
image_datastore: None,
snippets_datastore: None,
node_ssh_user: None,
node_ssh_key_file: None,
node_address: None,
key: None,
jump: None,
jump_known_hosts: None,
}
}
pub(super) fn api_address(&self) -> String {
let rest = self.endpoint.split_once("://").map_or(self.endpoint.as_str(), |(_, r)| r);
let authority = rest.split(['/', '?', '#']).next().unwrap_or_default();
let authority = authority.rsplit_once('@').map_or(authority, |(_, a)| a);
let has_port = match authority.rsplit_once(']') {
Some((_, after)) => after.starts_with(':'),
None => authority.contains(':'),
};
if has_port { authority.to_owned() } else { format!("{authority}:{API_PORT}") }
}
fn options(&self, vm: &TestVm<'_>, public: &str) -> RunOptions {
let (zone, vnet) = network_ids(vm.name);
let mut secrets = Secrets::new();
let mut opts = RunOptions::new()
.with_env("endpoint", self.endpoint.clone())
.with_env("insecure", self.insecure.to_string())
.with_env("node", self.node.clone())
.with_env("bridge", self.bridge.clone())
.with_env("name", vm.name)
.with_env("hostname", vm.hostname)
.with_env("zone", zone)
.with_env("vnet", vnet)
.with_env("ssh_public_key", public.trim());
match &self.sign_in {
SignIn::Token(token) => secrets.insert("api_token", token.clone()),
SignIn::Password { user, password } => {
opts = opts.with_env("username", user.clone());
secrets.insert("password", password.clone());
}
}
let optional = [
("datastore", self.datastore.clone()),
("image_datastore", self.image_datastore.clone()),
("snippets_datastore", self.snippets_datastore.clone()),
("node_ssh_user", self.node_ssh_user.clone()),
("node_ssh_key_file", self.node_ssh_key_file.as_ref().map(|f| f.to_string_lossy().into_owned())),
("node_address", self.node_address.clone()),
("ssh_private_key_file", self.key.as_ref().map(|k| k.private_file.to_string_lossy().into_owned())),
("ssh_jump", self.jump.clone()),
(
"ssh_jump_known_hosts_file",
self.jump_known_hosts.as_ref().map(|f| f.to_string_lossy().into_owned()),
),
];
for (name, value) in optional {
if let Some(value) = value.filter(|v| !v.is_empty()) {
opts = opts.with_env(name, value);
}
}
opts.with_secrets(secrets)
}
}
pub(super) fn project() -> (&'static str, String) {
("main.tf", MODULE.to_owned())
}
pub(super) async fn prepared(p: &Proxmox, vm: &TestVm<'_>, runner: &Runner) -> Result<(RunOptions, Pve), String> {
let public = match &p.key {
Some(key) => key.public.clone(),
None => runner.ssh_public_key().await.map_err(|e| e.to_string())?,
};
Ok((p.options(vm, &public), Pve))
}
fn network_ids(name: &str) -> (String, String) {
let hash = name.bytes().fold(0x811c_9dc5_u32, |h, b| (h ^ u32::from(b)).wrapping_mul(0x0100_0193)) & 0x00ff_ffff;
(format!("st{hash:06x}"), format!("sn{hash:06x}"))
}
pub(super) struct Pve;
impl Course for Pve {
const UP: &'static [Step] = &[Step::Image, Step::Network, Step::Vm, Step::Address];
fn machine(&self) -> &'static str {
MACHINE
}
fn ends(&mut self, line: &str) -> Option<usize> {
let (address, _) = line.split_once(": Creation complete")?;
if address.ends_with(IMAGE) {
Some(0)
} else if address.ends_with(NETWORK) {
Some(1)
} else if address.ends_with(VM) {
Some(3)
} else {
None
}
}
fn explain(&self, report: &ErrorReport) -> Option<(usize, Failure)> {
let said = &report.message;
let boxed = || Box::new(report.clone());
if said.contains(NO_ADDRESS) {
Some((3, Failure::NoAddress(boxed())))
} else if said.contains("does not exist") && said.contains("bridge") {
Some((2, Failure::NoBridge(boxed())))
} else if said.contains(IMAGE) {
Some((0, Failure::ImageDownload(boxed())))
} else if said.contains("_sdn_") {
Some((1, Failure::Runner(boxed())))
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn token() -> SignIn {
SignIn::Token(SecretValue::new("root@pam!t=s3cret"))
}
#[test]
fn the_api_address_is_the_endpoints_host_and_port() {
for (endpoint, address) in [
("https://pve.lan:8006/", "pve.lan:8006"),
("https://10.0.0.5/", "10.0.0.5:8006"),
("https://10.0.0.5", "10.0.0.5:8006"),
("https://[fd00::5]:8443/api2/json", "[fd00::5]:8443"),
("https://[fd00::5]/", "[fd00::5]:8006"),
("pve.lan:9000", "pve.lan:9000"),
] {
assert_eq!(Proxmox::new(endpoint, token(), "pve").api_address(), address, "{endpoint}");
}
}
#[test]
fn the_values_name_the_modules_variables_and_the_secret_is_never_a_value() {
let vm = TestVm::new("test-vm-1", "probe");
let mut p = Proxmox::new("https://pve:8006/", token(), "pve2");
p.bridge = "vmbr1".into();
p.image_datastore = Some("isos".into());
p.jump = Some("root@pve".into());
p.jump_known_hosts = Some("/app/nodes_known_hosts".into());
p.key = Some(Key::new("/keys/id_ed25519", "ssh-ed25519 AAAA me\n"));
let opts = p.options(&vm, "ssh-ed25519 AAAA me\n");
let value = |k: &str| opts.env.get(k).map(String::as_str);
assert_eq!(value("node"), Some("pve2"));
assert_eq!(value("bridge"), Some("vmbr1"));
assert_eq!(value("image_datastore"), Some("isos"));
assert_eq!(value("datastore"), None, "the module's default");
assert_eq!(value("ssh_public_key"), Some("ssh-ed25519 AAAA me"));
assert_eq!(value("ssh_private_key_file"), Some("/keys/id_ed25519"));
assert_eq!(value("ssh_jump"), Some("root@pve"));
assert_eq!(value("ssh_jump_known_hosts_file"), Some("/app/nodes_known_hosts"));
assert_eq!(opts.secrets.get("api_token").map(SecretValue::expose), Some("root@pam!t=s3cret"));
assert!(opts.env.values().all(|v| !v.contains("s3cret")));
for name in opts.env.keys().map(String::as_str).chain(opts.secrets.iter().map(|(k, _)| k)) {
let declared = MODULE.contains(&format!("variable \"{name}\""));
assert!(
declared || ["ssh_private_key_file", "ssh_jump", "ssh_jump_known_hosts_file"].contains(&name),
"{name} isn't a variable"
);
}
let password = Proxmox::new(
"https://pve:8006/",
SignIn::Password {
user: "root@pam".into(),
password: SecretValue::new("pw"),
},
"pve",
);
let signed_in = password.options(&vm, "k");
assert_eq!(signed_in.env.get("username").map(String::as_str), Some("root@pam"));
assert_eq!(signed_in.secrets.get("password").map(SecretValue::expose), Some("pw"));
}
#[test]
fn the_network_ids_are_short_stable_and_the_vms_own() {
let (zone, vnet) = network_ids("cyber-test-0123456789abcdef");
assert_eq!((zone.len(), vnet.len()), (8, 8));
assert!(zone.starts_with("st") && vnet.starts_with("sn") && zone.get(2..) == vnet.get(2..));
assert!(zone.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()));
assert_eq!(network_ids("cyber-test-0123456789abcdef"), (zone.clone(), vnet));
assert_ne!(network_ids("cyber-test-fedcba9876543210").0, zone);
}
#[test]
fn a_step_ends_on_its_resource_even_with_the_providers_name_masked() {
for prefix in ["proxmox", "***"] {
let mut pve = Pve;
assert_eq!(
pve.ends(&format!(
"{prefix}_download_file.selftest_image: Creation complete after 3s [id=local:iso/x.img]"
)),
Some(0)
);
assert_eq!(pve.ends(&format!("{prefix}_sdn_applier.selftest_net: Creation complete after 1s")), Some(1));
assert_eq!(
pve.ends(&format!("{prefix}_virtual_environment_vm.selftest_vm: Creation complete after 41s [id=101]")),
Some(3)
);
assert_eq!(
pve.ends(&format!("{prefix}_virtual_environment_vm.selftest_vm: Still creating... [10s elapsed]")),
None
);
assert_eq!(pve.ends(&format!("{prefix}_sdn_applier.selftest_finalizer: Creation complete after 1s")), None);
}
assert_eq!(Pve.machine(), MACHINE);
}
#[test]
fn the_failures_people_hit_belong_to_their_step() {
let failed = |message: &str| ErrorReport::new(isothrum_runner::ErrorCode::ToolFailed, message);
let explained = |message: &str| Pve.explain(&failed(message)).map(|(at, why)| (at, std::mem::discriminant(&why)));
let no_address = format!(
"step `up`: terraform failed: Resource postcondition failed (proxmox_virtual_environment_vm.selftest_vm): the VM {NO_ADDRESS} `vmbr1` within 8m"
);
let report = failed("x");
assert_eq!(
explained(&no_address),
Some((3, std::mem::discriminant(&Failure::NoAddress(Box::new(report.clone())))))
);
assert_eq!(
explained("error creating VM: bridge 'sn12ab34' does not exist"),
Some((2, std::mem::discriminant(&Failure::NoBridge(Box::new(report.clone())))))
);
assert_eq!(
explained("Error downloading (***_download_file.selftest_image): 404"),
Some((0, std::mem::discriminant(&Failure::ImageDownload(Box::new(report.clone())))))
);
assert_eq!(
explained("Error (proxmox_sdn_zone_simple.selftest_zone): zone exists").map(|(at, _)| at),
Some(1)
);
assert_eq!(explained("401 Unauthorized"), None);
}
#[test]
fn the_module_has_the_watched_resources_and_says_no_address_as_explained() {
for resource in [
"resource \"proxmox_download_file\" \"selftest_image\"",
"resource \"proxmox_sdn_applier\" \"selftest_net\"",
"resource \"proxmox_virtual_environment_vm\" \"selftest_vm\"",
"output \"ssh_user\"",
NO_ADDRESS,
] {
assert!(MODULE.contains(resource), "{resource} missing from the module");
}
for end in [IMAGE, NETWORK, VM] {
let (kind, name) = end.trim_start_matches('_').split_once('.').unwrap();
assert!(MODULE.contains(&format!("\"proxmox_{kind}\" \"{name}\"")), "{end}");
}
assert!(MODULE.contains("depends_on = [proxmox_sdn_applier.selftest_finalizer, proxmox_download_file.selftest_image]"));
assert!(MODULE.contains("depends_on = [proxmox_sdn_applier.selftest_net]"));
}
}