Tarantool development patches archive
 help / color / mirror / Atom feed
From: Leonid Vasiliev <lvasiliev@tarantool.org>
To: alexander.turenko@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH 5/6] error: Add test for extended error
Date: Tue, 24 Mar 2020 15:46:03 +0300	[thread overview]
Message-ID: <5012abade933bbbc034f969778c952cf4b76c9a1.1585053743.git.lvasiliev@tarantool.org> (raw)
In-Reply-To: <cover.1585053742.git.lvasiliev@tarantool.org>
In-Reply-To: <cover.1585053742.git.lvasiliev@tarantool.org>

Needed for #4398
---
 test/box-tap/extended_error.test.lua | 168 +++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100755 test/box-tap/extended_error.test.lua

diff --git a/test/box-tap/extended_error.test.lua b/test/box-tap/extended_error.test.lua
new file mode 100755
index 0000000..58a4984
--- /dev/null
+++ b/test/box-tap/extended_error.test.lua
@@ -0,0 +1,168 @@
+#! /usr/bin/env tarantool
+
+local netbox = require('net.box')
+local os = require('os')
+local tap = require('tap')
+local uri = require('uri')
+
+local test = tap.test('check an extended error')
+test:plan(2)
+
+
+function error_func()
+    box.error(box.error.PROC_LUA, "An old good error")
+end
+
+function custom_error_func()
+    box.error(box.error.CUSTOM_ERROR, "My Custom Type", "A modern custom error")
+end
+
+local function version_at_least(peer_version_str, major, minor, patch)
+    local major_p, minor_p, patch_p = string.match(peer_version_str,
+                                                   "(%d)%.(%d)%.(%d)")
+    major_p = tonumber(major_p)
+    minor_p = tonumber(minor_p)
+    patch_p = tonumber(patch_p)
+
+    if major_p < major
+        or (major_p == major and minor_p < minor)
+        or (major_p == major and minor_p == minor and patch_p < patch) then
+            return false
+    end
+
+    return true
+end
+
+local function grant_func(user, name)
+    box.schema.func.create(name, {if_not_exists = true})
+    box.schema.user.grant(user, 'execute', 'function', name, {
+        if_not_exists = true
+    })
+end
+
+local function check_error(err, check_list)
+    if type(check_list) ~= 'table' then
+        return false
+    end
+
+    for k, v in pairs(check_list) do
+        if k == 'type' then
+            if err.type ~= v then
+                return false
+            end
+        elseif k == 'custom_type' then
+            if err.custom_type ~= v then
+                return false
+            end
+        elseif k == 'message' then
+            if err.message ~= v then
+                return false
+            end
+        elseif k == 'trace' then
+            local trace = "File " .. err.trace[1]['file']
+                         .. "\nLine " .. err.trace[1]['line']
+            if not string.match(trace, v) then
+                return false
+            end
+        elseif k == 'errno' then
+            if err.errno ~= v then
+                return false
+            end
+        elseif k == 'bt' then
+            if not string.match(err.bt, v) then
+                return false
+            end
+        elseif k == 'is_custom' then
+            if err:is_custom() ~= v then
+                return false
+            end
+        else
+            return false
+        end
+    end
+
+    return true
+end
+
+local function test_old_transmission(host, port)
+    grant_func('guest', 'error_func')
+
+    local connection = netbox.connect(host, port)
+    local _, err = pcall(connection.call, connection, 'error_func')
+
+    local backtrace_pattern =
+    "^stack traceback:\n" ..
+    "%s+%[C%]: in function 'new'\n" ..
+    "%s+builtin/box/net_box.lua:%d+: in function 'perform_request'\n" ..
+    "%s+builtin/box/net_box.lua:%d+: in function '_request'\n" ..
+    "%s+builtin/box/net_box.lua:%d+: in function <builtin/box/net_box.lua:%d+>\n" ..
+    "%s+%[C%]: in function 'pcall'\n" ..
+    ".*box%-tap/extended_error.test.lua:%d+: in function 'test_old_transmission'\n" ..
+    ".*box%-tap/extended_error.test.lua:%d+: in main chunk$"
+
+    local check_list = {
+        type = 'ClientError',
+        message = 'An old good error',
+        trace = '^File builtin/box/net_box.lua\nLine %d+$',
+        bt = backtrace_pattern,
+        is_custom = false
+    }
+
+    local check_result = check_error(err, check_list)
+    test:ok(check_result, 'Check the old transmission type')
+    connection:close()
+end
+
+local function test_extended_transmission(host, port)
+    grant_func('guest', 'custom_error_func')
+
+    local connection = netbox.connect(host, port, {error_extended = true})
+    local _, err = pcall(connection.call, connection, 'custom_error_func')
+
+    local backtrace_pattern =
+    "^Remote%(.+:%d+%):\n" ..
+    "stack traceback:\n" ..
+    "%s+%[C%]: in function 'error'\n" ..
+    ".*box%-tap/extended_error.test.lua:%d+: in function <.*box%-tap/extended_error.test.lua:%d+>\n" ..
+    "%s+%[C%]: at 0x%w+\n" ..
+    "End Remote\n" ..
+    "stack traceback:\n" ..
+    "%s+builtin/box/net_box.lua:%d+: in function 'parse_extended_error'\n" ..
+    "%s+builtin/box/net_box.lua:%d+: in function 'perform_request'\n" ..
+    "%s+builtin/box/net_box.lua:%d+: in function '_request'\n" ..
+    "%s+builtin/box/net_box.lua:%d+: in function <builtin/box/net_box.lua:%d+>\n" ..
+    "%s+%[C%]: in function 'pcall'\n" ..
+    ".*box%-tap/extended_error.test.lua:%d+: in function 'test_extended_transmission'\n" ..
+    ".*box%-tap/extended_error.test.lua:%d+: in main chunk$"
+
+    local check_list = {
+        type = 'CustomError',
+        custom_type = 'My Custom Type',
+        message = 'User custom error: A modern custom error',
+        trace = '^File builtin/box/net_box.lua\nLine %d+$',
+        bt = backtrace_pattern,
+        is_custom = true
+    }
+
+    local check_result = check_error(err, check_list)
+    test:ok(check_result, 'Check the extended transmission type')
+    connection:close()
+end
+
+
+box.cfg{
+    listen = os.getenv('LISTEN')
+}
+local tarantool_ver = string.match(box.info.version, "%d%.%d%.%d")
+local host= uri.parse(box.cfg.listen).host or 'localhost'
+local port = uri.parse(box.cfg.listen).service 
+
+if version_at_least(box.info.version, 2, 4, 1) then
+    test_extended_transmission(host, port)
+else
+    test:ok(true, 'Current version of tarantool(' .. tarantool_ver .. ')' ..
+            ' don\'t support extended transmission')
+end
+test_old_transmission(host, port)
+
+os.exit(test:check() and 0 or 1)
-- 
2.7.4

  parent reply	other threads:[~2020-03-24 12:46 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-24 12:45 [Tarantool-patches] [PATCH 0/6] Extending error functionality Leonid Vasiliev
2020-03-24 12:45 ` [Tarantool-patches] [PATCH 1/6] error: Add a Lua backtrace to error Leonid Vasiliev
2020-04-05 22:14   ` Vladislav Shpilevoy
2020-04-08 13:56     ` Igor Munkin
2020-03-24 12:46 ` [Tarantool-patches] [PATCH 2/6] error: Add the custom error type Leonid Vasiliev
2020-04-05 22:14   ` Vladislav Shpilevoy
2020-03-24 12:46 ` [Tarantool-patches] [PATCH 3/6] iproto: Add negotiation phase Leonid Vasiliev
2020-03-24 20:02   ` Konstantin Osipov
2020-03-25  7:35     ` lvasiliev
2020-03-25  8:42       ` Konstantin Osipov
2020-03-25 10:56         ` Eugene Leonovich
2020-03-25 11:13           ` Konstantin Osipov
2020-03-26 11:37           ` lvasiliev
2020-03-26 11:18         ` lvasiliev
2020-03-26 12:16           ` Konstantin Osipov
2020-03-26 12:54             ` Kirill Yukhin
2020-03-26 13:19               ` Konstantin Osipov
2020-03-26 13:31                 ` Konstantin Osipov
2020-03-26 21:13       ` Alexander Turenko
2020-03-26 21:53         ` Alexander Turenko
2020-03-27  8:28         ` Konstantin Osipov
2020-03-26 23:35       ` Alexander Turenko
2020-03-27  8:39         ` Konstantin Osipov
2020-03-24 12:46 ` [Tarantool-patches] [PATCH 4/6] error: Add extended error transfer format Leonid Vasiliev
2020-03-24 12:46 ` Leonid Vasiliev [this message]
2020-03-24 12:46 ` [Tarantool-patches] [PATCH 6/6] error: Transmit an error through IPROTO_OK as object Leonid Vasiliev
2020-03-27 23:11 ` [Tarantool-patches] [PATCH 0/6] Extending error functionality lvasiliev
2020-03-28 13:54   ` Alexander Turenko
2020-03-30 10:48     ` lvasiliev
2020-04-01 15:35 ` Alexander Turenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5012abade933bbbc034f969778c952cf4b76c9a1.1585053743.git.lvasiliev@tarantool.org \
    --to=lvasiliev@tarantool.org \
    --cc=alexander.turenko@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 5/6] error: Add test for extended error' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox