Skip to content

Commit

Permalink
Fix testing examples. (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps authored Aug 10, 2023
1 parent 9fbb3f8 commit 1dca120
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 47 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ jobs:
with:
repository: proxy-wasm/proxy-wasm-rust-sdk
path: proxy-wasm-rust-sdk
ref: v0.2.1

- name: Update Rust
run: |
Expand Down Expand Up @@ -174,16 +175,19 @@ jobs:
- name: Build (Rust SDK examples)
env:
RUSTFLAGS: -C link-args=-S -D warnings
run: cd proxy-wasm-rust-sdk && cargo build --release --examples --target=wasm32-unknown-unknown && cd ..
run: |
cd proxy-wasm-rust-sdk/examples/hello_world && cargo build --target wasm32-unknown-unknown --release && cd ../../..
cd proxy-wasm-rust-sdk/examples/http_auth_random && cargo build --target wasm32-unknown-unknown --release && cd ../../..
cd proxy-wasm-rust-sdk/examples/http_headers && cargo build --target wasm32-unknown-unknown --release && cd ../../..
- name: Test (hello_world)
run: target/release/examples/hello_world proxy-wasm-rust-sdk/target/wasm32-unknown-unknown/release/examples/hello_world.wasm
run: target/release/examples/hello_world proxy-wasm-rust-sdk/examples/hello_world/target/wasm32-unknown-unknown/release/proxy_wasm_example_hello_world.wasm

- name: Test (http_auth_random)
run: target/release/examples/http_auth_random proxy-wasm-rust-sdk/target/wasm32-unknown-unknown/release/examples/http_auth_random.wasm -a
run: target/release/examples/http_auth_random proxy-wasm-rust-sdk/examples/http_auth_random/target/wasm32-unknown-unknown/release/proxy_wasm_example_http_auth_random.wasm -a

- name: Test (http_headers)
run: target/release/examples/http_headers proxy-wasm-rust-sdk/target/wasm32-unknown-unknown/release/examples/http_headers.wasm -a
run: target/release/examples/http_headers proxy-wasm-rust-sdk/examples/http_headers/target/wasm32-unknown-unknown/release/proxy_wasm_example_http_headers.wasm -a

outdated:
runs-on: ubuntu-latest
Expand Down
5 changes: 4 additions & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ fn main() -> Result<()> {
.call_proxy_on_tick(root_context)
.expect_get_current_time_nanos()
.returning(Some(0 * 10u64.pow(9)))
.expect_log(Some(LogLevel::Info), Some("It's 1970-01-01 00:00:00 UTC"))
.expect_log(
Some(LogLevel::Info),
Some("It's 1970-01-01 00:00:00 UTC, there is no lucky number."),
)
.execute_and_expect(ReturnType::None)?;

hello_world_test
Expand Down
6 changes: 3 additions & 3 deletions examples/http_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ fn main() -> Result<()> {
.call_proxy_on_response_headers(http_context, 0, false)
.expect_get_header_map_pairs(Some(MapType::HttpResponseHeaders))
.returning(Some(vec![(":status", "200"), ("Powered-By", "proxy-wasm")]))
.expect_log(Some(LogLevel::Trace), Some("#2 <- :status: 200"))
.expect_log(Some(LogLevel::Trace), Some("#2 <- Powered-By: proxy-wasm"))
.expect_log(Some(LogLevel::Info), Some("#2 <- :status: 200"))
.expect_log(Some(LogLevel::Info), Some("#2 <- Powered-By: proxy-wasm"))
.execute_and_expect(ReturnType::Action(Action::Continue))?;

http_headers_test
.call_proxy_on_log(http_context)
.expect_log(Some(LogLevel::Trace), Some("#2 completed."))
.expect_log(Some(LogLevel::Info), Some("#2 completed."))
.execute_and_expect(ReturnType::None)?;

return Ok(());
Expand Down
80 changes: 47 additions & 33 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ pub fn get_status() -> ExpectStatus {
pub fn get_abi_version(module: &Module) -> AbiVersion {
if module.get_export("proxy_abi_version_0_1_0").is_some() {
AbiVersion::ProxyAbiVersion0_1_0
} else if module.get_export("proxy_abi_version_0_2_0").is_some() {
} else if module.get_export("proxy_abi_version_0_2_0").is_some()
|| module.get_export("proxy_abi_version_0_2_1").is_some()
{
AbiVersion::ProxyAbiVersion0_2_0
} else {
panic!("Error: test-framework does not support proxy-wasm modules of this abi version");
Expand Down Expand Up @@ -320,41 +322,53 @@ fn get_hostfunc(

/* ---------------------------------- Continue/Close/Reply/Route ---------------------------------- */
"proxy_continue_stream" => {
Some(Func::wrap(store, |_caller: Caller<'_, ()>| -> i32 {
// Default Function:
// Expectation:
assert_eq!(
HOST.lock().unwrap().staged.get_abi_version(),
AbiVersion::ProxyAbiVersion0_2_0
);
println!(
"[vm->host] proxy_continue_stream() status: {:?}",
get_status()
);
println!(
"[vm<-host] proxy_continue_stream() return: {:?}",
Status::Ok
);
assert_ne!(get_status(), ExpectStatus::Failed);
set_status(ExpectStatus::Unexpected);
return Status::Ok as i32;
}))
Some(Func::wrap(
store,
|_caller: Caller<'_, ()>, stream_type: i32| -> i32 {
// Default Function:
// Expectation:
assert_eq!(
HOST.lock().unwrap().staged.get_abi_version(),
AbiVersion::ProxyAbiVersion0_2_0
);
println!(
"[vm->host] proxy_continue_stream(stream_type={stream_type}) status: {:?}",
get_status()
);
println!(
"[vm<-host] proxy_continue_stream(...) return: {:?}",
Status::Ok
);
assert_ne!(get_status(), ExpectStatus::Failed);
set_status(ExpectStatus::Unexpected);
return Status::Ok as i32;
},
))
}

"proxy_close_stream" => {
Some(Func::wrap(store, |_caller: Caller<'_, ()>| -> i32 {
// Default Function:
// Expectation:
assert_eq!(
HOST.lock().unwrap().staged.get_abi_version(),
AbiVersion::ProxyAbiVersion0_2_0
);
println!("[vm->host] proxy_close_stream() status: {:?}", get_status());
println!("[vm<-host] proxy_close_stream() return: {:?}", Status::Ok);
assert_ne!(get_status(), ExpectStatus::Failed);
set_status(ExpectStatus::Unexpected);
return Status::Ok as i32;
}))
Some(Func::wrap(
store,
|_caller: Caller<'_, ()>, stream_type: i32| -> i32 {
// Default Function:
// Expectation:
assert_eq!(
HOST.lock().unwrap().staged.get_abi_version(),
AbiVersion::ProxyAbiVersion0_2_0
);
println!(
"[vm->host] proxy_close_stream(stream_type={stream_type}) status: {:?}",
get_status()
);
println!(
"[vm<-host] proxy_close_stream(...) return: {:?}",
Status::Ok
);
assert_ne!(get_status(), ExpectStatus::Failed);
set_status(ExpectStatus::Unexpected);
return Status::Ok as i32;
},
))
}

"proxy_continue_request" => {
Expand Down
24 changes: 18 additions & 6 deletions src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,25 @@ impl Tester {
let mut return_wasm: Option<i32> = None;
match self.function_call.remove(0) {
FunctionCall::Start() => {
println!("[host->vm] _start()");
self.instance
.get_typed_func::<(), ()>(&mut self.store, "_start")
let (name, func) = self
.instance
.get_typed_func::<(), ()>(&mut self.store, "_initialize")
.map(|func| ("_initialize", func))
.or_else(|_| {
self.instance
.get_typed_func::<(), ()>(&mut self.store, "main")
.map(|func| ("main", func))
})
.or_else(|_| {
self.instance
.get_typed_func::<(), ()>(&mut self.store, "_start")
.map(|func| ("_start", func))
})
.or(Err(anyhow::format_err!(
"Error: failed to find `_start` function export"
)))?
.call(&mut self.store, ())?;
"Error: failed to find `_initialize`, `main` or `_start` function export"
)))?;
println!("[host->vm] {name}()");
func.call(&mut self.store, ())?;
}

FunctionCall::ProxyOnVmStart(context_id, vm_configuration_size) => {
Expand Down

0 comments on commit 1dca120

Please sign in to comment.