Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔨 Populate the guest cli args #393

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you also want to add this for the ComponentCtx::create_store case as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this for components and enabled testing for it, but the adapter will need some changes to get the test passing. I'll put that on my TODO list for component parity, but don't think the adapter changes should block this PR.

})
.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/[email protected];
import wasi:io/[email protected];
import wasi:random/[email protected];
import wasi:cli/[email protected];
import wasi:cli/[email protected];
import wasi:cli/[email protected];
import wasi:cli/[email protected];
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");
}
Loading