e2e_tests: Add vhost-user block with devices command

BUG=b:243127910
TEST=./e2e_tests/run

Change-Id: I22dfc4e1d75ff0b310a5f624568b390d2dba6c1e
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4054811
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
This commit is contained in:
Keiichi Watanabe 2022-11-24 17:53:25 +09:00 committed by crosvm LUCI
parent 6adf8e8a15
commit e060d6c639
2 changed files with 68 additions and 20 deletions

View file

@ -7,9 +7,11 @@
pub mod fixture;
use std::env;
use std::path::Path;
use std::process::Command;
use std::time;
use fixture::vhost_user::CmdType;
use fixture::vhost_user::Config as VuConfig;
use fixture::vhost_user::VhostUserBackend;
use fixture::vm::Config as VmConfig;
@ -37,6 +39,7 @@ fn prepare_disk_img() -> NamedTempFile {
disk
}
/// Tests virtio-blk device is mountable.
// TODO(b/243127498): Add tests for write and sync operations.
#[test]
fn mount_block() {
@ -55,6 +58,7 @@ fn mount_block() {
);
}
/// Tests `crosvm disk resize` works.
#[test]
fn resize() {
let disk = prepare_disk_img();
@ -112,23 +116,36 @@ fn resize() {
);
}
#[test]
fn vhost_user_mount() {
let disk = prepare_disk_img();
let disk_path = disk.path().to_str().unwrap().to_string();
let socket = NamedTempFile::new().unwrap();
let socket_path = socket.path().to_str().unwrap().to_string();
fn create_vu_config(cmd_type: CmdType, socket: &Path, disk: &Path) -> VuConfig {
let socket_path = socket.to_str().unwrap();
let disk_path = disk.to_str().unwrap();
println!("disk={disk_path}, socket={socket_path}");
match cmd_type {
CmdType::Device => VuConfig::new(cmd_type, "block").extra_args(vec![
"block".to_string(),
"--socket".to_string(),
socket_path.to_string(),
"--file".to_string(),
disk_path.to_string(),
]),
CmdType::Devices => VuConfig::new(cmd_type, "block").extra_args(vec![
"--block".to_string(),
format!("vhost={},path={}", socket_path, disk_path),
]),
}
}
let vu_config = VuConfig::new("block").extra_args(vec![
"--socket".to_string(),
socket_path.clone(),
"--file".to_string(),
disk_path,
]);
fn run_vhost_user_test(cmd_type: CmdType) {
let socket = NamedTempFile::new().unwrap();
let disk = prepare_disk_img();
let vu_config = create_vu_config(cmd_type, socket.path(), disk.path());
let _vu_device = VhostUserBackend::new(vu_config).unwrap();
let config = VmConfig::new().extra_args(vec!["--vhost-user-blk".to_string(), socket_path]);
let config = VmConfig::new().extra_args(vec![
"--vhost-user-blk".to_string(),
socket.path().to_str().unwrap().to_string(),
]);
let mut vm = TestVm::new(config).unwrap();
assert_eq!(
vm.exec_in_guest("mount -t ext4 /dev/vdb /mnt && echo 42")
@ -137,3 +154,15 @@ fn vhost_user_mount() {
"42"
);
}
/// Tests vhost-user block device with `crosvm device`.
#[test]
fn vhost_user_mount() {
run_vhost_user_test(CmdType::Device);
}
/// Tests vhost-user block device with `crosvm devices` (not `device`).
#[test]
fn vhost_user_mount_with_devices() {
run_vhost_user_test(CmdType::Devices);
}

View file

@ -12,22 +12,41 @@ use anyhow::Result;
use crate::fixture::utils::find_crosvm_binary;
#[derive(Default)]
pub struct Config {
dev_name: String,
pub enum CmdType {
/// `crosvm device` command
Device,
/// `crosvm devices` command that is newer and supports sandboxing and multiple device
/// processes.
Devices,
}
impl CmdType {
fn to_subcommand(&self) -> &str {
match self {
// `crosvm device`
CmdType::Device => "device",
// `crosvm devices`
CmdType::Devices => "devices",
}
}
}
pub struct Config {
cmd_type: CmdType,
dev_name: String,
extra_args: Vec<String>,
}
impl Config {
pub fn new(name: &str) -> Self {
pub fn new(cmd_type: CmdType, name: &str) -> Self {
Config {
cmd_type,
dev_name: name.to_string(),
..Default::default()
extra_args: Default::default(),
}
}
/// Uses extra arguments for `crosvm devices $dev_name`.
/// Uses extra arguments for `crosvm (device|devices)`.
pub fn extra_args(mut self, args: Vec<String>) -> Self {
self.extra_args = args;
self
@ -43,7 +62,7 @@ pub struct VhostUserBackend {
impl VhostUserBackend {
pub fn new(cfg: Config) -> Result<Self> {
let mut cmd = Command::new(find_crosvm_binary());
cmd.args(&["device", &cfg.dev_name]);
cmd.args(&[cfg.cmd_type.to_subcommand()]);
cmd.args(cfg.extra_args);
cmd.stdout(Stdio::piped());