Skip to content

Commit 52d261f

Browse files
committed
test: fix router-luatest/reload_test not reloading vshard
Before this commit the test loaded only the latest version, so the test ran 2 times on the latest commit and did not test the actual hot-reload. It tried to start the router on the latest version, changed `package.loaded` and that's it, `vshard.router` even wasn't required after that. Since we don't support hot-reload from new version to the old one (see 0.1.34 release notes), let's actually create the router on old version from the first start and then reload it to the latest. For that let's use `LUA_PATH` environment variable. The alternative of using `package.path` was rejected, since it requires modifying `instances/router.lua` file. However, changing `LUA_PATH` so that it points to `vshard_copy` directory is not enough. The test runs from the repository directory and `cwd` has higher priority, than `package.path` (which is set to `LUA_PATH`). To bypass that, let's move the vshard copy to the `override` directory, it has priority over `cwd`. This commit also replaces all usages of `vtest.router_cfg` with local function, since `ivtest.clear_test_cfg_options` may not present on old version. We also need to wait for service to be stopped before running the test after reload during master_search service. The alternative solution is using `router.master_search_f` to determine on which version function is running. It's nullified right on reload. But let's better test, that old services are actually stopped as soon as they're not needed. Closes tarantool/vshard-ee#11 Co-authored-by: Roman Gromov <r.gromov.official@gmail.com> NO_DOC=testfix
1 parent 0fd3693 commit 52d261f

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

test/luatest_helpers/vtest.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,13 +656,13 @@ end
656656
-- Create a new router in the cluster.
657657
-- If no cfg was passed configuration should be done manually with server:exec
658658
--
659-
local function router_new(g, name, cfg)
659+
local function router_new(g, name, cfg, server_config)
660660
if not g.cluster then
661661
g.cluster = cluster:new({})
662662
end
663-
local server = g.cluster:build_server({
664-
alias = name,
665-
}, 'router.lua')
663+
server_config = server_config or {}
664+
server_config.alias = name
665+
local server = g.cluster:build_server(server_config, 'router.lua')
666666
g[name] = server
667667
g.cluster:add_server(server)
668668
server:start()

test/router-luatest/reload_test.lua

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local t = require('luatest')
2+
local vutil = require('vshard.util')
23
local vtest = require('test.luatest_helpers.vtest')
34
local git_util = require('test.lua_libs.git_util')
45
local fio = require('fio')
@@ -27,11 +28,31 @@ local cfg_template = {
2728
local global_cfg
2829

2930
g.before_all(function()
31+
-- Override of the built in modules is available only since 2.11.0.
32+
t.run_only_if(vutil.version_is_at_least(2, 11, 0, nil, 0, 0))
3033
global_cfg = vtest.config_new(cfg_template)
3134

3235
-- The test works in the following directory
3336
local vardir = vtest.vardir or fio.tempdir()
34-
g.vshard_copy_path = vardir .. '/vshard_copy'
37+
g.vshard_copy_path_load = vardir .. '/vshard_copy'
38+
t.assert_equals(fio.mkdir(g.vshard_copy_path_load), true)
39+
--
40+
-- Tarantool searches for compilation units in the following order:
41+
-- 1. preload --> override --> builtin
42+
-- 2. path.cwd.dot
43+
-- 3. cpath.cwd.dot
44+
-- 4. path.cwd.rocks
45+
-- 5. cpath.cwd.rocks
46+
-- 6. package.path
47+
-- 7. package.cpath
48+
-- 8. croot
49+
--
50+
-- Since the test is launched from the repository directory, newest
51+
-- vshard will be always loaded, disregarding package.path or `LUA_PATH`.
52+
-- So we can use package.setsearchroot() to change cwd, luatest's `chdir`
53+
-- or override. The last one is used here, since it's the easiest.
54+
--
55+
g.vshard_copy_path = vardir .. '/vshard_copy/override'
3556
t.assert_equals(fio.mkdir(g.vshard_copy_path), true)
3657
-- Copy source to the temporary directory
3758
t.assert_equals(fio.mkdir(g.vshard_copy_path .. '/.git'), true)
@@ -71,6 +92,16 @@ g.after_all(function()
7192
g.cluster:drop()
7293
end)
7394

95+
--
96+
-- `vtest.router_cfg` cannot be used in this test, since
97+
-- `ivtest.clear_test_cfg_options` may be nil on old versions.
98+
--
99+
local function router_cfg(router, cfg)
100+
router:exec(function(cfg)
101+
ivshard.router.cfg(cfg)
102+
end, {cfg})
103+
end
104+
74105
--
75106
-- Reload test template:
76107
-- 1. Invoke create_router_at:
@@ -83,21 +114,17 @@ end)
83114
-- 5. Test smth on the new version
84115
-- 6. Drop a router with vtest.drop_instance
85116
--
86-
87117
local function create_router_at(hash)
88118
git_util.exec('checkout', {args = hash .. ' -f', dir = g.vshard_copy_path})
89-
local router = vtest.router_new(g, 'router')
90-
router:exec(function(vshard_path)
91-
-- Force 'require' to use new directory
92-
package.path = string.format('%s/?.lua;%s/?/init.lua;',
93-
vshard_path, vshard_path)
94-
for package_name in pairs(package.loaded) do
95-
if package_name:startswith('vshard') then
96-
package.loaded[package_name] = nil
97-
end
98-
end
99-
end, {g.vshard_copy_path})
100-
vtest.router_cfg(router, global_cfg)
119+
local path = g.vshard_copy_path_load
120+
local lua_path = string.format("%s/?.lua;%s/?/init.lua;", path, path)
121+
local router = vtest.router_new(g, 'router', nil, {
122+
env = {
123+
-- Force 'require' to use new directory
124+
['LUA_PATH'] = lua_path .. os.getenv('LUA_PATH')
125+
},
126+
})
127+
router_cfg(router, global_cfg)
101128
return router
102129
end
103130

@@ -196,7 +223,7 @@ g.test_discovery = function(g)
196223
end
197224

198225
local function test_master_search_template(g, router, auto_master_cfg)
199-
vtest.router_cfg(router, auto_master_cfg)
226+
router_cfg(router, auto_master_cfg)
200227

201228
-- Working with first replicaset (2 instances)
202229
local rs_uuid = g.replica_1_a:replicaset_uuid()
@@ -288,6 +315,17 @@ g.test_master_search = function(g)
288315

289316
test_master_search_template(g, router, auto_master_cfg)
290317
reload_router(router)
318+
-- Wait for old master_search service to be stopped.
319+
router:exec(function()
320+
local router = ivshard.router.internal.static_router
321+
ilt.helpers.retrying({}, function()
322+
local fiber = router.master_search_fiber
323+
if fiber then
324+
pcall(fiber.wakeup, fiber)
325+
end
326+
ilt.assert_equals(router.master_search_service, nil)
327+
end)
328+
end)
291329
test_master_search_template(g, router, auto_master_cfg)
292330
vtest.drop_instance(g, router)
293331
end

0 commit comments

Comments
 (0)