Skip to content

Commit f246567

Browse files
committed
Refactor luatest test related stuff
- Remove test/luatest_helpers.lua - Move `default_cfg`, `env_cfg`, `box_cfg` functions from test/luatest_helpers.lua to test/instances/default.lua - Move `instance_uri` function from test/luatest_helpers.lua to test/luatest_helpers/server.lua and rename it into `build_instance_uri` - Break down one test into four smaller ones
1 parent 5eadc16 commit f246567

File tree

4 files changed

+58
-70
lines changed

4 files changed

+58
-70
lines changed

test/instances/default.lua

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
11
#!/usr/bin/env tarantool
22

3-
local helpers = require('test.luatest_helpers')
3+
local fun = require('fun')
4+
local json = require('json')
45

5-
box.cfg(helpers.box_cfg())
6+
local function default_cfg()
7+
return {
8+
work_dir = os.getenv('TARANTOOL_WORKDIR'),
9+
listen = os.getenv('TARANTOOL_LISTEN'),
10+
log = ('%s/%s.log'):format(os.getenv('TARANTOOL_WORKDIR'),
11+
os.getenv('TARANTOOL_ALIAS')),
12+
}
13+
end
14+
15+
local function env_cfg()
16+
local cfg = os.getenv('TARANTOOL_BOX_CFG')
17+
if cfg == nil then
18+
return {}
19+
end
20+
21+
local res = json.decode(cfg)
22+
assert(type(res) == 'table')
23+
return res
24+
end
25+
26+
-- Create a table for box.cfg from values passed while server initialization and
27+
-- the given argument.
28+
local function box_cfg(cfg)
29+
return fun.chain(default_cfg(), env_cfg(), cfg or {}):tomap()
30+
end
31+
32+
box.cfg(box_cfg())
633
box.schema.user.grant('guest', 'super', nil, nil, {if_not_exists = true})
734

8-
-- luatest_helpers.Server:start() unblocks only when this variable
9-
-- becomes true.
35+
-- The Server:start function unblocks only when this variable becomes true.
1036
--
1137
-- Set it when the instance is fully operable:
38+
-- * The server listens for requests.
39+
-- * The database is bootstrapped.
40+
-- * Permissions are granted.
1241
--
13-
-- * The server listens for requests.
14-
-- * The database is bootstrapped.
15-
-- * Permissions are granted.
16-
--
17-
-- Use luatest_helpers.Server:start({wait_for_readiness = false})
18-
-- to don't wait for setting of this variable.
42+
-- Use server:start({wait_for_readiness = false}) to not wait for setting this
43+
-- variable.
1944
_G.ready = true

test/luatest_helpers.lua

-44
This file was deleted.

test/luatest_helpers/server.lua

+9-5
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ Server.constructor_checks = fun.chain(Server.constructor_checks, {
3434
engine = '?string',
3535
}):tomap()
3636

37-
function Server:initialize()
38-
local vardir = fio.abspath(os.getenv('VARDIR') or 'test/var')
37+
Server.socketdir = fio.abspath(os.getenv('VARDIR') or 'test/var')
38+
39+
function Server.build_instance_uri(alias)
40+
return ('%s/%s.iproto'):format(Server.socketdir, alias)
41+
end
3942

43+
function Server:initialize()
4044
if self.id == nil then
4145
local random = digest.urandom(9)
4246
self.id = digest.base64_encode(random, {urlsafe = true})
@@ -45,13 +49,13 @@ function Server:initialize()
4549
self.command = 'test/instances/default.lua'
4650
end
4751
if self.workdir == nil then
48-
self.workdir = ('%s/%s-%s'):format(vardir, self.alias, self.id)
52+
self.workdir = ('%s/%s-%s'):format(self.socketdir, self.alias, self.id)
4953
fio.rmtree(self.workdir)
5054
fio.mktree(self.workdir)
5155
end
5256
if self.net_box_port == nil and self.net_box_uri == nil then
53-
self.net_box_uri = ('%s/%s.iproto'):format(vardir, self.alias)
54-
fio.mktree(vardir)
57+
self.net_box_uri = self.build_instance_uri(self.alias)
58+
fio.mktree(self.socketdir)
5559
end
5660

5761
-- AFAIU, the inner getmetatable() returns our helpers.Server
+14-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
local t = require('luatest')
2-
local g = t.group()
32

4-
local luatest_helpers = require('test.luatest_helpers')
3+
local server = require('test.luatest_helpers.server')
4+
5+
local g = t.group()
56

67
g.before_all = function()
7-
g.server = luatest_helpers.Server:new({
8+
g.server = server:new({
89
alias = 'my_server',
910
env = {MY_ENV_VAR = 'test_value'},
1011
box_cfg = {memtx_memory = 100 * 1024 ^ 2},
@@ -16,20 +17,22 @@ g.after_all = function()
1617
g.server:stop()
1718
end
1819

19-
g.test_smoke = function()
20-
-- The server is started and operable.
20+
g.test_server_is_started_and_operable = function()
2121
local res = g.server:eval('return 42')
2222
t.assert_equals(res, 42)
23+
end
2324

24-
-- The database is bootstrapped and accessible.
25-
local res = g.server:eval('return box.info.status')
25+
g.test_database_is_bootstrapped_and_accessible = function()
26+
local res = g.server:exec(function() return box.info.status end)
2627
t.assert_equals(res, 'running')
28+
end
2729

28-
-- The environment variable is passed.
29-
local res = g.server:eval('return os.getenv("MY_ENV_VAR")')
30+
g.test_environment_variable_is_passed = function()
31+
local res = g.server:exec(function() return os.getenv('MY_ENV_VAR') end)
3032
t.assert_equals(res, 'test_value')
33+
end
3134

32-
-- The box.cfg() values are passed as well.
33-
local res = g.server:eval('return box.cfg.memtx_memory')
35+
g.test_box_cfg_values_are_passed = function()
36+
local res = g.server:exec(function() return box.cfg.memtx_memory end)
3437
t.assert_equals(res, 100 * 1024 ^ 2)
3538
end

0 commit comments

Comments
 (0)