From 7eadfac1223d6ccf8818d16556822e600775f26d Mon Sep 17 00:00:00 2001 From: Dennis Kempin Date: Thu, 3 Nov 2022 17:13:50 -0700 Subject: [PATCH] vhost: Move tests into an integration test The tests require access to vhost syscalls which are not available in all environments and are not suitable for unit tests. BUG=b:244623767 TEST=presubmit Change-Id: I2b6320d005da8966adab3f016237fedf1361055a Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4004981 Auto-Submit: Dennis Kempin Reviewed-by: Keiichi Watanabe Commit-Queue: Keiichi Watanabe --- tools/impl/test_config.py | 1 - vhost/src/lib.rs | 115 ------------------------------------ vhost/tests/unix.rs | 119 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 116 deletions(-) create mode 100644 vhost/tests/unix.rs diff --git a/tools/impl/test_config.py b/tools/impl/test_config.py index b65a1772f9..adcfa089b6 100755 --- a/tools/impl/test_config.py +++ b/tools/impl/test_config.py @@ -99,7 +99,6 @@ CRATE_OPTIONS: Dict[str, List[TestOption]] = { ], # b/181674144 "libvda": [TestOption.DO_NOT_RUN], # b/202293971 "sandbox": [TestOption.DO_NOT_RUN], - "vhost": [TestOption.UNIT_AS_INTEGRATION_TEST], } for name in WIN64_DISABLED_CRATES: diff --git a/vhost/src/lib.rs b/vhost/src/lib.rs index 22ca44cc2e..3fe6000452 100644 --- a/vhost/src/lib.rs +++ b/vhost/src/lib.rs @@ -394,118 +394,3 @@ pub trait Vhost: AsRawDescriptor + std::marker::Sized { Ok(()) } } - -// TODO(225193541): Enable/add tests for windows. -#[cfg(unix)] -#[cfg(test)] -mod tests { - use std::path::PathBuf; - use std::result; - - use net_util::sys::unix::fakes::FakeTap; - use vm_memory::GuestAddress; - use vm_memory::GuestMemory; - use vm_memory::GuestMemoryError; - - use super::*; - use crate::net::fakes::FakeNet; - - fn create_guest_memory() -> result::Result { - let start_addr1 = GuestAddress(0x0); - let start_addr2 = GuestAddress(0x1000); - GuestMemory::new(&[(start_addr1, 0x1000), (start_addr2, 0x4000)]) - } - - fn assert_ok_or_known_failure(res: Result) { - match &res { - // FakeNet won't respond to ioctl's - Ok(_t) => {} - Err(Error::IoctlError(ioe)) if ioe.raw_os_error().unwrap() == 25 => {} - Err(e) => panic!("Unexpected Error:\n{}", e), - } - } - - fn create_fake_vhost_net() -> FakeNet { - FakeNet::::new(&PathBuf::from("")).unwrap() - } - - #[test] - fn test_create_fake_vhost_net() { - create_fake_vhost_net(); - } - - #[test] - fn set_owner() { - let vhost_net = create_fake_vhost_net(); - let res = vhost_net.set_owner(); - assert_ok_or_known_failure(res); - } - - #[test] - fn get_features() { - let vhost_net = create_fake_vhost_net(); - let res = vhost_net.get_features(); - assert_ok_or_known_failure(res); - } - - #[test] - fn set_features() { - let vhost_net = create_fake_vhost_net(); - let res = vhost_net.set_features(0); - assert_ok_or_known_failure(res); - } - - #[test] - fn set_mem_table() { - let vhost_net = create_fake_vhost_net(); - let gm = create_guest_memory().unwrap(); - let res = vhost_net.set_mem_table(&gm); - assert_ok_or_known_failure(res); - } - - #[test] - fn set_vring_num() { - let vhost_net = create_fake_vhost_net(); - let res = vhost_net.set_vring_num(0, 1); - assert_ok_or_known_failure(res); - } - - #[test] - fn set_vring_addr() { - let vhost_net = create_fake_vhost_net(); - let gm = create_guest_memory().unwrap(); - let res = vhost_net.set_vring_addr( - &gm, - 1, - 1, - 0, - 0x0, - GuestAddress(0x0), - GuestAddress(0x0), - GuestAddress(0x0), - None, - ); - assert_ok_or_known_failure(res); - } - - #[test] - fn set_vring_base() { - let vhost_net = create_fake_vhost_net(); - let res = vhost_net.set_vring_base(0, 1); - assert_ok_or_known_failure(res); - } - - #[test] - fn set_vring_call() { - let vhost_net = create_fake_vhost_net(); - let res = vhost_net.set_vring_call(0, &Event::new().unwrap()); - assert_ok_or_known_failure(res); - } - - #[test] - fn set_vring_kick() { - let vhost_net = create_fake_vhost_net(); - let res = vhost_net.set_vring_kick(0, &Event::new().unwrap()); - assert_ok_or_known_failure(res); - } -} diff --git a/vhost/tests/unix.rs b/vhost/tests/unix.rs new file mode 100644 index 0000000000..dd36052624 --- /dev/null +++ b/vhost/tests/unix.rs @@ -0,0 +1,119 @@ +// Copyright 2022 The ChromiumOS Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// TODO(225193541): Enable/add tests for windows. +#![cfg(unix)] + +use std::path::PathBuf; +use std::result; + +use base::Event; +use net_util::sys::unix::fakes::FakeTap; +use vhost::net::fakes::FakeNet; +use vhost::net::NetT; +use vhost::Error; +use vhost::Result; +use vhost::Vhost; +use vm_memory::GuestAddress; +use vm_memory::GuestMemory; +use vm_memory::GuestMemoryError; + +fn create_guest_memory() -> result::Result { + let start_addr1 = GuestAddress(0x0); + let start_addr2 = GuestAddress(0x1000); + GuestMemory::new(&[(start_addr1, 0x1000), (start_addr2, 0x4000)]) +} + +fn assert_ok_or_known_failure(res: Result) { + match &res { + // FakeNet won't respond to ioctl's + Ok(_t) => {} + Err(Error::IoctlError(ioe)) if ioe.raw_os_error().unwrap() == 25 => {} + Err(e) => panic!("Unexpected Error:\n{}", e), + } +} + +fn create_fake_vhost_net() -> FakeNet { + FakeNet::::new(&PathBuf::from("")).unwrap() +} + +#[test] +fn test_create_fake_vhost_net() { + create_fake_vhost_net(); +} + +#[test] +fn set_owner() { + let vhost_net = create_fake_vhost_net(); + let res = vhost_net.set_owner(); + assert_ok_or_known_failure(res); +} + +#[test] +fn get_features() { + let vhost_net = create_fake_vhost_net(); + let res = vhost_net.get_features(); + assert_ok_or_known_failure(res); +} + +#[test] +fn set_features() { + let vhost_net = create_fake_vhost_net(); + let res = vhost_net.set_features(0); + assert_ok_or_known_failure(res); +} + +#[test] +fn set_mem_table() { + let vhost_net = create_fake_vhost_net(); + let gm = create_guest_memory().unwrap(); + let res = vhost_net.set_mem_table(&gm); + assert_ok_or_known_failure(res); +} + +#[test] +fn set_vring_num() { + let vhost_net = create_fake_vhost_net(); + let res = vhost_net.set_vring_num(0, 1); + assert_ok_or_known_failure(res); +} + +#[test] +fn set_vring_addr() { + let vhost_net = create_fake_vhost_net(); + let gm = create_guest_memory().unwrap(); + let res = vhost_net.set_vring_addr( + &gm, + 1, + 1, + 0, + 0x0, + GuestAddress(0x0), + GuestAddress(0x0), + GuestAddress(0x0), + None, + ); + assert_ok_or_known_failure(res); +} + +#[test] +fn set_vring_base() { + let vhost_net = create_fake_vhost_net(); + let res = vhost_net.set_vring_base(0, 1); + assert_ok_or_known_failure(res); +} + +#[test] +fn set_vring_call() { + let vhost_net = create_fake_vhost_net(); + let res = vhost_net.set_vring_call(0, &Event::new().unwrap()); + assert_ok_or_known_failure(res); +} + +#[test] +fn set_vring_kick() { + let vhost_net = create_fake_vhost_net(); + let res = vhost_net.set_vring_kick(0, &Event::new().unwrap()); + assert_ok_or_known_failure(res); +}