File: console_lua.test.lua

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (166 lines) | stat: -rwxr-xr-x 5,161 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/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, nil, {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, nil, {\n  1,\n  2,\n  3\n}',
        cleanup     = 'a = nil',
    }, {
        name        = 'trailing nils, box.NULL, line mode',
        opts        = {block = false},
        input       = '1, nil, box.NULL, nil',
        expected    = '1, nil, box.NULL, nil',
    }, {
        name        = 'leading nils, box.NULL, line mode',
        opts        = {block = false},
        input       = 'nil, 1, nil, box.NULL, nil',
        expected    = 'nil, 1, nil, box.NULL, nil',
    }, {
        name        = 'trailing nils, box.NULL, block mode',
        opts        = {block = true},
        input       = '1, nil, box.NULL, nil',
        expected    = '1, nil, box.NULL, nil',
    }, {
        name        = 'ULL constants, multireturn',
        opts        = {block = false},
        input       = '-1ULL, -2ULL, 1ULL, 2ULL',
        expected    = '18446744073709551615, 18446744073709551614, 1, 2',
    }, {
        name        = 'ULL key',
        opts        = {block = false},
        input       = '{[-1ULL] = 1}',
        expected    = '{[18446744073709551615] = 1}',
    }, {
        name        = 'empty output',
        input       = '\\set output',
        expected    = '{error = \"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)