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

test(migration): support access to latest code #11020

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions scripts/upgrade-tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ services:
KONG_TEST_PG_HOST: db_postgres
volumes:
- ../../worktree/${OLD_KONG_VERSION}:/kong
- ../..:/upgrade-test/latest
restart: on-failure
networks:
upgrade_tests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,10 @@ local cjson = require "cjson"
local tablex = require "pl.tablex"

local uh = require "spec/upgrade_helpers"
local new_helpers = uh.last_helpers
local http_server = new_helpers.http_server

local HTTP_PORT = 29100

-- Copied from 3.x helpers.lua

local function http_server(port, opts)
local threads = require "llthreads2.ex"
opts = opts or {}
local thread = threads.new(
{
function(port, opts)
local socket = require "socket"
local server = assert(socket.tcp())
server:settimeout(opts.timeout or 60)
assert(server:setoption('reuseaddr', true))
assert(server:bind("*", port))
assert(server:listen())
local client = assert(server:accept())

local lines = {}
local line, err
repeat
line, err = client:receive("*l")
if err then
break
else
table.insert(lines, line)
end
until line == ""

if #lines > 0 and lines[1] == "GET /delay HTTP/1.0" then
ngx.sleep(2)
end

if err then
server:close()
error(err)
end

local body, _ = client:receive("*a")

client:send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n")
client:close()
server:close()

return lines, body
end
},
port, opts)
return thread:start()
end
local HTTP_PORT = new_helpers.get_available_port()

describe("http-log plugin migration", function()

Expand Down
108 changes: 108 additions & 0 deletions spec/string_buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
-- this is a backport of `string.buffer` module of LuaJIT
-- for older version of Kong code to run tests with new helper functions
-- this implementation only promise the same behavior as the original but not the same performance
-- and the methods decode()/encode() are not implemented

local ffi = require "ffi"
local tbl_new = require "table.new"
local tbl_clr = require "table.clear"

local _M = {}
_M.__index = _M

function _M.new(size)
local buf = tbl_new(size or 0, 0)
buf.n = 0
return setmetatable(buf, _M)
end

function _M:put(...)
local n = select("#", ...)
if n == 0 then
return
end

local buf = self
local offset = self.n
self.n = offset + n

for i = 1, n do
local item = select(i, ...)
buf[offset + i] = tostring(item)
end

return buf
end

function _M:putf(fmt, ...)
return self:put(string.format(fmt, ...))
end

function _M:putcdata(cdata, len)
return self:put(ffi.string(cdata, len))
end

function _M:set(str, len)
self:reset()
if len then
return self:putcdata(str, len)
else
return self:put(str)
end
end

function _M:reserve()
end

function _M:commit()
end

function _M:reset()
tbl_clr(self)
self.n = 0
return self
end

function _M:tostring()
local result = table.concat(self)
self:reset()
self[1] = result
self.n = 1
return result
end

_M.__tostring = _M.tostring

function _M:ref()
return ffi.cast("const char *", self:tostring())
end

function _M:__len()
return #self:tostring()
end

function _M:skip(len)
self[1] = self:tostring():sub(len + 1)
end

function _M:get(...)
local str = self:tostring()
local n = select("#", ...)

local offset = 0
local results = {}
for i = 1, n do
local len = select(i, ...)
local result = str:sub(offset + 1, offset + len)
offset = offset + len
results[i] = result
end

self[1] = str:sub(offset + 1)

return unpack(results)
end

_M.free = _M.reset

return _M
50 changes: 49 additions & 1 deletion spec/upgrade_helpers.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
setmetatable(_G, nil)

local say = require "say"
local assert = require "luassert"

Expand Down Expand Up @@ -151,6 +153,50 @@ local function all_phases(phrase, f)
return it_when("all_phases", phrase, f)
end

local latest_kong_require_meta

-- This is a hack to make the latest kong module and helper functions available to the test suite
local function latest_kong_require(module)
-- backport of `string.buffer` module of LuaJIT
if module == "string.buffer" then
module = "latest.spec.string_buffer"
elseif "kong." == module:sub(1, 5) or "spec." == module:sub(1, 5) then
module = "latest." .. module
end

if package.loaded[module] then
return package.loaded[module]
end

-- kong module is too heavy to load, and we need to even go through the whole
-- build process to get it, so we just return an empty table and leave the functions
-- that uses them not implemented
if "latest.kong" == module:sub(1, 11) then
package.loaded[module] = {}
return package.loaded[module]
end

local path = package.searchpath(module, package.path .. ";./?/init.lua")

-- some buildin modules like ffi don't have a path
if not path then
return require(module)
end

local module_code = assert(loadfile(path))
-- recursively load with lastest code
setfenv(module_code, latest_kong_require_meta)

package.loaded[module] = assert(module_code())

return package.loaded[module]
end

latest_kong_require_meta = { require = latest_kong_require, __index = _G }
setmetatable(latest_kong_require_meta, latest_kong_require_meta)

local latest_helpers = latest_kong_require "spec.helpers"

return {
database_type = database_type,
get_database = get_database,
Expand All @@ -164,5 +210,7 @@ return {
old_after_up = old_after_up,
new_after_up = new_after_up,
new_after_finish = new_after_finish,
all_phases = all_phases
all_phases = all_phases,
latest_kong_require = latest_kong_require,
latest_helpers = latest_helpers,
}