use std::collections::BTreeMap;
use crate::status::MachineState;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Reading {
pub(crate) states: BTreeMap<String, String>,
}
pub(crate) fn parse(out: &str) -> Reading {
let mut read = Reading::default();
for (target, kind, data) in out.lines().filter_map(fields) {
if kind == "state" && !target.is_empty() {
read.states.insert(target.to_owned(), data.to_owned());
}
}
read
}
pub(crate) fn host_name(out: &str) -> Option<String> {
out.lines()
.filter_map(|line| line.trim().strip_prefix("HostName"))
.map(str::trim)
.find(|host| !host.is_empty())
.map(str::to_owned)
}
pub(crate) fn machine_state(label: &str) -> MachineState {
match label {
"running" | "started" | "powered_on" => MachineState::Running,
"not_created" => MachineState::Absent,
"poweroff" | "powered_off" | "off" | "shutoff" | "stopped" | "not_running" | "saved" | "suspended" | "paused" | "pmsuspended" | "aborted-saved" => {
MachineState::Stopped
}
other => MachineState::Unknown(other.to_owned()),
}
}
#[must_use]
pub fn plugins(out: &str) -> Vec<(String, Option<String>)> {
let lines: Vec<(&str, &str, &str)> = out.lines().filter_map(fields).collect();
let version = |name: &str| {
lines
.iter()
.find(|(target, kind, _)| *target == name && *kind == "plugin-version")
.and_then(|(_, _, data)| {
let v = data.split("%!(VAGRANT_COMMA)").next().unwrap_or_default().trim();
(!v.is_empty()).then(|| v.to_owned())
})
};
lines
.iter()
.filter(|(_, kind, data)| *kind == "plugin-name" && !data.is_empty())
.map(|(_, _, name)| ((*name).to_owned(), version(name)))
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct InstalledBox {
pub(crate) name: String,
pub(crate) provider: String,
pub(crate) version: String,
pub(crate) architecture: Option<String>,
}
pub(crate) fn boxes(out: &str) -> Vec<InstalledBox> {
type Reading = (String, Option<String>, Option<String>, Option<String>);
fn done(reading: Option<Reading>, into: &mut Vec<InstalledBox>) {
if let Some((name, Some(provider), Some(version), architecture)) = reading {
into.push(InstalledBox {
name,
provider,
version,
architecture: architecture.filter(|a| a != "n/a"),
});
}
}
let mut listed = Vec::new();
let mut reading: Option<Reading> = None;
for (_, kind, data) in out.lines().filter_map(fields) {
let data = unescape(data).trim().to_owned();
match (kind, reading.as_mut()) {
("box-name", _) => {
done(reading.take(), &mut listed);
reading = Some((data, None, None, None));
}
("box-provider", Some(r)) => r.1 = Some(data),
("box-version", Some(r)) => r.2 = Some(data),
("box-architecture", Some(r)) => r.3 = Some(data),
_ => {}
}
}
done(reading, &mut listed);
listed
}
pub(crate) fn error(stdout: &[String]) -> Option<String> {
let data = stdout
.iter()
.rev()
.filter_map(|line| fields(line))
.find_map(|(_, kind, data)| (kind == "error-exit").then_some(data))?;
let (class, message) = data.split_once(',').unwrap_or((data, ""));
let (class, message) = (unescape(class), unescape(message));
match (class.trim(), message.trim()) {
("", "") => None,
(class, "") => Some(class.to_owned()),
("", message) => Some(message.to_owned()),
(class, message) => Some(format!("{message} ({class})")),
}
}
const VAGRANT_LOCKED: [&str; 4] = [
"Vagrant::Errors::MachineActionLockedError",
"another process is already executing an action on the machine",
"Vagrant::Errors::MachineLocked,",
"Vagrant can't use the requested machine because it is locked!",
];
const ESXI_ALREADY_OFF: [&str; 2] = ["existingState = \"poweredOff\"", "in the current state (Powered off)"];
fn said_any<'a>(lines: impl IntoIterator<Item = &'a String>, wordings: &[&str]) -> bool {
lines.into_iter().any(|line| wordings.iter().any(|said| line.contains(said)))
}
fn unescape(field: &str) -> String {
field.replace("%!(VAGRANT_COMMA)", ",").replace("\\r", "").replace("\\n", "\n")
}
pub(crate) fn fields(line: &str) -> Option<(&str, &str, &str)> {
let mut parts = line.trim_end_matches('\r').splitn(4, ',');
let (_ts, target, kind, data) = (parts.next()?, parts.next()?, parts.next()?, parts.next()?);
Some((target, kind, data))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Failure {
Locked,
AlreadyOff,
Other,
}
impl Failure {
pub(crate) fn of<'a>(lines: impl IntoIterator<Item = &'a String> + Clone) -> Failure {
if said_any(lines.clone(), &VAGRANT_LOCKED) {
Failure::Locked
} else if said_any(lines, &ESXI_ALREADY_OFF) {
Failure::AlreadyOff
} else {
Failure::Other
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_held_lock_is_read_in_each_of_vagrant_s_wordings() {
let action = lines(
"1794307174,,error-exit,Vagrant::Errors::MachineActionLockedError,An action 'read_state' was attempted on the machine 'web'%!(VAGRANT_COMMA)\\nbut another process is already executing an action on the machine.",
);
let human = lines(
"An action 'read_state' was attempted on the machine 'web',\nbut another process is already executing an action on the machine.\nVagrant locks each machine for access by only one process at a time.",
);
let index = lines("1794342958,,error-exit,Vagrant::Errors::MachineLocked,Vagrant can't use the requested machine because it is locked! This");
for (wording, said) in [("action", &action), ("human", &human), ("index", &index)] {
assert_eq!(Failure::of(said.iter()), Failure::Locked, "{wording}");
}
assert_eq!(
Failure::of(&lines("VBoxManage: error: Cannot unregister the machine 'web-vm - web' while it is locked")),
Failure::Other
);
assert_eq!(Failure::of(&Vec::new()), Failure::Other);
}
#[test]
fn esxi_refusing_to_halt_a_vm_already_off_is_read() {
let off = lines(
"There was an error talking to ESXi.\n Unable to shutdown the VM:\n (vim.fault.InvalidPowerState) {\n existingState = \"poweredOff\"\n msg = \"shutdownGuest\nThe attempted operation cannot be performed in the current state (Powered off).\"\n}",
);
assert_eq!(Failure::of(&off), Failure::AlreadyOff);
assert_eq!(
Failure::of(&lines("There was an error talking to ESXi.\n Unable to shutdown the VM: timed out")),
Failure::Other
);
}
#[test]
fn boxes_are_read_one_per_architecture_and_one_of_no_architecture_has_none() {
let listed = boxes(include_str!("fixtures/vagrant/2.4.9-box-list.txt"));
let held = |provider: &str, arch: &str| InstalledBox {
name: "bento/debian-12".into(),
provider: provider.into(),
version: "202510.26.0".into(),
architecture: Some(arch.into()),
};
assert_eq!(
listed,
[held("virtualbox", "arm64"), held("vmware_desktop", "amd64"), held("vmware_desktop", "arm64")]
);
let out = "1,,box-name,generic/alpine319\n1,,box-provider,libvirt\n1,,box-version,4.3.12\n\
1,,box-architecture,n/a\n1,,box-info,Author,Ladar Levison\n1,,box-name,cut\n1,,box-provider,qemu\n";
assert_eq!(
boxes(out),
[InstalledBox {
name: "generic/alpine319".into(),
provider: "libvirt".into(),
version: "4.3.12".into(),
architecture: None,
}]
);
assert_eq!(boxes(""), []);
}
#[test]
fn plugins_are_read_by_name_with_their_versions() {
let out = "1794305819,,ui,info,vagrant-qemu (0.7.0%!(VAGRANT_COMMA) global)\n\
1794305819,,plugin-name,vagrant-qemu\n\
1794305819,vagrant-qemu,plugin-version,0.7.0%!(VAGRANT_COMMA) global\n\
1794305819,,plugin-name,vagrant-vmware-esxi\r\n\
1794305819,,plugin-name,vagrant_utm\n\
1794305819,vagrant_utm,plugin-version,0.2.0\n";
assert_eq!(
plugins(out),
[
("vagrant-qemu".to_owned(), Some("0.7.0".to_owned())),
("vagrant-vmware-esxi".to_owned(), None),
("vagrant_utm".to_owned(), Some("0.2.0".to_owned())),
]
);
assert_eq!(plugins(""), Vec::<(String, Option<String>)>::new());
}
fn lines(text: &str) -> Vec<String> {
text.lines().map(str::to_owned).collect()
}
#[test]
fn a_machine_readable_error_is_read_whole_with_its_class() {
let said = "Vagrant can't use the requested machine because it is locked! This\\nmeans that another Vagrant \
process is currently reading or modifying\\nthe machine.\\n\\nName: probe\\nProvider: libvirt";
let out = lines(&format!(
"1794342958,,ui,error,{said}\n1794342958,,error-exit,Vagrant::Errors::MachineLocked,{said}\n"
));
assert_eq!(
error(&out).as_deref(),
Some(
"Vagrant can't use the requested machine because it is locked! This\nmeans that another Vagrant \
process is currently reading or modifying\nthe machine.\n\nName: probe\nProvider: libvirt \
(Vagrant::Errors::MachineLocked)"
)
);
let commas = lines("1,,error-exit,Vagrant::Errors::VagrantError,a%!(VAGRANT_COMMA) b\\r\\n");
assert_eq!(error(&commas).as_deref(), Some("a, b (Vagrant::Errors::VagrantError)"));
assert_eq!(error(&lines("1,,error-exit,Vagrant::Errors::X,")).as_deref(), Some("Vagrant::Errors::X"));
assert_eq!(error(&lines("1,web,state,running\n==> web: up")), None);
assert_eq!(error(&lines("1,,error-exit,")), None);
}
#[test]
fn ssh_configs_host_name_is_the_vms_address() {
let out = "Host web\r\n HostName 172.16.1.53\r\n User vagrant\r\n Port 22\r\n";
assert_eq!(host_name(out).as_deref(), Some("172.16.1.53"));
assert_eq!(host_name("Host web\n HostName \n User vagrant\n"), None);
assert_eq!(host_name(""), None);
}
#[test]
fn windows_line_endings_and_blank_lines_are_read_the_same() {
let out = "1,dc01,provider-name,hyperv\r\n\r\n1,dc01,state,running\r\n1,gateway,state,off\r\n";
let r = parse(out);
assert_eq!(r.states["dc01"], "running", "no carriage return in the state");
assert_eq!(machine_state(&r.states["gateway"]), MachineState::Stopped);
}
#[test]
fn a_state_with_vagrants_comma_escape_is_kept_whole_and_is_not_running() {
let r = parse("1,a,state,running%!(VAGRANT_COMMA) with a note\n");
assert_eq!(r.states["a"], "running%!(VAGRANT_COMMA) with a note");
assert!(matches!(machine_state(&r.states["a"]), MachineState::Unknown(_)));
}
#[test]
fn non_state_lines_and_malformed_output_give_no_machine() {
let out = "ts,,ui,info,Current machine states:\nts,box,metadata,provider,virtualbox\nts,,state,running\nshort,line\n";
assert!(parse(out).states.is_empty());
assert!(parse("").states.is_empty());
}
#[test]
fn every_providers_labels_map_to_a_state() {
assert_eq!(machine_state("running"), MachineState::Running);
assert_eq!(machine_state("started"), MachineState::Running, "UTM's running VM");
assert_eq!(machine_state("powered_on"), MachineState::Running, "ESXi's VM before its SSH address");
assert_eq!(machine_state("not_created"), MachineState::Absent);
for stopped in [
"poweroff",
"saved",
"aborted-saved",
"suspended",
"shutoff",
"off",
"not_running",
"powered_off",
"stopped",
"paused",
] {
assert_eq!(machine_state(stopped), MachineState::Stopped, "{stopped}");
}
assert_eq!(machine_state("gurumeditation"), MachineState::Unknown("gurumeditation".into()));
assert_eq!(machine_state("aborted"), MachineState::Unknown("aborted".into()), "a VM that died");
}
#[test]
fn status_fixtures_read_as_vagrant_prints_them() {
let read = parse(include_str!("fixtures/vagrant/2.4.1-status-virtualbox.txt"));
assert_eq!(read.states.keys().collect::<Vec<_>>(), ["db", "web"]);
assert_eq!(
(machine_state(&read.states["web"]), machine_state(&read.states["db"])),
(MachineState::Running, MachineState::Stopped)
);
let hyperv = parse(include_str!("fixtures/vagrant/2.4.3-windows-status-hyperv.txt"));
assert_eq!(
machine_state(&hyperv.states["dc01"]),
MachineState::Stopped,
"no carriage return left in the label"
);
assert_eq!(machine_state(&hyperv.states["ws01"]), MachineState::Absent);
let cut = parse(include_str!("fixtures/vagrant/2.4.1-status-cut.txt"));
assert_eq!(machine_state(&cut.states["db"]), MachineState::Unknown("shut".into()));
assert_eq!(parse("").states.len(), 0);
let ssh = include_str!("fixtures/vagrant/2.4.1-ssh-config-windows.txt");
assert_eq!(host_name(ssh).as_deref(), Some("127.0.0.1"));
}
#[test]
fn failure_fixtures_read_as_vagrant_prints_them() {
let of = |text: &str| Failure::of(&lines(text));
assert_eq!(of(include_str!("fixtures/vagrant/2.4.9-error-machine-locked.txt")), Failure::Locked);
assert_eq!(of(include_str!("fixtures/vagrant/2.4.1-error-action-locked-human.txt")), Failure::Locked);
assert_eq!(of(include_str!("fixtures/vagrant/vmware-esxi-2.5.5-halt-already-off.txt")), Failure::AlreadyOff);
assert_eq!(of(include_str!("fixtures/vagrant/2.4.1-status-virtualbox.txt")), Failure::Other);
let locked = lines(include_str!("fixtures/vagrant/2.4.9-error-machine-locked.txt"));
assert!(error(&locked).is_some_and(|e| e.ends_with("Provider: libvirt (Vagrant::Errors::MachineLocked)")));
let plugins = plugins(include_str!("fixtures/vagrant/2.4.1-plugin-list.txt"));
assert_eq!(
plugins,
[
("vagrant-libvirt".to_owned(), Some("0.12.2".to_owned())),
("vagrant-qemu".to_owned(), Some("0.3.12".to_owned()))
]
);
}
}
#[cfg(test)]
mod proptests {
use proptest::prelude::*;
use super::*;
fn machine() -> impl Strategy<Value = String> {
prop_oneof![Just("gateway".to_owned()), "[a-z][a-z0-9_-]{0,12}"]
}
fn state() -> impl Strategy<Value = String> {
prop_oneof![
Just("running".to_owned()),
Just("poweroff".to_owned()),
Just("not_created".to_owned()),
"[a-z_ ,]{1,16}"
]
}
proptest! {
#[test]
fn any_output_is_read_without_panicking(text in "(?s).{0,400}") {
let _ = parse(&text);
drop(plugins(&text));
}
#[test]
fn every_machines_last_state_reads_back_whole(
machines in proptest::collection::vec((machine(), state(), "[0-9]{10}"), 0..6),
noise in proptest::collection::vec("[0-9]{10},[a-z]{0,6},(metadata|ui),[^\n]{0,20}", 0..4),
) {
let mut lines: Vec<String> = machines.iter().map(|(name, state, ts)| format!("{ts},{name},state,{state}")).collect();
lines.extend(noise);
let read = parse(&lines.join("\n"));
let mut want = BTreeMap::new();
for (name, state, _) in &machines {
want.insert(name.clone(), state.clone());
}
prop_assert_eq!(read.states, want);
}
}
}