Skip to content

Commit

Permalink
Refactor luatest test related stuff
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ylobankov committed Feb 21, 2022
1 parent 5eadc16 commit f246567
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 70 deletions.
45 changes: 35 additions & 10 deletions test/instances/default.lua
@@ -1,19 +1,44 @@
#!/usr/bin/env tarantool

local helpers = require('test.luatest_helpers')
local fun = require('fun')
local json = require('json')

box.cfg(helpers.box_cfg())
local function default_cfg()
return {
work_dir = os.getenv('TARANTOOL_WORKDIR'),
listen = os.getenv('TARANTOOL_LISTEN'),
log = ('%s/%s.log'):format(os.getenv('TARANTOOL_WORKDIR'),
os.getenv('TARANTOOL_ALIAS')),
}
end

local function env_cfg()
local cfg = os.getenv('TARANTOOL_BOX_CFG')
if cfg == nil then
return {}
end

local res = json.decode(cfg)
assert(type(res) == 'table')
return res
end

-- Create a table for box.cfg from values passed while server initialization and
-- the given argument.
local function box_cfg(cfg)
return fun.chain(default_cfg(), env_cfg(), cfg or {}):tomap()
end

box.cfg(box_cfg())
box.schema.user.grant('guest', 'super', nil, nil, {if_not_exists = true})

-- luatest_helpers.Server:start() unblocks only when this variable
-- becomes true.
-- The Server:start function unblocks only when this variable becomes true.
--
-- Set it when the instance is fully operable:
-- * The server listens for requests.
-- * The database is bootstrapped.
-- * Permissions are granted.
--
-- * The server listens for requests.
-- * The database is bootstrapped.
-- * Permissions are granted.
--
-- Use luatest_helpers.Server:start({wait_for_readiness = false})
-- to don't wait for setting of this variable.
-- Use server:start({wait_for_readiness = false}) to not wait for setting this
-- variable.
_G.ready = true
44 changes: 0 additions & 44 deletions test/luatest_helpers.lua

This file was deleted.

14 changes: 9 additions & 5 deletions test/luatest_helpers/server.lua
Expand Up @@ -34,9 +34,13 @@ Server.constructor_checks = fun.chain(Server.constructor_checks, {
engine = '?string',
}):tomap()

function Server:initialize()
local vardir = fio.abspath(os.getenv('VARDIR') or 'test/var')
Server.socketdir = fio.abspath(os.getenv('VARDIR') or 'test/var')

function Server.build_instance_uri(alias)
return ('%s/%s.iproto'):format(Server.socketdir, alias)
end

function Server:initialize()
if self.id == nil then
local random = digest.urandom(9)
self.id = digest.base64_encode(random, {urlsafe = true})
Expand All @@ -45,13 +49,13 @@ function Server:initialize()
self.command = 'test/instances/default.lua'
end
if self.workdir == nil then
self.workdir = ('%s/%s-%s'):format(vardir, self.alias, self.id)
self.workdir = ('%s/%s-%s'):format(self.socketdir, self.alias, self.id)
fio.rmtree(self.workdir)
fio.mktree(self.workdir)
end
if self.net_box_port == nil and self.net_box_uri == nil then
self.net_box_uri = ('%s/%s.iproto'):format(vardir, self.alias)
fio.mktree(vardir)
self.net_box_uri = self.build_instance_uri(self.alias)
fio.mktree(self.socketdir)
end

-- AFAIU, the inner getmetatable() returns our helpers.Server
Expand Down
25 changes: 14 additions & 11 deletions test/test-luatest/smoke_check_test.lua
@@ -1,10 +1,11 @@
local t = require('luatest')
local g = t.group()

local luatest_helpers = require('test.luatest_helpers')
local server = require('test.luatest_helpers.server')

local g = t.group()

g.before_all = function()
g.server = luatest_helpers.Server:new({
g.server = server:new({
alias = 'my_server',
env = {MY_ENV_VAR = 'test_value'},
box_cfg = {memtx_memory = 100 * 1024 ^ 2},
Expand All @@ -16,20 +17,22 @@ g.after_all = function()
g.server:stop()
end

g.test_smoke = function()
-- The server is started and operable.
g.test_server_is_started_and_operable = function()
local res = g.server:eval('return 42')
t.assert_equals(res, 42)
end

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

-- The environment variable is passed.
local res = g.server:eval('return os.getenv("MY_ENV_VAR")')
g.test_environment_variable_is_passed = function()
local res = g.server:exec(function() return os.getenv('MY_ENV_VAR') end)
t.assert_equals(res, 'test_value')
end

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

0 comments on commit f246567

Please sign in to comment.