Skip to content

Commit 9753915

Browse files
committed
tests(helpers) implement the 'errlog' and 'line' assertion and modifier
1 parent 828a898 commit 9753915

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

spec/helpers.lua

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,104 @@ luassert:register("assertion", "cn", assert_cn,
17721772
"assertion.cn.positive")
17731773

17741774

1775+
do
1776+
--- Generic modifier "errlog"
1777+
-- Will set an "errlog_path" value in the assertion state.
1778+
-- @name errlog
1779+
-- @param path A path to the errlog file (defaults to the test prefix's
1780+
-- errlog).
1781+
local function modifier_errlog(state, args)
1782+
local errlog_path = args[1] or conf.nginx_err_logs
1783+
1784+
assert(type(errlog_path) == "string", "errlog modifier expects nil, or " ..
1785+
"a string as argument, got: " ..
1786+
type(errlog_path))
1787+
1788+
rawset(state, "errlog_path", errlog_path)
1789+
1790+
return state
1791+
end
1792+
1793+
luassert:register("modifier", "errlog", modifier_errlog)
1794+
1795+
1796+
--- Assertion checking is any line from a file matches the given regex or
1797+
-- substring.
1798+
-- @name line
1799+
-- @param regex The regex to evaluate against each line.
1800+
-- @param plain If true, the regex argument will be considered as a plain
1801+
-- string.
1802+
-- @param timeout An optional timeout after which the assertion will fail if
1803+
-- reached.
1804+
-- @param fpath An optional path to the file (defaults to the errlog
1805+
-- modifier)
1806+
-- @see errlog
1807+
-- @usage
1808+
-- assert.not_line("[error]", true)
1809+
-- assert.errlog().not_has.line("[error]", true)
1810+
local function match_line(state, args)
1811+
local regex = args[1]
1812+
local plain = args[2]
1813+
local timeout = args[3] or 2
1814+
local fpath = args[4] or rawget(state, "errlog_path")
1815+
1816+
assert(type(regex) == "string",
1817+
"Expected the regex argument to be a string")
1818+
assert(type(fpath) == "string",
1819+
"Expected the file path argument to be a string")
1820+
assert(type(timeout) == "number" and timeout > 0,
1821+
"Expected the timeout argument to be a positive number")
1822+
1823+
local pok = pcall(wait_until, function()
1824+
local logs = pl_file.read(fpath)
1825+
local from, _, err
1826+
1827+
for line in logs:gmatch("[^\r\n]+") do
1828+
if plain then
1829+
from = string.find(line, regex, nil, true)
1830+
1831+
else
1832+
from, _, err = ngx.re.find(line, regex)
1833+
if err then
1834+
error(err)
1835+
end
1836+
end
1837+
1838+
if from then
1839+
table.insert(args, 1, line)
1840+
table.insert(args, 1, regex)
1841+
args.n = 2
1842+
return true
1843+
end
1844+
end
1845+
end, timeout)
1846+
1847+
table.insert(args, 1, fpath)
1848+
args.n = args.n + 1
1849+
1850+
return pok
1851+
end
1852+
1853+
say:set("assertion.match_line.negative", unindent [[
1854+
Expected file at:
1855+
%s
1856+
To match:
1857+
%s
1858+
]])
1859+
say:set("assertion.match_line.positive", unindent [[
1860+
Expected file at:
1861+
%s
1862+
To not match:
1863+
%s
1864+
But matched line:
1865+
%s
1866+
]])
1867+
luassert:register("assertion", "line", match_line,
1868+
"assertion.match_line.negative",
1869+
"assertion.match_line.positive")
1870+
end
1871+
1872+
17751873
----------------
17761874
-- DNS-record mocking.
17771875
-- These function allow to create mock dns records that the test Kong instance

0 commit comments

Comments
 (0)