Skip to content

Commit

Permalink
add test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
moonming committed Aug 25, 2019
1 parent b13d7f6 commit c816b7c
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 6 deletions.
2 changes: 2 additions & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ plugins: # plugin list
- zipkin
- ip-restriction
- grpc-transcode
- serverless-pre-function
- serverless-post-function
1 change: 1 addition & 0 deletions lua/apisix/plugins/serverless-post-function.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require("apisix.plugins.serverless")("serverless-post-function", -2000)
2 changes: 1 addition & 1 deletion lua/apisix/plugins/serverless-pre-function.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
return require("apisix.plugins.serverless")("serverless-pre-function", math.huge)
return require("apisix.plugins.serverless")("serverless-pre-function", 10000)
65 changes: 63 additions & 2 deletions lua/apisix/plugins/serverless.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ return function(plugin_name, priority)
local schema = {
type = "object",
properties = {
functions = {type = "array", items = {type = "string"}, minItems = 1},
phase = {
type = "string",
-- the default phase is access
enum = {"rewrite", "access", "header_filer", "body_filter",
"log", "balancer"}
},
functions = {
type = "array",
items = {type = "string"},
minItems = 1
},
},
required = {"functions"}
}
Expand All @@ -15,16 +25,67 @@ return function(plugin_name, priority)
name = plugin_name,
}

local function load_funcs(functions)
local funcs = core.table.new(#functions, 0)

local index = 1
for _, func_str in ipairs(functions) do
local _, func = pcall(loadstring(func_str))
funcs[index] = func
index = index + 1
end

return funcs
end

local function call_funcs(phase, conf, ctx)
if phase ~= conf.phase then
return
end

local functions = core.lrucache.plugin_ctx(plugin_name, ctx,
load_funcs, conf.functions)

for _, func in ipairs(functions) do
func()
end
end

function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end

if not conf.phase then
conf.phase = 'access'
end

return true
end

function _M.rewrite(conf, ctx)
call_funcs('rewrite', conf, ctx)
end

function _M.access(conf, ctx)
local functions = conf.functions
call_funcs('access', conf, ctx)
end

function _M.balancer(conf, ctx)
call_funcs('balancer', conf, ctx)
end

function _M.header_filer(conf, ctx)
call_funcs('header_filer', conf, ctx)
end

function _M.body_filter(conf, ctx)
call_funcs('body_filter', conf, ctx)
end

function _M.log(conf, ctx)
call_funcs('log', conf, ctx)
end

return _M
Expand Down
2 changes: 1 addition & 1 deletion lua/apisix/plugins/zipkin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ local schema = {

local _M = {
version = 0.1,
priority = -1000, -- last running plugin
priority = -1000, -- last running plugin, but before serverless post func
name = plugin_name,
schema = schema,
}
Expand Down
2 changes: 1 addition & 1 deletion t/admin/plugins.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ __DATA__
--- request
GET /apisix/admin/plugins/list
--- response_body_like eval
qr/\["limit-req","limit-count","limit-conn","key-auth","prometheus","node-status","jwt-auth","zipkin","ip-restriction","grpc-transcode"\]/
qr/\["limit-req","limit-count","limit-conn","key-auth","prometheus","node-status","jwt-auth","zipkin","ip-restriction","grpc-transcode","serverless-pre-function","serverless-post-function"\]/
--- no_error_log
[error]
2 changes: 2 additions & 0 deletions t/debug-mode.t
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ done
--- grep_error_log eval
qr/loaded plugin and sort by priority: [-\d]+ name: [\w-]+/
--- grep_error_log_out
loaded plugin and sort by priority: 10000 name: serverless-pre-function
loaded plugin and sort by priority: 3000 name: ip-restriction
loaded plugin and sort by priority: 2510 name: jwt-auth
loaded plugin and sort by priority: 2500 name: key-auth
Expand All @@ -50,6 +51,7 @@ loaded plugin and sort by priority: 506 name: grpc-transcode
loaded plugin and sort by priority: 500 name: prometheus
loaded plugin and sort by priority: 0 name: example-plugin
loaded plugin and sort by priority: -1000 name: zipkin
loaded plugin and sort by priority: -2000 name: serverless-post-function



Expand Down
Loading

0 comments on commit c816b7c

Please sign in to comment.