diff --git a/spec/helpers.lua b/spec/helpers.lua index 22b67c4434d3..3bd1a404c8c2 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -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 @@ -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 @@ -75,7 +99,7 @@ local wait = reload_module("spec.internal.wait") -- Exposed ---------- -- @export - return { + return setmetatable({ -- Penlight dir = require("pl.dir"), path = require("pl.path"), @@ -83,11 +107,6 @@ local wait = reload_module("spec.internal.wait") 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, @@ -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"), @@ -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) diff --git a/spec/internal/cmd.lua b/spec/internal/cmd.lua index 1c8bf3b0568d..7d3b86896f02 100644 --- a/spec/internal/cmd.lua +++ b/spec/internal/cmd.lua @@ -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") @@ -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