Skip to content

Commit

Permalink
test: replace tostring(error) with error.message for box.errors
Browse files Browse the repository at this point in the history
`box.error`'s string conversion metamethod is currently equivalent
to the `message` field of `box.error`. In scope of tarantool#9105, we are going to
increase the verbosity of the string conversion metamethod, and it will
no longer be suitable for testing the error message. Let's replace
`tostring(error)` usage with `error.message` to retain the old behaviour.

See also tarantool/luatest@xxx (WIP tarantool/luatest#355).

Needed for tarantool#9105

NO_CHANGELOG=<tests change>
NO_DOC=<tests change>
  • Loading branch information
CuriousGeorgiy committed Feb 22, 2024
1 parent a12998a commit a748542
Show file tree
Hide file tree
Showing 32 changed files with 98 additions and 95 deletions.
2 changes: 1 addition & 1 deletion test/app-luatest/fiber_slice_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ g.test_fiber_slice = function()
overhead = clock.monotonic() - overhead
local start_time = clock.monotonic()
local _, err = fib:join()
t.assert_equals(tostring(err), "fiber slice is exceeded")
t.assert_equals(err.message, "fiber slice is exceeded")
local time_elapsed = clock.monotonic() - start_time - overhead
t.assert_almost_equals(time_elapsed, expected_timeout, delta)
end
Expand Down
2 changes: 1 addition & 1 deletion test/app-luatest/http_client_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ g.test_headers = function(cg)
local headers = {[hname] = hvalue}
local ok, err = pcall(http.post, http, url, nil,
merge(opts, {headers = headers}))
t.assert_equals({ok, tostring(err)}, {false, exp_err},
t.assert_equals({ok, err.message}, {false, exp_err},
'8193 KiB header: error')
end

Expand Down
6 changes: 3 additions & 3 deletions test/app-tap/console.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,21 @@ box.cfg{
-- Connect to iproto console (CALL)
client:write(string.format("require('console').connect('/')\n"))
-- error: Connection is not established
test:ok(yaml.decode(client:read(EOL))[1].error:find('not established'),
test:ok(yaml.decode(client:read(EOL))[1].error.message:find('not established'),
'remote network error')

client:write(string.format("require('console').connect('%s')\n",
IPROTO_SOCKET))
-- error: Execute access is denied for user 'guest' to function 'dostring
test:ok(yaml.decode(client:read(EOL))[1].error:find('denied'),
test:ok(yaml.decode(client:read(EOL))[1].error.message:find('denied'),
'remote access denied')

-- create user
box.schema.user.create('test', { password = 'pass' })
client:write(string.format("require('console').connect('test:pass@%s')\n",
IPROTO_SOCKET))
-- error: Execute access denied for user 'test' to function 'dostring
test:ok(yaml.decode(client:read(EOL))[1].error:find('denied'),
test:ok(yaml.decode(client:read(EOL))[1].error.message:find('denied'),
'remote access denied')

-- Add permissions to execute for `test`
Expand Down
2 changes: 1 addition & 1 deletion test/app-tap/gh-5013-fiber-cancel.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ fiber.yield()
print(require('yaml').encode(result))

test:ok(result.res == false, 'expected result is false')
test:ok(tostring(result.err) == 'fiber is cancelled', 'fiber cancellation should be reported')
test:ok(result.err.message == 'fiber is cancelled', 'fiber cancellation should be reported')
os.exit(test:check() and 0 or 1)
6 changes: 3 additions & 3 deletions test/app-tap/logger.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test:plan(119)

local function test_invalid_cfg(cfg_method, cfg, name, expected)
local _, err = pcall(cfg_method, cfg)
test:ok(tostring(err):find(expected) ~= nil, name)
test:ok(err.message:find(expected) ~= nil, name)
end

local function test_allowed_types(cfg_method, cfg, name, allowed_types)
Expand Down Expand Up @@ -205,11 +205,11 @@ test_invalid_cfg(box.cfg, {log_nonblock = true},
-- Test invalid values for setters

_, err = pcall(log.log_format, {})
test:ok(tostring(err):find('should be of type string') ~= nil,
test:ok(err.message:find('should be of type string') ~= nil,
"invalid format setter value type")

_, err = pcall(log.level, {})
test:ok(tostring(err):find('should be one of types number, string') ~= nil,
test:ok(err.message:find('should be one of types number, string') ~= nil,
"invalid format setter value type")

-- Change format and levels.
Expand Down
4 changes: 2 additions & 2 deletions test/app-tap/popen.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ local function test_trivial_echo_output(test)
-- Sending a signal using a closed handle gives an error.
local exp_err = 'popen: attempt to operate on a closed handle'
local ok, err = pcall(ph.signal, ph, popen.signal.SIGTERM)
test:is_deeply({ok, err.type, tostring(err)},
test:is_deeply({ok, err.type, err.message},
{false, 'IllegalParams', exp_err},
'signal() on closed handle gives an error')
end
Expand Down Expand Up @@ -247,7 +247,7 @@ local function test_read_timeout(test)
-- Read and get a timeout error.
local exp_err = 'timed out'
local res, err = ph:read({timeout = 0.1})
test:is_deeply({res, err.type, tostring(err)}, {nil, 'TimedOut', exp_err},
test:is_deeply({res, err.type, err.message}, {nil, 'TimedOut', exp_err},
'timeout error')

-- Write and read after the timeout error.
Expand Down
68 changes: 34 additions & 34 deletions test/app-tap/uri.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ local function test_parse(test)
expected_errmsg = "Incorrect URI: expected host:service or /unix.socket"
u, error = uri.parse("")
test:isnil(u, "invalid uri", u)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
u, error = uri.parse("://")
test:isnil(u, "invalid uri", u)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
end

local function test_format(test)
Expand Down Expand Up @@ -245,13 +245,13 @@ local function test_parse_uri_query_params(test)
expected_errmsg = "Default URI parameters are not allowed for single URI"
u, error = uri.parse({ "/tmp/unix.sock", default_params = {q = "v"} })
test:isnil(u, "invalid uri", u)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Multiple URIs is not allowed in `parse` method,
-- use `parse_many` instead.
expected_errmsg = "Incorrect URI: expected host:service or /unix.socket"
u, error = uri.parse({ "/tmp/unix.sock, /tmp/unix.sock"})
test:isnil(u, "invalid uri", u)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
end

local function test_parse_uri_set_with_query_params(test)
Expand Down Expand Up @@ -314,24 +314,24 @@ local function test_parse_uri_set_with_query_params(test)
expected_errmsg = "Incorrect URI: expected host:service or /unix.socket"
uri_set , error= uri.parse_many("/tmp/unix.sock,,/tmp/unix.sock")
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
uri_set, error = uri.parse_many("/tmp/unix.sock, ,/tmp/unix.sock")
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
uri_set, error = uri.parse_many("/tmp/unix.sock,, /tmp/unix.sock")
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
uri_set, error = uri.parse_many("/tmp/unix.sock ,,/tmp/unix.sock")
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")


-- Check that we can't parse string with multiple URIs,
-- using old method.
expected_errmsg = "Incorrect URI: expected host:service or /unix.socket"
local u, error = uri.parse("/tmp/unix.sock, /tmp/unix.sock")
test:isnil(u, "invalid uri", u)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
end

local function test_parse_uri_set_from_lua_table(test)
Expand Down Expand Up @@ -575,75 +575,75 @@ local function test_parse_invalid_uri_set_from_lua_table(test)
expected_errmsg = "Incorrect type for URI: should be string, number or table"
uri_set, error = uri.parse_many(function() end)
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Invalid type of value for numerical key
expected_errmsg = "Incorrect type for URI: should be string, number or table"
uri_set, error = uri.parse_many({"/tmp/unix.sock", function() end})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Invalid type of value for string keys
expected_errmsg = "Invalid URI table: expected " ..
"{uri = string, params = table}" .. " or " ..
"{string, params = table}"
uri_set, error = uri.parse_many({"/tmp/unix.sock", uri = function() end})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
expected_errmsg = "Incorrect type for URI parameters: should be a table"
uri_set, error = uri.parse_many({"/tmp/unix.sock", params = function() end})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
expected_errmsg = "Incorrect type for URI parameters: should be a table"
uri_set, error = uri.parse_many({"/tmp/unix.sock", params = ""})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
expected_errmsg = "Default URI parameters are not allowed for single URI"
uri_set, error = uri.parse_many({"/tmp/unix.sock", default_params = ""})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
expected_errmsg = "Default URI parameters are not allowed for single URI"
uri_set, error = uri.parse_many({"/tmp/unix.sock", default_params = ""})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")

-- Mix "uri=" and numerical keys is banned
expected_errmsg = "Invalid URI table: expected " ..
"{uri = string, params = table}" .. " or " ..
"{string, params = table}"
uri_set, error = uri.parse_many({"/tmp/unix.sock", uri = "/tmp/unix.sock"})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Several URIs in one string is allowed only when the
-- passed as a single string.
expected_errmsg = "Incorrect URI: expected host:service or /unix.socket"
uri_set, error = uri.parse_many({"/tmp/unix.sock, /tmp/unix.sock"})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- "params" table is allowed only for single URI
expected_errmsg = "URI parameters are not allowed for multiple URIs"
uri_set, error = uri.parse_many({
"/tmp/unix.sock", "/tmp/unix.sock",
params = {q1 = "v1"}
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- "params" table is not allowed with nested tables
expected_errmsg = "URI parameters are not allowed for multiple URIs"
uri_set, error = uri.parse_many({
{"/tmp/unix.sock"},
params = {q1 = "v1"}
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- "default_params" table is not allowed in nested URI tables
expected_errmsg = "Default URI parameters are not allowed for single URI"
uri_set, error = uri.parse_many({{"/tmp/unix.sock", default_params = {}}})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- "default_params" table is not allowed for single URI
expected_errmsg = "Default URI parameters are not allowed for single URI"
uri_set, error = uri.parse_many({"/tmp/unix.sock", default_params = {}})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Only one URI is allowed in nested tables
expected_errmsg = "Invalid URI table: expected " ..
"{uri = string, params = table}" .. " or " ..
Expand All @@ -653,7 +653,7 @@ local function test_parse_invalid_uri_set_from_lua_table(test)
default_params = {q = "v"}
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Nested URI tables is not allowed in nested tables
expected_errmsg = "Invalid URI table: expected "..
"{uri = string, params = table}" .. " or " ..
Expand All @@ -662,7 +662,7 @@ local function test_parse_invalid_uri_set_from_lua_table(test)
{"/tmp/unix.sock", {}}
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Nested URI table without URI is now allowed
expected_errmsg = "Invalid URI table: expected "..
"{uri = string, params = table}" .. " or " ..
Expand All @@ -672,7 +672,7 @@ local function test_parse_invalid_uri_set_from_lua_table(test)
{ params = {q = "v"} }
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Only string key types are allowed in "params" and
-- "default_params" table
expected_errmsg = "Incorrect type for URI parameter name: " ..
Expand All @@ -682,14 +682,14 @@ local function test_parse_invalid_uri_set_from_lua_table(test)
params = {"v"},
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
expected_errmsg = "Default URI parameters are not allowed for single URI"
uri_set, error = uri.parse_many({
"/tmp/unix.sock",
default_params = {"v"},
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Invalid type of values in "params" and
-- "default_params" table
expected_errmsg = "Incorrect type for URI parameter value: " ..
Expand All @@ -699,53 +699,53 @@ local function test_parse_invalid_uri_set_from_lua_table(test)
params = {q = function() end},
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
expected_errmsg = "Default URI parameters are not allowed for single URI"
uri_set, error = uri.parse_many({
"/tmp/unix.sock",
default_params = {q = function() end},
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
expected_errmsg = "Incorrect type for URI parameter value: "..
"should be string or number"
uri_set, error = uri.parse_many({
"/tmp/unix.sock",
params = {q = {function() end}},
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
expected_errmsg = "Default URI parameters are not allowed for single URI"
uri_set, error = uri.parse_many({
"/tmp/unix.sock",
default_params = {q = {function() end}},
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Invalid uri string in URIs table
expected_errmsg = "Incorrect URI: expected host:service or /unix.socket"
uri_set, error = uri.parse_many({
"/tmp/unix.sock",
"://"
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Invalid uri in nested URI table
expected_errmsg = "Incorrect URI: expected host:service or /unix.socket"
uri_set, error = uri.parse_many({
"/tmp/unix.sock",
{"://"}
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
-- Same as previous but with "uri=" syntax
expected_errmsg = "Incorrect URI: expected host:service or /unix.socket"
uri_set, error = uri.parse_many({
"/tmp/unix.sock",
{uri = "://"}
})
test:isnil(uri_set, "invalid uri", uri_set)
test:is(tostring(error), expected_errmsg, "error message")
test:is(error.message, expected_errmsg, "error message")
end

local test = tap.test("uri")
Expand Down
4 changes: 2 additions & 2 deletions test/box-luatest/gh_6085_limit_iteration_in_space_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ g.test_limit_iteration = function()
local fib = fiber.new(fiber_f)
fib:set_joinable(true)
local _, err = fib:join()
t.assert_equals(tostring(err), "fiber slice is exceeded")
t.assert_equals(err.message, "fiber slice is exceeded")
end

fiber.set_max_slice(0.2)
Expand Down Expand Up @@ -92,7 +92,7 @@ g.test_limit_on_sigurg = function()
os.execute('kill -URG ' .. tonumber(pid))
local _, err = future:wait_result(1.5)
local end_time = clock.monotonic()
t.assert_equals(tostring(err), "fiber slice is exceeded")
t.assert_equals(err.message, "fiber slice is exceeded")
-- Must end before slice is over.
t.assert(end_time - start_time < 3)
end
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ g.test_misc_region_leaks = function(cg)
}
');]])
t.assert_equals(ret, nil)
t.assert_equals(tostring(err), errmsg)
t.assert_equals(err.message, errmsg)
t.assert_equals(before, mem_used())

ret, err = box.execute('SELECT ?;', {{trouble}})
t.assert_equals(ret, nil)
t.assert_equals(tostring(err), errmsg)
t.assert_equals(err.message, errmsg)
t.assert_equals(before, mem_used())
end)
end
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ g.test = function(cg)
t.assert_equals(ok, compat.box_session_push_deprecation:is_old(),
'box.session.push status')
if not ok then
t.assert_equals(tostring(err), 'box.session.push is deprecated',
t.assert_equals(err.message, 'box.session.push is deprecated',
'box.session.push error')
end
end)
Expand All @@ -84,7 +84,7 @@ g.test = function(cg)
t.assert_equals(ok, compat.box_session_push_deprecation:is_old(),
'box.session.push status')
if not ok then
t.assert_equals(tostring(err), 'box.session.push is deprecated',
t.assert_equals(err.message, 'box.session.push is deprecated',
'box.session.push error')
end
end)
Expand Down
Loading

0 comments on commit a748542

Please sign in to comment.