You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.5 KiB
86 lines
2.5 KiB
|
1 year ago
|
#[cfg(test)]
|
||
|
|
mod domain_list_tests {
|
||
|
|
use clap::Parser;
|
||
|
1 year ago
|
use kvm_install_vm::vm::{DomainState, VirtualMachine};
|
||
|
1 year ago
|
use kvm_install_vm::{Cli, cli::Commands};
|
||
|
|
use std::ffi::OsString;
|
||
|
|
|
||
|
|
// Helper function for CLI tests
|
||
|
|
fn get_args(args: &[&str]) -> Vec<OsString> {
|
||
|
|
vec![OsString::from("kvm-install-vm")]
|
||
|
|
.into_iter()
|
||
|
|
.chain(args.iter().map(|s| OsString::from(s)))
|
||
|
|
.collect()
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_cli_list_defaults() {
|
||
|
|
let args = get_args(&["list"]);
|
||
|
|
let cli = Cli::parse_from(args);
|
||
|
|
|
||
|
|
match cli.command {
|
||
|
1 year ago
|
Commands::List {
|
||
|
|
all,
|
||
|
|
running,
|
||
|
|
inactive,
|
||
|
|
} => {
|
||
|
1 year ago
|
assert_eq!(all, false);
|
||
|
|
assert_eq!(running, false);
|
||
|
|
assert_eq!(inactive, false);
|
||
|
1 year ago
|
}
|
||
|
1 year ago
|
_ => panic!("Expected List command"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_cli_list_with_flags() {
|
||
|
|
let args = get_args(&["list", "--all", "--running"]);
|
||
|
|
let cli = Cli::parse_from(args);
|
||
|
|
|
||
|
|
match cli.command {
|
||
|
1 year ago
|
Commands::List {
|
||
|
|
all,
|
||
|
|
running,
|
||
|
|
inactive,
|
||
|
|
} => {
|
||
|
1 year ago
|
assert_eq!(all, true);
|
||
|
|
assert_eq!(running, true);
|
||
|
|
assert_eq!(inactive, false);
|
||
|
1 year ago
|
}
|
||
|
1 year ago
|
_ => panic!("Expected List command"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// This test requires libvirt to be running
|
||
|
|
// Use #[ignore] to skip it in normal test runs
|
||
|
|
#[test]
|
||
|
|
#[ignore]
|
||
|
|
fn test_domain_listing() -> anyhow::Result<()> {
|
||
|
|
// This will connect to libvirt and list domains
|
||
|
|
let domains = VirtualMachine::list_domains(None)?;
|
||
|
1 year ago
|
|
||
|
1 year ago
|
// We can't assert much about the actual domains without knowing the test environment,
|
||
|
|
// but we can check that the function runs without errors and returns a Vec
|
||
|
|
println!("Found {} domains", domains.len());
|
||
|
1 year ago
|
|
||
|
1 year ago
|
for domain in &domains {
|
||
|
1 year ago
|
println!(
|
||
|
|
"Domain: {}, ID: {:?}, State: {:?}",
|
||
|
|
domain.name, domain.id, domain.state
|
||
|
|
);
|
||
|
|
|
||
|
1 year ago
|
// Check that inactive domains have no ID
|
||
|
|
if domain.state == DomainState::Shutoff {
|
||
|
|
assert_eq!(domain.id, None);
|
||
|
|
}
|
||
|
1 year ago
|
|
||
|
1 year ago
|
// If it's running, it should have an ID
|
||
|
|
if domain.state == DomainState::Running {
|
||
|
|
assert!(domain.id.is_some());
|
||
|
|
}
|
||
|
|
}
|
||
|
1 year ago
|
|
||
|
1 year ago
|
Ok(())
|
||
|
|
}
|
||
|
1 year ago
|
}
|