Skip to content

Commit c507d69

Browse files
committed
test(virtio-mem): add rust integration tests
These tests add unit test coverage to the builder.rs and vm.rs files which where previously untested in the memory hotplug case. Signed-off-by: Riccardo Mancini <[email protected]>
1 parent 922da76 commit c507d69

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

src/vmm/src/test_utils/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::vm_memory_vendored::GuestRegionCollection;
1616
use crate::vmm_config::boot_source::BootSourceConfig;
1717
use crate::vmm_config::instance_info::InstanceInfo;
1818
use crate::vmm_config::machine_config::HugePageConfig;
19+
use crate::vmm_config::memory_hotplug::MemoryHotplugConfig;
1920
use crate::vstate::memory::{self, GuestMemoryMmap, GuestRegionMmap, GuestRegionMmapExt};
2021
use crate::{EventManager, Vmm};
2122

@@ -73,6 +74,7 @@ pub fn create_vmm(
7374
is_diff: bool,
7475
boot_microvm: bool,
7576
pci_enabled: bool,
77+
memory_hotplug_enabled: bool,
7678
) -> (Arc<Mutex<Vmm>>, EventManager) {
7779
let mut event_manager = EventManager::new().unwrap();
7880
let empty_seccomp_filters = get_empty_filters();
@@ -96,6 +98,14 @@ pub fn create_vmm(
9698

9799
resources.pci_enabled = pci_enabled;
98100

101+
if memory_hotplug_enabled {
102+
resources.memory_hotplug = Some(MemoryHotplugConfig {
103+
total_size_mib: 1024,
104+
block_size_mib: 2,
105+
slot_size_mib: 128,
106+
});
107+
}
108+
99109
let vmm = build_microvm_for_boot(
100110
&InstanceInfo::default(),
101111
&resources,
@@ -112,23 +122,15 @@ pub fn create_vmm(
112122
}
113123

114124
pub fn default_vmm(kernel_image: Option<&str>) -> (Arc<Mutex<Vmm>>, EventManager) {
115-
create_vmm(kernel_image, false, true, false)
125+
create_vmm(kernel_image, false, true, false, false)
116126
}
117127

118128
pub fn default_vmm_no_boot(kernel_image: Option<&str>) -> (Arc<Mutex<Vmm>>, EventManager) {
119-
create_vmm(kernel_image, false, false, false)
120-
}
121-
122-
pub fn default_vmm_pci_no_boot(kernel_image: Option<&str>) -> (Arc<Mutex<Vmm>>, EventManager) {
123-
create_vmm(kernel_image, false, false, true)
129+
create_vmm(kernel_image, false, false, false, false)
124130
}
125131

126132
pub fn dirty_tracking_vmm(kernel_image: Option<&str>) -> (Arc<Mutex<Vmm>>, EventManager) {
127-
create_vmm(kernel_image, true, true, false)
128-
}
129-
130-
pub fn default_vmm_pci(kernel_image: Option<&str>) -> (Arc<Mutex<Vmm>>, EventManager) {
131-
create_vmm(kernel_image, false, true, false)
133+
create_vmm(kernel_image, true, true, false, false)
132134
}
133135

134136
#[allow(clippy::undocumented_unsafe_blocks)]

src/vmm/tests/integration_tests.rs

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ use vmm::rpc_interface::{
1818
use vmm::seccomp::get_empty_filters;
1919
use vmm::snapshot::Snapshot;
2020
use vmm::test_utils::mock_resources::{MockVmResources, NOISY_KERNEL_IMAGE};
21-
use vmm::test_utils::{
22-
create_vmm, default_vmm, default_vmm_no_boot, default_vmm_pci, default_vmm_pci_no_boot,
23-
};
21+
use vmm::test_utils::{create_vmm, default_vmm, default_vmm_no_boot};
2422
use vmm::vmm_config::balloon::BalloonDeviceConfig;
2523
use vmm::vmm_config::boot_source::BootSourceConfig;
2624
use vmm::vmm_config::drive::BlockDeviceConfig;
@@ -66,13 +64,12 @@ fn test_build_and_boot_microvm() {
6664
assert_eq!(format!("{:?}", vmm_ret.err()), "Some(MissingKernelConfig)");
6765
}
6866

69-
// Success case.
70-
let (vmm, evmgr) = default_vmm(None);
71-
check_booted_microvm(vmm, evmgr);
72-
73-
// microVM with PCI
74-
let (vmm, evmgr) = default_vmm_pci(None);
75-
check_booted_microvm(vmm, evmgr);
67+
for pci_enabled in [false, true] {
68+
for memory_hotplug in [false, true] {
69+
let (vmm, evmgr) = create_vmm(None, false, true, pci_enabled, memory_hotplug);
70+
check_booted_microvm(vmm, evmgr);
71+
}
72+
}
7673
}
7774

7875
#[allow(unused_mut, unused_variables)]
@@ -96,10 +93,12 @@ fn check_build_microvm(vmm: Arc<Mutex<Vmm>>, mut evmgr: EventManager) {
9693

9794
#[test]
9895
fn test_build_microvm() {
99-
let (vmm, evtmgr) = default_vmm_no_boot(None);
100-
check_build_microvm(vmm, evtmgr);
101-
let (vmm, evtmgr) = default_vmm_pci_no_boot(None);
102-
check_build_microvm(vmm, evtmgr);
96+
for pci_enabled in [false, true] {
97+
for memory_hotplug in [false, true] {
98+
let (vmm, evmgr) = create_vmm(None, false, false, pci_enabled, memory_hotplug);
99+
check_build_microvm(vmm, evmgr);
100+
}
101+
}
103102
}
104103

105104
fn pause_resume_microvm(vmm: Arc<Mutex<Vmm>>) {
@@ -118,13 +117,14 @@ fn pause_resume_microvm(vmm: Arc<Mutex<Vmm>>) {
118117

119118
#[test]
120119
fn test_pause_resume_microvm() {
121-
// Tests that pausing and resuming a microVM work as expected.
122-
let (vmm, _) = default_vmm(None);
120+
for pci_enabled in [false, true] {
121+
for memory_hotplug in [false, true] {
122+
// Tests that pausing and resuming a microVM work as expected.
123+
let (vmm, _) = create_vmm(None, false, true, pci_enabled, memory_hotplug);
123124

124-
pause_resume_microvm(vmm);
125-
126-
let (vmm, _) = default_vmm_pci(None);
127-
pause_resume_microvm(vmm);
125+
pause_resume_microvm(vmm);
126+
}
127+
}
128128
}
129129

130130
#[test]
@@ -195,11 +195,21 @@ fn test_disallow_dump_cpu_config_without_pausing() {
195195
vmm.lock().unwrap().stop(FcExitCode::Ok);
196196
}
197197

198-
fn verify_create_snapshot(is_diff: bool, pci_enabled: bool) -> (TempFile, TempFile) {
198+
fn verify_create_snapshot(
199+
is_diff: bool,
200+
pci_enabled: bool,
201+
memory_hotplug: bool,
202+
) -> (TempFile, TempFile) {
199203
let snapshot_file = TempFile::new().unwrap();
200204
let memory_file = TempFile::new().unwrap();
201205

202-
let (vmm, _) = create_vmm(Some(NOISY_KERNEL_IMAGE), is_diff, true, pci_enabled);
206+
let (vmm, _) = create_vmm(
207+
Some(NOISY_KERNEL_IMAGE),
208+
is_diff,
209+
true,
210+
pci_enabled,
211+
memory_hotplug,
212+
);
203213
let resources = VmResources {
204214
machine_config: MachineConfig {
205215
mem_size_mib: 1,
@@ -303,14 +313,19 @@ fn verify_load_snapshot(snapshot_file: TempFile, memory_file: TempFile) {
303313

304314
#[test]
305315
fn test_create_and_load_snapshot() {
306-
for (diff_snap, pci_enabled) in [(false, false), (false, true), (true, false), (true, true)] {
307-
// Create snapshot.
308-
let (snapshot_file, memory_file) = verify_create_snapshot(diff_snap, pci_enabled);
309-
// Create a new microVm from snapshot. This only tests code-level logic; it verifies
310-
// that a microVM can be built with no errors from given snapshot.
311-
// It does _not_ verify that the guest is actually restored properly. We're using
312-
// python integration tests for that.
313-
verify_load_snapshot(snapshot_file, memory_file);
316+
for diff_snap in [false, true] {
317+
for pci_enabled in [false, true] {
318+
for memory_hotplug in [false, true] {
319+
// Create snapshot.
320+
let (snapshot_file, memory_file) =
321+
verify_create_snapshot(diff_snap, pci_enabled, memory_hotplug);
322+
// Create a new microVm from snapshot. This only tests code-level logic; it verifies
323+
// that a microVM can be built with no errors from given snapshot.
324+
// It does _not_ verify that the guest is actually restored properly. We're using
325+
// python integration tests for that.
326+
verify_load_snapshot(snapshot_file, memory_file);
327+
}
328+
}
314329
}
315330
}
316331

@@ -338,15 +353,15 @@ fn check_snapshot(mut microvm_state: MicrovmState) {
338353

339354
fn get_microvm_state_from_snapshot(pci_enabled: bool) -> MicrovmState {
340355
// Create a diff snapshot
341-
let (snapshot_file, _) = verify_create_snapshot(true, pci_enabled);
356+
let (snapshot_file, _) = verify_create_snapshot(true, pci_enabled, false);
342357

343358
// Deserialize the microVM state.
344359
snapshot_file.as_file().seek(SeekFrom::Start(0)).unwrap();
345360
Snapshot::load(&mut snapshot_file.as_file()).unwrap().data
346361
}
347362

348363
fn verify_load_snap_disallowed_after_boot_resources(res: VmmAction, res_name: &str) {
349-
let (snapshot_file, memory_file) = verify_create_snapshot(false, false);
364+
let (snapshot_file, memory_file) = verify_create_snapshot(false, false, false);
350365

351366
let mut event_manager = EventManager::new().unwrap();
352367
let empty_seccomp_filters = get_empty_filters();

0 commit comments

Comments
 (0)