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(helpers): move some misc funcs #13667

Merged
merged 6 commits into from
Sep 19, 2024
Merged
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
96 changes: 5 additions & 91 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ local http = require "resty.http"
local log = require "kong.cmd.utils.log"
local ssl = require "ngx.ssl"
local ws_client = require "resty.websocket.client"
local table_clone = require "table.clone"
local https_server = require "spec.fixtures.https_server"
local stress_generator = require "spec.fixtures.stress_generator"
local lfs = require "lfs"
Expand Down Expand Up @@ -1505,79 +1504,6 @@ local function clustering_client(opts)
end


local make_temp_dir
do
local seeded = false

function make_temp_dir()
if not seeded then
ngx.update_time()
math.randomseed(ngx.worker.pid() + ngx.now())
seeded = true
end

local tmp
local ok, err

local tries = 1000
for _ = 1, tries do
local name = "/tmp/.kong-test" .. math.random()

ok, err = pl_path.mkdir(name)

if ok then
tmp = name
break
end
end

assert(tmp ~= nil, "failed to create temporary directory " ..
"after " .. tostring(tries) .. " tries, " ..
"last error: " .. tostring(err))

return tmp, function() pl_dir.rmtree(tmp) end
end
end

-- This function is used for plugin compatibility test.
-- It will use the old version plugin by including the path of the old plugin
-- at the first of LUA_PATH.
-- The return value is a function which when called will recover the original
-- LUA_PATH and remove the temporary directory if it exists.
-- For an example of how to use it, please see:
-- plugins-ee/rate-limiting-advanced/spec/06-old-plugin-compatibility_spec.lua
-- spec/03-plugins/03-http-log/05-old-plugin-compatibility_spec.lua
local function use_old_plugin(name)
assert(type(name) == "string", "must specify the plugin name")

local old_plugin_path
local temp_dir
if pl_path.exists(CONSTANTS.OLD_VERSION_KONG_PATH .. "/kong/plugins/" .. name) then
-- only include the path of the specified plugin into LUA_PATH
-- and keep the directory structure 'kong/plugins/...'
temp_dir = make_temp_dir()
old_plugin_path = temp_dir
local dest_dir = old_plugin_path .. "/kong/plugins"
assert(pl_dir.makepath(dest_dir), "failed to makepath " .. dest_dir)
assert(shell.run("cp -r " .. CONSTANTS.OLD_VERSION_KONG_PATH .. "/kong/plugins/" .. name .. " " .. dest_dir), "failed to copy the plugin directory")

else
error("the specified plugin " .. name .. " doesn't exist")
end

local origin_lua_path = os.getenv("LUA_PATH")
-- put the old plugin path at first
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 ()
misc.setenv("LUA_PATH", origin_lua_path)
if temp_dir then
pl_dir.rmtree(temp_dir)
end
end
end


----------------
-- Variables/constants
-- @section exported-fields
Expand Down Expand Up @@ -1755,34 +1681,22 @@ end
get_grpc_target_port = grpc.get_grpc_target_port,

-- plugin compatibility test
use_old_plugin = use_old_plugin,
use_old_plugin = misc.use_old_plugin,

-- Only use in CLI tests from spec/02-integration/01-cmd
kill_all = cmd.kill_all,

with_current_ws = function(ws,fn, db)
local old_ws = ngx.ctx.workspace
ngx.ctx.workspace = nil
ws = ws or {db.workspaces:select_by_name("default")}
ngx.ctx.workspace = ws[1] and ws[1].id
local res = fn()
ngx.ctx.workspace = old_ws
return res
end,
with_current_ws = misc.with_current_ws,

signal = cmd.signal,

-- send signal to all Nginx workers, not including the master
signal_workers = cmd.signal_workers,

-- returns the plugins and version list that is used by Hybrid mode tests
get_plugins_list = function()
local PLUGINS_LIST = DB.get_plugins_list()
assert(PLUGINS_LIST, "plugin list has not been initialized yet, " ..
"you must call get_db_utils first")
return table_clone(PLUGINS_LIST)
end,
get_plugins_list = DB.clone_plugins_list,

get_available_port = get_available_port,

make_temp_dir = make_temp_dir,
make_temp_dir = misc.make_temp_dir,
}
10 changes: 10 additions & 0 deletions spec/internal/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


local pl_tablex = require("pl.tablex")
local table_clone = require("table.clone")


local DB = require("kong.db")
Expand Down Expand Up @@ -388,6 +389,14 @@ local function get_plugins_list()
end


-- returns the plugins and version list that is used by Hybrid mode tests
local function clone_plugins_list()
assert(PLUGINS_LIST, "plugin list has not been initialized yet, " ..
"you must call get_db_utils first")
return table_clone(PLUGINS_LIST)
end


local validate_plugin_config_schema
do
local consumers_schema_def = require("kong.db.schema.entities.consumers")
Expand Down Expand Up @@ -444,6 +453,7 @@ return {

get_dcbp = get_dcbp,
get_plugins_list = get_plugins_list,
clone_plugins_list = clone_plugins_list,

get_cache = get_cache,
get_db_utils = get_db_utils,
Expand Down
90 changes: 90 additions & 0 deletions spec/internal/misc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

local ffi = require("ffi")
local pl_path = require("pl.path")
local pl_dir = require("pl.dir")
local pkey = require("resty.openssl.pkey")
local nginx_signals = require("kong.cmd.utils.nginx_signals")
local shell = require("spec.internal.shell")
Expand Down Expand Up @@ -229,6 +230,91 @@ local function lookup(t, k)
end


local function with_current_ws(ws,fn, db)
local old_ws = ngx.ctx.workspace
ngx.ctx.workspace = nil
ws = ws or {db.workspaces:select_by_name("default")}
ngx.ctx.workspace = ws[1] and ws[1].id
local res = fn()
ngx.ctx.workspace = old_ws
return res
end


local make_temp_dir
do
local seeded = false

function make_temp_dir()
if not seeded then
ngx.update_time()
math.randomseed(ngx.worker.pid() + ngx.now())
seeded = true
end

local tmp
local ok, err

local tries = 1000
for _ = 1, tries do
local name = "/tmp/.kong-test" .. math.random()

ok, err = pl_path.mkdir(name)

if ok then
tmp = name
break
end
end

assert(tmp ~= nil, "failed to create temporary directory " ..
"after " .. tostring(tries) .. " tries, " ..
"last error: " .. tostring(err))

return tmp, function() pl_dir.rmtree(tmp) end
end
end


-- This function is used for plugin compatibility test.
-- It will use the old version plugin by including the path of the old plugin
-- at the first of LUA_PATH.
-- The return value is a function which when called will recover the original
-- LUA_PATH and remove the temporary directory if it exists.
-- For an example of how to use it, please see:
-- plugins-ee/rate-limiting-advanced/spec/06-old-plugin-compatibility_spec.lua
-- spec/03-plugins/03-http-log/05-old-plugin-compatibility_spec.lua
local function use_old_plugin(name)
assert(type(name) == "string", "must specify the plugin name")

local old_plugin_path
local temp_dir
if pl_path.exists(CONSTANTS.OLD_VERSION_KONG_PATH .. "/kong/plugins/" .. name) then
-- only include the path of the specified plugin into LUA_PATH
-- and keep the directory structure 'kong/plugins/...'
temp_dir = make_temp_dir()
old_plugin_path = temp_dir
local dest_dir = old_plugin_path .. "/kong/plugins"
assert(pl_dir.makepath(dest_dir), "failed to makepath " .. dest_dir)
assert(shell.run("cp -r " .. CONSTANTS.OLD_VERSION_KONG_PATH .. "/kong/plugins/" .. name .. " " .. dest_dir), "failed to copy the plugin directory")

else
error("the specified plugin " .. name .. " doesn't exist")
end

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")

return function ()
setenv("LUA_PATH", origin_lua_path)
if temp_dir then
pl_dir.rmtree(temp_dir)
end
end
end


return {
pack = pack,
unpack = unpack,
Expand All @@ -244,4 +330,8 @@ return {
generate_keys = generate_keys,

lookup = lookup,

with_current_ws = with_current_ws,
make_temp_dir = make_temp_dir,
use_old_plugin = use_old_plugin,
}
Loading