66#[ cfg( test) ]
77#[ macro_use]
88extern crate lazy_static;
9+ extern crate regex;
910
1011#[ cfg( test) ]
1112mod 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 \n libvirtd 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