Skip to content

Commit

Permalink
Use the wasmtime library instead of the binary to run client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Jun 11, 2024
1 parent ce87cd5 commit 6c394c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 54 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 22 additions & 36 deletions waki/tests/client.rs
Original file line number Diff line number Diff line change
@@ -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());
}
41 changes: 29 additions & 12 deletions waki/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<HyperIncomingBody>,
) -> Result<Result<hyper::Response<Collected<Bytes>>, ErrorCode>> {
fn new_component(component_filename: &str) -> Result<(Store<Ctx>, Component, Linker<Ctx>)> {
let mut config = Config::new();
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
config.wasm_component_model(true);
Expand All @@ -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<HyperIncomingBody>,
) -> Result<Result<hyper::Response<Collected<Bytes>>, 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)?;

Expand Down Expand Up @@ -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"))
}
2 changes: 1 addition & 1 deletion waki/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6c394c6

Please sign in to comment.