diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bfde341..547bb05 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,10 +21,5 @@ jobs: run: cargo fmt --all -- --check - name: cargo clippy run: cargo clippy --all-targets --all-features -- -D warnings - - name: Install wasmtime - uses: bytecodealliance/actions/wasmtime/setup@v1 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - version: "v21.0.1" - name: cargo test run: cargo test diff --git a/waki/tests/client.rs b/waki/tests/client.rs index 92ba1be..0543023 100644 --- a/waki/tests/client.rs +++ b/waki/tests/client.rs @@ -1,52 +1,38 @@ -use std::process::Command; +mod common; -fn wasmtime() -> Command { - let mut wasmtime = Command::new("wasmtime"); - wasmtime.arg("-S").arg("http"); - wasmtime -} +use crate::common::run_wasi; -#[test] -fn client_get_chunk() { - let status = wasmtime() - .arg(test_programs_artifacts::CLIENT_GET_CHUNK_COMPONENT) - .status() +#[tokio::test(flavor = "multi_thread")] +async fn client_get_chunk() { + run_wasi(test_programs_artifacts::CLIENT_GET_CHUNK_COMPONENT) + .await .unwrap(); - assert!(status.success()); } -#[test] -fn client_get_with_query() { - let status = wasmtime() - .arg(test_programs_artifacts::CLIENT_GET_WITH_QUERY_COMPONENT) - .status() +#[tokio::test(flavor = "multi_thread")] +async fn client_get_with_query() { + run_wasi(test_programs_artifacts::CLIENT_GET_WITH_QUERY_COMPONENT) + .await .unwrap(); - assert!(status.success()); } -#[test] -fn client_post_with_form_data() { - let status = wasmtime() - .arg(test_programs_artifacts::CLIENT_POST_WITH_FORM_DATA_COMPONENT) - .status() +#[tokio::test(flavor = "multi_thread")] +async fn client_post_with_form_data() { + run_wasi(test_programs_artifacts::CLIENT_POST_WITH_FORM_DATA_COMPONENT) + .await .unwrap(); - assert!(status.success()); } -#[test] -fn client_post_with_json_data() { - let status = wasmtime() - .arg(test_programs_artifacts::CLIENT_POST_WITH_JSON_DATA_COMPONENT) - .status() +#[tokio::test(flavor = "multi_thread")] +async fn client_post_with_json_data() { + run_wasi(test_programs_artifacts::CLIENT_POST_WITH_JSON_DATA_COMPONENT) + .await .unwrap(); - assert!(status.success()); } -#[test] -fn client_post_with_multipart_form_data() { - let status = wasmtime() - .arg(test_programs_artifacts::CLIENT_POST_WITH_MULTIPART_FORM_DATA_COMPONENT) - .status() +#[tokio::test(flavor = "multi_thread")] +async fn client_post_with_multipart_form_data() { + run_wasi(test_programs_artifacts::CLIENT_POST_WITH_MULTIPART_FORM_DATA_COMPONENT) + .await .unwrap(); - assert!(status.success()); } diff --git a/waki/tests/common/mod.rs b/waki/tests/common/mod.rs index c3de5fa..566d002 100644 --- a/waki/tests/common/mod.rs +++ b/waki/tests/common/mod.rs @@ -5,9 +5,10 @@ use wasmtime::{ component::{Component, Linker, ResourceTable}, Config, Engine, Store, }; -use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView}; +use wasmtime_wasi::{bindings::Command, WasiCtx, WasiCtxBuilder, WasiView}; use wasmtime_wasi_http::{ - bindings::http::types::ErrorCode, body::HyperIncomingBody, WasiHttpCtx, WasiHttpView, + bindings::http::types::ErrorCode, body::HyperIncomingBody, proxy::Proxy, WasiHttpCtx, + WasiHttpView, }; struct Ctx { @@ -35,11 +36,7 @@ impl WasiHttpView for Ctx { } } -// ref: https://github.com/bytecodealliance/wasmtime/blob/af59c4d568d487b7efbb49d7d31a861e7c3933a6/crates/wasi-http/tests/all/main.rs#L129 -pub async fn run_wasi_http( - component_filename: &str, - req: hyper::Request, -) -> Result>, ErrorCode>> { +fn new_component(component_filename: &str) -> Result<(Store, Component, Linker)> { let mut config = Config::new(); config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); config.wasm_component_model(true); @@ -54,12 +51,21 @@ pub async fn run_wasi_http( http: WasiHttpCtx::new(), }; - let mut store = Store::new(&engine, ctx); + let store = Store::new(&engine, ctx); let mut linker = Linker::new(&engine); - wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?; - let (proxy, _) = - wasmtime_wasi_http::proxy::Proxy::instantiate_async(&mut store, &component, &linker) - .await?; + wasmtime_wasi::add_to_linker_async(&mut linker)?; + wasmtime_wasi_http::proxy::add_only_http_to_linker(&mut linker)?; + Ok((store, component, linker)) +} + +// ref: https://github.com/bytecodealliance/wasmtime/blob/af59c4d568d487b7efbb49d7d31a861e7c3933a6/crates/wasi-http/tests/all/main.rs#L129 +pub async fn run_wasi_http( + component_filename: &str, + req: hyper::Request, +) -> Result>, ErrorCode>> { + let (mut store, component, linker) = new_component(component_filename)?; + + let (proxy, _) = Proxy::instantiate_async(&mut store, &component, &linker).await?; let req = store.data_mut().new_incoming_request(req)?; @@ -95,3 +101,14 @@ pub async fn run_wasi_http( Ok(resp.expect("wasm never called set-response-outparam")) } + +pub async fn run_wasi(component_filename: &str) -> Result<()> { + let (mut store, component, linker) = new_component(component_filename)?; + + let (command, _) = Command::instantiate_async(&mut store, &component, &linker).await?; + command + .wasi_cli_run() + .call_run(&mut store) + .await? + .map_err(|()| anyhow::anyhow!("run returned a failure")) +} diff --git a/waki/tests/server.rs b/waki/tests/server.rs index 9dcb40c..d8cc7e7 100644 --- a/waki/tests/server.rs +++ b/waki/tests/server.rs @@ -5,7 +5,7 @@ use crate::common::run_wasi_http; use anyhow::Result; use wasmtime_wasi_http::body::HyperIncomingBody; -#[tokio::test] +#[tokio::test(flavor = "multi_thread")] async fn server_hello_query() -> Result<()> { let req = hyper::Request::builder() .uri("http://localhost?name=ia")