Skip to content

Commit

Permalink
tests(router): remove duplicated reload_router() (#13693)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw authored Sep 27, 2024
1 parent a6ccbb1 commit b64b6fb
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 213 deletions.
19 changes: 1 addition & 18 deletions spec/02-integration/05-proxy/01-proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,7 @@ end)


local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)
end


Expand Down
19 changes: 1 addition & 18 deletions spec/02-integration/05-proxy/02-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2673,24 +2673,7 @@ end
-- http expression 'http.queries.*'
do
local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)
end


Expand Down
19 changes: 1 addition & 18 deletions spec/02-integration/05-proxy/06-ssl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,7 @@ local fixtures = {
}

local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)

fixtures.dns_mock = helpers.dns_mock.new({ mocks_only = true })
fixtures.dns_mock:A {
Expand Down
19 changes: 1 addition & 18 deletions spec/02-integration/05-proxy/10-balancer/06-stream_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,7 @@ local helpers = require "spec.helpers"


local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)
end


Expand Down
19 changes: 1 addition & 18 deletions spec/02-integration/05-proxy/18-upstream_tls_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,7 @@ local fixtures = {


local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)

fixtures.dns_mock = helpers.dns_mock.new({ mocks_only = true })
fixtures.dns_mock:A {
Expand Down
19 changes: 1 addition & 18 deletions spec/02-integration/05-proxy/19-grpc_proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,7 @@ local FILE_LOG_PATH = os.tmpname()


local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)
end


Expand Down
19 changes: 1 addition & 18 deletions spec/02-integration/05-proxy/21-grpc_plugins_triggering_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,7 @@ local TEST_CONF = helpers.test_conf


local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)
end


Expand Down
19 changes: 1 addition & 18 deletions spec/02-integration/05-proxy/23-context_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,7 @@ local null = ngx.null


local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)
end


Expand Down
19 changes: 1 addition & 18 deletions spec/02-integration/05-proxy/26-udp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,7 @@ local UDP_PROXY_PORT = 26001


local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)
end


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,7 @@ end


local function reload_router(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

helpers.setenv("KONG_ROUTER_FLAVOR", flavor)

package.loaded["spec.helpers"] = nil
package.loaded["kong.global"] = nil
package.loaded["kong.cache"] = nil
package.loaded["kong.db"] = nil
package.loaded["kong.db.schema.entities.routes"] = nil
package.loaded["kong.db.schema.entities.routes_subschemas"] = nil

helpers = require "spec.helpers"

helpers.unsetenv("KONG_ROUTER_FLAVOR")
helpers = require("spec.internal.module").reload_helpers(flavor)
end


Expand Down
38 changes: 5 additions & 33 deletions spec/internal/misc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
-- miscellaneous


local ffi = require("ffi")
local pl_path = require("pl.path")
local pl_dir = require("pl.dir")
local pkey = require("resty.openssl.pkey")
Expand All @@ -18,12 +17,7 @@ local shell = require("spec.internal.shell")


local CONSTANTS = require("spec.internal.constants")


ffi.cdef [[
int setenv(const char *name, const char *value, int overwrite);
int unsetenv(const char *name);
]]
local sys = require("spec.internal.sys")


local pack = function(...) return { n = select("#", ...), ... } end
Expand Down Expand Up @@ -123,28 +117,6 @@ local function make_yaml_file(content, filename)
end


--- Set an environment variable
-- @function setenv
-- @param env (string) name of the environment variable
-- @param value the value to set
-- @return true on success, false otherwise
local function setenv(env, value)
assert(type(env) == "string", "env must be a string")
assert(type(value) == "string", "value must be a string")
return ffi.C.setenv(env, value, 1) == 0
end


--- Unset an environment variable
-- @function unsetenv
-- @param env (string) name of the environment variable
-- @return true on success, false otherwise
local function unsetenv(env)
assert(type(env) == "string", "env must be a string")
return ffi.C.unsetenv(env) == 0
end


local deep_sort
do
local function deep_compare(a, b)
Expand Down Expand Up @@ -304,10 +276,10 @@ local function use_old_plugin(name)

local origin_lua_path = os.getenv("LUA_PATH")
-- put the old plugin path at first
assert(setenv("LUA_PATH", old_plugin_path .. "/?.lua;" .. old_plugin_path .. "/?/init.lua;" .. origin_lua_path), "failed to set LUA_PATH env")
assert(sys.setenv("LUA_PATH", old_plugin_path .. "/?.lua;" .. old_plugin_path .. "/?/init.lua;" .. origin_lua_path), "failed to set LUA_PATH env")

return function ()
setenv("LUA_PATH", origin_lua_path)
sys.setenv("LUA_PATH", origin_lua_path)
if temp_dir then
pl_dir.rmtree(temp_dir)
end
Expand All @@ -323,8 +295,8 @@ return {
openresty_ver_num = openresty_ver_num(),
unindent = unindent,
make_yaml_file = make_yaml_file,
setenv = setenv,
unsetenv = unsetenv,
setenv = sys.setenv,
unsetenv = sys.unsetenv,
deep_sort = deep_sort,

generate_keys = generate_keys,
Expand Down
32 changes: 32 additions & 0 deletions spec/internal/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,38 @@ local function reload(name)
end


local reload_helpers
do
local sys = require("spec.internal.sys")

-- flavor could be "traditional","traditional_compatible" or "expressions"
-- changing flavor will change db's schema
reload_helpers = function(flavor)
_G.kong = {
configuration = {
router_flavor = flavor,
},
}

sys.setenv("KONG_ROUTER_FLAVOR", flavor)

-- reload db and global module
reload("kong.db.schema.entities.routes_subschemas")
reload("kong.db.schema.entities.routes")
reload("kong.cache")
reload("kong.global")

-- reload helpers module
local helpers = reload("spec.helpers")

sys.unsetenv("KONG_ROUTER_FLAVOR")

return helpers
end
end


return {
reload = reload,
reload_helpers = reload_helpers,
}
Loading

1 comment on commit b64b6fb

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:b64b6fb4ce7cef060c798d8b10ba83dda028ed92
Artifacts available https://github.com/Kong/kong/actions/runs/11067439036

Please sign in to comment.