summary refs log tree commit diff
path: root/qcow_utils
diff options
context:
space:
mode:
authorDylan Reid <dgreid@chromium.org>2018-01-30 19:28:30 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-03-16 18:05:03 -0700
commitfbbcf7ad1457b5daff1be151793441006f186683 (patch)
tree3e0f617489fea320cb9054f363cfe084209f6cc2 /qcow_utils
parent7a2592a2cb70d74cc9e71f3dd2329e6379c9e6c1 (diff)
downloadcrosvm-fbbcf7ad1457b5daff1be151793441006f186683.tar
crosvm-fbbcf7ad1457b5daff1be151793441006f186683.tar.gz
crosvm-fbbcf7ad1457b5daff1be151793441006f186683.tar.bz2
crosvm-fbbcf7ad1457b5daff1be151793441006f186683.tar.lz
crosvm-fbbcf7ad1457b5daff1be151793441006f186683.tar.xz
crosvm-fbbcf7ad1457b5daff1be151793441006f186683.tar.zst
crosvm-fbbcf7ad1457b5daff1be151793441006f186683.zip
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>
Diffstat (limited to 'qcow_utils')
-rw-r--r--qcow_utils/Cargo.toml3
-rw-r--r--qcow_utils/tests/client.rs10
-rw-r--r--qcow_utils/tests/create.c9
-rw-r--r--qcow_utils/tests/qcow_utils_test/Cargo.toml12
-rw-r--r--qcow_utils/tests/qcow_utils_test/utils_test.rs76
5 files changed, 110 insertions, 0 deletions
diff --git a/qcow_utils/Cargo.toml b/qcow_utils/Cargo.toml
index ca7e860..8c262b2 100644
--- a/qcow_utils/Cargo.toml
+++ b/qcow_utils/Cargo.toml
@@ -16,3 +16,6 @@ getopts = "*"
 libc = "*"
 qcow = { path = "../qcow" }
 sys_util = { path = "../sys_util" }
+
+[dev-dependencies]
+qcow_utils_test = { path = "tests/qcow_utils_test" }
diff --git a/qcow_utils/tests/client.rs b/qcow_utils/tests/client.rs
new file mode 100644
index 0000000..5d15210
--- /dev/null
+++ b/qcow_utils/tests/client.rs
@@ -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"));
+}
diff --git a/qcow_utils/tests/create.c b/qcow_utils/tests/create.c
new file mode 100644
index 0000000..77b5957
--- /dev/null
+++ b/qcow_utils/tests/create.c
@@ -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);
+}
diff --git a/qcow_utils/tests/qcow_utils_test/Cargo.toml b/qcow_utils/tests/qcow_utils_test/Cargo.toml
new file mode 100644
index 0000000..402a047
--- /dev/null
+++ b/qcow_utils/tests/qcow_utils_test/Cargo.toml
@@ -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" }
diff --git a/qcow_utils/tests/qcow_utils_test/utils_test.rs b/qcow_utils/tests/qcow_utils_test/utils_test.rs
new file mode 100644
index 0000000..564ea8a
--- /dev/null
+++ b/qcow_utils/tests/qcow_utils_test/utils_test.rs
@@ -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());
+}