Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- Auto-require `luatest` module in `Server:exec()` function where it is available
via the corresponding upvalue.
- Add new function `tarantool.skip_if_not_enterprise`.
- Raise an error when non-array arguments passed to the `server:exec()`.

## 0.5.7

Expand Down
18 changes: 18 additions & 0 deletions luatest/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,19 @@ local function exec_tail(ok, ...)
end
end

-- Check that the passed `args` to the `fn` function are an array.
local function are_fn_args_array(fn, args)
local fn_details = debug.getinfo(fn)
if args and #args ~= fn_details.nparams then
for k, _ in pairs(args) do
if type(k) ~= 'number' then
return false
end
end
end
return true
end

--- Run given function on the server.
--
-- Much like `Server:eval`, but takes a function instead of a string.
Expand Down Expand Up @@ -562,6 +575,11 @@ function Server:exec(fn, args, options)
error(err, 2)
end

if not are_fn_args_array(fn, args) then
error(('bad argument #3 for exec at %s: an array is required')
:format(utils.get_fn_location(fn)))
end

return exec_tail(self.net_box:eval([[
local dump, args, passthrough_ups = ...
local fn = loadstring(dump)
Expand Down
5 changes: 5 additions & 0 deletions luatest/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,10 @@ function utils.upvalues(fn)
return ret
end

function utils.get_fn_location(fn)
local fn_details = debug.getinfo(fn)
local fn_source = fn_details.source:split('/')
return ('%s:%s'):format(fn_source[#fn_source], fn_details.linedefined)
end

return utils
80 changes: 80 additions & 0 deletions test/malformed_args_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
local fio = require('fio')
local t = require('luatest')

local g = t.group()
local Server = t.Server

local root = fio.dirname(fio.dirname(fio.abspath(package.search('test.helper'))))
local datadir = fio.pathjoin(root, 'tmp', 'malformed_args')
local command = fio.pathjoin(root, 'test', 'server_instance.lua')

g.before_all(function()
fio.rmtree(datadir)

local log = fio.pathjoin(datadir, 'malformed_args_server.log')
g.server = Server:new({
command = command,
workdir = datadir,
env = {
TARANTOOL_LOG = log
},
http_port = 8186,
net_box_port = 3139,
})
fio.mktree(g.server.workdir)

g.server:start()
t.helpers.retrying({timeout = 2}, function()
g.server:http_request('get', '/ping')
end)

g.server:connect_net_box()
end)

g.after_all(function()
g.server:drop()
fio.rmtree(datadir)
end)

g.test_exec_correct_args = function()
local a = g.server:exec(function(a, b) return a + b end, {1, 1})
t.assert_equals(a, 2)
end

g.test_exec_no_args = function()
local a = g.server:exec(function() return 1 + 1 end)
t.assert_equals(a, 2)
end

g.test_exec_specific_args = function()
-- nil
local a = g.server:exec(function(a) return a end)
t.assert_equals(a, nil)

-- too few args
local b, c = g.server:exec(function(b, c) return b, c end, {1})
t.assert_equals(b, 1)
t.assert_equals(c, nil)

-- too many args
local d = g.server:exec(function(d) return d end, {1, 2})
t.assert_equals(d, 1)
end

g.test_exec_non_array_args = function()
local function f1()
g.server:exec(function(a, b, c) return a, b, c end, {a="a", 2, 3})
end

local function f2()
g.server:exec(function(a, b, c) return a, b, c end, {1, a="a", 2})
end

local function f3()
g.server:exec(function(a, b, c) return a, b, c end, {1, 2, a="a"})
end

t.assert_error_msg_contains("bad argument #3 for exec at malformed_args_test.lua:66:", f1)
t.assert_error_msg_contains("bad argument #3 for exec at malformed_args_test.lua:70:", f2)
t.assert_error_msg_contains("bad argument #3 for exec at malformed_args_test.lua:74:", f3)
end