|
| 1 | +#![cfg(all(feature = "test_programs", not(skip_wasi_http_tests)))] |
| 2 | +use anyhow::Context; |
| 3 | +use wasmtime::{ |
| 4 | + component::{Component, Linker}, |
| 5 | + Config, Engine, Store, |
| 6 | +}; |
| 7 | +use wasmtime_wasi::preview2::{ |
| 8 | + self, pipe::MemoryOutputPipe, IsATTY, Table, WasiCtx, WasiCtxBuilder, WasiView, |
| 9 | +}; |
| 10 | +use wasmtime_wasi_http::{proxy::Proxy, WasiHttpCtx, WasiHttpView}; |
| 11 | + |
| 12 | +lazy_static::lazy_static! { |
| 13 | + static ref ENGINE: Engine = { |
| 14 | + let mut config = Config::new(); |
| 15 | + config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); |
| 16 | + config.wasm_component_model(true); |
| 17 | + config.async_support(true); |
| 18 | + let engine = Engine::new(&config).unwrap(); |
| 19 | + engine |
| 20 | + }; |
| 21 | +} |
| 22 | +// uses ENGINE, creates a fn get_module(&str) -> Module |
| 23 | +include!(concat!( |
| 24 | + env!("OUT_DIR"), |
| 25 | + "/wasi_http_proxy_tests_components.rs" |
| 26 | +)); |
| 27 | + |
| 28 | +struct Ctx { |
| 29 | + table: Table, |
| 30 | + wasi: WasiCtx, |
| 31 | + http: WasiHttpCtx, |
| 32 | +} |
| 33 | + |
| 34 | +impl WasiView for Ctx { |
| 35 | + fn table(&self) -> &Table { |
| 36 | + &self.table |
| 37 | + } |
| 38 | + fn table_mut(&mut self) -> &mut Table { |
| 39 | + &mut self.table |
| 40 | + } |
| 41 | + fn ctx(&self) -> &WasiCtx { |
| 42 | + &self.wasi |
| 43 | + } |
| 44 | + fn ctx_mut(&mut self) -> &mut WasiCtx { |
| 45 | + &mut self.wasi |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl WasiHttpView for Ctx { |
| 50 | + fn table(&mut self) -> &mut Table { |
| 51 | + &mut self.table |
| 52 | + } |
| 53 | + fn ctx(&mut self) -> &mut WasiHttpCtx { |
| 54 | + &mut self.http |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +async fn instantiate(component: Component, ctx: Ctx) -> Result<(Store<Ctx>, Proxy), anyhow::Error> { |
| 59 | + let mut linker = Linker::new(&ENGINE); |
| 60 | + wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?; |
| 61 | + |
| 62 | + let mut store = Store::new(&ENGINE, ctx); |
| 63 | + |
| 64 | + let (proxy, _instance) = Proxy::instantiate_async(&mut store, &component, &linker).await?; |
| 65 | + Ok((store, proxy)) |
| 66 | +} |
| 67 | + |
| 68 | +#[test_log::test(tokio::test)] |
| 69 | +async fn wasi_http_proxy_tests() -> anyhow::Result<()> { |
| 70 | + let stdout = MemoryOutputPipe::new(4096); |
| 71 | + let stderr = MemoryOutputPipe::new(4096); |
| 72 | + |
| 73 | + let mut table = Table::new(); |
| 74 | + let component = get_component("wasi_http_proxy_tests"); |
| 75 | + |
| 76 | + // Create our wasi context. |
| 77 | + let mut builder = WasiCtxBuilder::new(); |
| 78 | + builder.stdout(stdout.clone(), IsATTY::No); |
| 79 | + builder.stderr(stderr.clone(), IsATTY::No); |
| 80 | + for (var, val) in test_programs::wasi_tests_environment() { |
| 81 | + builder.env(var, val); |
| 82 | + } |
| 83 | + let wasi = builder.build(&mut table)?; |
| 84 | + let http = WasiHttpCtx; |
| 85 | + |
| 86 | + let ctx = Ctx { table, wasi, http }; |
| 87 | + |
| 88 | + let (mut store, proxy) = instantiate(component, ctx).await?; |
| 89 | + |
| 90 | + let req = { |
| 91 | + use http_body_util::{BodyExt, Empty}; |
| 92 | + |
| 93 | + let req = hyper::Request::builder().method(http::Method::GET).body( |
| 94 | + Empty::<bytes::Bytes>::new() |
| 95 | + .map_err(|e| anyhow::anyhow!(e)) |
| 96 | + .boxed(), |
| 97 | + )?; |
| 98 | + store.data_mut().new_incoming_request(req)? |
| 99 | + }; |
| 100 | + |
| 101 | + let (sender, receiver) = tokio::sync::oneshot::channel(); |
| 102 | + let out = store.data_mut().new_response_outparam(sender)?; |
| 103 | + |
| 104 | + let handle = preview2::spawn(async move { |
| 105 | + proxy |
| 106 | + .wasi_http_incoming_handler() |
| 107 | + .call_handle(&mut store, req, out) |
| 108 | + .await?; |
| 109 | + |
| 110 | + Ok::<_, anyhow::Error>(()) |
| 111 | + }); |
| 112 | + |
| 113 | + let resp = match receiver.await { |
| 114 | + Ok(Ok(resp)) => { |
| 115 | + use http_body_util::BodyExt; |
| 116 | + let (parts, body) = resp.into_parts(); |
| 117 | + let collected = BodyExt::collect(body).await?; |
| 118 | + Ok(hyper::Response::from_parts(parts, collected)) |
| 119 | + } |
| 120 | + |
| 121 | + Ok(Err(e)) => Err(e), |
| 122 | + |
| 123 | + // This happens if the wasm never calls `set-response-outparam` |
| 124 | + Err(e) => panic!("Failed to receive a response: {e:?}"), |
| 125 | + }; |
| 126 | + |
| 127 | + // Now that the response has been processed, we can wait on the wasm to finish without |
| 128 | + // deadlocking. |
| 129 | + handle.await.context("Component execution")?; |
| 130 | + |
| 131 | + let stdout = stdout.contents(); |
| 132 | + if !stdout.is_empty() { |
| 133 | + println!("[guest] stdout:\n{}\n===", String::from_utf8_lossy(&stdout)); |
| 134 | + } |
| 135 | + let stderr = stderr.contents(); |
| 136 | + if !stderr.is_empty() { |
| 137 | + println!("[guest] stderr:\n{}\n===", String::from_utf8_lossy(&stderr)); |
| 138 | + } |
| 139 | + |
| 140 | + match resp { |
| 141 | + Ok(resp) => println!("response: {resp:?}"), |
| 142 | + Err(e) => panic!("Error given in response: {e:?}"), |
| 143 | + }; |
| 144 | + |
| 145 | + Ok(()) |
| 146 | +} |
0 commit comments