Skip to content

Commit d44b5cf

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 2cd78ba commit d44b5cf

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
@@ -1323,44 +1323,6 @@ mod tests {
13231323
aux_vm_config
13241324
);
13251325

1326-
// Invalid vcpu count.
1327-
aux_vm_config.vcpu_count = Some(0);
1328-
assert_eq!(
1329-
vm_resources.update_machine_config(&aux_vm_config),
1330-
Err(MachineConfigError::InvalidVcpuCount)
1331-
);
1332-
aux_vm_config.vcpu_count = Some(33);
1333-
assert_eq!(
1334-
vm_resources.update_machine_config(&aux_vm_config),
1335-
Err(MachineConfigError::InvalidVcpuCount)
1336-
);
1337-
1338-
// Check that SMT is not supported on aarch64, and that on x86_64 enabling it requires vcpu
1339-
// count to be even.
1340-
aux_vm_config.smt = Some(true);
1341-
#[cfg(target_arch = "aarch64")]
1342-
assert_eq!(
1343-
vm_resources.update_machine_config(&aux_vm_config),
1344-
Err(MachineConfigError::SmtNotSupported)
1345-
);
1346-
aux_vm_config.vcpu_count = Some(3);
1347-
#[cfg(target_arch = "x86_64")]
1348-
assert_eq!(
1349-
vm_resources.update_machine_config(&aux_vm_config),
1350-
Err(MachineConfigError::InvalidVcpuCount)
1351-
);
1352-
aux_vm_config.vcpu_count = Some(32);
1353-
#[cfg(target_arch = "x86_64")]
1354-
vm_resources.update_machine_config(&aux_vm_config).unwrap();
1355-
aux_vm_config.smt = Some(false);
1356-
1357-
// Invalid mem_size_mib.
1358-
aux_vm_config.mem_size_mib = Some(0);
1359-
assert_eq!(
1360-
vm_resources.update_machine_config(&aux_vm_config),
1361-
Err(MachineConfigError::InvalidMemorySize)
1362-
);
1363-
13641326
// Incompatible mem_size_mib with balloon size.
13651327
vm_resources.machine_config.mem_size_mib = 128;
13661328
vm_resources
@@ -1379,23 +1341,6 @@ mod tests {
13791341
// mem_size_mib compatible with balloon size.
13801342
aux_vm_config.mem_size_mib = Some(256);
13811343
vm_resources.update_machine_config(&aux_vm_config).unwrap();
1382-
1383-
// mem_size_mib incompatible with huge pages configuration
1384-
aux_vm_config.mem_size_mib = Some(129);
1385-
aux_vm_config.huge_pages = Some(HugePageConfig::Hugetlbfs2M);
1386-
assert_eq!(
1387-
vm_resources
1388-
.update_machine_config(&aux_vm_config)
1389-
.unwrap_err(),
1390-
MachineConfigError::InvalidMemorySize
1391-
);
1392-
1393-
// mem_size_mib compatible with huge page configuration
1394-
aux_vm_config.mem_size_mib = Some(2048);
1395-
// Remove the balloon device config that's added by `default_vm_resources` as it would
1396-
// trigger the "ballooning incompatible with huge pages" check.
1397-
vm_resources.balloon = BalloonBuilder::new();
1398-
vm_resources.update_machine_config(&aux_vm_config).unwrap();
13991344
}
14001345

14011346
#[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)