From fcfe8c8bb0f4887478c7d498e1c8e8a375a333a7 Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Wed, 6 Nov 2024 13:39:39 -0800 Subject: [PATCH 1/3] tests(wasm): add filter config test case for PATCH endpoint --- .../20-wasm/01-admin-api_spec.lua | 69 ++++++++++++++++++- .../proxy_wasm_filters/tests/src/test_http.rs | 9 +++ .../proxy_wasm_filters/tests/src/types.rs | 11 +++ 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/spec/02-integration/20-wasm/01-admin-api_spec.lua b/spec/02-integration/20-wasm/01-admin-api_spec.lua index 6ca6d75798c1..3b94a58ee63a 100644 --- a/spec/02-integration/20-wasm/01-admin-api_spec.lua +++ b/spec/02-integration/20-wasm/01-admin-api_spec.lua @@ -17,7 +17,7 @@ end for _, strategy in helpers.each_strategy({ "postgres" }) do describe("wasm admin API [#" .. strategy .. "]", function() - local admin + local admin, proxy local bp, db local service, route @@ -57,10 +57,13 @@ describe("wasm admin API [#" .. strategy .. "]", function() admin = helpers.admin_client() + proxy = helpers.proxy_client() + proxy.reopen = true end) lazy_teardown(function() if admin then admin:close() end + if proxy then proxy:close() end helpers.stop_kong() end) @@ -179,14 +182,23 @@ describe("wasm admin API [#" .. strategy .. "]", function() describe("PATCH", function() local chain + local host - lazy_setup(function() + before_each(function() + host = "wasm-" .. uuid.uuid() .. ".test" chain = bp.filter_chains:insert({ - service = assert(bp.services:insert({})), + route = assert(bp.routes:insert({ + hosts = { host }, + })), filters = { { name = "tests" } }, }, { nulls = true }) end) + after_each(function() + admin:delete("/filter-chains/" .. chain.id) + admin:delete("/routes/" .. chain.route.id) + end) + it("updates a filter chain in-place", function() assert.equals(ngx.null, chain.tags) assert.is_true(chain.enabled) @@ -211,6 +223,57 @@ describe("wasm admin API [#" .. strategy .. "]", function() assert.same({ name = "tests", config = "456", enabled = false }, patched.filters[2]) end) + + it("updates filter configuration #only", function() + helpers.wait_for_all_config_update() + + local function await_filter_response(body, context) + assert.eventually(function() + local res = proxy:get("/", { + headers = { + ["Host"] = host, + ["X-PW-Test"] = "dump_config", + } + }) + + local got = res:read_body() + -- something is adding a trailing newline to the response body + got = got and got:gsub("%s*$", "") + + return res.status == 200 and body == got, + { status = res.status, body = got } + end) + .is_truthy("(" .. context .. ") expected 200 response code and " + .. "body == '" .. body .. "'") + end + + local function update_filters(filters) + local res = admin:patch("/filter-chains/" .. chain[key], json { + filters = filters, + }) + assert.response(res).has.status(200) + end + + local function update_config(config) + update_filters({ + { name = "tests", config = config }, + }) + end + + await_filter_response("", "after setup") + + update_config("a=1 b=2") + await_filter_response("a=1 b=2", "after adding filter config") + + update_config("a=1 b=2 c=3") + await_filter_response("a=1 b=2 c=3", "after updating filter config") + + update_filters({ + { name = "tests", config = "a=1 b=2 c=3", enabled = false }, + { name = "tests", config = "foo=bar" }, + }) + await_filter_response("foo=bar", "after updating the filter chain") + end) end) describe("DELETE", function() diff --git a/spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs b/spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs index 38d78be5f2ba..609837f6fb5b 100644 --- a/spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs +++ b/spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs @@ -137,6 +137,15 @@ impl TestHttp { return self.send_http_dispatch(config); } + "dump_config" => { + let res = self + .config + .as_ref() + .map(|config| config.to_string()) + .unwrap_or_default(); + log::warn!("config: '{res}'"); + self.send_plain_response(StatusCode::OK, Some(&res)) + } _ => (), } } diff --git a/spec/fixtures/proxy_wasm_filters/tests/src/types.rs b/spec/fixtures/proxy_wasm_filters/tests/src/types.rs index 29f4d86a50fc..f9f39ef9ae13 100644 --- a/spec/fixtures/proxy_wasm_filters/tests/src/types.rs +++ b/spec/fixtures/proxy_wasm_filters/tests/src/types.rs @@ -1,6 +1,7 @@ use crate::*; use std::collections::HashMap; +#[derive(Default)] pub struct TestConfig { pub map: HashMap, } @@ -19,6 +20,16 @@ impl FromStr for TestConfig { } } +impl std::fmt::Display for TestConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut values: Vec = self.map.iter().map(|(k, v)| format!("{k}={v}")).collect(); + + values.sort(); + + write!(f, "{}", values.join(" ")) + } +} + #[derive(Debug, Eq, PartialEq, enum_utils::FromStr)] #[enumeration(rename_all = "snake_case")] pub enum TestPhase { From 37d7eafa571f0846238c78ed02d5e3391232be93 Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Wed, 6 Nov 2024 13:41:50 -0800 Subject: [PATCH 2/3] remove #only tag --- spec/02-integration/20-wasm/01-admin-api_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/02-integration/20-wasm/01-admin-api_spec.lua b/spec/02-integration/20-wasm/01-admin-api_spec.lua index 3b94a58ee63a..9d4297ec6c01 100644 --- a/spec/02-integration/20-wasm/01-admin-api_spec.lua +++ b/spec/02-integration/20-wasm/01-admin-api_spec.lua @@ -224,7 +224,7 @@ describe("wasm admin API [#" .. strategy .. "]", function() patched.filters[2]) end) - it("updates filter configuration #only", function() + it("updates filter configuration", function() helpers.wait_for_all_config_update() local function await_filter_response(body, context) From 6de20f9000b4d149aadc32fa5ff62b5624c4d434 Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Wed, 6 Nov 2024 13:46:44 -0800 Subject: [PATCH 3/3] simplify filter changes --- spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs | 9 ++------- spec/fixtures/proxy_wasm_filters/tests/src/types.rs | 1 - 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs b/spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs index 609837f6fb5b..9ad17e3c874e 100644 --- a/spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs +++ b/spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs @@ -138,13 +138,8 @@ impl TestHttp { return self.send_http_dispatch(config); } "dump_config" => { - let res = self - .config - .as_ref() - .map(|config| config.to_string()) - .unwrap_or_default(); - log::warn!("config: '{res}'"); - self.send_plain_response(StatusCode::OK, Some(&res)) + let res = self.config.as_ref().map(|config| config.to_string()); + self.send_plain_response(StatusCode::OK, res.as_deref()); } _ => (), } diff --git a/spec/fixtures/proxy_wasm_filters/tests/src/types.rs b/spec/fixtures/proxy_wasm_filters/tests/src/types.rs index f9f39ef9ae13..a6a585f7b57c 100644 --- a/spec/fixtures/proxy_wasm_filters/tests/src/types.rs +++ b/spec/fixtures/proxy_wasm_filters/tests/src/types.rs @@ -1,7 +1,6 @@ use crate::*; use std::collections::HashMap; -#[derive(Default)] pub struct TestConfig { pub map: HashMap, }