Skip to content

Commit

Permalink
🔨 Populate the guest cli args (fastly#393)
Browse files Browse the repository at this point in the history
Populate the guest cli args with the single value "compute-app". This should simplify adoption for languages
that assume the args list is non-empty.

Fixes fastly#376
  • Loading branch information
elliottt authored and GeeWee committed Jul 25, 2024
1 parent 832f866 commit 61c7cec
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
46 changes: 46 additions & 0 deletions cli/tests/integration/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::common::{Test, TestResult};
use hyper::{body::to_bytes, StatusCode};

/// Run a program that tests its args. This checks that we're populating the argument list with the
/// singleton "compute-app" value.
/// Check that an empty response is sent downstream by default.
///
/// `args.wasm` is a guest program checks its cli args.
#[tokio::test(flavor = "multi_thread")]
async fn empty_ok_response_by_default_after_args() -> TestResult {
let resp = Test::using_fixture("args.wasm").against_empty().await?;

assert_eq!(resp.status(), StatusCode::OK);
assert!(to_bytes(resp.into_body())
.await
.expect("can read body")
.to_vec()
.is_empty());

Ok(())
}

/// Run a program that tests its args. This checks that we're populating the argument list with the
/// singleton "compute-app" value.
/// Check that an empty response is sent downstream by default.
///
/// `args.wasm` is a guest program checks its cli args.
#[tokio::test(flavor = "multi_thread")]
// TODO: The adapter needs to plumb through support for argument handling. This was removed
// explicitly when we thought we would target the proxy world only, but we'll need it back to
// simplify adapting programs from languages that need a non-empty args list.
#[should_panic]
async fn empty_ok_response_by_default_after_args_component() {
let resp = Test::using_fixture("args.wasm")
.adapt_component()
.against_empty()
.await
.unwrap();

assert_eq!(resp.status(), StatusCode::OK);
assert!(to_bytes(resp.into_body())
.await
.expect("can read body")
.to_vec()
.is_empty());
}
1 change: 1 addition & 0 deletions cli/tests/integration/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod args;
mod async_io;
mod body;
mod client_certs;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/component/http_resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use {
cfg_if::cfg_if,
http::{HeaderName, HeaderValue},
hyper::http::response::Response,
std::str::FromStr,
};

const MAX_HEADER_NAME_LEN: usize = (1 << 16) - 1;
Expand Down Expand Up @@ -137,6 +136,7 @@ impl http_resp::Host for Session {
let _ = (h, name, max_len, cursor);
return Err(Error::FatalError("A fatal error occurred in the test-only implementation of header_values_get".to_string()).into());
} else {
use std::str::FromStr;
if name.len() > MAX_HEADER_NAME_LEN {
return Err(Error::InvalidArgument.into());
}
Expand Down
12 changes: 8 additions & 4 deletions lib/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,10 @@ impl ExecuteCtx {
let req = session.downstream_request();
let body = session.downstream_request_body();

let mut store = ComponentCtx::create_store(&self, session, None, |_| {})
.map_err(ExecutionError::Context)?;
let mut store = ComponentCtx::create_store(&self, session, None, |ctx| {
ctx.arg("compute-app");
})
.map_err(ExecutionError::Context)?;

let (compute, _instance) =
compute::Compute::instantiate_pre(&mut store, instance_pre)
Expand Down Expand Up @@ -511,8 +513,10 @@ impl ExecuteCtx {
// due to wasmtime limitations, in particular the fact that `Instance` is not `Send`.
// However, the fact that the module itself is created within `ExecuteCtx::new`
// means that the heavy lifting happens only once.
let mut store = create_store(&self, session, profiler, |_| {})
.map_err(ExecutionError::Context)?;
let mut store = create_store(&self, session, profiler, |ctx| {
ctx.arg("compute-app");
})
.map_err(ExecutionError::Context)?;

let instance = instance_pre
.instantiate_async(&mut store)
Expand Down
1 change: 1 addition & 0 deletions lib/wit/deps/fastly/compute.wit
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ world compute {
import wasi:io/error@0.2.0;
import wasi:io/streams@0.2.0;
import wasi:random/random@0.2.0;
import wasi:cli/environment@0.2.0;
import wasi:cli/stdout@0.2.0;
import wasi:cli/stderr@0.2.0;
import wasi:cli/stdin@0.2.0;
Expand Down
6 changes: 6 additions & 0 deletions test-fixtures/src/bin/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
let args = Vec::from_iter(std::env::args());

assert_eq!(args.len(), 1);
assert_eq!(args[0], "compute-app");
}

0 comments on commit 61c7cec

Please sign in to comment.