-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(request-aware-table): integration
- Loading branch information
Showing
4 changed files
with
187 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
spec/02-integration/05-proxy/31-request-aware-table_spec.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
local helpers = require "spec.helpers" | ||
|
||
|
||
local function clear_table(client, checks) | ||
local res = client:get("/", { | ||
query = { | ||
checks = checks, | ||
clear = true, | ||
} | ||
}) | ||
assert.response(res).has.status(200) | ||
assert.logfile().has.no.line("[error]", true) | ||
end | ||
|
||
for _, checks in ipairs({ true, false }) do | ||
for _, strategy in helpers.each_strategy() do | ||
describe("request aware table tests [#" .. strategy .. "] .. checks=" .. tostring(checks), function() | ||
local client | ||
lazy_setup(function() | ||
local bp = helpers.get_db_utils(strategy, { | ||
"plugins", | ||
"routes", | ||
"services", | ||
}, { | ||
"request-aware-table" | ||
}) | ||
|
||
local service = assert(bp.services:insert({ | ||
url = helpers.mock_upstream_url | ||
})) | ||
|
||
local route = bp.routes:insert({ | ||
service = service, | ||
paths = { "/" } | ||
}) | ||
|
||
bp.plugins:insert({ | ||
name = "request-aware-table", | ||
route = { id = route.id }, | ||
}) | ||
|
||
helpers.start_kong({ | ||
database = strategy, | ||
plugins = "bundled, request-aware-table", | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
}) | ||
end) | ||
|
||
lazy_teardown(function() | ||
helpers.stop_kong() | ||
end) | ||
|
||
before_each(function() | ||
helpers.clean_logfile() | ||
client = helpers.proxy_client() | ||
clear_table(client, checks) | ||
end) | ||
|
||
after_each(function() | ||
if client then | ||
client:close() | ||
end | ||
end) | ||
|
||
describe("with concurrency check enabled", function() | ||
it("allows access when there are no race conditions", function() | ||
local res = client:get("/", { | ||
query = { | ||
checks = checks, | ||
} | ||
}) | ||
assert.response(res).has.status(200) | ||
assert.logfile().has.no.line("[error]", true) | ||
end) | ||
it("denies access when there are race conditions and checks are enabled", function() | ||
-- access from request 1 (don't clear) | ||
local ok, r = pcall(client.get, client, "/", { | ||
query = { | ||
checks = checks, | ||
}, | ||
}) | ||
assert(ok) | ||
assert.response(r).has.status(200) | ||
|
||
-- access from request 2 | ||
ok, r = pcall(client.get, client, "/", { | ||
query = { | ||
checks = checks, | ||
}, | ||
}) | ||
if checks then | ||
assert(not ok) | ||
assert.logfile().has.line("race condition detected", true) | ||
else | ||
assert(ok) | ||
assert.response(r).has.status(200) | ||
end | ||
end) | ||
it("allows access when table is cleared between requests", function() | ||
-- access from request 1 (clear) | ||
local r = client:get("/", { | ||
query = { | ||
checks = checks, | ||
clear = true, | ||
}, | ||
}) | ||
assert.response(r).has.status(200) | ||
|
||
-- access from request 2 | ||
r = client:get("/", { | ||
query = { | ||
checks = checks, | ||
}, | ||
}) | ||
assert.response(r).has.status(200) | ||
assert.logfile().has.no.line("[error]", true) | ||
end) | ||
end) | ||
end) | ||
end | ||
end |
52 changes: 52 additions & 0 deletions
52
spec/fixtures/custom_plugins/kong/plugins/request-aware-table/handler.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
local RAT = require "kong.tools.request_aware_table" | ||
|
||
local get_phase = ngx.get_phase | ||
local ngx = ngx | ||
local kong = kong | ||
|
||
|
||
local _M = { | ||
PRIORITY = 1001, | ||
VERSION = "1.0", | ||
} | ||
|
||
local checks_tab = RAT.new({}, "on") | ||
local no_checks_tab = RAT.new({}, "off") | ||
|
||
local function access_tables() | ||
local query = kong.request.get_query() | ||
local tab | ||
|
||
if query.checks ~= "false" then | ||
tab = checks_tab | ||
else | ||
tab = no_checks_tab | ||
end | ||
|
||
if query.clear == "true" and get_phase() == "access" then | ||
tab.clear() | ||
end | ||
|
||
-- write access | ||
tab.foo = "bar" | ||
-- read access | ||
ngx.log(ngx.DEBUG, "accessing to tab.foo" .. tab.foo) | ||
|
||
if query.clear == "true" and get_phase() == "body_filter" then | ||
tab.clear() | ||
end | ||
end | ||
|
||
function _M:access(conf) | ||
access_tables() | ||
end | ||
|
||
function _M:header_filter(conf) | ||
access_tables() | ||
end | ||
|
||
function _M:body_filter(conf) | ||
access_tables() | ||
end | ||
|
||
return _M |
11 changes: 11 additions & 0 deletions
11
spec/fixtures/custom_plugins/kong/plugins/request-aware-table/schema.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
return { | ||
name = "request-aware-table", | ||
fields = { | ||
{ | ||
config = { | ||
type = "record", | ||
fields = { } | ||
} | ||
} | ||
} | ||
} |