22// SPDX-License-Identifier: Apache-2.0
33
44use std:: io:: { Seek , SeekFrom } ;
5+ use std:: sync:: { Arc , Mutex } ;
56use std:: thread;
67use std:: time:: Duration ;
78
@@ -15,7 +16,9 @@ use vmm::rpc_interface::{
1516use vmm:: seccomp:: get_empty_filters;
1617use vmm:: snapshot:: Snapshot ;
1718use vmm:: test_utils:: mock_resources:: { MockVmResources , NOISY_KERNEL_IMAGE } ;
18- use vmm:: test_utils:: { create_vmm, default_vmm, default_vmm_no_boot} ;
19+ use vmm:: test_utils:: {
20+ create_vmm, default_vmm, default_vmm_no_boot, default_vmm_pci, default_vmm_pci_no_boot,
21+ } ;
1922use vmm:: vmm_config:: balloon:: BalloonDeviceConfig ;
2023use vmm:: vmm_config:: boot_source:: BootSourceConfig ;
2124use vmm:: vmm_config:: drive:: BlockDeviceConfig ;
@@ -26,9 +29,23 @@ use vmm::vmm_config::snapshot::{
2629 CreateSnapshotParams , LoadSnapshotParams , MemBackendConfig , MemBackendType , SnapshotType ,
2730} ;
2831use vmm:: vmm_config:: vsock:: VsockDeviceConfig ;
29- use vmm:: { DumpCpuConfigError , EventManager , FcExitCode } ;
32+ use vmm:: { DumpCpuConfigError , EventManager , FcExitCode , Vmm } ;
3033use vmm_sys_util:: tempfile:: TempFile ;
3134
35+ fn check_booted_microvm ( vmm : Arc < Mutex < Vmm > > , mut evmgr : EventManager ) {
36+ // On x86_64, the vmm should exit once its workload completes and signals the exit event.
37+ // On aarch64, the test kernel doesn't exit, so the vmm is force-stopped.
38+ #[ cfg( target_arch = "x86_64" ) ]
39+ evmgr. run_with_timeout ( 500 ) . unwrap ( ) ;
40+ #[ cfg( target_arch = "aarch64" ) ]
41+ vmm. lock ( ) . unwrap ( ) . stop ( FcExitCode :: Ok ) ;
42+
43+ assert_eq ! (
44+ vmm. lock( ) . unwrap( ) . shutdown_exit_code( ) ,
45+ Some ( FcExitCode :: Ok )
46+ ) ;
47+ }
48+
3249#[ test]
3350fn test_build_and_boot_microvm ( ) {
3451 // Error case: no boot source configured.
@@ -47,33 +64,24 @@ fn test_build_and_boot_microvm() {
4764 }
4865
4966 // Success case.
50- let ( vmm, mut _evmgr) = default_vmm ( None ) ;
51-
52- // On x86_64, the vmm should exit once its workload completes and signals the exit event.
53- // On aarch64, the test kernel doesn't exit, so the vmm is force-stopped.
54- #[ cfg( target_arch = "x86_64" ) ]
55- _evmgr. run_with_timeout ( 500 ) . unwrap ( ) ;
56- #[ cfg( target_arch = "aarch64" ) ]
57- vmm. lock ( ) . unwrap ( ) . stop ( FcExitCode :: Ok ) ;
67+ let ( vmm, evmgr) = default_vmm ( None ) ;
68+ check_booted_microvm ( vmm, evmgr) ;
5869
59- assert_eq ! (
60- vmm. lock( ) . unwrap( ) . shutdown_exit_code( ) ,
61- Some ( FcExitCode :: Ok )
62- ) ;
70+ // microVM with PCI
71+ let ( vmm, evmgr) = default_vmm_pci ( None ) ;
72+ check_booted_microvm ( vmm, evmgr) ;
6373}
6474
65- #[ test]
66- fn test_build_microvm ( ) {
75+ fn check_build_microvm ( vmm : Arc < Mutex < Vmm > > , mut evmgr : EventManager ) {
6776 // The built microVM should be in the `VmState::Paused` state here.
68- let ( vmm, mut _evtmgr) = default_vmm_no_boot ( None ) ;
6977 assert_eq ! ( vmm. lock( ) . unwrap( ) . instance_info( ) . state, VmState :: Paused ) ;
7078
7179 // The microVM should be able to resume and exit successfully.
7280 // On x86_64, the vmm should exit once its workload completes and signals the exit event.
7381 // On aarch64, the test kernel doesn't exit, so the vmm is force-stopped.
7482 vmm. lock ( ) . unwrap ( ) . resume_vm ( ) . unwrap ( ) ;
7583 #[ cfg( target_arch = "x86_64" ) ]
76- _evtmgr . run_with_timeout ( 500 ) . unwrap ( ) ;
84+ evmgr . run_with_timeout ( 500 ) . unwrap ( ) ;
7785 #[ cfg( target_arch = "aarch64" ) ]
7886 vmm. lock ( ) . unwrap ( ) . stop ( FcExitCode :: Ok ) ;
7987 assert_eq ! (
@@ -83,10 +91,14 @@ fn test_build_microvm() {
8391}
8492
8593#[ test]
86- fn test_pause_resume_microvm ( ) {
87- // Tests that pausing and resuming a microVM work as expected.
88- let ( vmm, _) = default_vmm ( None ) ;
94+ fn test_build_microvm ( ) {
95+ let ( vmm, evtmgr) = default_vmm_no_boot ( None ) ;
96+ check_build_microvm ( vmm, evtmgr) ;
97+ let ( vmm, evtmgr) = default_vmm_pci_no_boot ( None ) ;
98+ check_build_microvm ( vmm, evtmgr) ;
99+ }
89100
101+ fn pause_resume_microvm ( vmm : Arc < Mutex < Vmm > > ) {
90102 let mut api_controller = RuntimeApiController :: new ( VmResources :: default ( ) , vmm. clone ( ) ) ;
91103
92104 // There's a race between this thread and the vcpu thread, but this thread
@@ -100,6 +112,17 @@ fn test_pause_resume_microvm() {
100112 vmm. lock ( ) . unwrap ( ) . stop ( FcExitCode :: Ok ) ;
101113}
102114
115+ #[ test]
116+ fn test_pause_resume_microvm ( ) {
117+ // Tests that pausing and resuming a microVM work as expected.
118+ let ( vmm, _) = default_vmm ( None ) ;
119+
120+ pause_resume_microvm ( vmm) ;
121+
122+ let ( vmm, _) = default_vmm_pci ( None ) ;
123+ pause_resume_microvm ( vmm) ;
124+ }
125+
103126#[ test]
104127fn test_dirty_bitmap_error ( ) {
105128 // Error case: dirty tracking disabled.
0 commit comments