Skip to content

Commit 087a589

Browse files
committed
refactor(test): Move MachineConfig::update tests to machine_config.rs
There's no need to test this through VmResources when it can be tested in isolation. Also, everytime I touch MachineConfig I get confsued by where the hell the tests are, cuz not only are they in a different module, they're also one directory level away. So move the tests into machine_config.rs, where it makes sense to have them. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
1 parent a567d9d commit 087a589

File tree

2 files changed

+94
-56
lines changed

2 files changed

+94
-56
lines changed

src/vmm/src/resources.rs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,44 +1375,6 @@ mod tests {
13751375
aux_vm_config
13761376
);
13771377

1378-
// Invalid vcpu count.
1379-
aux_vm_config.vcpu_count = Some(0);
1380-
assert_eq!(
1381-
vm_resources.update_machine_config(&aux_vm_config),
1382-
Err(MachineConfigError::InvalidVcpuCount)
1383-
);
1384-
aux_vm_config.vcpu_count = Some(33);
1385-
assert_eq!(
1386-
vm_resources.update_machine_config(&aux_vm_config),
1387-
Err(MachineConfigError::InvalidVcpuCount)
1388-
);
1389-
1390-
// Check that SMT is not supported on aarch64, and that on x86_64 enabling it requires vcpu
1391-
// count to be even.
1392-
aux_vm_config.smt = Some(true);
1393-
#[cfg(target_arch = "aarch64")]
1394-
assert_eq!(
1395-
vm_resources.update_machine_config(&aux_vm_config),
1396-
Err(MachineConfigError::SmtNotSupported)
1397-
);
1398-
aux_vm_config.vcpu_count = Some(3);
1399-
#[cfg(target_arch = "x86_64")]
1400-
assert_eq!(
1401-
vm_resources.update_machine_config(&aux_vm_config),
1402-
Err(MachineConfigError::InvalidVcpuCount)
1403-
);
1404-
aux_vm_config.vcpu_count = Some(32);
1405-
#[cfg(target_arch = "x86_64")]
1406-
vm_resources.update_machine_config(&aux_vm_config).unwrap();
1407-
aux_vm_config.smt = Some(false);
1408-
1409-
// Invalid mem_size_mib.
1410-
aux_vm_config.mem_size_mib = Some(0);
1411-
assert_eq!(
1412-
vm_resources.update_machine_config(&aux_vm_config),
1413-
Err(MachineConfigError::InvalidMemorySize)
1414-
);
1415-
14161378
// Incompatible mem_size_mib with balloon size.
14171379
vm_resources.machine_config.mem_size_mib = 128;
14181380
vm_resources
@@ -1431,23 +1393,6 @@ mod tests {
14311393
// mem_size_mib compatible with balloon size.
14321394
aux_vm_config.mem_size_mib = Some(256);
14331395
vm_resources.update_machine_config(&aux_vm_config).unwrap();
1434-
1435-
// mem_size_mib incompatible with huge pages configuration
1436-
aux_vm_config.mem_size_mib = Some(129);
1437-
aux_vm_config.huge_pages = Some(HugePageConfig::Hugetlbfs2M);
1438-
assert_eq!(
1439-
vm_resources
1440-
.update_machine_config(&aux_vm_config)
1441-
.unwrap_err(),
1442-
MachineConfigError::InvalidMemorySize
1443-
);
1444-
1445-
// mem_size_mib compatible with huge page configuration
1446-
aux_vm_config.mem_size_mib = Some(2048);
1447-
// Remove the balloon device config that's added by `default_vm_resources` as it would
1448-
// trigger the "ballooning incompatible with huge pages" check.
1449-
vm_resources.balloon = BalloonBuilder::new();
1450-
vm_resources.update_machine_config(&aux_vm_config).unwrap();
14511396
}
14521397

14531398
#[test]

src/vmm/src/vmm_config/machine_config.rs

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,100 @@ impl MachineConfig {
290290
#[cfg(test)]
291291
mod tests {
292292
use crate::cpu_config::templates::{CpuTemplateType, CustomCpuTemplate, StaticCpuTemplate};
293-
use crate::vmm_config::machine_config::MachineConfig;
293+
use crate::vmm_config::machine_config::{
294+
HugePageConfig, MachineConfig, MachineConfigError, MachineConfigUpdate,
295+
};
296+
297+
#[test]
298+
#[allow(unused)] // some assertions exist only on specific architectures.
299+
fn test_machine_config_update() {
300+
let mconf = MachineConfig::default();
301+
302+
// Assert that the default machine config is valid
303+
assert_eq!(
304+
mconf
305+
.update(&MachineConfigUpdate::from(mconf.clone()))
306+
.unwrap(),
307+
mconf
308+
);
309+
310+
// Invalid vCPU counts
311+
let res = mconf.update(&MachineConfigUpdate {
312+
vcpu_count: Some(0),
313+
..Default::default()
314+
});
315+
assert_eq!(res, Err(MachineConfigError::InvalidVcpuCount));
316+
317+
let res = mconf.update(&MachineConfigUpdate {
318+
vcpu_count: Some(33),
319+
..Default::default()
320+
});
321+
assert_eq!(res, Err(MachineConfigError::InvalidVcpuCount));
322+
323+
// Invalid memory size
324+
let res = mconf.update(&MachineConfigUpdate {
325+
mem_size_mib: Some(0),
326+
..Default::default()
327+
});
328+
assert_eq!(res, Err(MachineConfigError::InvalidMemorySize));
329+
330+
// Memory Size incompatible with huge page configuration
331+
let res = mconf.update(&MachineConfigUpdate {
332+
mem_size_mib: Some(31),
333+
huge_pages: Some(HugePageConfig::Hugetlbfs2M),
334+
..Default::default()
335+
});
336+
assert_eq!(res, Err(MachineConfigError::InvalidMemorySize));
337+
338+
// works if the memory size is a multiple of huge page size indeed
339+
let updated = mconf
340+
.update(&MachineConfigUpdate {
341+
mem_size_mib: Some(32),
342+
huge_pages: Some(HugePageConfig::Hugetlbfs2M),
343+
..Default::default()
344+
})
345+
.unwrap();
346+
assert_eq!(updated.huge_pages, HugePageConfig::Hugetlbfs2M);
347+
assert_eq!(updated.mem_size_mib, 32);
348+
}
349+
350+
#[test]
351+
#[cfg(target_arch = "aarch64")]
352+
fn test_machine_config_update_aarch64() {
353+
let mconf = MachineConfig::default();
354+
355+
// Check that SMT is not supported on aarch64
356+
let res = mconf.update(&MachineConfigUpdate {
357+
smt: Some(true),
358+
..Default::default()
359+
});
360+
assert_eq!(res, Err(MachineConfigError::SmtNotSupported));
361+
}
362+
363+
#[test]
364+
#[cfg(target_arch = "x86_64")]
365+
fn test_machine_config_update_x86_64() {
366+
let mconf = MachineConfig::default();
367+
368+
// Test that SMT requires an even vcpu count
369+
let res = mconf.update(&MachineConfigUpdate {
370+
vcpu_count: Some(3),
371+
smt: Some(true),
372+
..Default::default()
373+
});
374+
assert_eq!(res, Err(MachineConfigError::InvalidVcpuCount));
375+
376+
// Works if the vcpu count is even indeed
377+
let updated = mconf
378+
.update(&MachineConfigUpdate {
379+
vcpu_count: Some(32),
380+
smt: Some(true),
381+
..Default::default()
382+
})
383+
.unwrap();
384+
assert_eq!(updated.vcpu_count, 32);
385+
assert!(updated.smt);
386+
}
294387

295388
// Ensure the special (de)serialization logic for the cpu_template field works:
296389
// only static cpu templates can be specified via the machine-config endpoint, but

0 commit comments

Comments
 (0)