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

Commit

Permalink
WIP: Add wasmldr integration test(s)
Browse files Browse the repository at this point in the history
* 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 <will@profian.com>
  • Loading branch information
wgwoods committed Oct 4, 2021
1 parent 807a54f commit bb26350
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 35 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ vdso = "0.1"

[build-dependencies]
cc = "1.0"
wat = "1.0"
walkdir = "2"
protobuf-codegen-pure = "2.25"
sallyport = { git = "https://github.com/enarx/sallyport", rev = "a567a22665c7e5ba88a8c4acd64ab43ee32b4681", features = [ "asm" ] }
Expand Down
14 changes: 14 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ fn build_cc_tests(in_path: &Path, out_path: &Path) {
}
}

use wat;

fn build_wasm_tests(in_path: &Path, out_path: &Path) {
for wat in find_files_with_extensions(&["wat"], &in_path) {
let wasm = out_path
.join(wat.file_stem().unwrap())
.with_extension("wasm");
let bin = wat::parse_file(&wat).unwrap_or_else(|_| panic!("failed to compile {:?}", &wat));
std::fs::write(&wasm, &bin).unwrap_or_else(|_| panic!("failed to write {:?}", &wasm));
println!("cargo:rerun-if-changed={}", &wat.display());
}
}

// Build a binary named `bin_name` from the crate located at `in_dir`,
// targeting `target_name`, then strip the resulting binary and place it
// at `out_dir`/bin/`bin_name`.
Expand Down Expand Up @@ -235,6 +248,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

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

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

Expand Down
26 changes: 0 additions & 26 deletions internal/wasmldr/build.rs

This file was deleted.

8 changes: 0 additions & 8 deletions internal/wasmldr/fixtures/bundle/config.yaml

This file was deleted.

1 change: 0 additions & 1 deletion internal/wasmldr/fixtures/bundle/stdin.txt

This file was deleted.

File renamed without changes.
File renamed without changes.
77 changes: 77 additions & 0 deletions tests/wasmldr_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: Apache-2.0

use std::fs::File;
use std::path::Path;
use std::process::{Command, Stdio};
//use std::os::unix::process::CommandExt;
use process_control::{ChildExt, Output, Timeout};
use std::io;
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
use std::time::Duration;

extern crate libc;
use libc::c_int;

mod common;
use common::{CRATE, KEEP_BIN, OUT_DIR, TEST_BINS_OUT, TIMEOUT_SECS};

use serial_test::serial;
//use anyhow::Result;

const MODULE_FD: RawFd = 3;

// wrap a libc call to return io::Result
fn cvt(rv: c_int) -> io::Result<c_int> {
if rv == -1 {
Err(io::Error::last_os_error())
} else {
Ok(rv)
}
}

fn open_module(path: &Path) -> io::Result<File> {
let mut file = File::open(path)?;
let fd = file.into_raw_fd();
unsafe { file = File::from_raw_fd(cvt(libc::dup2(fd, MODULE_FD))?) }
Ok(file)
}

fn run_test(wasm: &str) -> Output {
let path = Path::new(CRATE)
.join(OUT_DIR)
.join(TEST_BINS_OUT)
.join(wasm);
let _fd3 = open_module(&path).unwrap();
let child = Command::new(KEEP_BIN)
.arg("exec")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap_or_else(|e| panic!("failed to run `{:?}`: {:#?}", wasm, e));
let output = child
.with_output_timeout(Duration::from_secs(TIMEOUT_SECS))
.terminating()
.wait()
.unwrap_or_else(|e| panic!("failed to run `{}`: {:#?}", wasm, e))
.unwrap_or_else(|| panic!("process `{}` timed out", wasm));

let _exit_status = output.status.code().unwrap_or_else(|| {
panic!(
"process `{}` terminated by signal {:?}",
wasm,
output.status.signal()
)
});

// TODO: compare output.stdout, output.stderr, etc.

output
}

#[test]
#[serial]
fn hello_wasi_snapshot1() {
let out = run_test("hello_wasi_snapshot1.wasm");
assert_eq!(out.stdout, &b"Hello, world!\n"[..]);
}

0 comments on commit bb26350

Please sign in to comment.