Skip to content

Commit

Permalink
fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Aug 25, 2023
1 parent 70055e6 commit dc81fa7
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 225 deletions.
255 changes: 108 additions & 147 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ anyhow = "1"
cap-std = "1.0.12"
heck = { version = "0.4" }
tokio = { version = "1.30.0", features = ["macros"] }
wasmtime = { version = "12.0.0", features = ["component-model"] }
wasmtime-wasi = "12.0.0"
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", features = ["component-model"] }
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime" }

[workspace.dependencies]
anyhow = "1"
Expand Down
Binary file modified lib/virtual_adapter.wasm
Binary file not shown.
23 changes: 9 additions & 14 deletions src/virt_deny.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,7 @@ pub(crate) fn deny_http_virt(module: &mut Module) -> Result<()> {
)?;
add_stub_exported_func(
module,
"wasi:http/types#incoming-request-path",
vec![ValType::I32],
vec![ValType::I32],
)?;
add_stub_exported_func(
module,
"wasi:http/types#incoming-request-query",
"wasi:http/types#incoming-request-path-with-query",
vec![ValType::I32],
vec![ValType::I32],
)?;
Expand Down Expand Up @@ -255,7 +249,13 @@ pub(crate) fn deny_http_virt(module: &mut Module) -> Result<()> {
add_stub_exported_func(
module,
"wasi:http/types#set-response-outparam",
vec![ValType::I32, ValType::I32, ValType::I32, ValType::I32],
vec![
ValType::I32,
ValType::I32,
ValType::I32,
ValType::I32,
ValType::I32,
],
vec![ValType::I32],
)?;
add_stub_exported_func(
Expand Down Expand Up @@ -368,12 +368,7 @@ pub(crate) fn deny_random_virt(module: &mut Module) -> Result<()> {
}

pub(crate) fn deny_exit_virt(module: &mut Module) -> Result<()> {
add_stub_exported_func(
module,
"wasi:cli-base/exit#exit",
vec![ValType::I32],
vec![],
)?;
add_stub_exported_func(module, "wasi:cli/exit#exit", vec![ValType::I32], vec![])?;
Ok(())
}

Expand Down
10 changes: 6 additions & 4 deletions src/virt_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,16 @@ pub(crate) fn create_env_virt<'a>(module: &'a mut Module, env: &VirtEnv) -> Resu
}

pub(crate) fn stub_env_virt(module: &mut Module) -> Result<()> {
stub_imported_func(module, "wasi:cli-base/environment", "get-arguments", true)?;
stub_imported_func(module, "wasi:cli-base/environment", "get-environment", true)?;
stub_imported_func(module, "wasi:cli/environment", "get-arguments", true)?;
stub_imported_func(module, "wasi:cli/environment", "get-environment", true)?;
stub_imported_func(module, "wasi:cli/environment", "initial-cwd", true)?;
Ok(())
}

pub(crate) fn strip_env_virt(module: &mut Module) -> Result<()> {
stub_env_virt(module)?;
remove_exported_func(module, "wasi:cli-base/environment#get-arguments")?;
remove_exported_func(module, "wasi:cli-base/environment#get-environment")?;
remove_exported_func(module, "wasi:cli/environment#get-arguments")?;
remove_exported_func(module, "wasi:cli/environment#get-environment")?;
remove_exported_func(module, "wasi:cli/environment#initial-cwd")?;
Ok(())
}
122 changes: 88 additions & 34 deletions src/virt_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,30 +248,55 @@ impl FsEntry {
Ok(())
}

pub fn visit_pre<'a, Visitor>(&'a self, base_path: &str, visit: &mut Visitor) -> Result<()>
pub fn visit_bfs<'a, Visitor>(&'a self, base_path: &str, visit: &mut Visitor) -> Result<()>
where
Visitor: FnMut(&FsEntry, &str, &str, usize) -> Result<()>,
{
visit(self, base_path, "", 0)?;
self.visit_pre_inner(visit, base_path)
visit(self, base_path, "", 1)?;
let mut children_of = vec![(base_path.to_string(), self)];
let mut next_children_of;
while children_of.len() > 0 {
next_children_of = Vec::new();
FsEntry::visit_bfs_level(children_of, visit, &mut next_children_of)?;
children_of = next_children_of;
}
Ok(())
}

fn visit_pre_inner<'a, Visitor>(&'a self, visit: &mut Visitor, base_path: &str) -> Result<()>
fn visit_bfs_level<'a, Visitor>(
children_of: Vec<(String, &'a FsEntry)>,
visit: &mut Visitor,
next_children_of: &mut Vec<(String, &'a FsEntry)>,
) -> Result<()>
where
Visitor: FnMut(&FsEntry, &str, &str, usize) -> Result<()>,
{
match self {
FsEntry::Dir(dir) => {
let len = dir.iter().len();
for (idx, (name, sub_entry)) in dir.iter().enumerate() {
visit(sub_entry, name, base_path, len - idx - 1)?;
// first we do a full len count at this depth to be able to predict the
// next depth offset position for children of this item from the current index
let mut child_offset = 0;
for (_, parent) in &children_of {
match parent {
FsEntry::Dir(dir) => {
child_offset += dir.iter().len();
}
for (name, sub_entry) in dir {
let path = format!("{base_path}/{name}");
sub_entry.visit_pre_inner(visit, &path)?;
_ => {}
}
}
for (base_path, parent) in children_of {
match parent {
FsEntry::Dir(dir) => {
for (name, sub_entry) in dir.iter() {
visit(sub_entry, name, &base_path, child_offset)?;
child_offset -= 1;
let path = format!("{base_path}/{name}");
next_children_of.push((path, sub_entry));
if let FsEntry::Dir(dir) = sub_entry {
child_offset += dir.iter().len();
}
}
}
_ => {}
}
_ => {}
}
Ok(())
}
Expand Down Expand Up @@ -386,7 +411,8 @@ pub(crate) fn create_io_virt<'a>(
if let Some(fs) = &fs {
for (name, entry) in &fs.preopens {
preopen_indices.push(static_fs_data.len() as u32);
entry.visit_pre(name, &mut |entry, name, _path, remaining_siblings| {
let mut cur_idx = 0;
entry.visit_bfs(name, &mut |entry, name, _path, child_offset| {
let name_str_ptr = data_section.string(name)?;
let (ty, data) = match &entry {
// removed during previous step
Expand All @@ -408,20 +434,12 @@ pub(crate) fn create_io_virt<'a>(
StaticFileData { host_path: str },
)
}
FsEntry::Dir(dir) => {
let child_cnt = dir.len() as u32;
// children will be visited next in preorder and contiguously
// therefore the child index in the static fs data is known
// to be the next index
let start_idx = static_fs_data.len() as u32 + 1;
let child_idx = start_idx + remaining_siblings as u32;
(
StaticIndexType::Dir,
StaticFileData {
dir: (child_idx, child_cnt),
},
)
}
FsEntry::Dir(dir) => (
StaticIndexType::Dir,
StaticFileData {
dir: (child_offset as u32, dir.len() as u32),
},
),
FsEntry::File(bytes) => {
let byte_len = bytes.len();
if byte_len > fs.passive_cutoff.unwrap_or(1024) as usize {
Expand All @@ -448,6 +466,7 @@ pub(crate) fn create_io_virt<'a>(
ty,
data,
});
cur_idx += 1;
Ok(())
})?;
}
Expand Down Expand Up @@ -636,9 +655,39 @@ pub(crate) fn stub_clocks_virt(module: &mut Module) -> Result<()> {
}

pub(crate) fn stub_stdio_virt(module: &mut Module) -> Result<()> {
stub_imported_func(module, "wasi:cli-base/stdin", "get-stdin", true)?;
stub_imported_func(module, "wasi:cli-base/stdout", "get-stdout", true)?;
stub_imported_func(module, "wasi:cli-base/stderr", "get-stderr", true)?;
stub_imported_func(module, "wasi:cli/stdin", "get-stdin", true)?;
stub_imported_func(module, "wasi:cli/stdout", "get-stdout", true)?;
stub_imported_func(module, "wasi:cli/stderr", "get-stderr", true)?;
stub_imported_func(
module,
"wasi:cli/terminal-stdin",
"get-terminal-stdin",
false,
)?;
stub_imported_func(
module,
"wasi:cli/terminal-stdout",
"get-terminal-stdout",
false,
)?;
stub_imported_func(
module,
"wasi:cli/terminal-stderr",
"get-terminal-stderr",
false,
)?;
stub_imported_func(
module,
"wasi:cli/terminal-input",
"drop-terminal-input",
false,
)?;
stub_imported_func(
module,
"wasi:cli/terminal-output",
"drop-terminal-output",
false,
)?;
Ok(())
}

Expand Down Expand Up @@ -876,9 +925,14 @@ pub(crate) fn stub_http_virt(module: &mut Module) -> Result<()> {

pub(crate) fn strip_stdio_virt(module: &mut Module) -> Result<()> {
stub_stdio_virt(module)?;
remove_exported_func(module, "wasi:cli-base/stdin#get-stdin")?;
remove_exported_func(module, "wasi:cli-base/stdout#get-stdout")?;
remove_exported_func(module, "wasi:cli-base/stderr#get-stderr")?;
remove_exported_func(module, "wasi:cli/stdin#get-stdin")?;
remove_exported_func(module, "wasi:cli/stdout#get-stdout")?;
remove_exported_func(module, "wasi:cli/stderr#get-stderr")?;
remove_exported_func(module, "wasi:cli/terminal-stdin#get-terminal-stdin")?;
remove_exported_func(module, "wasi:cli/terminal-stdout#get-terminal-stdout")?;
remove_exported_func(module, "wasi:cli/terminal-stderr#get-terminal-stderr")?;
remove_exported_func(module, "wasi:cli/terminal-input#drop-terminal-input")?;
remove_exported_func(module, "wasi:cli/terminal-output#drop-terminal-output")?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion tests/components/do-everything/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ wit_bindgen::generate!({

struct VirtTestComponent;

impl VirtTest for VirtTestComponent {
impl Guest for VirtTestComponent {
fn test_get_env() -> Vec<(String, String)> {
unreachable!();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/components/file-read/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ wit_bindgen::generate!({

struct VirtTestComponent;

impl VirtTest for VirtTestComponent {
impl Guest for VirtTestComponent {
fn test_get_env() -> Vec<(String, String)> {
Vec::new()
}
Expand Down
43 changes: 30 additions & 13 deletions tests/virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn virt_test() -> Result<()> {
let test_case_name = test_case_file_name.strip_suffix(".toml").unwrap();

// Filtering...
// if test_case_name == "encapsulate" {
// if test_case_name != "fs-nested-dir-read" {
// continue;
// }

Expand All @@ -89,13 +89,21 @@ async fn virt_test() -> Result<()> {
let generated_path = PathBuf::from("tests/generated");
fs::create_dir_all(&generated_path)?;

if DEBUG {
println!("- Building test component");
}

let mut generated_component_path = generated_path.join(component_name);
generated_component_path.set_extension("component.wasm");
cmd(&format!(
"cargo build -p {component_name} --target wasm32-wasi {}",
if DEBUG { "" } else { "--release" }
))?;

if DEBUG {
println!("- Encoding test component");
}

// encode the component
let component_core = fs::read(&format!(
"target/wasm32-wasi/{}/{}.wasm",
Expand All @@ -112,6 +120,10 @@ async fn virt_test() -> Result<()> {
)?;

// create the test case specific virtualization
if DEBUG {
println!("- Creating virtualization");
}

let mut virt_component_path = generated_path.join(test_case_name);
virt_component_path.set_extension("virt.wasm");
let mut virt_opts = test.virt_opts.clone().unwrap_or_default();
Expand All @@ -127,6 +139,9 @@ async fn virt_test() -> Result<()> {
fs::write(&virt_component_path, virt_component.adapter)?;

// compose the test component with the defined test virtualization
if DEBUG {
println!("- Composing virtualization");
}
let component_bytes = ComponentComposer::new(
&generated_component_path,
&wasm_compose::config::Config {
Expand All @@ -143,16 +158,19 @@ async fn virt_test() -> Result<()> {
}

// execute the composed virtualized component test function
if DEBUG {
println!("- Executing composition");
}
let mut builder = WasiCtxBuilder::new();
builder = builder.inherit_stdio().push_preopened_dir(
builder.inherit_stdio().preopened_dir(
Dir::open_ambient_dir(".", ambient_authority())?,
DirPerms::READ,
FilePerms::READ,
"/",
);
if let Some(host_env) = &test.host_env {
for (k, v) in host_env {
builder = builder.push_env(k, v);
builder.env(k, v);
}
}
let mut table = Table::new();
Expand Down Expand Up @@ -201,6 +219,10 @@ async fn virt_test() -> Result<()> {
let (instance, _instance) =
VirtTest::instantiate_async(&mut store, &component, &linker).await?;

if DEBUG {
println!("- Checking expectations");
}

// env var expectation check
if let Some(expect_env) = &test.expect.env {
let env_vars = instance.call_test_get_env(&mut store).await?;
Expand All @@ -225,18 +247,13 @@ async fn virt_test() -> Result<()> {
let file_read = instance
.call_test_file_read(&mut store, test.host_fs_path.as_ref().unwrap())
.await?;
if file_read.starts_with("ERR") {
eprintln!("> {}", file_read);
}
if !file_read.eq(expect_file_read) {
return Err(anyhow!(
"Unexpected file read result testing {:?}:
\x1b[1mExpected:\x1b[0m {:?}
\x1b[1mActual:\x1b[0m {:?}
{:?}",
test_case_path,
expect_file_read,
file_read,
test
"Unexpected file read result testing {:?}",
test_case_path
));
}
}
Expand Down
Loading

0 comments on commit dc81fa7

Please sign in to comment.