From e5c0a25f9551b99b6ab2b7898c6c5c007c9346e5 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Mon, 15 May 2023 20:41:51 -0700 Subject: [PATCH] convert wasi-http test --- crates/test-programs/tests/http_tests/main.rs | 12 ------ .../tests/http_tests/runtime/mod.rs | 1 - .../test-programs/tests/http_tests/utils.rs | 13 ------- .../wasi_http_tests.rs => wasi-http.rs} | 37 ++++++++++++------- 4 files changed, 23 insertions(+), 40 deletions(-) delete mode 100644 crates/test-programs/tests/http_tests/main.rs delete mode 100644 crates/test-programs/tests/http_tests/runtime/mod.rs delete mode 100644 crates/test-programs/tests/http_tests/utils.rs rename crates/test-programs/tests/{http_tests/runtime/wasi_http_tests.rs => wasi-http.rs} (72%) diff --git a/crates/test-programs/tests/http_tests/main.rs b/crates/test-programs/tests/http_tests/main.rs deleted file mode 100644 index 28a8c956d9f4..000000000000 --- a/crates/test-programs/tests/http_tests/main.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![cfg(feature = "test_programs_http")] -use std::sync::Once; -mod runtime; -mod utils; - -static LOG_INIT: Once = Once::new(); - -fn setup_log() { - LOG_INIT.call_once(tracing_subscriber::fmt::init) -} - -include!(concat!(env!("OUT_DIR"), "/wasi_http_tests.rs")); diff --git a/crates/test-programs/tests/http_tests/runtime/mod.rs b/crates/test-programs/tests/http_tests/runtime/mod.rs deleted file mode 100644 index 5af38c5cb518..000000000000 --- a/crates/test-programs/tests/http_tests/runtime/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod wasi_http_tests; diff --git a/crates/test-programs/tests/http_tests/utils.rs b/crates/test-programs/tests/http_tests/utils.rs deleted file mode 100644 index d455b96590c3..000000000000 --- a/crates/test-programs/tests/http_tests/utils.rs +++ /dev/null @@ -1,13 +0,0 @@ -use std::path::Path; - -pub fn extract_exec_name_from_path(path: &Path) -> anyhow::Result { - path.file_stem() - .and_then(|s| s.to_str()) - .map(String::from) - .ok_or_else(|| { - anyhow::anyhow!( - "couldn't extract the file stem from path {}", - path.display() - ) - }) -} diff --git a/crates/test-programs/tests/http_tests/runtime/wasi_http_tests.rs b/crates/test-programs/tests/wasi-http.rs similarity index 72% rename from crates/test-programs/tests/http_tests/runtime/wasi_http_tests.rs rename to crates/test-programs/tests/wasi-http.rs index 5ed731dbcdb2..8355de928e0d 100644 --- a/crates/test-programs/tests/http_tests/runtime/wasi_http_tests.rs +++ b/crates/test-programs/tests/wasi-http.rs @@ -1,6 +1,5 @@ -use anyhow::Context; -use std::path::Path; -use wasmtime::{Config, Engine, Linker, Module, Store}; +#![cfg(feature = "test_programs")] +use wasmtime::{Config, Engine, Linker, Store}; use wasmtime_wasi::{sync::WasiCtxBuilder, WasiCtx}; use wasmtime_wasi_http::WasiHttp; @@ -11,6 +10,17 @@ use hyper::{body::Bytes, service::service_fn, Request, Response}; use std::{error::Error, net::SocketAddr}; use tokio::net::TcpListener; +lazy_static::lazy_static! { + static ref ENGINE: Engine = { + let mut config = Config::new(); + config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); + let engine = Engine::new(&config).unwrap(); + engine + }; +} +// uses ENGINE, creates a fn get_module(&str) -> Module +include!(concat!(env!("OUT_DIR"), "/wasi_http_tests_modules.rs")); + async fn test( req: Request, ) -> http::Result>> { @@ -48,19 +58,13 @@ fn run_server() -> Result<(), Box> { Ok(()) } -pub fn instantiate_inherit_stdio( - data: &[u8], - bin_name: &str, - _workspace: Option<&Path>, -) -> anyhow::Result<()> { +pub fn run(name: &str) -> anyhow::Result<()> { let _thread = std::thread::spawn(|| { run_server().unwrap(); }); - let config = Config::new(); - let engine = Engine::new(&config)?; - let module = Module::new(&engine, &data).context("failed to create wasm module")?; - let mut linker = Linker::new(&engine); + let module = get_module(name); + let mut linker = Linker::new(&ENGINE); struct Ctx { wasi: WasiCtx, @@ -71,10 +75,10 @@ pub fn instantiate_inherit_stdio( wasmtime_wasi_http::add_to_linker(&mut linker, |cx: &mut Ctx| &mut cx.http)?; // Create our wasi context. - let builder = WasiCtxBuilder::new().inherit_stdio().arg(bin_name)?; + let builder = WasiCtxBuilder::new().inherit_stdio().arg(name)?; let mut store = Store::new( - &engine, + &ENGINE, Ctx { wasi: builder.build(), http: WasiHttp::new(), @@ -85,3 +89,8 @@ pub fn instantiate_inherit_stdio( let start = instance.get_typed_func::<(), ()>(&mut store, "_start")?; start.call(&mut store, ()) } + +#[test_log::test] +fn outbound_request() { + run("outbound_request").unwrap() +}