forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code motion: cli related feature names (bytecodealliance#104)
* rename host runtime tests to command tests * add a test for using wasi in a reactor * commit cargo.lock * reactor tests: fix wit deps * test-programs: reactor-tests can build on stable note that this fails because it exposes wasi-libc ctors calling import functions from inside cabi_realloc * test-programs: show that ctors fix in wit-bindgen fixes bug * ci: install wasm32 targets for stable as well as nightly * wit-bindgen: use 0.4.0 * ci: use wit-bindgen 0.4.0 * Co-habitate with wasi-common from wasmtime * adapter: code motion in cargo feature & artifact names to cli-command, cli-reactor there will shortly be a third type of reactor (non-cli, idk what to call it) --------- Co-authored-by: Trevor Elliott <telliott@fastly.com>
- Loading branch information
Showing
15 changed files
with
222 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use anyhow::Result; | ||
use host::{add_to_linker, WasiCtx}; | ||
use wasi_cap_std_sync::WasiCtxBuilder; | ||
use wasmtime::{ | ||
component::{Component, Linker}, | ||
Config, Engine, Store, | ||
}; | ||
test_programs_macros::reactor_tests!(); | ||
|
||
wasmtime::component::bindgen!({ | ||
path: "../test-programs/reactor-tests/wit", | ||
world: "test-reactor", | ||
async: true, | ||
}); | ||
|
||
async fn instantiate(path: &str) -> Result<(Store<WasiCtx>, TestReactor)> { | ||
println!("{}", path); | ||
|
||
let mut config = Config::new(); | ||
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); | ||
config.wasm_component_model(true); | ||
config.async_support(true); | ||
|
||
let engine = Engine::new(&config)?; | ||
let component = Component::from_file(&engine, &path)?; | ||
let mut linker = Linker::new(&engine); | ||
add_to_linker(&mut linker, |x| x)?; | ||
|
||
let mut store = Store::new(&engine, WasiCtxBuilder::new().build()); | ||
|
||
let (wasi, _instance) = TestReactor::instantiate_async(&mut store, &component, &linker).await?; | ||
Ok((store, wasi)) | ||
} | ||
|
||
async fn run_reactor_tests(mut store: Store<WasiCtx>, reactor: TestReactor) -> Result<()> { | ||
store | ||
.data_mut() | ||
.env | ||
.push(("GOOD_DOG".to_owned(), "gussie".to_owned())); | ||
|
||
let r = reactor | ||
.call_add_strings(&mut store, &["hello", "$GOOD_DOG"]) | ||
.await?; | ||
assert_eq!(r, 2); | ||
|
||
// Redefine the env, show that the adapter only fetches it once | ||
// even if the libc ctors copy it in multiple times: | ||
store.data_mut().env.clear(); | ||
store | ||
.data_mut() | ||
.env | ||
.push(("GOOD_DOG".to_owned(), "cody".to_owned())); | ||
// Cody is indeed good but this should be "hello again" "gussie" | ||
let r = reactor | ||
.call_add_strings(&mut store, &["hello again", "$GOOD_DOG"]) | ||
.await?; | ||
assert_eq!(r, 4); | ||
|
||
let contents = reactor.call_get_strings(&mut store).await?; | ||
assert_eq!(contents, &["hello", "gussie", "hello again", "gussie"]); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ quote = "1.0" | |
|
||
[build-dependencies] | ||
wit-component = "0.7.0" | ||
cargo_metadata = "0.15.3" | ||
heck = "0.4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "reactor-tests" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
crate-type=["cdylib"] | ||
|
||
[dependencies] | ||
wit-bindgen = "0.4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
wit_bindgen::generate!("test-reactor"); | ||
|
||
export_test_reactor!(T); | ||
|
||
struct T; | ||
|
||
static mut STATE: Vec<String> = Vec::new(); | ||
|
||
impl TestReactor for T { | ||
fn add_strings(ss: Vec<String>) -> u32 { | ||
for s in ss { | ||
match s.split_once("$") { | ||
Some((prefix, var)) if prefix.is_empty() => match std::env::var(var) { | ||
Ok(val) => unsafe { STATE.push(val) }, | ||
Err(_) => unsafe { STATE.push("undefined".to_owned()) }, | ||
}, | ||
_ => unsafe { STATE.push(s) }, | ||
} | ||
} | ||
unsafe { STATE.len() as u32 } | ||
} | ||
fn get_strings() -> Vec<String> { | ||
unsafe { STATE.clone() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../wit/deps/clocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../wit/deps/filesystem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../wit/deps/io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../wit/deps/poll |
1 change: 1 addition & 0 deletions
1
test-programs/reactor-tests/wit/deps/wasi/environment-preopens.wit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../../wit/environment-preopens.wit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../../wit/environment.wit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
default world test-reactor { | ||
|
||
import wasi-environment: wasi.environment | ||
|
||
export add-strings: func(s: list<string>) -> u32 | ||
export get-strings: func() -> list<string> | ||
} |