mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
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 <muislam@microsoft.com>
This commit is contained in:
committed by
Rob Bradford
parent
dfd07ae6d0
commit
fb02146e2c
@@ -3,7 +3,7 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::fs::{self, OpenOptions};
|
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::path::{Path, PathBuf};
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
@@ -14,6 +14,7 @@ use net_util::MacAddr;
|
|||||||
use test_infra::*;
|
use test_infra::*;
|
||||||
use vmm_sys_util::tempdir::TempDir;
|
use vmm_sys_util::tempdir::TempDir;
|
||||||
use vmm_sys_util::tempfile::TempFile;
|
use vmm_sys_util::tempfile::TempFile;
|
||||||
|
use wait_timeout::ChildExt;
|
||||||
|
|
||||||
use crate::common::utils::{TargetApi, *};
|
use crate::common::utils::{TargetApi, *};
|
||||||
|
|
||||||
@@ -2401,3 +2402,48 @@ pub(crate) fn _test_virtio_console(guest: &Guest) {
|
|||||||
|
|
||||||
handle_child_output(r, &output);
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2203,52 +2203,8 @@ mod common_parallel {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_console_file() {
|
fn test_console_file() {
|
||||||
let disk_config = UbuntuDiskConfig::new(JAMMY_IMAGE_NAME.to_string());
|
let guest = basic_regular_guest!(JAMMY_IMAGE_NAME);
|
||||||
let guest = Guest::new(Box::new(disk_config));
|
_test_console_file(&guest);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user