integration_tests: Define Config struct

Define `struct Config` to specify crosvm arguments so it'll make it
easier to add more complicated arguments in future CLs.

BUG=b:220292205
TEST=./integration_tests/run

Change-Id: I89921e171a81071dbfc58d22b233e9c71abcdf54
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3686717
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Keiichi Watanabe 2022-06-06 22:00:24 +09:00 committed by Chromeos LUCI
parent f54f7ed040
commit 0974be8042
2 changed files with 37 additions and 7 deletions

View file

@ -2,17 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
mod fixture; mod fixture;
use fixture::TestVm; use fixture::{Config, TestVm};
#[test] #[test]
fn boot_test_vm() { fn boot_test_vm() {
let mut vm = TestVm::new(&[], false /* o_direct */).unwrap(); let mut vm = TestVm::new(Config::new()).unwrap();
assert_eq!(vm.exec_in_guest("echo 42").unwrap().trim(), "42"); assert_eq!(vm.exec_in_guest("echo 42").unwrap().trim(), "42");
} }
#[test] #[test]
fn boot_test_vm_odirect() { fn boot_test_vm_odirect() {
let mut vm = TestVm::new(&[], true /* o_direct */).unwrap(); let mut vm = TestVm::new(Config::new().o_direct()).unwrap();
assert_eq!(vm.exec_in_guest("echo 42").unwrap().trim(), "42"); assert_eq!(vm.exec_in_guest("echo 42").unwrap().trim(), "42");
} }
@ -20,7 +20,7 @@ fn boot_test_vm_odirect() {
fn boot_test_suspend_resume() { fn boot_test_suspend_resume() {
// There is no easy way for us to check if the VM is actually suspended. But at // There is no easy way for us to check if the VM is actually suspended. But at
// least exercise the code-path. // least exercise the code-path.
let mut vm = TestVm::new(&[], false /*o_direct */).unwrap(); let mut vm = TestVm::new(Config::new()).unwrap();
vm.suspend().unwrap(); vm.suspend().unwrap();
vm.resume().unwrap(); vm.resume().unwrap();
assert_eq!(vm.exec_in_guest("echo 42").unwrap().trim(), "42"); assert_eq!(vm.exec_in_guest("echo 42").unwrap().trim(), "42");

View file

@ -142,6 +142,36 @@ fn download_file(url: &str, destination: &Path) -> Result<()> {
} }
} }
/// Configuration to start `TestVm`.
#[derive(Default)]
pub struct Config {
/// Extra arguments for the `run` subcommand.
extra_args: Vec<String>,
/// Use `O_DIRECT` for the rootfs.
o_direct: bool,
}
impl Config {
/// Creates a new `run` command with `extra_args`.
pub fn new() -> Self {
Default::default()
}
/// Uses extra arguments for `crosvm run`.
#[allow(dead_code)]
pub fn extra_args(mut self, args: Vec<String>) -> Self {
self.extra_args = args;
self
}
/// Uses `O_DIRECT` for the rootfs.
pub fn o_direct(mut self) -> Self {
self.o_direct = true;
self
}
}
/// Test fixture to spin up a VM running a guest that can be communicated with. /// Test fixture to spin up a VM running a guest that can be communicated with.
/// ///
/// After creation, commands can be sent via exec_in_guest. The VM is stopped /// After creation, commands can be sent via exec_in_guest. The VM is stopped
@ -246,7 +276,7 @@ impl TestVm {
/// Instanciate a new crosvm instance. The first call will trigger the download of prebuilt /// Instanciate a new crosvm instance. The first call will trigger the download of prebuilt
/// files if necessary. /// files if necessary.
pub fn new(additional_arguments: &[&str], o_direct: bool) -> Result<TestVm> { pub fn new(cfg: Config) -> Result<TestVm> {
static PREP_ONCE: Once = Once::new(); static PREP_ONCE: Once = Once::new();
PREP_ONCE.call_once(TestVm::initialize_once); PREP_ONCE.call_once(TestVm::initialize_once);
@ -263,9 +293,9 @@ impl TestVm {
command.args(&["run", "--disable-sandbox"]); command.args(&["run", "--disable-sandbox"]);
TestVm::configure_serial_devices(&mut command, &from_guest_pipe, &to_guest_pipe); TestVm::configure_serial_devices(&mut command, &from_guest_pipe, &to_guest_pipe);
command.args(&["--socket", control_socket_path.to_str().unwrap()]); command.args(&["--socket", control_socket_path.to_str().unwrap()]);
command.args(additional_arguments); command.args(cfg.extra_args);
TestVm::configure_kernel(&mut command, o_direct); TestVm::configure_kernel(&mut command, cfg.o_direct);
// Set `Stdio::piped` so we can forward the outputs to stdout later. // Set `Stdio::piped` so we can forward the outputs to stdout later.
command.stdout(Stdio::piped()); command.stdout(Stdio::piped());