[Tarantool-patches] [PATCH v2 5/5] test: Add app/popen test
Cyrill Gorcunov
gorcunov at gmail.com
Tue Dec 10 12:48:55 MSK 2019
To test basic functionality of popen code.
Fixes #4031
Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
test/app/popen.result | 179 ++++++++++++++++++++++++++++++++++++++++
test/app/popen.test.lua | 68 +++++++++++++++
2 files changed, 247 insertions(+)
create mode 100644 test/app/popen.result
create mode 100644 test/app/popen.test.lua
diff --git a/test/app/popen.result b/test/app/popen.result
new file mode 100644
index 000000000..99efbe512
--- /dev/null
+++ b/test/app/popen.result
@@ -0,0 +1,179 @@
+-- test-run result file version 2
+fio = require('fio')
+ | ---
+ | ...
+buffer = require('buffer')
+ | ---
+ | ...
+ffi = require('ffi')
+ | ---
+ | ...
+
+test_run = require('test_run').new()
+ | ---
+ | ...
+
+local err, reason, exit_code
+ | ---
+ | ...
+buf = buffer.ibuf()
+ | ---
+ | ...
+
+--
+-- Trivial echo output
+--
+script = "echo -n 1 2 3 4 5"
+ | ---
+ | ...
+popen = fio.popen(script, "r")
+ | ---
+ | ...
+popen:wait() -- wait echo to finish
+ | ---
+ | - null
+ | - 1
+ | - 0
+ | ...
+popen:read() -- read the output
+ | ---
+ | - 1 2 3 4 5
+ | ...
+popen:close() -- release the popen
+ | ---
+ | - true
+ | ...
+
+--
+-- Test info and killing of a child process
+--
+script = "while [ 1 ]; do sleep 10; done"
+ | ---
+ | ...
+popen = fio.popen(script, "r")
+ | ---
+ | ...
+popen:kill()
+ | ---
+ | - true
+ | ...
+--
+-- Killing child may be queued and depends on
+-- system load, so we may get ESRCH here.
+err, reason, exit_code = popen:wait()
+ | ---
+ | ...
+popen:state()
+ | ---
+ | - null
+ | - 3
+ | - 9
+ | ...
+info = popen:info()
+ | ---
+ | ...
+info["state"]
+ | ---
+ | - signaled
+ | ...
+info["flags"]
+ | ---
+ | - 834
+ | ...
+info["exit_code"]
+ | ---
+ | - 9
+ | ...
+popen:close()
+ | ---
+ | - true
+ | ...
+
+--
+-- Test for stdin/out stream
+--
+script="prompt=''; read -n 5 prompt; echo -n $prompt"
+ | ---
+ | ...
+popen = fio.popen(script, "rw")
+ | ---
+ | ...
+popen:write("input")
+ | ---
+ | - 5
+ | ...
+popen:read()
+ | ---
+ | - input
+ | ...
+popen:close()
+ | ---
+ | - true
+ | ...
+
+--
+-- Test reading stderr (simply redirect stdout to stderr)
+--
+script = "echo -n 1 2 3 4 5 1>&2"
+ | ---
+ | ...
+popen = fio.popen(script, "rw", {stderr = true})
+ | ---
+ | ...
+popen:wait()
+ | ---
+ | - null
+ | - 1
+ | - 0
+ | ...
+size = 128
+ | ---
+ | ...
+dst = buf:reserve(size)
+ | ---
+ | ...
+res, err = popen:read2({buf = dst, size = size, nil, flags = {stderr = true}})
+ | ---
+ | ...
+res = buf:alloc(res)
+ | ---
+ | ...
+ffi.string(buf.rpos, buf:size())
+ | ---
+ | - 1 2 3 4 5
+ | ...
+buf:recycle()
+ | ---
+ | ...
+popen:close()
+ | ---
+ | - true
+ | ...
+
+--
+-- Test timeouts: just wait for 10 microseconds
+-- to elapse, then write data and re-read for sure.
+--
+script = "prompt=''; read -n 5 prompt && echo -n $prompt;"
+ | ---
+ | ...
+popen = fio.popen(script, "rw")
+ | ---
+ | ...
+popen:read(nil, nil, {timeout_msecs = 10, flags = {stdout = true}})
+ | ---
+ | - null
+ | - null
+ | ...
+popen:write("input")
+ | ---
+ | - 5
+ | ...
+popen:read()
+ | ---
+ | - input
+ | ...
+popen:close()
+ | ---
+ | - true
+ | ...
diff --git a/test/app/popen.test.lua b/test/app/popen.test.lua
new file mode 100644
index 000000000..b6c8cbbba
--- /dev/null
+++ b/test/app/popen.test.lua
@@ -0,0 +1,68 @@
+fio = require('fio')
+buffer = require('buffer')
+ffi = require('ffi')
+
+test_run = require('test_run').new()
+
+local err, reason, exit_code
+buf = buffer.ibuf()
+
+--
+-- Trivial echo output
+--
+script = "echo -n 1 2 3 4 5"
+popen = fio.popen(script, "r")
+popen:wait() -- wait echo to finish
+popen:read() -- read the output
+popen:close() -- release the popen
+
+--
+-- Test info and killing of a child process
+--
+script = "while [ 1 ]; do sleep 10; done"
+popen = fio.popen(script, "r")
+popen:kill()
+--
+-- Killing child may be queued and depends on
+-- system load, so we may get ESRCH here.
+err, reason, exit_code = popen:wait()
+popen:state()
+info = popen:info()
+info["state"]
+info["flags"]
+info["exit_code"]
+popen:close()
+
+--
+-- Test for stdin/out stream
+--
+script="prompt=''; read -n 5 prompt; echo -n $prompt"
+popen = fio.popen(script, "rw")
+popen:write("input")
+popen:read()
+popen:close()
+
+--
+-- Test reading stderr (simply redirect stdout to stderr)
+--
+script = "echo -n 1 2 3 4 5 1>&2"
+popen = fio.popen(script, "rw", {stderr = true})
+popen:wait()
+size = 128
+dst = buf:reserve(size)
+res, err = popen:read2({buf = dst, size = size, nil, flags = {stderr = true}})
+res = buf:alloc(res)
+ffi.string(buf.rpos, buf:size())
+buf:recycle()
+popen:close()
+
+--
+-- Test timeouts: just wait for 10 microseconds
+-- to elapse, then write data and re-read for sure.
+--
+script = "prompt=''; read -n 5 prompt && echo -n $prompt;"
+popen = fio.popen(script, "rw")
+popen:read(nil, nil, {timeout_msecs = 10, flags = {stdout = true}})
+popen:write("input")
+popen:read()
+popen:close()
--
2.20.1
More information about the Tarantool-patches
mailing list