From: Cyrill Gorcunov <gorcunov@gmail.com> To: tml <tarantool-patches@dev.tarantool.org> Subject: [Tarantool-patches] [PATCH v3 3/3] test/app-tap: add console_lua test Date: Mon, 20 Jan 2020 20:59:50 +0300 [thread overview] Message-ID: <20200120175950.28060-4-gorcunov@gmail.com> (raw) In-Reply-To: <20200120175950.28060-1-gorcunov@gmail.com> Test multireturn and empty output in lua mode. Co-developed-by: Alexander Turenko <alexander.turenko@tarantool.org> Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com> --- test/app-tap/console_lua.test.lua | 141 ++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100755 test/app-tap/console_lua.test.lua diff --git a/test/app-tap/console_lua.test.lua b/test/app-tap/console_lua.test.lua new file mode 100755 index 000000000..5848bdf69 --- /dev/null +++ b/test/app-tap/console_lua.test.lua @@ -0,0 +1,141 @@ +#!/usr/bin/env tarantool +-- +-- vim: ts=4 sw=4 et + +local console = require('console') +local socket = require('socket') +local tap = require('tap') +local fio = require('fio') + +local EOL = ';' +local CONSOLE_SOCKET = 'console-lua.sock' + +-- +-- Set Lua output mode. +local function set_lua_output(client, opts) + local opts = opts or {} + local mode = opts.block and 'lua,block' or 'lua' + client:write(('\\set output %s\n'):format(mode)) + assert(client:read(EOL), 'true' .. EOL, 'set lua output mode') +end + +-- +-- Start console and setup a client. +local function start_console() + -- Make sure not stale sockets are + -- left from previous runs. + fio.unlink(CONSOLE_SOCKET) + + local server = console.listen('unix/:./' .. CONSOLE_SOCKET) + assert(server ~= nil, 'console.listen started') + + local client = socket.tcp_connect('unix/', CONSOLE_SOCKET) + assert(client ~= nil, 'connect to console') + local handshake = client:read({chunk = 128}) + assert(string.match(handshake, '^Tarantool .*console') ~= nil, 'handshake') + + -- Switch to Lua output mode. + set_lua_output(client, {block = false}) + return server, client +end + +-- +-- Disconnect from console and stop it. +local function stop_console(server, client) + client:close() + server:close() + + local new_client = socket.tcp_connect('unix/', CONSOLE_SOCKET) + assert(new_client == nil, 'console.listen stopped') +end + +-- +-- Give `{x}` for a scalar `x`, just `x` otherwise. +local function totable(x) + return type(x) == 'table' and x or {x} +end + +-- +-- Execute a list of statements, show requests and responses. +local function execute_statements(test, client, statements, name) + test:test(name, function(test) + test:plan(2 * #statements) + + for _, stmt in ipairs(statements) do + local request = stmt .. '\n' + local res = client:write(request) + test:ok(res ~= nil, ('-> [[%s]]'):format(request:gsub('\n', '\\n'))) + + local response = client:read(EOL) + test:ok(response ~= nil and response:endswith(EOL), + ('<- [[%s]]'):format(response)) + end + end) +end + +-- +-- Execute a statement and verify its response. +local function execute_and_verify(test, client, input, exp_output, name) + test:test(name, function(test) + test:plan(2) + + local res = client:write(input .. '\n') + test:ok(res ~= nil, ('-> [[%s]]'):format(input)) + + local exp = exp_output .. EOL + local res = client:read(EOL) + test:is(res, exp, ('<- [[%s]]'):format(exp:gsub('\n', '\\n'))) + end) +end + +-- +-- Test cases table: +-- @name: Name of the test, mandatory +-- @prepare: Preparation script, optional +-- @opts: Console options, optional +-- @input: Input statement to execute, mandatory +-- @expected: Expected results to compare with, mandatory +-- @cleanup: Cleanup script to run after the test, optional +local cases = { + { + name = 'multireturn line mode', + prepare = 'a = {1, 2, 3}', + opts = {block = false}, + input = '1, 2, nil, a', + expected = '1, 2, box.NULL, {1, 2, 3}', + cleanup = 'a = nil', + }, { + name = 'multireturn block mode', + prepare = 'a = {1, 2, 3}', + opts = {block = true}, + input = '1, 2, nil, a', + expected = '1, 2, box.NULL, {\n 1,\n 2,\n 3\n}', + cleanup = 'a = nil', + }, { + name = 'empty output', + input = '\\set output', + expected = '"Specify output format: lua or yaml."', + } +} + +local test = tap.test('console-lua') +test:plan(#cases) + +local server, client = start_console() + +for _, case in ipairs(cases) do + test:test(case.name, function(test) + test:plan(3) + + execute_statements(test, client, totable(case.prepare), 'prepare') + + set_lua_output(client, case.opts) + execute_and_verify(test, client, case.input, case.expected, 'run') + + execute_statements(test, client, totable(case.cleanup), 'cleanup') + end) +end + +stop_console(server, client) + +os.exit(test:check() and 0 or 1) -- 2.20.1
next prev parent reply other threads:[~2020-01-20 18:00 UTC|newest] Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-01-20 17:59 [Tarantool-patches] [PATCH v3 0/3] box/console: Fix multireturn and empty output Cyrill Gorcunov 2020-01-20 17:59 ` [Tarantool-patches] [PATCH v3 1/3] box/console: handle multireturn in lua mode Cyrill Gorcunov 2020-01-20 17:59 ` [Tarantool-patches] [PATCH v3 2/3] box/console: handle empty output format Cyrill Gorcunov 2020-01-20 17:59 ` Cyrill Gorcunov [this message] 2020-01-21 15:16 ` [Tarantool-patches] [PATCH v3 0/3] box/console: Fix multireturn and empty output Alexander Turenko 2020-01-21 15:27 ` Cyrill Gorcunov
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=20200120175950.28060-4-gorcunov@gmail.com \ --to=gorcunov@gmail.com \ --cc=tarantool-patches@dev.tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v3 3/3] test/app-tap: add console_lua test' \ /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