use std::collections::BTreeMap;
use std::path::Path;
use std::time::Duration;
use super::vagrant::{a_modelled_vagrant_stack, vagrant_answer};
use super::*;
use crate::process::fake::FakeProcesses;
fn locked(name: &str) -> Reply {
let said = include_str!("../../tool_output/fixtures/vagrant/2.4.9-error-machine-locked.txt").replace("Name: probe", &format!("Name: {name}"));
Reply { code: 1, ..Reply::ok(&said) }
}
#[derive(Debug, Default)]
struct Stack {
made: BTreeMap<&'static str, bool>,
held_by_another: Option<&'static str>,
attach_takes: Duration,
up_takes: Duration,
reads: usize,
}
fn answer(stack: &Mutex<Stack>, call: &Call) -> Reply {
vagrant_answer(stack, call, |stack, args| match args {
["up", "--provider", "virtualbox", "toolbox"] => {
stack.made.insert("toolbox", true);
Reply {
delay: stack.attach_takes,
..Reply::ok("")
}
}
["up", "--provider", "virtualbox", ..] => {
stack.made.insert("web", true);
Reply {
delay: stack.up_takes,
..Reply::ok("")
}
}
["status", "--machine-readable"] => {
stack.reads += 1;
if let Some(name) = stack.held_by_another {
return locked(name);
}
let line = |m: &str| format!("1,{m},state,{}", stack.made.get(m).map_or("not_created", |_| "running"));
Reply::ok(&format!("{}\n{}", line("toolbox"), line("web")))
}
["port", m, "--machine-readable"] => Reply::ok(&format!("1,{m},forwarded_port,22,2222")),
["destroy", "-f"] => {
stack.made.clear();
Reply::ok("")
}
_ => Reply::ok(""),
})
}
fn stack() -> (Arc<Mutex<Stack>>, Arc<FakeProcesses>, tempfile::TempDir, std::path::PathBuf, Runner) {
a_modelled_vagrant_stack(answer)
}
fn another(root: &Path, fake: &Arc<FakeProcesses>) -> Runner {
let config = runner_config(&root.join("home"), processes(fake), no_ports(), crate::runner::this_host());
Runner::new(config).expect("a second runner")
}
fn states(status: &crate::Status) -> BTreeMap<String, MachineState> {
status.machines.iter().map(|m| (m.name.clone(), m.state.clone())).collect()
}
#[tokio::test(start_paused = true)]
async fn a_status_while_another_process_attaches_reads_the_run_as_last_read_without_asking_vagrant() {
let (stack, fake, root, project, runner) = stack();
let opts = RunOptions::new().with_provider(crate::Provider::Virtualbox);
let run = runner.open(&project, None).expect("opened");
run.up(&opts, &Recorder::new()).await.expect("up");
assert!(run.status_with(&opts).await.expect("status").live);
stack.lock().unwrap_or_else(PoisonError::into_inner).attach_takes = Duration::from_secs(600);
let app = another(root.path(), &fake);
let watched = app.open(&project, None).expect("opened from the app");
let reads = async {
tokio::time::sleep(Duration::from_secs(120)).await;
let before = stack.lock().unwrap_or_else(PoisonError::into_inner).reads;
let read = watched.status_with(&opts).await;
(read, before, stack.lock().unwrap_or_else(PoisonError::into_inner).reads)
};
let events = Recorder::new();
let (attached, (read, before, after)) = tokio::join!(run.attach("toolbox", &opts, &events), reads);
attached.expect("attached");
let read = read.expect("a status while the attach provisions");
assert_eq!(
(read.state, read.live, before, after),
(RunState::Ready, false, before, before),
"Vagrant isn't asked"
);
assert_eq!(states(&read).get("web"), Some(&MachineState::Running), "as last read");
let live = watched.status_with(&opts).await.expect("status");
assert!(live.live && states(&live).get("toolbox") == Some(&MachineState::Running), "{live:?}");
}
#[tokio::test(start_paused = true)]
async fn a_machine_another_vagrant_process_holds_reads_busy_and_its_screen_says_why() {
let (stack, _fake, _root, project, runner) = stack();
let opts = RunOptions::new().with_provider(crate::Provider::Virtualbox);
let run = runner.open(&project, None).expect("opened");
run.up(&opts, &Recorder::new()).await.expect("up");
run.status_with(&opts).await.expect("read live once");
stack.lock().unwrap_or_else(PoisonError::into_inner).held_by_another = Some("web");
let read = run.status_with(&opts).await.expect("a status Vagrant's lock refused still reads");
assert_eq!((read.state, read.live), (RunState::Ready, false));
assert_eq!(states(&read).get("web"), Some(&MachineState::Busy), "{:?}", read.machines);
let screen = run.graphical_console("web", &opts).await.expect_err("busy");
assert_eq!(screen.code(), ErrorCode::NoConsole, "{screen}");
assert!(screen.to_string().contains("another process is acting on it"), "{screen}");
stack.lock().unwrap_or_else(PoisonError::into_inner).held_by_another = None;
let after = run.status_with(&opts).await.expect("status");
assert!(after.live, "{after:?}");
isothrum_test_support::assert_every!(states(&after).into_values(), |s: &MachineState| *s != MachineState::Busy);
}
#[tokio::test(start_paused = true)]
async fn a_held_status_after_a_down_shows_none_of_the_machines_that_went() {
let (stack, fake, root, project, runner) = stack();
let opts = RunOptions::new().with_provider(crate::Provider::Virtualbox);
let run = runner.open(&project, None).expect("opened");
run.up(&opts, &Recorder::new()).await.expect("up");
assert_eq!(run.status_with(&opts).await.expect("read once").machines.len(), 2);
run.down(&opts, &Recorder::new()).await.expect("down");
stack.lock().unwrap_or_else(PoisonError::into_inner).up_takes = Duration::from_secs(600);
let app = another(root.path(), &fake);
let watched = app.open(&project, None).expect("opened from the app");
let reads = async {
tokio::time::sleep(Duration::from_secs(60)).await;
watched.status_with(&opts).await
};
let events = Recorder::new();
let (started, read) = tokio::join!(run.up(&opts, &events), reads);
started.expect("up again");
let read = read.expect("status");
assert_eq!((read.state, read.live), (RunState::Creating, false));
assert_eq!(read.machines, [], "nothing of the run that went");
}