diff --git a/scripts/upgrade-tests/docker-compose.yml b/scripts/upgrade-tests/docker-compose.yml index a127a91b011..6f73a512cd0 100644 --- a/scripts/upgrade-tests/docker-compose.yml +++ b/scripts/upgrade-tests/docker-compose.yml @@ -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: diff --git a/spec/05-migration/plugins/http-log/migrations/001_280_to_300_spec.lua b/spec/05-migration/plugins/http-log/migrations/001_280_to_300_spec.lua index 4399aef2a45..54da7f9e9b9 100644 --- a/spec/05-migration/plugins/http-log/migrations/001_280_to_300_spec.lua +++ b/spec/05-migration/plugins/http-log/migrations/001_280_to_300_spec.lua @@ -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() diff --git a/spec/string_buffer.lua b/spec/string_buffer.lua new file mode 100644 index 00000000000..fe5e544f378 --- /dev/null +++ b/spec/string_buffer.lua @@ -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 diff --git a/spec/upgrade_helpers.lua b/spec/upgrade_helpers.lua index c7426ee1cde..8f8bd581eef 100644 --- a/spec/upgrade_helpers.lua +++ b/spec/upgrade_helpers.lua @@ -1,3 +1,5 @@ +setmetatable(_G, nil) + local say = require "say" local assert = require "luassert" @@ -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, @@ -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, }