Path: blob/main/crates/wasi-tls/tests/main.rs
1692 views
use anyhow::{Result, anyhow};1use wasmtime::{2Store,3component::{Component, Linker, ResourceTable},4};5use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView, p2::bindings::Command};6use wasmtime_wasi_tls::{LinkOptions, WasiTls, WasiTlsCtx, WasiTlsCtxBuilder};78struct Ctx {9table: ResourceTable,10wasi_ctx: WasiCtx,11wasi_tls_ctx: WasiTlsCtx,12}1314impl WasiView for Ctx {15fn ctx(&mut self) -> WasiCtxView<'_> {16WasiCtxView {17ctx: &mut self.wasi_ctx,18table: &mut self.table,19}20}21}2223async fn run_test(path: &str) -> Result<()> {24let ctx = Ctx {25table: ResourceTable::new(),26wasi_ctx: WasiCtx::builder()27.inherit_stdout()28.inherit_stderr()29.inherit_network()30.allow_ip_name_lookup(true)31.build(),32wasi_tls_ctx: WasiTlsCtxBuilder::new().build(),33};3435let engine = test_programs_artifacts::engine(|config| {36config.async_support(true);37});38let mut store = Store::new(&engine, ctx);39let component = Component::from_file(&engine, path)?;4041let mut linker = Linker::new(&engine);42wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;43let mut opts = LinkOptions::default();44opts.tls(true);45wasmtime_wasi_tls::add_to_linker(&mut linker, &mut opts, |h: &mut Ctx| {46WasiTls::new(&h.wasi_tls_ctx, &mut h.table)47})?;4849let command = Command::instantiate_async(&mut store, &component, &linker).await?;50command51.wasi_cli_run()52.call_run(&mut store)53.await?54.map_err(|()| anyhow!("command returned with failing exit status"))55}5657macro_rules! assert_test_exists {58($name:ident) => {59#[expect(unused_imports, reason = "just here to assert it exists")]60use self::$name as _;61};62}6364test_programs_artifacts::foreach_tls!(assert_test_exists);6566#[tokio::test(flavor = "multi_thread")]67async fn tls_sample_application() -> Result<()> {68run_test(test_programs_artifacts::TLS_SAMPLE_APPLICATION_COMPONENT).await69}707172