Skip to content

Commit 5c53cc8

Browse files
committed
ch: Add define/undefine test
Add simple test to define and undefine a domain and check that the domain remains defined across libvirtd restarts.
1 parent aac4dd5 commit 5c53cc8

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

ch_integration_tests/Cargo.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch_integration_tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dirs = "3.0.1"
1010
epoll = "4.3.1"
1111
libc = ">=0.2.91"
1212
lazy_static= "1.4.0"
13+
regex = "1.4.5"
1314
ssh2 = "0.9.1"
1415
test_infra = { git = "https://github.com/cloud-hypervisor/cloud-hypervisor" }
1516
uuid = { version = "0.8", features = ["v4"] }

ch_integration_tests/tests/integration.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#[cfg(test)]
77
#[macro_use]
88
extern crate lazy_static;
9+
extern crate regex;
910

1011
#[cfg(test)]
1112
mod tests {
@@ -16,6 +17,7 @@ mod tests {
1617
use std::{ffi::OsStr, path::PathBuf};
1718
use test_infra::*;
1819

20+
use regex::Regex;
1921
use uuid::Uuid;
2022
use vmm_sys_util::tempdir::TempDir;
2123

@@ -277,6 +279,63 @@ mod tests {
277279
assert!(r.is_ok());
278280
}
279281

282+
#[test]
283+
fn test_defines() {
284+
cleanup_libvirt_state();
285+
let mut libvirtd = spawn_libvirtd().unwrap();
286+
thread::sleep(std::time::Duration::new(5, 0));
287+
288+
let mut disk = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_owned());
289+
let guest = Guest::new(&mut disk);
290+
let domain_path = guest.create_domain(DEFAULT_VCPUS, DEFAULT_RAM_SIZE);
291+
let output = spawn_virsh(&["define", domain_path.to_str().unwrap()])
292+
.unwrap()
293+
.wait_with_output()
294+
.unwrap();
295+
296+
libvirtd.kill().unwrap();
297+
// libvirtd got SIGKILL, cleanup /var/run manually
298+
// to avoid getting non-persistent state leftovers
299+
let _ = std::fs::remove_dir_all("/var/lib/libvirt");
300+
let _ = std::fs::remove_file("/var/run/libvirtd.pid");
301+
let _ = std::fs::remove_dir_all("/var/run/libvirt");
302+
303+
// verify persistent state exists
304+
let mut libvirtd = spawn_libvirtd().unwrap();
305+
thread::sleep(std::time::Duration::new(5, 0));
306+
let list_output = spawn_virsh(&["list", "--all"])
307+
.unwrap()
308+
.wait_with_output()
309+
.unwrap();
310+
311+
let undefine_output = spawn_virsh(&["undefine", &guest.vm_name])
312+
.unwrap()
313+
.wait_with_output()
314+
.unwrap();
315+
316+
libvirtd.kill().unwrap();
317+
let libvirtd_output = libvirtd.wait_with_output().unwrap();
318+
319+
eprintln!(
320+
"libvirtd stdout\n\n{}\n\nlibvirtd stderr\n\n{}",
321+
std::str::from_utf8(&libvirtd_output.stdout).unwrap(),
322+
std::str::from_utf8(&libvirtd_output.stderr).unwrap()
323+
);
324+
325+
assert!(std::str::from_utf8(&output.stdout)
326+
.unwrap()
327+
.trim()
328+
.starts_with(&format!("Domain {} defined", guest.vm_name)));
329+
330+
let re = Regex::new(&format!(r"\s+-\s+{}\s+shut off", guest.vm_name)).unwrap();
331+
assert!(re.is_match(std::str::from_utf8(&list_output.stdout).unwrap().trim()));
332+
333+
assert!(std::str::from_utf8(&undefine_output.stdout)
334+
.unwrap()
335+
.trim()
336+
.starts_with(&format!("Domain {} has been undefined", guest.vm_name)));
337+
}
338+
280339
#[test]
281340
fn test_huge_memory() {
282341
cleanup_libvirt_state();

0 commit comments

Comments
 (0)