From e8b0dcf6cd0592f7611881e948c58697a9213018 Mon Sep 17 00:00:00 2001 From: Yaroslav Lobankov <y.lobankov@tarantool.org> Date: Mon, 26 Aug 2024 19:12:57 +0400 Subject: [PATCH] test: use `treegen` module from luatest Also, adapt tests and helpers in accordance with the module interface. NO_DOC=test NO_TEST=test NO_CHANGELOG=test (cherry picked from commit bd27df009c403e89c003d5b66763c0f0bbf08440) --- ...0_abort_recording_on_fiber_switch_test.lua | 13 +- .../gh_5983_jit_library_smoke_tests_test.lua | 7 +- test/app-luatest/module_search_test.lua | 21 +-- test/app-luatest/override_misc_test.lua | 21 +-- test/app-luatest/override_panic_test.lua | 26 +-- test/app-luatest/override_test.lua | 25 ++- test/app-luatest/preload_test.lua | 17 +- test/box-luatest/box_cfg_env_test.lua | 17 +- .../gh_8051_set_box_cfg_thru_env_test.lua | 12 +- test/treegen.lua | 170 ------------------ 10 files changed, 57 insertions(+), 272 deletions(-) delete mode 100644 test/treegen.lua diff --git a/test/app-luatest/gh_1700_abort_recording_on_fiber_switch_test.lua b/test/app-luatest/gh_1700_abort_recording_on_fiber_switch_test.lua index 16574b6335..86b7131096 100644 --- a/test/app-luatest/gh_1700_abort_recording_on_fiber_switch_test.lua +++ b/test/app-luatest/gh_1700_abort_recording_on_fiber_switch_test.lua @@ -1,5 +1,5 @@ local t = require('luatest') -local treegen = require('test.treegen') +local treegen = require('luatest.treegen') local justrun = require('test.justrun').tarantool local pathjoin = require('fio').pathjoin @@ -48,17 +48,12 @@ local env = { LUA_CPATH = ('%s/?.%s;%s;'):format(libpath, libext, os.getenv('LUA_CPATH')), } -g.before_all(function(g) +g.before_all(function() t.skip_if(not jit.status(), 'Test requires JIT enabled') t.skip_if(jit.os == 'BSD', 'Disabled on *BSD due to #4819') - treegen.init(g) - treegen.add_template(g, '^script%.lua$', script) - rundir = treegen.prepare_directory(g, {'script.lua'}) -end) - -g.after_all(function(g) - treegen.clean(g) + treegen.add_template('^script%.lua$', script) + rundir = treegen.prepare_directory({'script.lua'}) end) local function runcmd(...) diff --git a/test/app-luatest/gh_5983_jit_library_smoke_tests_test.lua b/test/app-luatest/gh_5983_jit_library_smoke_tests_test.lua index a6a0d4f78b..fb7d7d957c 100644 --- a/test/app-luatest/gh_5983_jit_library_smoke_tests_test.lua +++ b/test/app-luatest/gh_5983_jit_library_smoke_tests_test.lua @@ -1,5 +1,5 @@ local t = require('luatest') -local treegen = require('test.treegen') +local treegen = require('luatest.treegen') local justrun = require('test.justrun').tarantool local g = t.group() @@ -20,9 +20,8 @@ g.before_each(function() t.skip_if(not jit.status(), 'Test requires JIT enabled') t.skip_if(jit.os == 'BSD', 'Disabled on *BSD due to #4819') - treegen.init(g) - treegen.add_template(g, '^script%.lua$', script) - rundir = treegen.prepare_directory(g, {'script.lua'}) + treegen.add_template('^script%.lua$', script) + rundir = treegen.prepare_directory({'script.lua'}) end) g.test_jit_dump = function() diff --git a/test/app-luatest/module_search_test.lua b/test/app-luatest/module_search_test.lua index 5741f556f4..33d2080e15 100644 --- a/test/app-luatest/module_search_test.lua +++ b/test/app-luatest/module_search_test.lua @@ -1,14 +1,14 @@ local fio = require('fio') local t = require('luatest') -local treegen = require('test.treegen') +local treegen = require('luatest.treegen') local justrun = require('test.justrun') local g = t.group() local MODULE_SCRIPT_TEMPLATE = [[ print(require('json').encode({ - ['script'] = '<script>', + ['script'] = '<filename>', })) return {whoami = '<module_name>'} ]] @@ -16,19 +16,14 @@ return {whoami = '<module_name>'} -- Print a result of the require call. local MAIN_SCRIPT_TEMPLATE = [[ print(require('json').encode({ - ['script'] = '<script>', + ['script'] = '<filename>', ['<module_name>'] = require('<module_name>'), })) ]] -g.before_all(function(g) - treegen.init(g) - treegen.add_template(g, '^main%.lua$', MAIN_SCRIPT_TEMPLATE) - treegen.add_template(g, '^.*%.lua$', MODULE_SCRIPT_TEMPLATE) -end) - -g.after_all(function(g) - treegen.clean(g) +g.before_all(function() + treegen.add_template('^main%.lua$', MAIN_SCRIPT_TEMPLATE) + treegen.add_template('^.*%.lua$', MODULE_SCRIPT_TEMPLATE) end) local function expected_output(module_relpath, module_name) @@ -75,10 +70,10 @@ for _, case in ipairs({ local module_slug = module_relpath :gsub('^%.rocks/share/tarantool/', 'rocks/'):gsub('/', '_'):sub(1, -5) - g['test_' .. module_slug] = function(g) + g['test_' .. module_slug] = function() local scripts = {module_relpath, 'main.lua'} local replacements = {module_name = module_name} - local dir = treegen.prepare_directory(g, scripts, replacements) + local dir = treegen.prepare_directory(scripts, replacements) local main_script = fio.pathjoin(dir, 'main.lua') -- The current working directory is in the filesystem -- root, so the only way to reach the modules is to search diff --git a/test/app-luatest/override_misc_test.lua b/test/app-luatest/override_misc_test.lua index 96fa750c1f..04a2be6342 100644 --- a/test/app-luatest/override_misc_test.lua +++ b/test/app-luatest/override_misc_test.lua @@ -1,5 +1,5 @@ local t = require('luatest') -local treegen = require('test.treegen') +local treegen = require('luatest.treegen') local justrun = require('test.justrun') local g = t.group() @@ -20,21 +20,16 @@ local loaders = require('internal.loaders') print(json.encode({ ['<module_name>'] = require('<module_name>'), - ['<script>'] = { - whoami = '<script>', + ['<filename>'] = { + whoami = '<filename>', initializing = loaders.initializing, } })) ]] -g.before_all(function(g) - treegen.init(g) - treegen.add_template(g, '^override/.*%.lua$', OVERRIDE_SCRIPT_TEMPLATE) - treegen.add_template(g, '^main%.lua$', MAIN_SCRIPT_TEMPLATE) -end) - -g.after_all(function(g) - treegen.clean(g) +g.before_all(function() + treegen.add_template('^override/.*%.lua$', OVERRIDE_SCRIPT_TEMPLATE) + treegen.add_template('^main%.lua$', MAIN_SCRIPT_TEMPLATE) end) local function expected_output(module_name) @@ -57,10 +52,10 @@ local function expected_output(module_name) } end -g.test_initializing = function(g) +g.test_initializing = function() local scripts = {'override/socket.lua', 'main.lua'} local replacements = {module_name = 'socket'} - local dir = treegen.prepare_directory(g, scripts, replacements) + local dir = treegen.prepare_directory(scripts, replacements) local res = justrun.tarantool(dir, {}, {'main.lua'}) local exp = expected_output('socket') t.assert_equals(res, exp) diff --git a/test/app-luatest/override_panic_test.lua b/test/app-luatest/override_panic_test.lua index ab7db52fec..a0d693c96c 100644 --- a/test/app-luatest/override_panic_test.lua +++ b/test/app-luatest/override_panic_test.lua @@ -1,17 +1,9 @@ local t = require('luatest') -local treegen = require('test.treegen') +local treegen = require('luatest.treegen') local justrun = require('test.justrun') local g = t.group() -g.before_all(function(g) - treegen.init(g) -end) - -g.after_all(function(g) - treegen.clean(g) -end) - -- Trigger luaT_newmodule() panic. -- -- fio is required internally before box initialization, so we can @@ -20,14 +12,14 @@ end) -- already registered. -- -- luaT_newmodule() must panic in the case. -g.test_newmodule_panic = function(g) - local dir = treegen.prepare_directory(g, {}, {}) - treegen.write_script(dir, 'override/fio.lua', [[ +g.test_newmodule_panic = function() + local dir = treegen.prepare_directory({}, {}) + treegen.write_file(dir, 'override/fio.lua', [[ local loaders = require('internal.loaders') loaders.builtin.box = {} return loaders.builtin.fio ]]) - treegen.write_script(dir, 'main.lua', '') + treegen.write_file(dir, 'main.lua', '') local opts = {nojson = true, stderr = true} local res = justrun.tarantool(dir, {}, {'main.lua'}, opts) t.assert_equals(res, { @@ -58,12 +50,12 @@ end -- -- luaT_setmodule() must panic if it meets attempt to register -- different values as the same built-in module. -g.test_setmodule_panic = function(g) - local dir = treegen.prepare_directory(g, {}, {}) - treegen.write_script(dir, 'override/fio.lua', [[ +g.test_setmodule_panic = function() + local dir = treegen.prepare_directory({}, {}) + treegen.write_file(dir, 'override/fio.lua', [[ return {} ]]) - treegen.write_script(dir, 'main.lua', '') + treegen.write_file(dir, 'main.lua', '') local opts = {nojson = true, stderr = true} local res = justrun.tarantool(dir, {}, {'main.lua'}, opts) t.assert_equals(res, { diff --git a/test/app-luatest/override_test.lua b/test/app-luatest/override_test.lua index ee073e4053..71cf625741 100644 --- a/test/app-luatest/override_test.lua +++ b/test/app-luatest/override_test.lua @@ -1,5 +1,5 @@ local t = require('luatest') -local treegen = require('test.treegen') +local treegen = require('luatest.treegen') local justrun = require('test.justrun') local g = t.group() @@ -11,7 +11,7 @@ local g = t.group() -- value. local OVERRIDE_SCRIPT_TEMPLATE = [[ print(require('json').encode({ - ['script'] = '<script>', + ['script'] = '<filename>', ['...'] = {...}, ['arg[-1]'] = arg[-1], ['arg[0]'] = arg[0], @@ -25,19 +25,14 @@ local MAIN_SCRIPT_TEMPLATE = [[ local json = require('json').new() json.cfg({encode_use_tostring = true}) print(json.encode({ - ['script'] = '<script>', + ['script'] = '<filename>', ['<module_name>'] = require('<module_name>'), })) ]] -g.before_all(function(g) - treegen.init(g) - treegen.add_template(g, '^override/.*%.lua$', OVERRIDE_SCRIPT_TEMPLATE) - treegen.add_template(g, '^main%.lua$', MAIN_SCRIPT_TEMPLATE) -end) - -g.after_all(function(g) - treegen.clean(g) +g.before_all(function() + treegen.add_template('^override/.*%.lua$', OVERRIDE_SCRIPT_TEMPLATE) + treegen.add_template('^main%.lua$', MAIN_SCRIPT_TEMPLATE) end) -- Test oracle. @@ -137,10 +132,10 @@ for _, module_name in ipairs(override_cases) do local module_name_as_snake = table.concat(module_name:split('.'), '_') local case_name = ('test_override_%s'):format(module_name_as_snake) - g[case_name] = function(g) + g[case_name] = function() local scripts = {override_filename, 'main.lua'} local replacements = {module_name = module_name} - local dir = treegen.prepare_directory(g, scripts, replacements) + local dir = treegen.prepare_directory(scripts, replacements) local res = justrun.tarantool(dir, {}, {'main.lua'}) local exp = expected_output(module_name) t.assert_equals(res, exp) @@ -162,10 +157,10 @@ end for _, envvar in ipairs({'false', 'true', '0', '1', '', 'X'}) do local case_slug = envvar == '' and 'empty' or envvar:lower() local case_name = ('test_override_onoff_%s'):format(case_slug) - g[case_name] = function(g) + g[case_name] = function() local scripts = {'override/socket.lua', 'main.lua'} local replacements = {module_name = 'socket'} - local dir = treegen.prepare_directory(g, scripts, replacements) + local dir = treegen.prepare_directory(scripts, replacements) local env = {['TT_OVERRIDE_BUILTIN'] = envvar} local res = justrun.tarantool(dir, env, {'main.lua'}) if parse_boolean(envvar, true) then diff --git a/test/app-luatest/preload_test.lua b/test/app-luatest/preload_test.lua index d8f34317db..522c979ae3 100644 --- a/test/app-luatest/preload_test.lua +++ b/test/app-luatest/preload_test.lua @@ -1,6 +1,6 @@ local fio = require('fio') local t = require('luatest') -local treegen = require('test.treegen') +local treegen = require('luatest.treegen') local justrun = require('test.justrun') local g = t.group() @@ -36,7 +36,7 @@ end -- all items from 1 to the end. local SCRIPT_TEMPLATE = [[ print(require('json').encode({ - ['script'] = '<script>', + ['script'] = '<filename>', ['...'] = {...}, ['arg[-1]'] = arg[-1], ['arg[0]'] = arg[0], @@ -44,13 +44,8 @@ print(require('json').encode({ })) ]] -g.before_all(function(g) - treegen.init(g) - treegen.add_template(g, '^.*$', SCRIPT_TEMPLATE) -end) - -g.after_all(function(g) - treegen.clean(g) +g.before_all(function() + treegen.add_template('^.*$', SCRIPT_TEMPLATE) end) -- Generate output expected from tarantool running in given @@ -233,8 +228,8 @@ for _, case in ipairs({ args = {'main.lua'}, }, }) do - g[case[1]] = function(g) - local dir = treegen.prepare_directory(g, case.scripts) + g[case[1]] = function() + local dir = treegen.prepare_directory(case.scripts) local res = justrun.tarantool(dir, case.env, case.args) local exp = expected_output(case.scripts, case.env, case.args) t.assert_equals(res, exp) diff --git a/test/box-luatest/box_cfg_env_test.lua b/test/box-luatest/box_cfg_env_test.lua index 88eab17390..4d1ee116c7 100644 --- a/test/box-luatest/box_cfg_env_test.lua +++ b/test/box-luatest/box_cfg_env_test.lua @@ -1,14 +1,11 @@ local fun = require('fun') local json = require('json') local t = require('luatest') -local treegen = require('test.treegen') +local treegen = require('luatest.treegen') local justrun = require('test.justrun') local g = t.group() -g.before_all(treegen.init) -g.after_all(treegen.clean) - -- TT_LISTEN and TT_REPLICATION have many allowed forms. -- -- We can't listen on hardcoded port numbers in the test, because @@ -124,11 +121,11 @@ local uri_list_cases = { -- -- All the test cases are run using one popen call -- (justrun.tarantool()) that speeds up the execution. -g.test_uri_list = function(g) +g.test_uri_list = function() -- Write cases.lua and main.lua. - local dir = treegen.prepare_directory(g, {}, {}) - treegen.write_script(dir, 'cases.lua', json.encode(uri_list_cases)) - treegen.write_script(dir, 'main.lua', string.dump(function() + local dir = treegen.prepare_directory({}, {}) + treegen.write_file(dir, 'cases.lua', json.encode(uri_list_cases)) + treegen.write_file(dir, 'main.lua', string.dump(function() local json = require('json') local fio = require('fio') local t = require('luatest') @@ -219,8 +216,8 @@ local cases = { -- Write a script to be used in the test cases below. g.before_all(function(g) - g.dir = treegen.prepare_directory(g, {}, {}) - treegen.write_script(g.dir, 'main.lua', [[ + g.dir = treegen.prepare_directory({}, {}) + treegen.write_file(g.dir, 'main.lua', [[ local json = require('json') box.cfg() print(json.encode(box.cfg)) diff --git a/test/box-luatest/gh_8051_set_box_cfg_thru_env_test.lua b/test/box-luatest/gh_8051_set_box_cfg_thru_env_test.lua index ed04755f97..3fc2356475 100644 --- a/test/box-luatest/gh_8051_set_box_cfg_thru_env_test.lua +++ b/test/box-luatest/gh_8051_set_box_cfg_thru_env_test.lua @@ -1,16 +1,12 @@ local server = require('luatest.server') -local treegen = require('test.treegen') +local treegen = require('luatest.treegen') local it = require('test.interactive_tarantool') local t = require('luatest') local g = t.group() -g.before_all(function(g) - treegen.init(g) -end) - g.before_each(function(g) - g.dir = treegen.prepare_directory(g, {}, {}) + g.dir = treegen.prepare_directory({}, {}) end) g.after_each(function(g) @@ -23,10 +19,6 @@ g.after_each(function(g) end end) -g.after_all(function(g) - treegen.clean(g) -end) - g.test_json_table_curly_bracket = function() local env = {["TT_METRICS"] = '{"labels":{"alias":"gh_8051"},' .. '"include":"all","exclude":["vinyl"]}'} diff --git a/test/treegen.lua b/test/treegen.lua deleted file mode 100644 index 5d41065f56..0000000000 --- a/test/treegen.lua +++ /dev/null @@ -1,170 +0,0 @@ --- Working tree generator. --- --- Generates a tree of Lua files using provided templates and --- filenames. --- --- Basic usage: --- --- | local t = require('luatest') --- | local treegen = require('test.treegen') --- | --- | local g = t.group() --- | --- | local SCRIPT_TEMPLATE = [[ --- | <...> --- | ]] --- | --- | g.before_all(function(g) --- | treegen.init(g) --- | treegen.add_template(g, '^.*$', SCRIPT_TEMPLATE) --- | end) --- | --- | g.after_all(function(g) --- | treegen.clean(g) --- | end) --- | --- | g.foobar_test = function(g) --- | local dir = treegen.prepare_directory(g, --- | {'foo/bar.lua', 'main.lua'}) --- | <..test case..> --- | end - -local fio = require('fio') -local log = require('log') -local fun = require('fun') - -local treegen = {} - -local function find_template(g, script) - for _, template_def in ipairs(g.templates) do - if script:match(template_def.pattern) then - return template_def.template - end - end - error(("treegen: can't find a template for script %q"):format(script)) -end - --- Write provided script into the given directory. -function treegen.write_script(dir, script, body) - local script_abspath = fio.pathjoin(dir, script) - local flags = {'O_CREAT', 'O_WRONLY', 'O_TRUNC'} - local mode = tonumber('644', 8) - - local scriptdir_abspath = fio.dirname(script_abspath) - log.info(('Creating a directory: %s'):format(scriptdir_abspath)) - fio.mktree(scriptdir_abspath) - - log.info(('Writing a script: %s'):format(script_abspath)) - local fh = fio.open(script_abspath, flags, mode) - fh:write(body) - fh:close() - return script_abspath -end - --- Generate a script that follows a template and write it at the --- given path in the given directory. -local function gen_script(g, dir, script, replacements) - local template = find_template(g, script) - local replacements = fun.chain({script = script}, replacements):tomap() - local body = template:gsub('<(.-)>', replacements) - treegen.write_script(dir, script, body) -end - -function treegen.init(g) - g.tempdirs = {} - g.templates = {} -end - --- Remove all temporary directories created by the test --- unless KEEP_DATA environment variable is set to a --- non-empty value. -function treegen.clean(g) - local dirs = table.copy(g.tempdirs) or {} - g.tempdirs = nil - - local keep_data = (os.getenv('KEEP_DATA') or '') ~= '' - - for _, dir in ipairs(dirs) do - if keep_data then - log.info(('Left intact due to KEEP_DATA env var: %s'):format(dir)) - else - log.info(('Recursively removing: %s'):format(dir)) - fio.rmtree(dir) - end - end - - g.templates = nil -end - -function treegen.add_template(g, pattern, template) - table.insert(g.templates, { - pattern = pattern, - template = template, - }) -end - --- Create a temporary directory with given scripts. --- --- The scripts are generated using templates added by --- treegen.add_template(). --- --- Example for {'foo/bar.lua', 'baz.lua'}: --- --- / --- + tmp/ --- + rfbWOJ/ --- + foo/ --- | + bar.lua --- + baz.lua --- --- The return value is '/tmp/rfbWOJ' for this example. -function treegen.prepare_directory(g, scripts, replacements) - local replacements = replacements or {} - - assert(type(scripts) == 'table') - assert(type(replacements) == 'table') - - local dir = fio.tempdir() - - -- fio.tempdir() follows the TMPDIR environment variable. - -- If it ends with a slash, the return value contains a double - -- slash in the middle: for example, if TMPDIR=/tmp/, the - -- result is like `/tmp//rfbWOJ`. - -- - -- It looks harmless on the first glance, but this directory - -- path may be used later to form an URI for a Unix domain - -- socket. As result the URI looks like - -- `unix/:/tmp//rfbWOJ/instance-001.iproto`. - -- - -- It confuses net_box.connect(): it reports EAI_NONAME error - -- from getaddrinfo(). - -- - -- It seems, the reason is a peculiar of the URI parsing: - -- - -- tarantool> uri.parse('unix/:/foo/bar.iproto') - -- --- - -- - host: unix/ - -- service: /foo/bar.iproto - -- unix: /foo/bar.iproto - -- ... - -- - -- tarantool> uri.parse('unix/:/foo//bar.iproto') - -- --- - -- - host: unix - -- path: /foo//bar.iproto - -- ... - -- - -- Let's normalize the path using fio.abspath(), which - -- eliminates the double slashes. - dir = fio.abspath(dir) - - table.insert(g.tempdirs, dir) - - for _, script in ipairs(scripts) do - gen_script(g, dir, script, replacements) - end - - return dir -end - -return treegen -- GitLab