use super::*;
use crate::testing::https::{Request, Server, fingerprint};
const PROXMOX_STATE: &str = r#"{"format_version":"1.0","values":{"root_module":{"resources":[
{"address":"proxmox_virtual_environment_vm.web","mode":"managed","type":"proxmox_virtual_environment_vm","values":{"id":"101","node_name":"pve","vm_id":101}},
{"address":"proxmox_virtual_environment_vm.db","mode":"managed","type":"proxmox_virtual_environment_vm","values":{"id":"102","node_name":"pve","vm_id":102}}
]}}}"#;
const TOKEN: &str = "root@pam!stack=t0ken";
fn terraform() -> FakeProcesses {
FakeProcesses::model(|call| match (call.program.as_str(), call.args.first().map(String::as_str)) {
("terraform", Some("show")) => Reply::ok(PROXMOX_STATE),
("terraform", Some("output")) => Reply::ok("{}"),
_ => panic!("unscripted `{}`", call.line()),
})
}
fn api(listing: &'static str, free_id: Option<&'static str>) -> impl Fn(&Request) -> (u16, String) + Send + Sync + 'static {
move |request| {
assert_eq!(
request.header("authorization"),
Some(format!("PVEAPIToken={TOKEN}").as_str()),
"signed in with the run's token"
);
assert_eq!(request.method, "GET", "a read is a GET");
if request.target.ends_with("/api2/json/cluster/resources?type=vm") {
(200, listing.to_owned())
} else if request.target.ends_with("/api2/json/cluster/nextid?vmid=102") {
match free_id {
Some(id) => (200, format!("{{\"data\":\"{id}\"}}")),
None => (400, "{\"data\":null,\"errors\":{\"vmid\":\"VM 102 already exists\"}}".to_owned()),
}
} else {
panic!("an unscripted request: {request:?}")
}
}
}
fn run(server: &Server, pin: Option<String>, token: bool) -> Vec<(String, String)> {
let mut given = vec![("PROXMOX_VE_ENDPOINT".to_owned(), server.endpoint.clone())];
given.extend(pin.map(|p| ("PROXMOX_VE_TLS_FINGERPRINT".to_owned(), p)));
if token {
given.push(("PROXMOX_VE_API_TOKEN".to_owned(), TOKEN.to_owned()));
}
given
}
fn pinned() -> String {
fingerprint().to_string()
}
async fn proxmox_states(fake: &FakeProcesses, given: &[(String, String)]) -> Result<Vec<(String, MachineState)>> {
let stack = stack().with_state();
let given: Vec<(&str, &str)> = given.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
let (seen, _) = with_cx_given(&stack, fake, &given, async |cx| Terraform.observe(cx).await).await?;
Ok(seen.machines.into_iter().map(|m| (m.name, m.state)).collect())
}
#[tokio::test]
async fn a_proxmox_vm_powered_off_or_removed_on_its_server_reads_so() {
let listing = r#"{"data":[{"vmid":101,"node":"pve","status":"stopped","type":"qemu"}]}"#;
let server = Server::start(api(listing, Some("102"))).await.unwrap();
let states = proxmox_states(&terraform(), &run(&server, Some(pinned()), true)).await.expect("observed");
assert_eq!(states, [("web".to_owned(), MachineState::Stopped), ("db".to_owned(), MachineState::Absent)]);
}
#[tokio::test]
async fn a_proxmox_vm_hidden_from_the_listing_but_holding_its_id_keeps_its_state() {
let listing = r#"{"data":[{"vmid":"101","node":"pve","status":"running","type":"qemu"}]}"#;
let server = Server::start(api(listing, None)).await.unwrap();
let states = proxmox_states(&terraform(), &run(&server, Some(pinned()), true)).await.expect("observed");
assert_eq!(states, [("web".to_owned(), MachineState::Running), ("db".to_owned(), MachineState::Running)]);
}
#[tokio::test]
async fn a_proxmox_server_refusing_the_token_is_server_auth_failed() {
let server = Server::start(|_| (401, "{\"data\":null}".to_owned())).await.unwrap();
let e = proxmox_states(&terraform(), &run(&server, Some(pinned()), true)).await.expect_err("refused");
assert_eq!(e.code(), ErrorCode::ServerAuthFailed, "{e}");
}
#[tokio::test]
async fn a_run_without_its_token_or_the_servers_pin_sends_it_nothing() {
let server = Server::start(api("{}", None)).await.unwrap();
let other = crate::tls::Fingerprint::of(b"another certificate").to_string();
let runs = [
run(&server, Some(pinned()), false),
run(&server, Some(other), true),
run(&server, None, true),
run(&server, Some("not a pin".to_owned()), true),
];
for given in runs {
let states = proxmox_states(&terraform(), &given).await.expect("observed");
assert_eq!(
states.iter().map(|(_, s)| s.clone()).collect::<Vec<_>>(),
[MachineState::Running, MachineState::Running],
"{given:?}"
);
}
assert_eq!(server.requests().len(), 0, "no request reached it");
}