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..9d4297ec6c01 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", 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..9ad17e3c874e 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,10 @@ impl TestHttp { return self.send_http_dispatch(config); } + "dump_config" => { + 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 29f4d86a50fc..a6a585f7b57c 100644 --- a/spec/fixtures/proxy_wasm_filters/tests/src/types.rs +++ b/spec/fixtures/proxy_wasm_filters/tests/src/types.rs @@ -19,6 +19,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 {