Skip to content

Commit

Permalink
details.shell
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Aug 22, 2024
1 parent 160a6ae commit 95d257c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 99 deletions.
99 changes: 3 additions & 96 deletions spec/details/misc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -297,9 +208,5 @@ return {
unsetenv = unsetenv,
deep_sort = deep_sort,

conf = conf,
exec = exec,
kong_exec = kong_exec,

generate_keys = generate_keys,
}
8 changes: 5 additions & 3 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 95d257c

Please sign in to comment.