tests: Refactor test_cpu_affinity into reusable helper

Extract CPU affinity test logic from test_cpu_affinity into a
standalone _test_cpu_affinity helper that accepts a Guest
reference. The helper verifies the host has at least 4 CPUs,
boots a VM with affinity settings, and asserts vcpu0 is pinned
to cores 0,2 and vcpu1 to cores 1,3.

Add default_cpus_with_affinity_string() to Guest and
default_cpus_with_affinity() to GuestCommand in test_infra
to generate CPU arguments with affinity configuration.

Update the test_cpu_affinity call site in common_parallel to
use GuestFactory and delegate to the new helper, enabling
reuse with different guest types.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
This commit is contained in:
Muminul Islam
2026-03-05 19:44:59 -08:00
committed by Rob Bradford
parent 848a280483
commit 9059fb902d
2 changed files with 58 additions and 36 deletions

View File

@@ -1442,6 +1442,14 @@ impl Guest {
)
}
pub fn default_cpus_with_affinity_string(&self) -> String {
format!(
"boot={},affinity=[0@[0,2],1@[1,3]]{}",
self.num_cpu,
if self.nested { "" } else { ",nested=off" }
)
}
pub fn default_memory_string(&self) -> String {
format!("size={}", self.mem_size_str)
}
@@ -1716,6 +1724,16 @@ impl<'a> GuestCommand<'a> {
self.args(["--cpus", self.guest.default_cpus_string().as_str()])
}
pub fn default_cpus_with_affinity(&mut self) -> &mut Self {
// Only support cpu affinity for 2 VCPUs for now,
// as it is only used in a test that validates cpu affinity is applied correctly.
assert_eq!(self.guest.num_cpu, 2);
self.args([
"--cpus",
self.guest.default_cpus_with_affinity_string().as_str(),
])
}
pub fn default_memory(&mut self) -> &mut Self {
self.args(["--memory", self.guest.default_memory_string().as_str()])
}