From 9238d4a611d9e8574f5264d977c7f7d4fa3cb2cf Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Tue, 12 Nov 2024 09:54:16 -0800 Subject: [PATCH] tests(lib) catch possible 'dispatch_http_call' failures --- t/lib/Cargo.lock | 1 + .../context-checks/src/lib.rs | 14 ++++++++++++-- t/lib/proxy-wasm-tests/hostcalls/src/lib.rs | 15 ++++++++++++--- .../hostcalls/src/types/test_http.rs | 15 ++++++++++++--- .../rust-sdk-ver-zero-one/Cargo.toml | 1 + .../rust-sdk-ver-zero-one/src/filter.rs | 19 ++++++++++++++++--- 6 files changed, 54 insertions(+), 11 deletions(-) diff --git a/t/lib/Cargo.lock b/t/lib/Cargo.lock index c1fe84706..4e418ff3c 100644 --- a/t/lib/Cargo.lock +++ b/t/lib/Cargo.lock @@ -369,6 +369,7 @@ name = "rust-sdk-ver-zero-one" version = "0.1.0" dependencies = [ "http", + "log", "proxy-wasm 0.1.4", ] diff --git a/t/lib/proxy-wasm-tests/context-checks/src/lib.rs b/t/lib/proxy-wasm-tests/context-checks/src/lib.rs index eea6eae50..1d63167e6 100644 --- a/t/lib/proxy-wasm-tests/context-checks/src/lib.rs +++ b/t/lib/proxy-wasm-tests/context-checks/src/lib.rs @@ -69,13 +69,23 @@ impl HttpContext for TestHttp { .get_config("on_http_dispatch_response") .is_some() { - let _ = self.dispatch_http_call( + match self.dispatch_http_call( self.tester.get_config("host").unwrap_or(""), vec![(":path", "/dispatch"), (":method", "GET")], None, vec![], Duration::from_secs(0), - ); + ) { + Ok(_) => {} + Err(status) => match status { + Status::BadArgument => error!("dispatch_http_call: bad argument"), + Status::InternalFailure => error!("dispatch_http_call: internal failure"), + status => panic!( + "dispatch_http_call: unexpected status \"{}\"", + status as u32 + ), + }, + } return Action::Pause; } diff --git a/t/lib/proxy-wasm-tests/hostcalls/src/lib.rs b/t/lib/proxy-wasm-tests/hostcalls/src/lib.rs index aaf25142d..95f2a3e44 100644 --- a/t/lib/proxy-wasm-tests/hostcalls/src/lib.rs +++ b/t/lib/proxy-wasm-tests/hostcalls/src/lib.rs @@ -156,14 +156,23 @@ impl RootContext for TestRoot { info!("call {}", self.n_sync_calls); - self.dispatch_http_call( + match self.dispatch_http_call( self.get_config("host").unwrap_or(""), headers, self.get_config("body").map(|v| v.as_bytes()), vec![], timeout, - ) - .unwrap(); + ) { + Ok(_) => {} + Err(status) => match status { + Status::BadArgument => error!("dispatch_http_call: bad argument"), + Status::InternalFailure => error!("dispatch_http_call: internal failure"), + status => panic!( + "dispatch_http_call: unexpected status \"{}\"", + status as u32 + ), + }, + } } _ => (), } diff --git a/t/lib/proxy-wasm-tests/hostcalls/src/types/test_http.rs b/t/lib/proxy-wasm-tests/hostcalls/src/types/test_http.rs index 5240f512e..6601ea288 100644 --- a/t/lib/proxy-wasm-tests/hostcalls/src/types/test_http.rs +++ b/t/lib/proxy-wasm-tests/hostcalls/src/types/test_http.rs @@ -247,13 +247,22 @@ impl TestHttp { None => self.get_config("host").unwrap_or(""), }; - self.dispatch_http_call( + match self.dispatch_http_call( host, headers, self.get_config("body").map(|v| v.as_bytes()), vec![], timeout, - ) - .unwrap(); + ) { + Ok(_) => {} + Err(status) => match status { + Status::BadArgument => error!("dispatch_http_call: bad argument"), + Status::InternalFailure => error!("dispatch_http_call: internal failure"), + status => panic!( + "dispatch_http_call: unexpected status \"{}\"", + status as u32 + ), + }, + } } } diff --git a/t/lib/proxy-wasm-tests/rust-sdk-ver-zero-one/Cargo.toml b/t/lib/proxy-wasm-tests/rust-sdk-ver-zero-one/Cargo.toml index 887a8b326..e9a4925a8 100644 --- a/t/lib/proxy-wasm-tests/rust-sdk-ver-zero-one/Cargo.toml +++ b/t/lib/proxy-wasm-tests/rust-sdk-ver-zero-one/Cargo.toml @@ -11,3 +11,4 @@ crate-type = ["cdylib"] [dependencies] proxy-wasm = "0.1" http = "0.2" +log = "0.4" diff --git a/t/lib/proxy-wasm-tests/rust-sdk-ver-zero-one/src/filter.rs b/t/lib/proxy-wasm-tests/rust-sdk-ver-zero-one/src/filter.rs index a8a63abd3..fcc7379f7 100644 --- a/t/lib/proxy-wasm-tests/rust-sdk-ver-zero-one/src/filter.rs +++ b/t/lib/proxy-wasm-tests/rust-sdk-ver-zero-one/src/filter.rs @@ -1,6 +1,7 @@ #![allow(clippy::single_match)] use http::StatusCode; +use log::*; use proxy_wasm::{traits::*, types::*}; use std::collections::HashMap; use std::time::Duration; @@ -88,7 +89,7 @@ impl HttpContext for TestHttpHostcalls { if let Some(test) = self.config.get("test") { match test.as_str() { "dispatch" => { - self.dispatch_http_call( + match self.dispatch_http_call( self.config.get("host").map(|v| v.as_str()).unwrap_or(""), vec![ (":method", "GET"), @@ -104,8 +105,20 @@ impl HttpContext for TestHttpHostcalls { self.config.get("body").map(|v| v.as_bytes()), vec![], Duration::from_secs(3), - ) - .unwrap(); + ) { + Ok(_) => {} + Err(status) => match status { + Status::BadArgument => error!("dispatch_http_call: bad argument"), + Status::InternalFailure => { + error!("dispatch_http_call: internal failure") + } + status => panic!( + "dispatch_http_call: unexpected status \"{}\"", + status as u32 + ), + }, + } + return Action::Pause; } "proxy_get_header_map_value" => {