Skip to content

Commit

Permalink
chore(tests): make DB helpers imported on demand
Browse files Browse the repository at this point in the history
This makes helpers function available for env where no DB provided
  • Loading branch information
StarlightIbuki committed Dec 10, 2024
1 parent 96076c3 commit 82e73b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
41 changes: 27 additions & 14 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ local CONSTANTS = reload_module("spec.internal.constants")
local conf = reload_module("spec.internal.conf")
local shell = reload_module("spec.internal.shell")
local misc = reload_module("spec.internal.misc")
local DB = reload_module("spec.internal.db")
local grpc = reload_module("spec.internal.grpc")
local dns_mock = reload_module("spec.internal.dns")
local asserts = reload_module("spec.internal.asserts") -- luacheck: ignore
Expand All @@ -29,6 +28,31 @@ local client = reload_module("spec.internal.client")
local wait = reload_module("spec.internal.wait")


local db_entries = {
blueprints = true,
get_db_utils = true,
get_cache = true,
bootstrap_database = true,
each_strategy = true,
all_strategies = true,
validate_plugin_config_schema = true,
get_plugins_list = true,
}


local lazy_db_load_mt = {
__index = function(self, key)
if db_entries[key] or key == "db" then
DB = reload_module("spec.internal.db")
self.db = DB
for k, _ in pairs(db_entries) do
self[k] = DB[k]
end

return rawget(self, key)
end
end,
}
----------------
-- Variables/constants
-- @section exported-fields
Expand Down Expand Up @@ -75,19 +99,14 @@ local wait = reload_module("spec.internal.wait")
-- Exposed
----------
-- @export
return {
return setmetatable({
-- Penlight
dir = require("pl.dir"),
path = require("pl.path"),
file = require("pl.file"),
utils = require("pl.utils"),

-- Kong testing properties
db = DB.db,
blueprints = DB.blueprints,
get_db_utils = DB.get_db_utils,
get_cache = DB.get_cache,
bootstrap_database = DB.bootstrap_database,
bin_path = CONSTANTS.BIN_PATH,
test_conf = conf,
test_conf_path = CONSTANTS.TEST_CONF_PATH,
Expand Down Expand Up @@ -175,9 +194,6 @@ local wait = reload_module("spec.internal.wait")
clean_prefix = cmd.clean_prefix,
clean_logfile = cmd.clean_logfile,
wait_for_invalidation = wait.wait_for_invalidation,
each_strategy = DB.each_strategy,
all_strategies = DB.all_strategies,
validate_plugin_config_schema = DB.validate_plugin_config_schema,
clustering_client = client.clustering_client,
https_server = require("spec.fixtures.https_server"),
stress_generator = require("spec.fixtures.stress_generator"),
Expand Down Expand Up @@ -218,12 +234,9 @@ local wait = reload_module("spec.internal.wait")
-- 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 = DB.clone_plugins_list,

get_available_port = wait.get_available_port,

make_temp_dir = misc.make_temp_dir,

build_go_plugins = cmd.build_go_plugins,
}
}, lazy_db_load_mt)
6 changes: 5 additions & 1 deletion spec/internal/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local prefix_handler = require("kong.cmd.utils.prefix_handler")
local CONSTANTS = require("spec.internal.constants")
local conf = require("spec.internal.conf")
local shell = require("spec.internal.shell")
local DB = require("spec.internal.db")
local DB
local pid = require("spec.internal.pid")
local dns_mock = require("spec.internal.dns")

Expand Down Expand Up @@ -263,6 +263,10 @@ end
--
-- assert(helpers.start_kong( {database = "postgres"}, nil, nil, fixtures))
local function start_kong(env, tables, preserve_prefix, fixtures)
if not DB then
DB = require("spec.internal.db")
end

if tables ~= nil and type(tables) ~= "table" then
error("arg #2 must be a list of tables to truncate")
end
Expand Down

0 comments on commit 82e73b7

Please sign in to comment.