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

tests(wasm): add filter config test case for PATCH endpoint #13840

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
69 changes: 66 additions & 3 deletions spec/02-integration/20-wasm/01-admin-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/proxy_wasm_filters/tests/src/test_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
_ => (),
}
}
Expand Down
10 changes: 10 additions & 0 deletions spec/fixtures/proxy_wasm_filters/tests/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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 {
Expand Down
Loading