Skip to content

Commit

Permalink
Merge pull request #723 from jsturtevant/support-csharp
Browse files Browse the repository at this point in the history
Include cli implementations for adapter components
  • Loading branch information
Mossaka authored Nov 22, 2024
2 parents 69cc945 + 8f2d6a6 commit 251575a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Binary file not shown.
7 changes: 6 additions & 1 deletion crates/containerd-shim-wasmtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,21 @@ where
// This is a adapter logic that converts wasip1 `_start` function to wasip2 `run` function.
let status = match target {
ComponentTarget::HttpProxy => {
log::info!("Found HTTP proxy target");
let mut linker = component::Linker::new(&self.engine);
wasmtime_wasi_http::add_to_linker_async(&mut linker)?;
wasmtime_wasi::add_to_linker_async(&mut linker)?;
wasmtime_wasi_http::add_only_http_to_linker_async(&mut linker)?;

let pre = linker.instantiate_pre(&component)?;
log::info!("pre-instantiate_pre");
let instance = ProxyPre::new(pre)?;

log::info!("starting HTTP server");
let cancel = self.cancel.clone();
serve_conn(ctx, instance, cancel).await
}
ComponentTarget::Command => {
log::info!("Found command target");
let wasi_ctx = WasiPreview2Ctx::new(ctx)?;
let (mut store, linker) = store_for_context(&self.engine, wasi_ctx)?;

Expand All @@ -273,6 +277,7 @@ where
})
}
ComponentTarget::Core(func) => {
log::info!("Found Core target");
let wasi_ctx = WasiPreview2Ctx::new(ctx)?;
let (mut store, linker) = store_for_context(&self.engine, wasi_ctx)?;

Expand Down
39 changes: 36 additions & 3 deletions crates/containerd-shim-wasmtime/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,35 @@ fn test_wasip2_component_http_proxy() -> anyhow::Result<()> {
Ok(())
}

// The wasm component is built using componentize-dotnet as illustrated in the following example::
// https://bytecodealliance.org/articles/simplifying-components-for-dotnet-developers-with-componentize-dotnet
// this ensures we are able to use wasm built from other languages https://github.com/containerd/runwasi/pull/723
#[test]
#[serial]
fn test_wasip2_component_http_proxy_csharp() -> anyhow::Result<()> {
let srv = WasiTest::<WasiInstance>::builder()?
.with_wasm(HELLO_WASI_HTTP_CSHARP)?
.with_host_network()
.build()?;

let srv = srv.start()?;

// dotnet takes a bit longer to start up
// Todo: find out why this doesn't happen in wasmtime directly
let response = http_get_with_backoff_secs(2);

let response = response.expect("Server did not start in time");
assert!(response.status().is_success());

let body = response.text().unwrap();
assert_eq!(body, "Hello, from C#!");

let (exit_code, _, _) = srv.ctrl_c()?.wait(Duration::from_secs(5))?;
assert_eq!(exit_code, 0);

Ok(())
}

// Test that the shim can terminate component targeting wasi:http/proxy by sending SIGTERM.
#[test]
#[serial]
Expand All @@ -308,10 +337,14 @@ fn test_wasip2_component_http_proxy_force_shutdown() -> anyhow::Result<()> {
Ok(())
}

// Helper method to make a `GET` request
fn http_get() -> reqwest::Result<reqwest::blocking::Response> {
http_get_with_backoff_secs(1)
}

// Helper method to make a `GET` request
fn http_get_with_backoff_secs(backoff: u64) -> reqwest::Result<reqwest::blocking::Response> {
const MAX_ATTEMPTS: u32 = 10;
const BACKOFF_DURATION: Duration = Duration::from_secs(1);
let backoff_duration: Duration = Duration::from_secs(backoff);

let mut attempts = 0;

Expand All @@ -320,7 +353,7 @@ fn http_get() -> reqwest::Result<reqwest::blocking::Response> {
Ok(resp) => break Ok(resp),
Err(err) if attempts == MAX_ATTEMPTS => break Err(err),
Err(_) => {
std::thread::sleep(BACKOFF_DURATION);
std::thread::sleep(backoff_duration);
attempts += 1;
}
}
Expand Down

0 comments on commit 251575a

Please sign in to comment.