qcow_utils: Add tests

Add basic tests that qcow_utils compiles.

Change-Id: I433dc7cb55d42997ba060f9bd989ca3b5c8b0045
Reviewed-on: https://chromium-review.googlesource.com/895189
Commit-Ready: Dylan Reid <dgreid@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Dylan Reid 2018-01-30 19:28:30 -08:00 committed by chrome-bot
parent 7a2592a2cb
commit fbbcf7ad14
6 changed files with 120 additions and 0 deletions

10
Cargo.lock generated
View file

@ -392,9 +392,19 @@ dependencies = [
"getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
"qcow 0.1.0",
"qcow_utils_test 0.1.0",
"sys_util 0.1.0",
]
[[package]]
name = "qcow_utils_test"
version = "0.1.0"
dependencies = [
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
"qcow_utils 0.1.0",
"tempdir 0.3.7",
]
[[package]]
name = "quote"
version = "0.6.10"

View file

@ -16,3 +16,6 @@ getopts = "*"
libc = "*"
qcow = { path = "../qcow" }
sys_util = { path = "../sys_util" }
[dev-dependencies]
qcow_utils_test = { path = "tests/qcow_utils_test" }

View file

@ -0,0 +1,10 @@
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
extern crate qcow_utils_test;
#[test]
fn test_create() {
qcow_utils_test::run_c_test(include_str!("create.c"));
}

View file

@ -0,0 +1,9 @@
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "qcow_utils.h"
int main(int argc, char **argv) {
return create_qcow_with_size("/tmp/test.qcow2", 1024*1024*100);
}

View file

@ -0,0 +1,12 @@
[package]
name = "qcow_utils_test"
version = "0.1.0"
authors = ["The Chromium OS Authors"]
[lib]
path = "utils_test.rs"
[dependencies]
libc = "*"
qcow_utils = { path = "../.." }
tempdir = { path = "../../../tempdir" }

View file

@ -0,0 +1,76 @@
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
extern crate tempdir;
use tempdir::TempDir;
use std::env::{current_exe, var_os};
use std::ffi::OsString;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::thread::sleep;
use std::time::Duration;
fn get_crosvm_path() -> PathBuf {
let mut crosvm_path = current_exe()
.ok()
.map(|mut path| {
path.pop();
path
})
.expect("failed to get crosvm binary directory");
crosvm_path.push("crosvm");
crosvm_path
}
fn build_test(src: &str) -> TempDir {
let mut libqcow_utils = get_crosvm_path();
libqcow_utils.set_file_name("libqcow_utils.so");
let temp_dir = TempDir::new("qcow_util_test").expect("Failed to make temporary directory");
let out_bin_file = PathBuf::from(temp_dir.path()).join("target");
let mut child = Command::new(var_os("CC").unwrap_or(OsString::from("cc")))
.args(&["-Isrc", "-pthread", "-o"])
.arg(&out_bin_file)
.arg(libqcow_utils)
.args(&["-xc", "-"])
.stdin(Stdio::piped())
.spawn()
.expect("failed to spawn compiler");
{
let stdin = child.stdin.as_mut().expect("failed to open stdin");
stdin
.write_all(src.as_bytes())
.expect("failed to write source to stdin");
}
let status = child.wait().expect("failed to wait for compiler");
assert!(status.success(), "failed to build test");
temp_dir
}
fn run_test(bin_path: &Path) {
let mut child = Command::new(PathBuf::from(bin_path).join("target"))
.spawn()
.expect("failed to spawn test");
for _ in 0..12 {
match child.try_wait().expect("failed to wait for test") {
Some(status) => {
assert!(status.success(), "Test returned failure.");
return;
}
None => sleep(Duration::from_millis(100)),
}
}
child.kill().expect("failed to kill test");
panic!("test subprocess has timed out");
}
pub fn run_c_test(src: &str) {
let bin_path = build_test(src);
run_test(bin_path.path());
}