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

chore(ci): cleanup kong before running test suite (a spec file) #12567

Closed
wants to merge 1 commit into from
Closed
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
91 changes: 58 additions & 33 deletions spec/busted-ci-helper.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
-- busted-ci-helper.lua
local busted = require("busted")

local busted = require 'busted'
local cjson = require 'cjson'
local socket_unix = require 'socket.unix'
local function cleanup()
local prefix = assert(require("kong.conf_loader")(os.getenv("KONG_SPEC_TEST_CONF_PATH") or "spec/kong_tests.conf")).prefix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that only one prefix directory is being deleted here. However, if there are tests like spec-ee/02-integration/03-vitals/08-hybrid_spec.lua that create multiple prefix paths, how can we clear all of them?

os.remove(prefix .. "/worker_events.sock")
os.remove(prefix .. "/stream_worker_events.sock")
local pl_path = require "pl.path"
local pl_dir = require "pl.dir"
if pl_path.exists(prefix) and not pl_path.islink(prefix) then
for root, _, files in pl_dir.walk(prefix, true) do
if pl_path.islink(root) then
os.remove(root)
else
for _, f in ipairs(files) do
f = pl_path.join(root,f)
os.remove(f)
end
pl_path.rmdir(root)
end
end
end
return nil, true --continue
end

busted.subscribe({ "file", "start" }, cleanup)

local busted_event_path = os.getenv("BUSTED_EVENT_PATH")
if not busted_event_path then
return
end

local cjson = require("cjson")
local socket_unix = require("socket.unix")

-- Function to recursively copy a table, skipping keys associated with functions
local function copyTable(original, copied, cache, max_depth, current_depth)
Expand All @@ -31,37 +58,35 @@ local function copyTable(original, copied, cache, max_depth, current_depth)
return copied
end

if busted_event_path then
local sock = assert(socket_unix())
assert(sock:connect(busted_event_path))
local sock = assert(socket_unix())
assert(sock:connect(busted_event_path))

local events = {{ 'suite', 'reset' },
{ 'suite', 'start' },
{ 'suite', 'end' },
{ 'file', 'start' },
{ 'file', 'end' },
{ 'test', 'start' },
{ 'test', 'end' },
{ 'pending' },
{ 'failure', 'it' },
{ 'error', 'it' },
{ 'failure' },
{ 'error' }}
for _, event in ipairs(events) do
busted.subscribe(event, function (...)
local args = {}
for i, original in ipairs{...} do
if type(original) == "table" then
args[i] = copyTable(original)
elseif type(original) == "userdata" then
args[i] = tostring(original)
elseif type(original) ~= "function" then
args[i] = original
end
local events = {{ "suite", "reset" },
{ "suite", "start" },
{ "suite", "end" },
{ "file", "start" },
{ "file", "end" },
{ "test", "start" },
{ "test", "end" },
{ "pending" },
{ "failure", "it" },
{ "error", "it" },
{ "failure" },
{ "error" }}
for _, event in ipairs(events) do
busted.subscribe(event, function (...)
local args = {}
for i, original in ipairs{...} do
if type(original) == "table" then
args[i] = copyTable(original)
elseif type(original) == "userdata" then
args[i] = tostring(original)
elseif type(original) ~= "function" then
args[i] = original
end
end

sock:send(cjson.encode({ event = event[1] .. (event[2] and ":" .. event[2] or ""), args = args }) .. "\n")
return nil, true --continue
end)
end
sock:send(cjson.encode({ event = event[1] .. (event[2] and ":" .. event[2] or ""), args = args }) .. "\n")
return nil, true --continue
end)
end
Loading