From fb02146e2cbc99767e9e41f437fbfe5c4062494b Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Sat, 21 Mar 2026 18:24:50 -0700 Subject: [PATCH] tests: extract _test_console_file to tests_wrappers Extract test logic from test_console_file into a shared _test_console_file wrapper function in tests_wrappers.rs. Update the parent test case to use the basic_regular_guest macro. The wrapper uses default_kernel_cmdline() for kernel/cmdline setup. Signed-off-by: Muminul Islam --- .../tests/common/tests_wrappers.rs | 48 ++++++++++++++++++- cloud-hypervisor/tests/integration.rs | 48 +------------------ 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/cloud-hypervisor/tests/common/tests_wrappers.rs b/cloud-hypervisor/tests/common/tests_wrappers.rs index 874ad795c..f268ed75d 100644 --- a/cloud-hypervisor/tests/common/tests_wrappers.rs +++ b/cloud-hypervisor/tests/common/tests_wrappers.rs @@ -3,7 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 use std::ffi::CStr; use std::fs::{self, OpenOptions}; -use std::io::{Seek, SeekFrom, Write}; +use std::io::{Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; use std::string::String; use std::sync::mpsc; @@ -14,6 +14,7 @@ use net_util::MacAddr; use test_infra::*; use vmm_sys_util::tempdir::TempDir; use vmm_sys_util::tempfile::TempFile; +use wait_timeout::ChildExt; use crate::common::utils::{TargetApi, *}; @@ -2401,3 +2402,48 @@ pub(crate) fn _test_virtio_console(guest: &Guest) { handle_child_output(r, &output); } + +pub(crate) fn _test_console_file(guest: &Guest) { + let console_path = guest.tmp_dir.as_path().join("console-output"); + let mut child = GuestCommand::new(guest) + .default_cpus() + .default_memory() + .default_kernel_cmdline() + .default_disks() + .default_net() + .args([ + "--console", + format!("file={}", console_path.to_str().unwrap()).as_str(), + ]) + .capture_output() + .spawn() + .unwrap(); + + guest.wait_vm_boot().unwrap(); + + guest.ssh_command("sudo shutdown -h now").unwrap(); + + let _ = child.wait_timeout(std::time::Duration::from_secs(20)); + kill_child(&mut child); + let output = child.wait_with_output().unwrap(); + + let r = std::panic::catch_unwind(|| { + // Check that the cloud-hypervisor binary actually terminated + assert!(output.status.success()); + + // Do this check after shutdown of the VM as an easy way to ensure + // all writes are flushed to disk + let mut f = std::fs::File::open(console_path).unwrap(); + let mut buf = String::new(); + f.read_to_string(&mut buf).unwrap(); + + if !buf.contains(CONSOLE_TEST_STRING) { + eprintln!( + "\n\n==== Console file output ====\n\n{buf}\n\n==== End console file output ====" + ); + } + assert!(buf.contains(CONSOLE_TEST_STRING)); + }); + + handle_child_output(r, &output); +} diff --git a/cloud-hypervisor/tests/integration.rs b/cloud-hypervisor/tests/integration.rs index 55f081d12..4e63ad932 100644 --- a/cloud-hypervisor/tests/integration.rs +++ b/cloud-hypervisor/tests/integration.rs @@ -2203,52 +2203,8 @@ mod common_parallel { #[test] fn test_console_file() { - let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string()); - let guest = Guest::new(Box::new(disk_config)); - - let console_path = guest.tmp_dir.as_path().join("console-output"); - let mut child = GuestCommand::new(&guest) - .default_cpus() - .default_memory() - .args(["--kernel", direct_kernel_boot_path().to_str().unwrap()]) - .args(["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) - .default_disks() - .default_net() - .args([ - "--console", - format!("file={}", console_path.to_str().unwrap()).as_str(), - ]) - .capture_output() - .spawn() - .unwrap(); - - guest.wait_vm_boot().unwrap(); - - guest.ssh_command("sudo shutdown -h now").unwrap(); - - let _ = child.wait_timeout(std::time::Duration::from_secs(20)); - kill_child(&mut child); - let output = child.wait_with_output().unwrap(); - - let r = std::panic::catch_unwind(|| { - // Check that the cloud-hypervisor binary actually terminated - assert!(output.status.success()); - - // Do this check after shutdown of the VM as an easy way to ensure - // all writes are flushed to disk - let mut f = std::fs::File::open(console_path).unwrap(); - let mut buf = String::new(); - f.read_to_string(&mut buf).unwrap(); - - if !buf.contains(CONSOLE_TEST_STRING) { - eprintln!( - "\n\n==== Console file output ====\n\n{buf}\n\n==== End console file output ====" - ); - } - assert!(buf.contains(CONSOLE_TEST_STRING)); - }); - - handle_child_output(r, &output); + let guest = basic_regular_guest!(JAMMY_IMAGE_NAME); + _test_console_file(&guest); } #[test]