Skip to content

Commit

Permalink
Merge pull request #5 from wacker-dev/client-tests
Browse files Browse the repository at this point in the history
Use the wasmtime library instead of the binary to run client tests
  • Loading branch information
iawia002 authored Jun 11, 2024
2 parents ce87cd5 + 454cf8a commit f5bb417
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 74 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
36 changes: 36 additions & 0 deletions waki/tests/all/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use super::run_wasi;

#[tokio::test(flavor = "multi_thread")]
async fn get_chunk() {
run_wasi(test_programs_artifacts::CLIENT_GET_CHUNK_COMPONENT)
.await
.unwrap();
}

#[tokio::test(flavor = "multi_thread")]
async fn get_with_query() {
run_wasi(test_programs_artifacts::CLIENT_GET_WITH_QUERY_COMPONENT)
.await
.unwrap();
}

#[tokio::test(flavor = "multi_thread")]
async fn post_with_form_data() {
run_wasi(test_programs_artifacts::CLIENT_POST_WITH_FORM_DATA_COMPONENT)
.await
.unwrap();
}

#[tokio::test(flavor = "multi_thread")]
async fn post_with_json_data() {
run_wasi(test_programs_artifacts::CLIENT_POST_WITH_JSON_DATA_COMPONENT)
.await
.unwrap();
}

#[tokio::test(flavor = "multi_thread")]
async fn post_with_multipart_form_data() {
run_wasi(test_programs_artifacts::CLIENT_POST_WITH_MULTIPART_FORM_DATA_COMPONENT)
.await
.unwrap();
}
44 changes: 32 additions & 12 deletions waki/tests/common/mod.rs → waki/tests/all/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
mod client;
mod server;

use anyhow::{Context, Result};
use http_body_util::Collected;
use hyper::body::Bytes;
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 +39,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 +54,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 +104,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"))
}
8 changes: 3 additions & 5 deletions waki/tests/server.rs → waki/tests/all/server.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
mod common;

use crate::common::run_wasi_http;
use super::run_wasi_http;

use anyhow::Result;
use wasmtime_wasi_http::body::HyperIncomingBody;

#[tokio::test]
async fn server_hello_query() -> Result<()> {
#[tokio::test(flavor = "multi_thread")]
async fn query() -> Result<()> {
let req = hyper::Request::builder()
.uri("http://localhost?name=ia")
.body(HyperIncomingBody::default())?;
Expand Down
52 changes: 0 additions & 52 deletions waki/tests/client.rs

This file was deleted.

0 comments on commit f5bb417

Please sign in to comment.