tests: Make api_create_body() parameterless

Refactor api_create_body() to read cpu_count, kernel_path,
and kernel_cmdline from Guest fields instead of taking them
as parameters. This makes Guest the single source of truth
for VM configuration.

Update all call sites in HTTP and DBus API tests to use the
new parameterless signature. Switch guest creation to use
GuestFactory for consistent 4-CPU configuration.

Replace manual CPU and memory assertions with
validate_cpu_count() and validate_memory() helpers.
Replace thread::sleep with wait_vm_boot() in
_test_api_create_boot for proper boot synchronization.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
This commit is contained in:
Muminul Islam
2026-02-27 19:14:19 -08:00
committed by Rob Bradford
parent f443bae9d9
commit 7a0019514f
2 changed files with 55 additions and 62 deletions

View File

@@ -109,12 +109,7 @@ fn _test_api_create_boot(target_api: &TargetApi, guest: &Guest) {
assert!(target_api.remote_command("ping", None));
// Create the VM first
let cpu_count: u8 = 4;
let request_body = guest.api_create_body(
cpu_count,
direct_kernel_boot_path().to_str().unwrap(),
DIRECT_KERNEL_BOOT_CMDLINE,
);
let request_body = guest.api_create_body();
let temp_config_path = guest.tmp_dir.as_path().join("config");
std::fs::write(&temp_config_path, request_body).unwrap();
@@ -124,12 +119,12 @@ fn _test_api_create_boot(target_api: &TargetApi, guest: &Guest) {
// Then boot it
assert!(target_api.remote_command("boot", None));
thread::sleep(std::time::Duration::new(20, 0));
let r = std::panic::catch_unwind(|| {
guest.wait_vm_boot().unwrap();
// Check that the VM booted as expected
assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count);
assert!(guest.get_total_memory().unwrap_or_default() > 480_000);
guest.validate_cpu_count(None);
guest.validate_memory(None);
});
kill_child(&mut child);
@@ -154,12 +149,7 @@ fn _test_api_shutdown(target_api: &TargetApi, guest: &Guest) {
assert!(target_api.remote_command("ping", None));
// Create the VM first
let cpu_count: u8 = 4;
let request_body = guest.api_create_body(
cpu_count,
direct_kernel_boot_path().to_str().unwrap(),
DIRECT_KERNEL_BOOT_CMDLINE,
);
let request_body = guest.api_create_body();
let temp_config_path = guest.tmp_dir.as_path().join("config");
std::fs::write(&temp_config_path, request_body).unwrap();
@@ -174,8 +164,8 @@ fn _test_api_shutdown(target_api: &TargetApi, guest: &Guest) {
guest.wait_vm_boot().unwrap();
// Check that the VM booted as expected
assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count);
assert!(guest.get_total_memory().unwrap_or_default() > 480_000);
guest.validate_cpu_count(None);
guest.validate_memory(None);
// Sync and shutdown without powering off to prevent filesystem
// corruption.
@@ -194,8 +184,8 @@ fn _test_api_shutdown(target_api: &TargetApi, guest: &Guest) {
guest.wait_vm_boot().unwrap();
// Check that the VM booted as expected
assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count);
assert!(guest.get_total_memory().unwrap_or_default() > 480_000);
guest.validate_cpu_count(None);
guest.validate_memory(None);
});
kill_child(&mut child);
@@ -220,12 +210,8 @@ fn _test_api_delete(target_api: &TargetApi, guest: &Guest) {
assert!(target_api.remote_command("ping", None));
// Create the VM first
let cpu_count: u8 = 4;
let request_body = guest.api_create_body(
cpu_count,
direct_kernel_boot_path().to_str().unwrap(),
DIRECT_KERNEL_BOOT_CMDLINE,
);
let request_body = guest.api_create_body();
let temp_config_path = guest.tmp_dir.as_path().join("config");
std::fs::write(&temp_config_path, request_body).unwrap();
let create_config = temp_config_path.as_os_str().to_str().unwrap();
@@ -239,8 +225,8 @@ fn _test_api_delete(target_api: &TargetApi, guest: &Guest) {
guest.wait_vm_boot().unwrap();
// Check that the VM booted as expected
assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count);
assert!(guest.get_total_memory().unwrap_or_default() > 480_000);
guest.validate_cpu_count(None);
guest.validate_memory(None);
// Sync and shutdown without powering off to prevent filesystem
// corruption.
@@ -261,8 +247,8 @@ fn _test_api_delete(target_api: &TargetApi, guest: &Guest) {
guest.wait_vm_boot().unwrap();
// Check that the VM booted as expected
assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count);
assert!(guest.get_total_memory().unwrap_or_default() > 480_000);
guest.validate_cpu_count(None);
guest.validate_memory(None);
});
kill_child(&mut child);
@@ -288,12 +274,7 @@ fn _test_api_pause_resume(target_api: &TargetApi, guest: &Guest) {
assert!(target_api.remote_command("ping", None));
// Create the VM first
let cpu_count: u8 = 4;
let request_body = guest.api_create_body(
cpu_count,
direct_kernel_boot_path().to_str().unwrap(),
DIRECT_KERNEL_BOOT_CMDLINE,
);
let request_body = guest.api_create_body();
let temp_config_path = guest.tmp_dir.as_path().join("config");
std::fs::write(&temp_config_path, request_body).unwrap();
@@ -307,8 +288,8 @@ fn _test_api_pause_resume(target_api: &TargetApi, guest: &Guest) {
let r = std::panic::catch_unwind(|| {
// Check that the VM booted as expected
assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count);
assert!(guest.get_total_memory().unwrap_or_default() > 480_000);
guest.validate_cpu_count(None);
guest.validate_memory(None);
// We now pause the VM
assert!(target_api.remote_command("pause", None));
@@ -336,7 +317,7 @@ fn _test_api_pause_resume(target_api: &TargetApi, guest: &Guest) {
thread::sleep(std::time::Duration::new(2, 0));
// Now we should be able to SSH back in and get the right number of CPUs
assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count);
guest.validate_cpu_count(None);
});
kill_child(&mut child);
@@ -2522,6 +2503,7 @@ mod common_parallel {
use std::process::Command;
use block::ImageType;
use test_infra::GuestFactory;
use crate::*;
@@ -5961,7 +5943,9 @@ mod common_parallel {
#[test]
fn test_api_http_shutdown() {
let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
let guest = GuestFactory::new_regular_guest_factory()
.create_guest(Box::new(disk_config))
.with_cpu(4);
let target_api = TargetApi::new_http_api(&guest.tmp_dir);
_test_api_shutdown(&target_api, &guest);
@@ -5970,7 +5954,9 @@ mod common_parallel {
#[test]
fn test_api_http_delete() {
let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
let guest = GuestFactory::new_regular_guest_factory()
.create_guest(Box::new(disk_config))
.with_cpu(4);
let target_api = TargetApi::new_http_api(&guest.tmp_dir);
_test_api_delete(&target_api, &guest);
@@ -5979,7 +5965,9 @@ mod common_parallel {
#[test]
fn test_api_http_pause_resume() {
let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
let guest = GuestFactory::new_regular_guest_factory()
.create_guest(Box::new(disk_config))
.with_cpu(4);
let target_api = TargetApi::new_http_api(&guest.tmp_dir);
_test_api_pause_resume(&target_api, &guest);
@@ -5988,7 +5976,9 @@ mod common_parallel {
#[test]
fn test_api_http_create_boot() {
let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
let guest = GuestFactory::new_regular_guest_factory()
.create_guest(Box::new(disk_config))
.with_cpu(4);
let target_api = TargetApi::new_http_api(&guest.tmp_dir);
_test_api_create_boot(&target_api, &guest);
@@ -10105,12 +10095,7 @@ mod dbus_api {
assert!(http_api.remote_command("ping", None));
// Create the VM first
let cpu_count: u8 = 4;
let request_body = guest.api_create_body(
cpu_count,
direct_kernel_boot_path().to_str().unwrap(),
DIRECT_KERNEL_BOOT_CMDLINE,
);
let request_body = guest.api_create_body();
let temp_config_path = guest.tmp_dir.as_path().join("config");
std::fs::write(&temp_config_path, request_body).unwrap();
@@ -10125,8 +10110,8 @@ mod dbus_api {
guest.wait_vm_boot().unwrap();
// Check that the VM booted as expected
assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count);
assert!(guest.get_total_memory().unwrap_or_default() > 480_000);
guest.validate_cpu_count(None);
guest.validate_memory(None);
// Sync and shutdown without powering off to prevent filesystem
// corruption.
@@ -10144,8 +10129,8 @@ mod dbus_api {
guest.wait_vm_boot().unwrap();
// Check that the VM booted as expected
assert_eq!(guest.get_cpu_count().unwrap_or_default() as u8, cpu_count);
assert!(guest.get_total_memory().unwrap_or_default() > 480_000);
guest.validate_cpu_count(None);
guest.validate_memory(None);
});
kill_child(&mut child);
@@ -10157,7 +10142,9 @@ mod dbus_api {
#[test]
fn test_api_dbus_create_boot() {
let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
let guest = GuestFactory::new_regular_guest_factory()
.create_guest(Box::new(disk_config))
.with_cpu(4);
let target_api = TargetApi::new_dbus_api(&guest.tmp_dir);
_test_api_create_boot(&target_api, &guest);
@@ -10166,7 +10153,9 @@ mod dbus_api {
#[test]
fn test_api_dbus_shutdown() {
let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
let guest = GuestFactory::new_regular_guest_factory()
.create_guest(Box::new(disk_config))
.with_cpu(4);
let target_api = TargetApi::new_dbus_api(&guest.tmp_dir);
_test_api_shutdown(&target_api, &guest);
@@ -10175,7 +10164,9 @@ mod dbus_api {
#[test]
fn test_api_dbus_delete() {
let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
let guest = GuestFactory::new_regular_guest_factory()
.create_guest(Box::new(disk_config))
.with_cpu(4);
let target_api = TargetApi::new_dbus_api(&guest.tmp_dir);
_test_api_delete(&target_api, &guest);
@@ -10184,7 +10175,9 @@ mod dbus_api {
#[test]
fn test_api_dbus_pause_resume() {
let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
let guest = GuestFactory::new_regular_guest_factory()
.create_guest(Box::new(disk_config))
.with_cpu(4);
let target_api = TargetApi::new_dbus_api(&guest.tmp_dir);
_test_api_pause_resume(&target_api, &guest);

View File

@@ -1083,11 +1083,11 @@ impl Guest {
)
}
pub fn api_create_body(&self, cpu_count: u8, kernel_path: &str, kernel_cmd: &str) -> String {
pub fn api_create_body(&self) -> String {
let mut body = serde_json::json!({
"cpus": {
"boot_vcpus": cpu_count,
"max_vcpus": cpu_count,
"boot_vcpus": self.num_cpu,
"max_vcpus": self.num_cpu,
},
"net": [
{
@@ -1117,13 +1117,13 @@ impl Guest {
.unwrap()
.to_str()
.unwrap(),
"cmdline": kernel_cmd,
"cmdline": self.kernel_cmdline.as_deref().unwrap(),
"host_data": generate_host_data(),
});
} else {
body["payload"] = serde_json::json!({
"kernel": kernel_path,
"cmdline": kernel_cmd,
"kernel": self.kernel_path.as_deref().unwrap(),
"cmdline": self.kernel_cmdline.as_deref().unwrap(),
});
}