Skip to content
This repository was archived by the owner on Oct 26, 2021. It is now read-only.

Commit bb26350

Browse files
committed
WIP: Add wasmldr integration test(s)
* Remove internal/wasmldr's build.rs * Move wasmldr test sources to tests/wasm/*.wat * Build tests/wasm/*.wat to OUT_DIR/bin/*.wasm in build.rs * Add tests/wasmldr_tests.rs, which tries to run .wasm test binaries TODO: gotta fix how we pass the module into the keep, because the test doesn't work but this works OK: cargo run -- exec 3< $(find target -name hello_wasi_snapshot1.wasm | tail -n1) Signed-off-by: Will Woods <[email protected]>
1 parent 807a54f commit bb26350

File tree

11 files changed

+117
-35
lines changed

11 files changed

+117
-35
lines changed

Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ vdso = "0.1"
5555

5656
[build-dependencies]
5757
cc = "1.0"
58+
wat = "1.0"
5859
walkdir = "2"
5960
protobuf-codegen-pure = "2.25"
6061
sallyport = { git = "https://github.com/enarx/sallyport", rev = "a567a22665c7e5ba88a8c4acd64ab43ee32b4681", features = [ "asm" ] }

build.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ fn build_cc_tests(in_path: &Path, out_path: &Path) {
107107
}
108108
}
109109

110+
use wat;
111+
112+
fn build_wasm_tests(in_path: &Path, out_path: &Path) {
113+
for wat in find_files_with_extensions(&["wat"], &in_path) {
114+
let wasm = out_path
115+
.join(wat.file_stem().unwrap())
116+
.with_extension("wasm");
117+
let bin = wat::parse_file(&wat).unwrap_or_else(|_| panic!("failed to compile {:?}", &wat));
118+
std::fs::write(&wasm, &bin).unwrap_or_else(|_| panic!("failed to write {:?}", &wasm));
119+
println!("cargo:rerun-if-changed={}", &wat.display());
120+
}
121+
}
122+
110123
// Build a binary named `bin_name` from the crate located at `in_dir`,
111124
// targeting `target_name`, then strip the resulting binary and place it
112125
// at `out_dir`/bin/`bin_name`.
@@ -235,6 +248,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
235248

236249
build_cc_tests(&Path::new(CRATE).join(TEST_BINS_IN), &out_dir_bin);
237250
build_rs_tests(&Path::new(CRATE).join(TEST_BINS_IN), &out_dir_bin);
251+
build_wasm_tests(&Path::new(CRATE).join("tests/wasm"), &out_dir_bin);
238252

239253
let target = "x86_64-unknown-linux-musl";
240254

internal/wasmldr/build.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

internal/wasmldr/fixtures/bundle/config.yaml

Lines changed: 0 additions & 8 deletions
This file was deleted.

internal/wasmldr/fixtures/bundle/stdin.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
File renamed without changes.
File renamed without changes.

tests/wasmldr_tests.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
use std::fs::File;
4+
use std::path::Path;
5+
use std::process::{Command, Stdio};
6+
//use std::os::unix::process::CommandExt;
7+
use process_control::{ChildExt, Output, Timeout};
8+
use std::io;
9+
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
10+
use std::time::Duration;
11+
12+
extern crate libc;
13+
use libc::c_int;
14+
15+
mod common;
16+
use common::{CRATE, KEEP_BIN, OUT_DIR, TEST_BINS_OUT, TIMEOUT_SECS};
17+
18+
use serial_test::serial;
19+
//use anyhow::Result;
20+
21+
const MODULE_FD: RawFd = 3;
22+
23+
// wrap a libc call to return io::Result
24+
fn cvt(rv: c_int) -> io::Result<c_int> {
25+
if rv == -1 {
26+
Err(io::Error::last_os_error())
27+
} else {
28+
Ok(rv)
29+
}
30+
}
31+
32+
fn open_module(path: &Path) -> io::Result<File> {
33+
let mut file = File::open(path)?;
34+
let fd = file.into_raw_fd();
35+
unsafe { file = File::from_raw_fd(cvt(libc::dup2(fd, MODULE_FD))?) }
36+
Ok(file)
37+
}
38+
39+
fn run_test(wasm: &str) -> Output {
40+
let path = Path::new(CRATE)
41+
.join(OUT_DIR)
42+
.join(TEST_BINS_OUT)
43+
.join(wasm);
44+
let _fd3 = open_module(&path).unwrap();
45+
let child = Command::new(KEEP_BIN)
46+
.arg("exec")
47+
.stdin(Stdio::piped())
48+
.stdout(Stdio::piped())
49+
.stderr(Stdio::piped())
50+
.spawn()
51+
.unwrap_or_else(|e| panic!("failed to run `{:?}`: {:#?}", wasm, e));
52+
let output = child
53+
.with_output_timeout(Duration::from_secs(TIMEOUT_SECS))
54+
.terminating()
55+
.wait()
56+
.unwrap_or_else(|e| panic!("failed to run `{}`: {:#?}", wasm, e))
57+
.unwrap_or_else(|| panic!("process `{}` timed out", wasm));
58+
59+
let _exit_status = output.status.code().unwrap_or_else(|| {
60+
panic!(
61+
"process `{}` terminated by signal {:?}",
62+
wasm,
63+
output.status.signal()
64+
)
65+
});
66+
67+
// TODO: compare output.stdout, output.stderr, etc.
68+
69+
output
70+
}
71+
72+
#[test]
73+
#[serial]
74+
fn hello_wasi_snapshot1() {
75+
let out = run_test("hello_wasi_snapshot1.wasm");
76+
assert_eq!(out.stdout, &b"Hello, world!\n"[..]);
77+
}

0 commit comments

Comments
 (0)