From 1acc0a28d9f65c74a7f8dac2ce8c8f6f58e65d88 Mon Sep 17 00:00:00 2001 From: Vikram Auradkar Date: Thu, 6 Oct 2022 20:17:00 +0000 Subject: [PATCH] prebuilts: move prebuilts tests under tests BUG=b:246987526 TEST=presubmit Change-Id: I86eed7c84f7e21eb7155695489ee9446f999dc1d Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3936733 Reviewed-by: Daniel Verkamp Commit-Queue: Vikram Auradkar --- Cargo.lock | 3 +- Cargo.toml | 1 + integration_tests/Cargo.toml | 1 + integration_tests/tests/prebuilts.rs | 63 ++++++++++++++++++++++++++++ prebuilts/Cargo.toml | 3 -- prebuilts/src/lib.rs | 63 ---------------------------- tools/impl/test_config.py | 1 - 7 files changed, 67 insertions(+), 68 deletions(-) create mode 100644 integration_tests/tests/prebuilts.rs diff --git a/Cargo.lock b/Cargo.lock index 6e97076ad1..72ee70505c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -456,6 +456,7 @@ dependencies = [ "net_util", "once_cell", "p9", + "prebuilts", "protobuf", "protos", "rand", @@ -982,6 +983,7 @@ dependencies = [ "base", "cfg-if", "libc", + "prebuilts", "tempfile", ] @@ -1399,7 +1401,6 @@ version = "0.1.0" dependencies = [ "anyhow", "cfg-if", - "tempfile", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 36f341426d..a7f590fd9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -259,6 +259,7 @@ cc = "*" [dev-dependencies] base = "*" +prebuilts = { path = "prebuilts" } [patch.crates-io] assertions = { path = "common/assertions" } diff --git a/integration_tests/Cargo.toml b/integration_tests/Cargo.toml index 805274149d..8019136424 100644 --- a/integration_tests/Cargo.toml +++ b/integration_tests/Cargo.toml @@ -10,6 +10,7 @@ arch = { path = "../arch" } base = "*" cfg-if = "*" libc = "0.2.65" +prebuilts = { path = "../prebuilts" } tempfile = "3" [features] diff --git a/integration_tests/tests/prebuilts.rs b/integration_tests/tests/prebuilts.rs new file mode 100644 index 0000000000..4b1d60c720 --- /dev/null +++ b/integration_tests/tests/prebuilts.rs @@ -0,0 +1,63 @@ +// 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. + +use std::env; + +use tempfile::TempDir; + +use prebuilts::download_prebuilt; +use prebuilts::download_prebuilts; + +static LIBRARY: &str = "prebuilts_test"; +static PREBUILT_FILE1: &str = "prebuilt_test"; +static PREBUILT_FILE2: &str = "prebuilt_test2"; +static VERSION: u32 = 1; + +fn setup_env(build_type: &str) -> TempDir { + let tempdir = tempfile::tempdir().unwrap(); + if build_type == "debug" { + env::set_var("DEBUG", ""); + } else { + env::remove_var("DEBUG"); + } + env::set_var("CARGO_CFG_TARGET_FAMILY", "windows"); + env::set_var("CARGO_CFG_TARGET_ARCH", "x86_64"); + env::set_var("CARGO_CFG_TARGET_ENV", "gnu"); + let deps = tempdir.path().join("deps"); + std::fs::create_dir_all(&deps).unwrap(); + let out_dir = tempdir.path().join("build").join("crate_name").join("out"); + std::fs::create_dir_all(&out_dir).unwrap(); + env::set_var("OUT_DIR", out_dir.as_os_str().to_str().unwrap()); + tempdir +} + +#[test] +fn test_download_prebuilt() { + for build_type in ["release", "debug"] { + let _tempdir = setup_env(build_type); + let file = download_prebuilt(LIBRARY, VERSION, PREBUILT_FILE1).unwrap(); + assert!(file.exists()); + assert_eq!( + std::fs::read_to_string(&file).unwrap(), + format!("hello world {}\n", build_type) + ); + } +} + +#[test] +fn test_download_prebuilt_files() { + for build_type in ["release", "debug"] { + let _tempdir = setup_env(build_type); + let files = + download_prebuilts(LIBRARY, VERSION, &[PREBUILT_FILE1, PREBUILT_FILE2]).unwrap(); + for file in files { + assert!(file.exists()); + assert_eq!( + std::fs::read_to_string(&file).unwrap(), + format!("hello world {}\n", build_type), + "failed for file {file:?}" + ); + } + } +} diff --git a/prebuilts/Cargo.toml b/prebuilts/Cargo.toml index 80d8049ce6..7c364b20bb 100644 --- a/prebuilts/Cargo.toml +++ b/prebuilts/Cargo.toml @@ -7,6 +7,3 @@ edition = "2021" [dependencies] anyhow = "*" cfg-if = "*" - -[dev-dependencies] -tempfile = "*" diff --git a/prebuilts/src/lib.rs b/prebuilts/src/lib.rs index b789c49ff0..c338317470 100644 --- a/prebuilts/src/lib.rs +++ b/prebuilts/src/lib.rs @@ -91,66 +91,3 @@ pub fn download_prebuilts(library: &str, version: u32, filenames: &[&str]) -> Re } Ok(paths) } - -#[cfg(test)] -mod tests { - use std::env; - - use tempfile::TempDir; - - use super::download_prebuilt; - use super::download_prebuilts; - - static LIBRARY: &str = "prebuilts_test"; - static PREBUILT_FILE1: &str = "prebuilt_test"; - static PREBUILT_FILE2: &str = "prebuilt_test2"; - static VERSION: u32 = 1; - - fn setup_env(build_type: &str) -> TempDir { - let tempdir = tempfile::tempdir().unwrap(); - if build_type == "debug" { - env::set_var("DEBUG", ""); - } else { - env::remove_var("DEBUG"); - } - env::set_var("CARGO_CFG_TARGET_FAMILY", "windows"); - env::set_var("CARGO_CFG_TARGET_ARCH", "x86_64"); - env::set_var("CARGO_CFG_TARGET_ENV", "gnu"); - let deps = tempdir.path().join("deps"); - std::fs::create_dir_all(&deps).unwrap(); - let out_dir = tempdir.path().join("build").join("crate_name").join("out"); - std::fs::create_dir_all(&out_dir).unwrap(); - env::set_var("OUT_DIR", out_dir.as_os_str().to_str().unwrap()); - tempdir - } - - #[test] - fn test_download_prebuilt() { - for build_type in ["release", "debug"] { - let _tempdir = setup_env(build_type); - let file = download_prebuilt(LIBRARY, VERSION, PREBUILT_FILE1).unwrap(); - assert!(file.exists()); - assert_eq!( - std::fs::read_to_string(&file).unwrap(), - format!("hello world {}\n", build_type) - ); - } - } - - #[test] - fn test_download_prebuilt_files() { - for build_type in ["release", "debug"] { - let _tempdir = setup_env(build_type); - let files = - download_prebuilts(LIBRARY, VERSION, &[PREBUILT_FILE1, PREBUILT_FILE2]).unwrap(); - for file in files { - assert!(file.exists()); - assert_eq!( - std::fs::read_to_string(&file).unwrap(), - format!("hello world {}\n", build_type), - "failed for file {file:?}" - ); - } - } - } -} diff --git a/tools/impl/test_config.py b/tools/impl/test_config.py index de4ea0f811..e32e3648da 100755 --- a/tools/impl/test_config.py +++ b/tools/impl/test_config.py @@ -129,7 +129,6 @@ CRATE_OPTIONS: Dict[str, List[TestOption]] = { TestOption.DO_NOT_RUN_ARMHF, ], "libvda": [TestOption.DO_NOT_BUILD], # b/202293971 - "prebuilts": [TestOption.SINGLE_THREADED], # tests set and clear process wide env variable "rutabaga_gfx": [TestOption.DO_NOT_BUILD_ARMHF], # b/210015864 "vhost": [TestOption.DO_NOT_RUN_ON_FOREIGN_KERNEL, TestOption.UNIT_AS_INTEGRATION_TEST], "vm_control": [TestOption.DO_NOT_BUILD_ARMHF], # b/210015864