use std::collections::BTreeMap;
use std::path::Path;
use super::vagrant::{a_modelled_vagrant_stack, vagrant_answer};
use super::*;
use crate::process::fake::FakeProcesses;
#[derive(Debug, Default)]
struct Stack {
made: BTreeMap<&'static str, bool>,
ups: usize,
}
fn dotfile(call: &Call) -> std::path::PathBuf {
Path::new(call.env("VAGRANT_DOTFILE_PATH").expect("the dotfile folder")).join("machines")
}
fn answer(stack: &Mutex<Stack>, call: &Call) -> Reply {
vagrant_answer(stack, call, |stack, args| match args {
["up", "--provider", "virtualbox", ..] => {
stack.ups += 1;
if !stack.made.contains_key("web") {
let dir = dotfile(call).join("web").join("virtualbox");
std::fs::create_dir_all(&dir).expect("its folder");
std::fs::write(dir.join("id"), format!("uuid-web-{}", stack.ups)).expect("its id");
}
stack.made.insert("web", true);
Reply::ok("")
}
["status", "--machine-readable"] => {
let lines: Vec<String> = ["toolbox", "web"]
.iter()
.map(|m| {
let state = stack.made.get(m).map_or("not_created", |runs| if *runs { "running" } else { "poweroff" });
format!("1794305819,{m},state,{state}")
})
.collect();
Reply::ok(&lines.join("\n"))
}
["port", m, "--machine-readable"] => Reply::ok(&format!("1794305819,{m},forwarded_port,22,2222")),
["destroy", "-f"] => {
stack.made.clear();
Reply::ok("")
}
["box", "list", ..] => Reply::ok(""),
_ => panic!("a call the model doesn't know: {}", call.line()),
})
}
fn destroy_web_outside(stack: &Mutex<Stack>, dot: &Path) {
stack.lock().unwrap_or_else(PoisonError::into_inner).made.remove("web");
let id = dot.join("machines/web/virtualbox/id");
std::fs::remove_file(&id).expect("its id was recorded");
}
fn virtualbox_stack() -> (Arc<Mutex<Stack>>, Arc<FakeProcesses>, tempfile::TempDir, std::path::PathBuf, Runner) {
a_modelled_vagrant_stack(answer)
}
#[tokio::test]
async fn a_vm_destroyed_outside_reads_absent_beside_one_never_made_its_resume_is_refused_and_an_up_makes_it_again() {
let (stack, fake, _root, project, runner) = virtualbox_stack();
let run = runner.open(&project, None).expect("opened");
let opts = RunOptions::new().with_provider(crate::Provider::Virtualbox);
let read = async || {
let s = run.status_with(&opts).await.expect("status");
(s.state, s.machines.into_iter().map(|m| (m.name, m.state)).collect::<Vec<_>>())
};
let absent = |m: &str| (m.to_owned(), MachineState::Absent);
let running = |m: &str| (m.to_owned(), MachineState::Running);
run.up(&opts, &Recorder::new()).await.expect("up");
assert_eq!(
read().await,
(RunState::Ready, vec![absent("toolbox"), running("web")]),
"the toolbox was never made: not gone"
);
let up = fake.made("vagrant up").pop().expect("an up");
destroy_web_outside(&stack, Path::new(up.env("VAGRANT_DOTFILE_PATH").expect("the dotfile folder")));
assert_eq!(
read().await,
(RunState::Failed, vec![absent("toolbox"), absent("web")]),
"web was made, then destroyed: gone, and the run failed"
);
refused_as_gone(&run.resume(&opts, &Recorder::new()).await.expect_err("web is gone"));
assert_eq!(stack.lock().unwrap_or_else(PoisonError::into_inner).ups, 1, "nothing was started");
run.up(&opts, &Recorder::new()).await.expect("up makes it again");
assert_eq!(read().await, (RunState::Ready, vec![absent("toolbox"), running("web")]));
}
#[tokio::test]
async fn a_vm_made_again_has_its_pinned_host_keys_forgotten_and_one_kept_keeps_them() {
let (stack, fake, _root, project, runner) = virtualbox_stack();
let run = runner.open(&project, None).expect("opened");
let opts = RunOptions::new().with_provider(crate::Provider::Virtualbox);
run.up(&opts, &Recorder::new()).await.expect("up");
let up = fake.made("vagrant up").pop().expect("an up");
let dot = Path::new(up.env("VAGRANT_DOTFILE_PATH").expect("the dotfile folder")).to_path_buf();
let known = dot.parent().expect("the run's state folder").join(crate::ssh::KNOWN_HOSTS);
std::fs::write(&known, "10.0.0.5 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOld\n").expect("a console pinned web's keys");
run.update(&opts, &Recorder::new()).await.expect("an update keeps web's VM");
assert!(known.exists(), "the same VM: its keys still hold");
destroy_web_outside(&stack, &dot);
run.up(&opts, &Recorder::new()).await.expect("up makes web again");
assert!(!known.exists(), "a new VM, maybe at the old address: its old keys would refuse it");
}