diff --git a/rootfs/etc/nginx/lua/plugins.lua b/rootfs/etc/nginx/lua/plugins.lua index 0c1fd899b8..55e208a328 100644 --- a/rootfs/etc/nginx/lua/plugins.lua +++ b/rootfs/etc/nginx/lua/plugins.lua @@ -1,6 +1,5 @@ local require = require local ngx = ngx -local pairs = pairs local ipairs = ipairs local string_format = string.format local ngx_log = ngx.log @@ -20,8 +19,11 @@ local function load_plugin(name) ngx_log(ERR, string_format("error loading plugin \"%s\": %s", path, plugin)) return end - - plugins[name] = plugin + local index = #plugins + if (plugin.name == nil or plugin.name == '') then + plugin.name = name + end + plugins[index + 1] = plugin end function _M.init(names) @@ -39,9 +41,9 @@ end function _M.run() local phase = ngx.get_phase() - for name, plugin in pairs(plugins) do + for _, plugin in ipairs(plugins) do if plugin[phase] then - ngx_log(INFO, string_format("running plugin \"%s\" in phase \"%s\"", name, phase)) + ngx_log(INFO, string_format("running plugin \"%s\" in phase \"%s\"", plugin.name, phase)) -- TODO: consider sandboxing this, should we? -- probably yes, at least prohibit plugin from accessing env vars etc @@ -50,7 +52,7 @@ function _M.run() local ok, err = pcall(plugin[phase]) if not ok then ngx_log(ERR, string_format("error while running plugin \"%s\" in phase \"%s\": %s", - name, phase, err)) + plugin.name, phase, err)) end end end diff --git a/rootfs/etc/nginx/lua/test/plugins_test.lua b/rootfs/etc/nginx/lua/test/plugins_test.lua new file mode 100644 index 0000000000..d7f789d0fe --- /dev/null +++ b/rootfs/etc/nginx/lua/test/plugins_test.lua @@ -0,0 +1,23 @@ +describe("plugins", function() + describe("#run", function() + it("runs the plugins in the given order", function() + ngx.get_phase = function() return "rewrite" end + local plugins = require("plugins") + local called_plugins = {} + local plugins_to_mock = {"plugins.pluginfirst.main", "plugins.pluginsecond.main", "plugins.pluginthird.main"} + for i=1, 3, 1 + do + package.loaded[plugins_to_mock[i]] = { + rewrite = function() + called_plugins[#called_plugins + 1] = plugins_to_mock[i] + end + } + end + assert.has_no.errors(function() + plugins.init({"pluginfirst", "pluginsecond", "pluginthird"}) + end) + assert.has_no.errors(plugins.run) + assert.are.same(plugins_to_mock, called_plugins) + end) + end) +end) \ No newline at end of file