File: debug.test.lua

package info (click to toggle)
tarantool 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 85,364 kB
  • sloc: ansic: 513,760; cpp: 69,489; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,173; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (94 lines) | stat: -rwxr-xr-x 3,641 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
#!/usr/bin/env tarantool

local TNTBIN = 'tarantool'

local ok, test_run = pcall(require, 'test_run')
test_run = ok and test_run.new() or nil

local tests = {
    "print(debug.sourcefile())",
    "print(debug.sourcedir())",
    "print(debug.__file__)",
    "print(debug.__dir__)",
    "print(require('net.box').self:call('debug.sourcefile'))",
    "print(require('net.box').self:call('debug.sourcedir'))",
    "fn = function() return debug.__file__ end; print(require('net.box').self:call('fn'))",
    "fn = function() return debug.__dir__ end; print(require('net.box').self:call('fn'))",
    "fn = function() local res = debug.sourcefile(); return res end; print(require('net.box').self:call('fn'))",
    "fn = function() local res = debug.sourcedir(); return res end; print(require('net.box').self:call('fn'))",
    "print(loadstring('return debug.sourcefile()')())",
    "print(loadstring('return debug.sourcedir()')())",
    "print(loadstring('return debug.__file__')())",
    "print(loadstring('return debug.__dir__')())",
    "print(loadstring('local res = debug.sourcefile(); return res')())",
    "print(loadstring('local res = debug.sourcedir(); return res')())",
}

print('=========================================')
print('When running netbox call on remote server')
print('=========================================')

local script_path = 'app-tap/debug/server.lua'
local cmd = ("create server remote with script='%s', return_listen_uri=True"):format(script_path)
local listen_uri = test_run:cmd(cmd)
test_run:cmd("start server remote")

-- Connecting to the server
local netbox = require('net.box')
local addr = string.format(listen_uri)
local conn = netbox.connect(addr)
assert(conn:ping(), 'Ping to the server must work')

local result, err = conn:call('debug.sourcefile')
print(('debug.sourcefile() => %s; %s'):format(tostring(result), tostring(err)))
assert(result == box.NULL, 'debug.sourcefile() returns box.NULL')
assert(err == nil, 'debug.sourcefile() returns no error')

local result, err = conn:call('debug.sourcedir')
print(('debug.sourcedir() => %s; %s'):format(tostring(result), tostring(err)))
assert(result == '.', 'debug.sourcedir() returns "."')
assert(err == nil, 'debug.sourcedir() returns no error')

test_run:cmd("stop server remote")
test_run:cmd("cleanup server remote")
test_run:cmd("delete server remote")

print('==================================')
print('When running lua code from console')
print('==================================')
-- debug.sourcefile() returns cwd when running within console
for _, test in ipairs(tests) do
    local cmd = string.format('%s -e "%s; os.exit(0)"', TNTBIN, test)
    print('Exec: '..cmd)
    io.flush()
    assert(os.execute(cmd) == 0, string.format('cmd: "%s" must execute successfully', cmd))
end

local fio = require('fio')
local dirname = 'debug'
local filename = fio.pathjoin(dirname, 'test.lua')
local dirstat = fio.stat(dirname)
if not dirstat then
    fio.mkdir(dirname)
    dirstat = fio.stat(dirname)
end
assert(dirstat:is_dir(), dirname..' must be a directory')

local cmd = TNTBIN..' '..filename
print('======================================')
print('When running lua code from script file')
print('======================================')
print('Exec: '..cmd)
print('==============================')
io.flush()
for _, test in ipairs(tests) do
    local file = fio.open(filename, {'O_CREAT', 'O_WRONLY', 'O_TRUNC'}, tonumber('644',8))
    file:write(test)
    file:close()
    print('Source: '..test)
    io.flush()
    assert(os.execute(cmd) == 0, string.format('cmd: "%s" must execute successfully', cmd))
end

os.remove(filename)
fio.rmdir(dirname)