pub mod test_server;
use crate::common::WasmSource::Precompiled;
use anyhow::anyhow;
use camino::{Utf8Path, Utf8PathBuf};
use camino_tempfile::{NamedUtf8TempFile, Utf8TempDir};
use heck::ToSnakeCase;
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::io::Write;
use std::process::Command;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::time::timeout;
use wac_graph::types::{Package, SubtypeChecker};
use wac_graph::{CompositionGraph, EncodeOptions, PackageId, PlugError};
use wasm_rquickjs::{EmbeddingMode, JsModuleSpec, generate_wrapper_crate};
use wasmtime::component::{Component, Func, Instance, Linker, ResourceAny, ResourceTable, Val};
use wasmtime::{Engine, Store};
use wasmtime_wasi::p2::{IoView, OutputFile, WasiCtx, WasiView, bindings};
use wasmtime_wasi::{DirPerms, FilePerms};
use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
pub fn collect_example_paths() -> anyhow::Result<Vec<Utf8PathBuf>> {
let mut result = Vec::new();
let paths = fs::read_dir("examples")?;
for example_path in paths {
let example_path = example_path?;
let metadata = example_path.metadata()?;
if metadata.is_dir() {
let path = Utf8PathBuf::from_path_buf(example_path.path())
.map_err(|_| anyhow!("Non UTF-8 example path"))?;
result.push(path);
}
}
Ok(result)
}
#[derive(Copy, Clone)]
pub enum FeatureCombination {
None,
LogOnly,
HttpOnly,
Default,
}
impl FeatureCombination {
pub fn all() -> Vec<FeatureCombination> {
vec![Self::None, Self::LogOnly, Self::HttpOnly, Self::Default]
}
pub fn label(&self) -> &str {
match self {
Self::None => "none",
Self::LogOnly => "log",
Self::HttpOnly => "http",
Self::Default => "default",
}
}
pub fn cargo_args(&self) -> Vec<&'static str> {
match self {
FeatureCombination::None => vec!["--no-default-features"],
FeatureCombination::LogOnly => {
vec!["--no-default-features", "--features", "logging"]
}
FeatureCombination::HttpOnly => vec!["--no-default-features", "--features", "http"],
FeatureCombination::Default => vec![],
}
}
}
#[allow(dead_code)]
pub struct TestInstance {
engine: Engine,
linker: Linker<Host>,
component: Component,
store: Store<Host>,
instance: Instance,
stdout_file: NamedUtf8TempFile,
stderr_file: NamedUtf8TempFile,
temp_dir: Utf8TempDir,
}
impl TestInstance {
pub async fn new(wasm_path: &Utf8Path) -> anyhow::Result<Self> {
let mut config = wasmtime::Config::default();
config.async_support(true);
config.wasm_component_model(true);
let engine = Engine::new(&config)?;
let mut linker: Linker<Host> = Linker::new(&engine);
wasmtime_wasi::p2::add_to_linker_with_options_async(
&mut linker,
&bindings::LinkOptions::default(),
)?;
wasmtime_wasi_http::add_only_http_to_linker_async(&mut linker)?;
let stdout_file = NamedUtf8TempFile::new()?;
let stderr_file = NamedUtf8TempFile::new()?;
let temp_dir = Utf8TempDir::new()?;
fs::write(temp_dir.path().join("input.txt"), "test file contents")?;
fs::create_dir(temp_dir.path().join("test"))?;
let ctx = WasiCtx::builder()
.stdout(OutputFile::new(stdout_file.reopen()?))
.stderr(OutputFile::new(stderr_file.reopen()?))
.arg("first-arg")
.arg("second-arg")
.env("TEST_KEY", "TEST_VALUE")
.env("TEST_KEY_2", "TEST_VALUE_2")
.preopened_dir(&temp_dir, "/", DirPerms::all(), FilePerms::all())?
.build();
let http_ctx = WasiHttpCtx::new();
let host = Host {
table: Arc::new(Mutex::new(ResourceTable::new())),
wasi: Arc::new(Mutex::new(ctx)),
wasi_http: Arc::new(http_ctx),
};
let component = Component::from_file(&engine, wasm_path)?;
let mut store = Store::new(&engine, host);
let instance = linker.instantiate_async(&mut store, &component).await?;
Ok(Self {
engine,
linker,
component,
store,
instance,
stdout_file,
stderr_file,
temp_dir,
})
}
pub async fn invoke_and_capture_output(
&mut self,
interface_name: Option<&str>,
function_name: &str,
args: &[Val],
) -> (anyhow::Result<Option<Val>>, String) {
let (results, stdout, _stderr) = self
.invoke_and_capture_output_with_stderr(interface_name, function_name, args)
.await;
(results, stdout)
}
pub async fn invoke_and_capture_output_with_stderr(
&mut self,
interface_name: Option<&str>,
function_name: &str,
args: &[Val],
) -> (anyhow::Result<Option<Val>>, String, String) {
let results = self
.invoke_and_capture_output_inner(interface_name, function_name, args)
.await;
let stdout = fs::read_to_string(&self.stdout_file).expect("failed to read stdout");
let stderr = fs::read_to_string(&self.stderr_file).expect("failed to read stderr");
if results.is_err() {
for line in stdout.lines() {
println!("[stdout] {line}");
}
}
for line in stderr.lines() {
println!("[stderr] {line}");
}
(
results.map(|results| results.first().cloned()),
stdout,
stderr,
)
}
pub fn temp_dir_path(&self) -> &Utf8Path {
self.temp_dir.path()
}
async fn invoke_and_capture_output_inner(
&mut self,
interface_name: Option<&str>,
function_name: &str,
args: &[Val],
) -> anyhow::Result<Vec<Val>> {
let func = match interface_name {
Some(interface_name) => {
let (_, exported_instance_id) = self
.instance
.get_export(&mut self.store, None, interface_name)
.ok_or_else(|| anyhow!("Interface {interface_name} not found"))?;
let (_, func_id) = self
.instance
.get_export(&mut self.store, Some(&exported_instance_id), function_name)
.ok_or_else(|| {
anyhow!("Function {function_name} not found in interface {interface_name}")
})?;
self.instance
.get_func(&mut self.store, func_id)
.ok_or_else(|| anyhow!("Function {function_name} not found"))?
}
None => self
.instance
.get_func(&mut self.store, function_name)
.ok_or_else(|| anyhow!("Function {function_name} not found"))?,
};
match timeout(Duration::from_secs(120), self.perform_invoke(func, args)).await {
Ok(result) => result,
Err(_) => Err(anyhow!("Function {function_name} timed out")),
}
}
async fn perform_invoke(&mut self, func: Func, args: &[Val]) -> anyhow::Result<Vec<Val>> {
let mut results = (0..func.results(&mut self.store).len())
.map(|_| Val::Bool(false))
.collect::<Vec<_>>();
func.call_async(&mut self.store, args, &mut results).await?;
func.post_return_async(&mut self.store).await?;
Ok(results)
}
pub async fn drop_resource(&mut self, resource: ResourceAny) -> anyhow::Result<()> {
resource.resource_drop_async::<Host>(&mut self.store).await
}
}
pub async fn invoke_and_capture_output(
wasm_path: &Utf8Path,
interface_name: Option<&str>,
function_name: &str,
args: &[Val],
) -> (anyhow::Result<Option<Val>>, String) {
match TestInstance::new(wasm_path).await {
Ok(mut test_instance) => {
test_instance
.invoke_and_capture_output(interface_name, function_name, args)
.await
}
Err(e) => (Err(e), String::new()),
}
}
pub async fn invoke_and_capture_output_with_stderr(
wasm_path: &Utf8Path,
interface_name: Option<&str>,
function_name: &str,
args: &[Val],
) -> (anyhow::Result<Option<Val>>, String, String) {
match TestInstance::new(wasm_path).await {
Ok(mut test_instance) => {
test_instance
.invoke_and_capture_output_with_stderr(interface_name, function_name, args)
.await
}
Err(e) => (Err(e), String::new(), String::new()),
}
}
enum WasmSource {
Precompiled(Utf8PathBuf),
OwnedTemporary(NamedUtf8TempFile),
}
pub struct CompiledTest {
wasm: WasmSource,
}
impl CompiledTest {
pub fn new(path: &Utf8Path, use_shared_target: bool) -> anyhow::Result<CompiledTest> {
let feature_combination = FeatureCombination::HttpOnly;
let name = path.file_name().unwrap();
let wrapper_crate_root = Utf8Path::new("tmp")
.join(name)
.join(feature_combination.label());
let shared_target = Utf8Path::new("..").join("..").join("rt-target");
println!("Generating wrapper create for example '{name}' to {wrapper_crate_root}");
generate_wrapper_crate(
&path.join("wit"),
&[JsModuleSpec {
name: name.to_string(),
mode: EmbeddingMode::EmbedFile(path.join("src").join(format!("{name}.js"))),
}],
&wrapper_crate_root,
None,
)?;
println!("Compiling wrapper crate in {wrapper_crate_root}");
let mut command = Command::new("cargo-component");
command.arg("build");
if use_shared_target {
command.arg("--target-dir");
command.arg(shared_target);
}
command
.args(feature_combination.cargo_args())
.current_dir(&wrapper_crate_root)
.status()?;
if use_shared_target {
Ok(CompiledTest {
wasm: Precompiled(
Utf8Path::new("tmp")
.join("rt-target")
.join("wasm32-wasip1")
.join("debug")
.join(format!("{}.wasm", name.to_snake_case())),
),
})
} else {
Ok(CompiledTest {
wasm: Precompiled(
wrapper_crate_root
.join("target")
.join("wasm32-wasip1")
.join("debug")
.join(format!("{}.wasm", name.to_snake_case())),
),
})
}
}
pub fn wasm_path(&self) -> &Utf8Path {
match &self.wasm {
WasmSource::Precompiled(path) => path,
WasmSource::OwnedTemporary(temp_file) => temp_file.path(),
}
}
pub fn plug_into(&self, other: &CompiledTest) -> anyhow::Result<CompiledTest> {
let mut graph = CompositionGraph::new();
let socket_package =
Package::from_file("socket", None, other.wasm_path(), graph.types_mut())?;
let socket_id = graph.register_package(socket_package)?;
let plug_package = Package::from_file("plug", None, self.wasm_path(), graph.types_mut())?;
let plug_id = graph.register_package(plug_package)?;
plug(
&mut graph,
vec![(self.wasm_path().to_string(), plug_id)],
socket_id,
)?;
let bytes = graph.encode(EncodeOptions::default())?;
let mut wasm_path = NamedUtf8TempFile::new()?;
wasm_path.write_all(bytes.as_slice())?;
wasm_path.flush()?;
Ok(CompiledTest {
wasm: WasmSource::OwnedTemporary(wasm_path),
})
}
}
#[derive(Clone)]
struct Host {
pub table: Arc<Mutex<ResourceTable>>,
pub wasi: Arc<Mutex<WasiCtx>>,
pub wasi_http: Arc<WasiHttpCtx>,
}
impl IoView for Host {
fn table(&mut self) -> &mut ResourceTable {
Arc::get_mut(&mut self.table)
.expect("ResourceTable is shared and cannot be borrowed mutably")
.get_mut()
.expect("ResourceTable mutex must never fail")
}
}
impl WasiView for Host {
fn ctx(&mut self) -> &mut WasiCtx {
Arc::get_mut(&mut self.wasi)
.expect("WasiCtx is shared and cannot be borrowed mutably")
.get_mut()
.expect("WasiCtx mutex must never fail")
}
}
impl WasiHttpView for Host {
fn ctx(&mut self) -> &mut WasiHttpCtx {
Arc::get_mut(&mut self.wasi_http)
.expect("WasiHttpCtx is shared and cannot be borrowed mutably")
}
}
fn plug(
graph: &mut CompositionGraph,
plugs: Vec<(String, PackageId)>,
socket: PackageId,
) -> Result<(), PlugError> {
let socket_instantiation = graph.instantiate(socket);
let mut requested_plugs = BTreeSet::<String>::new();
let mut plug_exports_to_plug = BTreeMap::<String, String>::new();
for (plug_name, plug) in plugs {
requested_plugs.insert(plug_name.clone());
let mut plug_exports = Vec::new();
let mut cache = Default::default();
let mut checker = SubtypeChecker::new(&mut cache);
for (name, plug_ty) in &graph.types()[graph[plug].ty()].exports {
if let Some(socket_ty) = graph.types()[graph[socket].ty()].imports.get(name)
&& checker
.is_subtype(*plug_ty, graph.types(), *socket_ty, graph.types())
.is_ok()
{
plug_exports.push(name.clone());
}
}
let mut plug_instantiation = None;
for plug_export_name in plug_exports {
plug_exports_to_plug.insert(plug_export_name.clone(), plug_name.clone());
let plug_instantiation =
*plug_instantiation.get_or_insert_with(|| graph.instantiate(plug));
let export = graph
.alias_instance_export(plug_instantiation, &plug_export_name)
.map_err(|err| PlugError::GraphError { source: err.into() })?;
graph
.set_instantiation_argument(socket_instantiation, &plug_export_name, export)
.map_err(|err| PlugError::GraphError { source: err.into() })?;
}
}
for name in graph.types()[graph[socket].ty()]
.exports
.keys()
.cloned()
.collect::<Vec<_>>()
{
let export = graph
.alias_instance_export(socket_instantiation, &name)
.map_err(|err| PlugError::GraphError { source: err.into() })?;
graph
.export(export, &name)
.map_err(|err| PlugError::GraphError { source: err.into() })?;
}
Ok(())
}