From 14cdbc711bb86baeb6d0b36ba373333fbcbfa9a8 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 12:58:00 +0800 Subject: [PATCH 01/13] tests(helpers): separate misc functions --- spec/details/misc.lua | 186 ++++++++++++++++++++++++++++++++++++++++++ spec/helpers.lua | 170 +++----------------------------------- 2 files changed, 196 insertions(+), 160 deletions(-) create mode 100644 spec/details/misc.lua diff --git a/spec/details/misc.lua b/spec/details/misc.lua new file mode 100644 index 000000000000..5efa6477427c --- /dev/null +++ b/spec/details/misc.lua @@ -0,0 +1,186 @@ +-- miscellaneous + +local ffi = require("ffi") +local pl_path = require("pl.path") +local shell = require("resty.shell") +local nginx_signals = require("kong.cmd.utils.nginx_signals") +local CONSTANTS = require("spec.details.constants") + + +ffi.cdef [[ + int setenv(const char *name, const char *value, int overwrite); + int unsetenv(const char *name); +]] + + +local pack = function(...) return { n = select("#", ...), ... } end +local unpack = function(t) return unpack(t, 1, t.n) end + + +--- Prints all returned parameters. +-- Simple debugging aid, it will pass all received parameters, hence will not +-- influence the flow of the code. See also `fail`. +-- @function intercept +-- @see fail +-- @usage -- modify +-- local a,b = some_func(c,d) +-- -- into +-- local a,b = intercept(some_func(c,d)) +local function intercept(...) + local args = pack(...) + print(require("pl.pretty").write(args)) + return unpack(args) +end + + +--- Returns the OpenResty version. +-- Extract the current OpenResty version in use and returns +-- a numerical representation of it. +-- Ex: `1.11.2.2` -> `11122` +-- @function openresty_ver_num +local function openresty_ver_num() + local nginx_bin = assert(nginx_signals.find_nginx_bin()) + local _, _, stderr = shell.run(string.format("%s -V", nginx_bin), nil, 0) + + local a, b, c, d = string.match(stderr or "", "openresty/(%d+)%.(%d+)%.(%d+)%.(%d+)") + if not a then + error("could not execute 'nginx -V': " .. stderr) + end + + return tonumber(a .. b .. c .. d) +end + + +--- Unindent a multi-line string for proper indenting in +-- square brackets. +-- @function unindent +-- @usage +-- local u = helpers.unindent +-- +-- u[[ +-- hello world +-- foo bar +-- ]] +-- +-- -- will return: "hello world\nfoo bar" +local function unindent(str, concat_newlines, spaced_newlines) + str = string.match(str, "(.-%S*)%s*$") + if not str then + return "" + end + + local level = math.huge + local prefix = "" + local len + + str = str:match("^%s") and "\n" .. str or str + for pref in str:gmatch("\n(%s+)") do + len = #prefix + + if len < level then + level = len + prefix = pref + end + end + + local repl = concat_newlines and "" or "\n" + repl = spaced_newlines and " " or repl + + return (str:gsub("^\n%s*", ""):gsub("\n" .. prefix, repl):gsub("\n$", ""):gsub("\\r", "\r")) +end + + +--- Write a yaml file. +-- @function make_yaml_file +-- @param content (string) the yaml string to write to the file, if omitted the +-- current database contents will be written using `kong config db_export`. +-- @param filename (optional) if not provided, a temp name will be created +-- @return filename of the file written +local function make_yaml_file(content, filename) + local filename = filename or pl_path.tmpname() .. ".yml" + if content then + local fd = assert(io.open(filename, "w")) + assert(fd:write(unindent(content))) + assert(fd:write("\n")) -- ensure last line ends in newline + assert(fd:close()) + else + assert(kong_exec("config db_export --conf "..CONSTANTS.TEST_CONF_PATH.." "..filename)) + end + return filename +end + + +--- Set an environment variable +-- @function setenv +-- @param env (string) name of the environment variable +-- @param value the value to set +-- @return true on success, false otherwise +local function setenv(env, value) + return ffi.C.setenv(env, value, 1) == 0 +end + + +--- Unset an environment variable +-- @function unsetenv +-- @param env (string) name of the environment variable +-- @return true on success, false otherwise +local function unsetenv(env) + return ffi.C.unsetenv(env) == 0 +end + + +local deep_sort +do + local function deep_compare(a, b) + if a == nil then + a = "" + end + + if b == nil then + b = "" + end + + deep_sort(a) + deep_sort(b) + + if type(a) ~= type(b) then + return type(a) < type(b) + end + + if type(a) == "table" then + return deep_compare(a[1], b[1]) + end + + -- compare cjson.null or ngx.null + if type(a) == "userdata" and type(b) == "userdata" then + return false + end + + return a < b + end + + deep_sort = function(t) + if type(t) == "table" then + for _, v in pairs(t) do + deep_sort(v) + end + table.sort(t, deep_compare) + end + + return t + end +end + + +return { + pack = pack, + unpack = unpack, + + intercept = intercept, + openresty_ver_num = openresty_ver_num(), + unindent = unindent, + make_yaml_file = make_yaml_file, + setenv = setenv, + unsetenv = unsetenv, + deep_sort = deep_sort, +} diff --git a/spec/helpers.lua b/spec/helpers.lua index 13fdcdc65e93..0ede42d1178c 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -6,6 +6,7 @@ -- @module spec.helpers local CONSTANTS = require("spec.details.constants") +local misc = require("spec.details.misc") local PLUGINS_LIST @@ -36,7 +37,6 @@ local nginx_signals = require "kong.cmd.utils.nginx_signals" local log = require "kong.cmd.utils.log" local DB = require "kong.db" local shell = require "resty.shell" -local ffi = require "ffi" local ssl = require "ngx.ssl" local ws_client = require "resty.websocket.client" local table_clone = require "table.clone" @@ -50,10 +50,15 @@ local colors = require "ansicolors" local strip = require("kong.tools.string").strip local splitlines = require("pl.stringx").splitlines -ffi.cdef [[ - int setenv(const char *name, const char *value, int overwrite); - int unsetenv(const char *name); -]] + +local unpack = misc.unpack +local intercept = misc.intercept +local openresty_ver_num = misc.openresty_ver_num() +local unindent = misc.unindent +local make_yaml_file = misc.make_yaml_file +local setenv = misc.setenv +local unsetenv = misc.unsetenv +local deep_sort = misc.deep_sort local kong_exec -- forward declaration @@ -71,101 +76,6 @@ do package.path = table.concat(paths, ";") end ---- Returns the OpenResty version. --- Extract the current OpenResty version in use and returns --- a numerical representation of it. --- Ex: `1.11.2.2` -> `11122` --- @function openresty_ver_num -local function openresty_ver_num() - local nginx_bin = assert(nginx_signals.find_nginx_bin()) - local _, _, stderr = shell.run(string.format("%s -V", nginx_bin), nil, 0) - - local a, b, c, d = string.match(stderr or "", "openresty/(%d+)%.(%d+)%.(%d+)%.(%d+)") - if not a then - error("could not execute 'nginx -V': " .. stderr) - end - - return tonumber(a .. b .. c .. d) -end - ---- Unindent a multi-line string for proper indenting in --- square brackets. --- @function unindent --- @usage --- local u = helpers.unindent --- --- u[[ --- hello world --- foo bar --- ]] --- --- -- will return: "hello world\nfoo bar" -local function unindent(str, concat_newlines, spaced_newlines) - str = string.match(str, "(.-%S*)%s*$") - if not str then - return "" - end - - local level = math.huge - local prefix = "" - local len - - str = str:match("^%s") and "\n" .. str or str - for pref in str:gmatch("\n(%s+)") do - len = #prefix - - if len < level then - level = len - prefix = pref - end - end - - local repl = concat_newlines and "" or "\n" - repl = spaced_newlines and " " or repl - - return (str:gsub("^\n%s*", ""):gsub("\n" .. prefix, repl):gsub("\n$", ""):gsub("\\r", "\r")) -end - - ---- Set an environment variable --- @function setenv --- @param env (string) name of the environment variable --- @param value the value to set --- @return true on success, false otherwise -local function setenv(env, value) - return ffi.C.setenv(env, value, 1) == 0 -end - - ---- Unset an environment variable --- @function unsetenv --- @param env (string) name of the environment variable --- @return true on success, false otherwise -local function unsetenv(env) - return ffi.C.unsetenv(env) == 0 -end - - ---- Write a yaml file. --- @function make_yaml_file --- @param content (string) the yaml string to write to the file, if omitted the --- current database contents will be written using `kong config db_export`. --- @param filename (optional) if not provided, a temp name will be created --- @return filename of the file written -local function make_yaml_file(content, filename) - local filename = filename or pl_path.tmpname() .. ".yml" - if content then - local fd = assert(io.open(filename, "w")) - assert(fd:write(unindent(content))) - assert(fd:write("\n")) -- ensure last line ends in newline - assert(fd:close()) - else - assert(kong_exec("config db_export --conf "..CONSTANTS.TEST_CONF_PATH.." "..filename)) - end - return filename -end - - local get_available_port do local USED_PORTS = {} @@ -529,25 +439,6 @@ end local resty_http_proxy_mt = setmetatable({}, { __index = http }) resty_http_proxy_mt.__index = resty_http_proxy_mt -local pack = function(...) return { n = select("#", ...), ... } end -local unpack = function(t) return unpack(t, 1, t.n) end - ---- Prints all returned parameters. --- Simple debugging aid, it will pass all received parameters, hence will not --- influence the flow of the code. See also `fail`. --- @function intercept --- @see fail --- @usage -- modify --- local a,b = some_func(c,d) --- -- into --- local a,b = intercept(some_func(c,d)) -local function intercept(...) - local args = pack(...) - print(require("pl.pretty").write(args)) - return unpack(args) -end - - -- Prepopulate Schema's cache Schema.new(consumers_schema_def) Schema.new(services_schema_def) @@ -2409,47 +2300,6 @@ luassert:register("assertion", "contains", contains, "assertion.contains.negative", "assertion.contains.positive") -local deep_sort do - local function deep_compare(a, b) - if a == nil then - a = "" - end - - if b == nil then - b = "" - end - - deep_sort(a) - deep_sort(b) - - if type(a) ~= type(b) then - return type(a) < type(b) - end - - if type(a) == "table" then - return deep_compare(a[1], b[1]) - end - - -- compare cjson.null or ngx.null - if type(a) == "userdata" and type(b) == "userdata" then - return false - end - - return a < b - end - - function deep_sort(t) - if type(t) == "table" then - for _, v in pairs(t) do - deep_sort(v) - end - table.sort(t, deep_compare) - end - - return t - end -end - local function copy_errlog(errlog_path) local file_path = "Unknown path" From 60b065e000195d1a08d3000115673a0eafe9e337 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 13:11:38 +0800 Subject: [PATCH 02/13] kong_exec --- spec/details/misc.lua | 96 +++++++++++++++++++++++++++++++++++++++++++ spec/helpers.lua | 90 ++-------------------------------------- 2 files changed, 99 insertions(+), 87 deletions(-) diff --git a/spec/details/misc.lua b/spec/details/misc.lua index 5efa6477427c..0b89088e43bf 100644 --- a/spec/details/misc.lua +++ b/spec/details/misc.lua @@ -1,9 +1,14 @@ -- miscellaneous + local ffi = require("ffi") local pl_path = require("pl.path") local shell = require("resty.shell") +local conf_loader = require("kong.conf_loader") local nginx_signals = require("kong.cmd.utils.nginx_signals") +local strip = require("kong.tools.string").strip + + local CONSTANTS = require("spec.details.constants") @@ -13,6 +18,9 @@ ffi.cdef [[ ]] +local kong_exec -- forward declaration + + local pack = function(...) return { n = select("#", ...), ... } end local unpack = function(t) return unpack(t, 1, t.n) end @@ -172,6 +180,90 @@ do end +---------------- +-- Shell helpers +-- @section Shell-helpers + +--- Execute a command. +-- Modified version of `pl.utils.executeex()` so the output can directly be +-- used on an assertion. +-- @function execute +-- @param cmd command string to execute +-- @param returns (optional) boolean: if true, this function will +-- return the same values as Penlight's executeex. +-- @return if `returns` is true, returns four return values +-- (ok, code, stdout, stderr); if `returns` is false, +-- returns either (false, stderr) or (true, stderr, stdout). +local function exec(cmd, returns) + --100MB for retrieving stdout & stderr + local ok, stdout, stderr, _, code = shell.run(cmd, nil, 0, 1024*1024*100) + if returns then + return ok, code, stdout, stderr + end + if not ok then + stdout = nil -- don't return 3rd value if fail because of busted's `assert` + end + return ok, stderr, stdout +end + + +local conf = assert(conf_loader(CONSTANTS.TEST_CONF_PATH)) + + +--- Execute a Kong command. +-- @function kong_exec +-- @param cmd Kong command to execute, eg. `start`, `stop`, etc. +-- @param env (optional) table with kong parameters to set as environment +-- variables, overriding the test config (each key will automatically be +-- prefixed with `KONG_` and be converted to uppercase) +-- @param returns (optional) boolean: if true, this function will +-- return the same values as Penlight's `executeex`. +-- @param env_vars (optional) a string prepended to the command, so +-- that arbitrary environment variables may be passed +-- @return if `returns` is true, returns four return values +-- (ok, code, stdout, stderr); if `returns` is false, +-- returns either (false, stderr) or (true, stderr, stdout). +function kong_exec(cmd, env, returns, env_vars) + cmd = cmd or "" + env = env or {} + + -- Insert the Lua path to the custom-plugin fixtures + do + local function cleanup(t) + if t then + t = strip(t) + if t:sub(-1,-1) == ";" then + t = t:sub(1, -2) + end + end + return t ~= "" and t or nil + end + local paths = {} + table.insert(paths, cleanup(CONSTANTS.CUSTOM_PLUGIN_PATH)) + table.insert(paths, cleanup(CONSTANTS.CUSTOM_VAULT_PATH)) + table.insert(paths, cleanup(env.lua_package_path)) + table.insert(paths, cleanup(conf.lua_package_path)) + env.lua_package_path = table.concat(paths, ";") + -- note; the nginx config template will add a final ";;", so no need to + -- include that here + end + + if not env.plugins then + env.plugins = "bundled,dummy,cache,rewriter,error-handler-log," .. + "error-generator,error-generator-last," .. + "short-circuit" + end + + -- build Kong environment variables + env_vars = env_vars or "" + for k, v in pairs(env) do + env_vars = string.format("%s KONG_%s='%s'", env_vars, k:upper(), v) + end + + return exec(env_vars .. " " .. CONSTANTS.BIN_PATH .. " " .. cmd, returns) +end + + return { pack = pack, unpack = unpack, @@ -183,4 +275,8 @@ return { setenv = setenv, unsetenv = unsetenv, deep_sort = deep_sort, + + conf = conf, + exec = exec, + kong_exec = kong_exec, } diff --git a/spec/helpers.lua b/spec/helpers.lua index 0ede42d1178c..4fdef4e80ffa 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -59,8 +59,9 @@ local make_yaml_file = misc.make_yaml_file local setenv = misc.setenv local unsetenv = misc.unsetenv local deep_sort = misc.deep_sort - -local kong_exec -- forward declaration +local conf = misc.conf +local exec = misc.exec +local kong_exec = misc.kong_exec log.set_lvl(log.levels.quiet) -- disable stdout logs in tests @@ -107,8 +108,6 @@ end --------------- -- Conf and DAO --------------- -local conf = assert(conf_loader(CONSTANTS.TEST_CONF_PATH)) - _G.kong = kong_global.new() kong_global.init_pdk(_G.kong, conf) ngx.ctx.KONG_PHASE = kong_global.phases.access @@ -1045,8 +1044,6 @@ local function proxy_client_h2() return http2_client(proxy_ip, proxy_port, true) end -local exec -- forward declaration - --- Creates a gRPC client, based on the grpcurl CLI. -- @function grpc_client -- @param host hostname to connect to @@ -3170,87 +3167,6 @@ luassert:register("assertion", "partial_match", partial_match, "assertion.partial_match.negative") ----------------- --- Shell helpers --- @section Shell-helpers - ---- Execute a command. --- Modified version of `pl.utils.executeex()` so the output can directly be --- used on an assertion. --- @function execute --- @param cmd command string to execute --- @param returns (optional) boolean: if true, this function will --- return the same values as Penlight's executeex. --- @return if `returns` is true, returns four return values --- (ok, code, stdout, stderr); if `returns` is false, --- returns either (false, stderr) or (true, stderr, stdout). -function exec(cmd, returns) - --100MB for retrieving stdout & stderr - local ok, stdout, stderr, _, code = shell.run(cmd, nil, 0, 1024*1024*100) - if returns then - return ok, code, stdout, stderr - end - if not ok then - stdout = nil -- don't return 3rd value if fail because of busted's `assert` - end - return ok, stderr, stdout -end - - ---- Execute a Kong command. --- @function kong_exec --- @param cmd Kong command to execute, eg. `start`, `stop`, etc. --- @param env (optional) table with kong parameters to set as environment --- variables, overriding the test config (each key will automatically be --- prefixed with `KONG_` and be converted to uppercase) --- @param returns (optional) boolean: if true, this function will --- return the same values as Penlight's `executeex`. --- @param env_vars (optional) a string prepended to the command, so --- that arbitrary environment variables may be passed --- @return if `returns` is true, returns four return values --- (ok, code, stdout, stderr); if `returns` is false, --- returns either (false, stderr) or (true, stderr, stdout). -function kong_exec(cmd, env, returns, env_vars) - cmd = cmd or "" - env = env or {} - - -- Insert the Lua path to the custom-plugin fixtures - do - local function cleanup(t) - if t then - t = strip(t) - if t:sub(-1,-1) == ";" then - t = t:sub(1, -2) - end - end - return t ~= "" and t or nil - end - local paths = {} - table.insert(paths, cleanup(CONSTANTS.CUSTOM_PLUGIN_PATH)) - table.insert(paths, cleanup(CONSTANTS.CUSTOM_VAULT_PATH)) - table.insert(paths, cleanup(env.lua_package_path)) - table.insert(paths, cleanup(conf.lua_package_path)) - env.lua_package_path = table.concat(paths, ";") - -- note; the nginx config template will add a final ";;", so no need to - -- include that here - end - - if not env.plugins then - env.plugins = "bundled,dummy,cache,rewriter,error-handler-log," .. - "error-generator,error-generator-last," .. - "short-circuit" - end - - -- build Kong environment variables - env_vars = env_vars or "" - for k, v in pairs(env) do - env_vars = string.format("%s KONG_%s='%s'", env_vars, k:upper(), v) - end - - return exec(env_vars .. " " .. CONSTANTS.BIN_PATH .. " " .. cmd, returns) -end - - --- Prepares the Kong environment. -- Creates the working directory if it does not exist. -- @param prefix (optional) path to the working directory, if omitted the test From 044def05ee9b8c5993a2ff8fd5f02fc670986fcc Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 13:18:51 +0800 Subject: [PATCH 03/13] code clean --- spec/helpers.lua | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/spec/helpers.lua b/spec/helpers.lua index 4fdef4e80ffa..a1598682fa1a 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -51,14 +51,6 @@ local strip = require("kong.tools.string").strip local splitlines = require("pl.stringx").splitlines -local unpack = misc.unpack -local intercept = misc.intercept -local openresty_ver_num = misc.openresty_ver_num() -local unindent = misc.unindent -local make_yaml_file = misc.make_yaml_file -local setenv = misc.setenv -local unsetenv = misc.unsetenv -local deep_sort = misc.deep_sort local conf = misc.conf local exec = misc.exec local kong_exec = misc.kong_exec @@ -2273,7 +2265,7 @@ luassert:register("assertion", "fail", fail, -- local i = assert.contains("two", arr) --> fails -- local i = assert.contains("ee$", arr, true) --> passes; i == 2 local function contains(state, args) - local expected, arr, pattern = unpack(args) + local expected, arr, pattern = misc.unpack(args) local found for i = 1, #arr do if (pattern and string.match(arr[i], expected)) or arr[i] == expected then @@ -2836,13 +2828,13 @@ do return found end - say:set("assertion.match_line.negative", unindent [[ + say:set("assertion.match_line.negative", misc.unindent [[ Expected file at: %s To match: %s ]]) - say:set("assertion.match_line.positive", unindent [[ + say:set("assertion.match_line.positive", misc.unindent [[ Expected file at: %s To not match: @@ -2886,13 +2878,13 @@ local function match_re(_, args) end end -say:set("assertion.match_re.negative", unindent [[ +say:set("assertion.match_re.negative", misc.unindent [[ Expected log: %s To match: %s ]]) -say:set("assertion.match_re.positive", unindent [[ +say:set("assertion.match_re.positive", misc.unindent [[ Expected log: %s To not match: @@ -3978,10 +3970,10 @@ local function use_old_plugin(name) local origin_lua_path = os.getenv("LUA_PATH") -- put the old plugin path at first - assert(setenv("LUA_PATH", old_plugin_path .. "/?.lua;" .. old_plugin_path .. "/?/init.lua;" .. origin_lua_path), "failed to set LUA_PATH env") + assert(misc.setenv("LUA_PATH", old_plugin_path .. "/?.lua;" .. old_plugin_path .. "/?/init.lua;" .. origin_lua_path), "failed to set LUA_PATH env") return function () - setenv("LUA_PATH", origin_lua_path) + misc.setenv("LUA_PATH", origin_lua_path) if temp_dir then pl_dir.rmtree(temp_dir) end @@ -4143,13 +4135,13 @@ end stress_generator = stress_generator, -- miscellaneous - intercept = intercept, - openresty_ver_num = openresty_ver_num(), - unindent = unindent, - make_yaml_file = make_yaml_file, - setenv = setenv, - unsetenv = unsetenv, - deep_sort = deep_sort, + intercept = misc.intercept, + openresty_ver_num = misc.openresty_ver_num(), + unindent = misc.unindent, + make_yaml_file = misc.make_yaml_file, + setenv = misc.setenv, + unsetenv = misc.unsetenv, + deep_sort = misc.deep_sort, -- launching Kong subprocesses start_kong = start_kong, From 29fa9ab06ed0bf7e5f8a1cef291421b09095ea59 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 13:21:12 +0800 Subject: [PATCH 04/13] fix mistake --- spec/helpers.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/helpers.lua b/spec/helpers.lua index a1598682fa1a..acc59f4efc7b 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -33,7 +33,6 @@ local cjson = require "cjson.safe" local kong_table = require "kong.tools.table" local http = require "resty.http" local pkey = require "resty.openssl.pkey" -local nginx_signals = require "kong.cmd.utils.nginx_signals" local log = require "kong.cmd.utils.log" local DB = require "kong.db" local shell = require "resty.shell" @@ -4136,7 +4135,7 @@ end -- miscellaneous intercept = misc.intercept, - openresty_ver_num = misc.openresty_ver_num(), + openresty_ver_num = misc.openresty_ver_num, unindent = misc.unindent, make_yaml_file = misc.make_yaml_file, setenv = misc.setenv, From 8921d965d68228c97b961aeb37dd945aaed16583 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 13:54:35 +0800 Subject: [PATCH 05/13] generate_keys --- spec/details/misc.lua | 23 +++++++++++++++++++++++ spec/helpers.lua | 23 +---------------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/spec/details/misc.lua b/spec/details/misc.lua index 0b89088e43bf..b6dbfab62795 100644 --- a/spec/details/misc.lua +++ b/spec/details/misc.lua @@ -4,6 +4,7 @@ local ffi = require("ffi") local pl_path = require("pl.path") local shell = require("resty.shell") +local pkey = require "resty.openssl.pkey" local conf_loader = require("kong.conf_loader") local nginx_signals = require("kong.cmd.utils.nginx_signals") local strip = require("kong.tools.string").strip @@ -264,6 +265,26 @@ function kong_exec(cmd, env, returns, env_vars) end +--- Generate asymmetric keys +-- @function generate_keys +-- @param fmt format to receive the public and private pair +-- @return `pub, priv` key tuple or `nil + err` on failure +local function generate_keys(fmt) + fmt = string.upper(fmt) or "JWK" + local key, err = pkey.new({ + -- only support RSA for now + type = 'RSA', + bits = 2048, + exp = 65537 + }) + assert(key) + assert(err == nil, err) + local pub = key:tostring("public", fmt) + local priv = key:tostring("private", fmt) + return pub, priv +end + + return { pack = pack, unpack = unpack, @@ -279,4 +300,6 @@ return { conf = conf, exec = exec, kong_exec = kong_exec, + + generate_keys = generate_keys, } diff --git a/spec/helpers.lua b/spec/helpers.lua index acc59f4efc7b..227af2decc0d 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -32,7 +32,6 @@ local Entity = require "kong.db.schema.entity" local cjson = require "cjson.safe" local kong_table = require "kong.tools.table" local http = require "resty.http" -local pkey = require "resty.openssl.pkey" local log = require "kong.cmd.utils.log" local DB = require "kong.db" local shell = require "resty.shell" @@ -3887,26 +3886,6 @@ local function clustering_client(opts) end ---- Generate asymmetric keys --- @function generate_keys --- @param fmt format to receive the public and private pair --- @return `pub, priv` key tuple or `nil + err` on failure -local function generate_keys(fmt) - fmt = string.upper(fmt) or "JWK" - local key, err = pkey.new({ - -- only support RSA for now - type = 'RSA', - bits = 2048, - exp = 65537 - }) - assert(key) - assert(err == nil, err) - local pub = key:tostring("public", fmt) - local priv = key:tostring("private", fmt) - return pub, priv -end - - local make_temp_dir do local seeded = false @@ -4141,6 +4120,7 @@ end setenv = misc.setenv, unsetenv = misc.unsetenv, deep_sort = misc.deep_sort, + generate_keys = misc.generate_keys, -- launching Kong subprocesses start_kong = start_kong, @@ -4154,7 +4134,6 @@ end start_grpc_target = start_grpc_target, stop_grpc_target = stop_grpc_target, get_grpc_target_port = get_grpc_target_port, - generate_keys = generate_keys, -- plugin compatibility test use_old_plugin = use_old_plugin, From 3ddc22a79b984804b1140cd91b0eccfb1aad5233 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 16:17:55 +0800 Subject: [PATCH 06/13] fix tests --- spec/02-integration/05-proxy/02-router_spec.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/02-integration/05-proxy/02-router_spec.lua b/spec/02-integration/05-proxy/02-router_spec.lua index aa52eaa13c6b..5a5dd5891a0f 100644 --- a/spec/02-integration/05-proxy/02-router_spec.lua +++ b/spec/02-integration/05-proxy/02-router_spec.lua @@ -2682,6 +2682,7 @@ do helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil From 9eac6f26abd060b6ede9027d6814b8de9e2be573 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 16:42:19 +0800 Subject: [PATCH 07/13] update reload_flavor() --- spec/01-unit/19-hybrid/03-compat_spec.lua | 1 + spec/02-integration/05-proxy/01-proxy_spec.lua | 1 + spec/02-integration/05-proxy/06-ssl_spec.lua | 1 + spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua | 1 + spec/02-integration/05-proxy/18-upstream_tls_spec.lua | 1 + spec/02-integration/05-proxy/19-grpc_proxy_spec.lua | 1 + spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua | 1 + spec/02-integration/05-proxy/23-context_spec.lua | 1 + spec/02-integration/05-proxy/26-udp_spec.lua | 1 + .../05-proxy/28-stream_plugins_triggering_spec.lua | 1 + 10 files changed, 10 insertions(+) diff --git a/spec/01-unit/19-hybrid/03-compat_spec.lua b/spec/01-unit/19-hybrid/03-compat_spec.lua index b2e16b071134..52d6ec83e251 100644 --- a/spec/01-unit/19-hybrid/03-compat_spec.lua +++ b/spec/01-unit/19-hybrid/03-compat_spec.lua @@ -650,6 +650,7 @@ describe("kong.clustering.compat", function() package.loaded["kong.db.schema.entities.routes"] = nil package.loaded["kong.db.schema.entities.routes_subschemas"] = nil package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.clustering.compat"] = nil package.loaded["kong.db.declarative"] = nil diff --git a/spec/02-integration/05-proxy/01-proxy_spec.lua b/spec/02-integration/05-proxy/01-proxy_spec.lua index e240d2ca4347..08b4d163359e 100644 --- a/spec/02-integration/05-proxy/01-proxy_spec.lua +++ b/spec/02-integration/05-proxy/01-proxy_spec.lua @@ -152,6 +152,7 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/06-ssl_spec.lua b/spec/02-integration/05-proxy/06-ssl_spec.lua index 770300354419..ca85e0bac4a7 100644 --- a/spec/02-integration/05-proxy/06-ssl_spec.lua +++ b/spec/02-integration/05-proxy/06-ssl_spec.lua @@ -43,6 +43,7 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua b/spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua index b898979d6ed4..610a6914bd91 100644 --- a/spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua +++ b/spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua @@ -11,6 +11,7 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/18-upstream_tls_spec.lua b/spec/02-integration/05-proxy/18-upstream_tls_spec.lua index 13789ccdaee8..30c8eec05b0a 100644 --- a/spec/02-integration/05-proxy/18-upstream_tls_spec.lua +++ b/spec/02-integration/05-proxy/18-upstream_tls_spec.lua @@ -84,6 +84,7 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/19-grpc_proxy_spec.lua b/spec/02-integration/05-proxy/19-grpc_proxy_spec.lua index bc578ffd744e..8523d6a0d864 100644 --- a/spec/02-integration/05-proxy/19-grpc_proxy_spec.lua +++ b/spec/02-integration/05-proxy/19-grpc_proxy_spec.lua @@ -15,6 +15,7 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua b/spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua index daaedb55d92c..39f171e8cea4 100644 --- a/spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua +++ b/spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua @@ -15,6 +15,7 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/23-context_spec.lua b/spec/02-integration/05-proxy/23-context_spec.lua index f5e82e9e45ef..bb97388e2015 100644 --- a/spec/02-integration/05-proxy/23-context_spec.lua +++ b/spec/02-integration/05-proxy/23-context_spec.lua @@ -12,6 +12,7 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/26-udp_spec.lua b/spec/02-integration/05-proxy/26-udp_spec.lua index f0e342938223..0941a6a12e37 100644 --- a/spec/02-integration/05-proxy/26-udp_spec.lua +++ b/spec/02-integration/05-proxy/26-udp_spec.lua @@ -14,6 +14,7 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/28-stream_plugins_triggering_spec.lua b/spec/02-integration/05-proxy/28-stream_plugins_triggering_spec.lua index 3bab5d24f02f..5e5014773185 100644 --- a/spec/02-integration/05-proxy/28-stream_plugins_triggering_spec.lua +++ b/spec/02-integration/05-proxy/28-stream_plugins_triggering_spec.lua @@ -84,6 +84,7 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil + package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil From 1967cd4ba14a22e14818f58e04612f1d91dfe49a Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 16:56:53 +0800 Subject: [PATCH 08/13] reload_module misc --- spec/01-unit/19-hybrid/03-compat_spec.lua | 1 - spec/02-integration/05-proxy/01-proxy_spec.lua | 1 - spec/02-integration/05-proxy/02-router_spec.lua | 1 - spec/02-integration/05-proxy/06-ssl_spec.lua | 1 - .../05-proxy/10-balancer/06-stream_spec.lua | 1 - .../02-integration/05-proxy/18-upstream_tls_spec.lua | 1 - spec/02-integration/05-proxy/19-grpc_proxy_spec.lua | 1 - .../05-proxy/21-grpc_plugins_triggering_spec.lua | 1 - spec/02-integration/05-proxy/23-context_spec.lua | 1 - spec/02-integration/05-proxy/26-udp_spec.lua | 1 - .../05-proxy/28-stream_plugins_triggering_spec.lua | 1 - spec/helpers.lua | 12 +++++++++++- 12 files changed, 11 insertions(+), 12 deletions(-) diff --git a/spec/01-unit/19-hybrid/03-compat_spec.lua b/spec/01-unit/19-hybrid/03-compat_spec.lua index 52d6ec83e251..b2e16b071134 100644 --- a/spec/01-unit/19-hybrid/03-compat_spec.lua +++ b/spec/01-unit/19-hybrid/03-compat_spec.lua @@ -650,7 +650,6 @@ describe("kong.clustering.compat", function() package.loaded["kong.db.schema.entities.routes"] = nil package.loaded["kong.db.schema.entities.routes_subschemas"] = nil package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.clustering.compat"] = nil package.loaded["kong.db.declarative"] = nil diff --git a/spec/02-integration/05-proxy/01-proxy_spec.lua b/spec/02-integration/05-proxy/01-proxy_spec.lua index 08b4d163359e..e240d2ca4347 100644 --- a/spec/02-integration/05-proxy/01-proxy_spec.lua +++ b/spec/02-integration/05-proxy/01-proxy_spec.lua @@ -152,7 +152,6 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/02-router_spec.lua b/spec/02-integration/05-proxy/02-router_spec.lua index 5a5dd5891a0f..aa52eaa13c6b 100644 --- a/spec/02-integration/05-proxy/02-router_spec.lua +++ b/spec/02-integration/05-proxy/02-router_spec.lua @@ -2682,7 +2682,6 @@ do helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/06-ssl_spec.lua b/spec/02-integration/05-proxy/06-ssl_spec.lua index ca85e0bac4a7..770300354419 100644 --- a/spec/02-integration/05-proxy/06-ssl_spec.lua +++ b/spec/02-integration/05-proxy/06-ssl_spec.lua @@ -43,7 +43,6 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua b/spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua index 610a6914bd91..b898979d6ed4 100644 --- a/spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua +++ b/spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua @@ -11,7 +11,6 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/18-upstream_tls_spec.lua b/spec/02-integration/05-proxy/18-upstream_tls_spec.lua index 30c8eec05b0a..13789ccdaee8 100644 --- a/spec/02-integration/05-proxy/18-upstream_tls_spec.lua +++ b/spec/02-integration/05-proxy/18-upstream_tls_spec.lua @@ -84,7 +84,6 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/19-grpc_proxy_spec.lua b/spec/02-integration/05-proxy/19-grpc_proxy_spec.lua index 8523d6a0d864..bc578ffd744e 100644 --- a/spec/02-integration/05-proxy/19-grpc_proxy_spec.lua +++ b/spec/02-integration/05-proxy/19-grpc_proxy_spec.lua @@ -15,7 +15,6 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua b/spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua index 39f171e8cea4..daaedb55d92c 100644 --- a/spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua +++ b/spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua @@ -15,7 +15,6 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/23-context_spec.lua b/spec/02-integration/05-proxy/23-context_spec.lua index bb97388e2015..f5e82e9e45ef 100644 --- a/spec/02-integration/05-proxy/23-context_spec.lua +++ b/spec/02-integration/05-proxy/23-context_spec.lua @@ -12,7 +12,6 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/26-udp_spec.lua b/spec/02-integration/05-proxy/26-udp_spec.lua index 0941a6a12e37..f0e342938223 100644 --- a/spec/02-integration/05-proxy/26-udp_spec.lua +++ b/spec/02-integration/05-proxy/26-udp_spec.lua @@ -14,7 +14,6 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/02-integration/05-proxy/28-stream_plugins_triggering_spec.lua b/spec/02-integration/05-proxy/28-stream_plugins_triggering_spec.lua index 5e5014773185..3bab5d24f02f 100644 --- a/spec/02-integration/05-proxy/28-stream_plugins_triggering_spec.lua +++ b/spec/02-integration/05-proxy/28-stream_plugins_triggering_spec.lua @@ -84,7 +84,6 @@ local function reload_router(flavor) helpers.setenv("KONG_ROUTER_FLAVOR", flavor) package.loaded["spec.helpers"] = nil - package.loaded["spec.details.misc"] = nil package.loaded["kong.global"] = nil package.loaded["kong.cache"] = nil package.loaded["kong.db"] = nil diff --git a/spec/helpers.lua b/spec/helpers.lua index 227af2decc0d..053ab0b56aed 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -6,10 +6,11 @@ -- @module spec.helpers local CONSTANTS = require("spec.details.constants") -local misc = require("spec.details.misc") + local PLUGINS_LIST + local consumers_schema_def = require "kong.db.schema.entities.consumers" local services_schema_def = require "kong.db.schema.entities.services" local plugins_schema_def = require "kong.db.schema.entities.plugins" @@ -49,6 +50,15 @@ local strip = require("kong.tools.string").strip local splitlines = require("pl.stringx").splitlines +local function reload_module(name) + package.loaded[name] = nil + return require(name) +end + + +local misc = reload_module("spec.details.misc") + + local conf = misc.conf local exec = misc.exec local kong_exec = misc.kong_exec From 819cdf58a3b5a92e3d9b3cfc7ea73e7b36f04ec7 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 17:45:24 +0800 Subject: [PATCH 09/13] details.shell --- spec/details/misc.lua | 99 ++---------------------------------------- spec/details/shell.lua | 99 ++++++++++++++++++++++++++++++++++++++++++ spec/helpers.lua | 8 ++-- 3 files changed, 107 insertions(+), 99 deletions(-) create mode 100644 spec/details/shell.lua diff --git a/spec/details/misc.lua b/spec/details/misc.lua index b6dbfab62795..459192379c58 100644 --- a/spec/details/misc.lua +++ b/spec/details/misc.lua @@ -3,11 +3,9 @@ local ffi = require("ffi") local pl_path = require("pl.path") -local shell = require("resty.shell") -local pkey = require "resty.openssl.pkey" -local conf_loader = require("kong.conf_loader") +local pkey = require("resty.openssl.pkey") local nginx_signals = require("kong.cmd.utils.nginx_signals") -local strip = require("kong.tools.string").strip +local shell = require("spec.details.shell") local CONSTANTS = require("spec.details.constants") @@ -19,9 +17,6 @@ ffi.cdef [[ ]] -local kong_exec -- forward declaration - - local pack = function(...) return { n = select("#", ...), ... } end local unpack = function(t) return unpack(t, 1, t.n) end @@ -113,7 +108,7 @@ local function make_yaml_file(content, filename) assert(fd:write("\n")) -- ensure last line ends in newline assert(fd:close()) else - assert(kong_exec("config db_export --conf "..CONSTANTS.TEST_CONF_PATH.." "..filename)) + assert(shell.kong_exec("config db_export --conf "..CONSTANTS.TEST_CONF_PATH.." "..filename)) end return filename end @@ -181,90 +176,6 @@ do end ----------------- --- Shell helpers --- @section Shell-helpers - ---- Execute a command. --- Modified version of `pl.utils.executeex()` so the output can directly be --- used on an assertion. --- @function execute --- @param cmd command string to execute --- @param returns (optional) boolean: if true, this function will --- return the same values as Penlight's executeex. --- @return if `returns` is true, returns four return values --- (ok, code, stdout, stderr); if `returns` is false, --- returns either (false, stderr) or (true, stderr, stdout). -local function exec(cmd, returns) - --100MB for retrieving stdout & stderr - local ok, stdout, stderr, _, code = shell.run(cmd, nil, 0, 1024*1024*100) - if returns then - return ok, code, stdout, stderr - end - if not ok then - stdout = nil -- don't return 3rd value if fail because of busted's `assert` - end - return ok, stderr, stdout -end - - -local conf = assert(conf_loader(CONSTANTS.TEST_CONF_PATH)) - - ---- Execute a Kong command. --- @function kong_exec --- @param cmd Kong command to execute, eg. `start`, `stop`, etc. --- @param env (optional) table with kong parameters to set as environment --- variables, overriding the test config (each key will automatically be --- prefixed with `KONG_` and be converted to uppercase) --- @param returns (optional) boolean: if true, this function will --- return the same values as Penlight's `executeex`. --- @param env_vars (optional) a string prepended to the command, so --- that arbitrary environment variables may be passed --- @return if `returns` is true, returns four return values --- (ok, code, stdout, stderr); if `returns` is false, --- returns either (false, stderr) or (true, stderr, stdout). -function kong_exec(cmd, env, returns, env_vars) - cmd = cmd or "" - env = env or {} - - -- Insert the Lua path to the custom-plugin fixtures - do - local function cleanup(t) - if t then - t = strip(t) - if t:sub(-1,-1) == ";" then - t = t:sub(1, -2) - end - end - return t ~= "" and t or nil - end - local paths = {} - table.insert(paths, cleanup(CONSTANTS.CUSTOM_PLUGIN_PATH)) - table.insert(paths, cleanup(CONSTANTS.CUSTOM_VAULT_PATH)) - table.insert(paths, cleanup(env.lua_package_path)) - table.insert(paths, cleanup(conf.lua_package_path)) - env.lua_package_path = table.concat(paths, ";") - -- note; the nginx config template will add a final ";;", so no need to - -- include that here - end - - if not env.plugins then - env.plugins = "bundled,dummy,cache,rewriter,error-handler-log," .. - "error-generator,error-generator-last," .. - "short-circuit" - end - - -- build Kong environment variables - env_vars = env_vars or "" - for k, v in pairs(env) do - env_vars = string.format("%s KONG_%s='%s'", env_vars, k:upper(), v) - end - - return exec(env_vars .. " " .. CONSTANTS.BIN_PATH .. " " .. cmd, returns) -end - - --- Generate asymmetric keys -- @function generate_keys -- @param fmt format to receive the public and private pair @@ -297,9 +208,5 @@ return { unsetenv = unsetenv, deep_sort = deep_sort, - conf = conf, - exec = exec, - kong_exec = kong_exec, - generate_keys = generate_keys, } diff --git a/spec/details/shell.lua b/spec/details/shell.lua new file mode 100644 index 000000000000..fffdfc49358d --- /dev/null +++ b/spec/details/shell.lua @@ -0,0 +1,99 @@ +local shell = require("resty.shell") +local conf_loader = require("kong.conf_loader") +local strip = require("kong.tools.string").strip + + +local CONSTANTS = require("spec.details.constants") + + +---------------- +-- Shell helpers +-- @section Shell-helpers + +--- Execute a command. +-- Modified version of `pl.utils.executeex()` so the output can directly be +-- used on an assertion. +-- @function execute +-- @param cmd command string to execute +-- @param returns (optional) boolean: if true, this function will +-- return the same values as Penlight's executeex. +-- @return if `returns` is true, returns four return values +-- (ok, code, stdout, stderr); if `returns` is false, +-- returns either (false, stderr) or (true, stderr, stdout). +local function exec(cmd, returns) + --100MB for retrieving stdout & stderr + local ok, stdout, stderr, _, code = shell.run(cmd, nil, 0, 1024*1024*100) + if returns then + return ok, code, stdout, stderr + end + if not ok then + stdout = nil -- don't return 3rd value if fail because of busted's `assert` + end + return ok, stderr, stdout +end + + +local conf = assert(conf_loader(CONSTANTS.TEST_CONF_PATH)) + + +--- Execute a Kong command. +-- @function kong_exec +-- @param cmd Kong command to execute, eg. `start`, `stop`, etc. +-- @param env (optional) table with kong parameters to set as environment +-- variables, overriding the test config (each key will automatically be +-- prefixed with `KONG_` and be converted to uppercase) +-- @param returns (optional) boolean: if true, this function will +-- return the same values as Penlight's `executeex`. +-- @param env_vars (optional) a string prepended to the command, so +-- that arbitrary environment variables may be passed +-- @return if `returns` is true, returns four return values +-- (ok, code, stdout, stderr); if `returns` is false, +-- returns either (false, stderr) or (true, stderr, stdout). +local function kong_exec(cmd, env, returns, env_vars) + cmd = cmd or "" + env = env or {} + + -- Insert the Lua path to the custom-plugin fixtures + do + local function cleanup(t) + if t then + t = strip(t) + if t:sub(-1,-1) == ";" then + t = t:sub(1, -2) + end + end + return t ~= "" and t or nil + end + local paths = {} + table.insert(paths, cleanup(CONSTANTS.CUSTOM_PLUGIN_PATH)) + table.insert(paths, cleanup(CONSTANTS.CUSTOM_VAULT_PATH)) + table.insert(paths, cleanup(env.lua_package_path)) + table.insert(paths, cleanup(conf.lua_package_path)) + env.lua_package_path = table.concat(paths, ";") + -- note; the nginx config template will add a final ";;", so no need to + -- include that here + end + + if not env.plugins then + env.plugins = "bundled,dummy,cache,rewriter,error-handler-log," .. + "error-generator,error-generator-last," .. + "short-circuit" + end + + -- build Kong environment variables + env_vars = env_vars or "" + for k, v in pairs(env) do + env_vars = string.format("%s KONG_%s='%s'", env_vars, k:upper(), v) + end + + return exec(env_vars .. " " .. CONSTANTS.BIN_PATH .. " " .. cmd, returns) +end + + +return { + run = shell.run, + + conf = conf, + exec = exec, + kong_exec = kong_exec, +} diff --git a/spec/helpers.lua b/spec/helpers.lua index 053ab0b56aed..68fdb00340a9 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -56,12 +56,14 @@ local function reload_module(name) end +-- reload some modules +local shell = reload_module("spec.details.shell") local misc = reload_module("spec.details.misc") -local conf = misc.conf -local exec = misc.exec -local kong_exec = misc.kong_exec +local conf = shell.conf +local exec = shell.exec +local kong_exec = shell.kong_exec log.set_lvl(log.levels.quiet) -- disable stdout logs in tests From 84f5d3e00391d3bda1b7ef428fe567b1bbfc67b3 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 18:09:58 +0800 Subject: [PATCH 10/13] lint fix --- spec/helpers.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/helpers.lua b/spec/helpers.lua index 68fdb00340a9..06cab30714e8 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -35,7 +35,6 @@ local kong_table = require "kong.tools.table" local http = require "resty.http" local log = require "kong.cmd.utils.log" local DB = require "kong.db" -local shell = require "resty.shell" local ssl = require "ngx.ssl" local ws_client = require "resty.websocket.client" local table_clone = require "table.clone" From cefd909adef15f1ef7318e14500c9760a55063aa Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 18:13:41 +0800 Subject: [PATCH 11/13] comments --- spec/helpers.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/helpers.lua b/spec/helpers.lua index 06cab30714e8..32f979ba29a1 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -55,7 +55,7 @@ local function reload_module(name) end --- reload some modules +-- reload some modules when env or _G changes local shell = reload_module("spec.details.shell") local misc = reload_module("spec.details.misc") From 0392b5963109ad81fa1e4141de9f4d9db2f13750 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 22 Aug 2024 20:16:17 +0800 Subject: [PATCH 12/13] reload constants --- spec/helpers.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/helpers.lua b/spec/helpers.lua index 32f979ba29a1..b36da513d757 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -5,9 +5,6 @@ -- @license [Apache 2.0](https://opensource.org/licenses/Apache-2.0) -- @module spec.helpers -local CONSTANTS = require("spec.details.constants") - - local PLUGINS_LIST @@ -56,6 +53,7 @@ end -- reload some modules when env or _G changes +local CONSTANTS = reload_module("spec.details.constants") local shell = reload_module("spec.details.shell") local misc = reload_module("spec.details.misc") From 904d5f5fb661d872f24bbd390afe99cb41149bc3 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 23 Aug 2024 07:48:38 +0800 Subject: [PATCH 13/13] setenv/unsetenv --- spec/details/misc.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/details/misc.lua b/spec/details/misc.lua index 459192379c58..1ec4bd9dc3ee 100644 --- a/spec/details/misc.lua +++ b/spec/details/misc.lua @@ -120,6 +120,8 @@ end -- @param value the value to set -- @return true on success, false otherwise local function setenv(env, value) + assert(type(env) == "string", "env must be a string") + assert(type(value) == "string", "value must be a string") return ffi.C.setenv(env, value, 1) == 0 end @@ -129,6 +131,7 @@ end -- @param env (string) name of the environment variable -- @return true on success, false otherwise local function unsetenv(env) + assert(type(env) == "string", "env must be a string") return ffi.C.unsetenv(env) == 0 end