From: Marek VavrusÌŒa Date: Thu, 23 Nov 2017 07:40:28 +0000 (-0800) Subject: tests: fixed config tests locking up on error, added test for predict X-Git-Tag: v1.5.1~16^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52d4f519b699007aa297fdaa682bad8d90d096b6;p=thirdparty%2Fknot-resolver.git tests: fixed config tests locking up on error, added test for predict The config tests locked up on error as if error was raised from the event callback, it would never reach the `quit()` statement, so server would never close on error. Added a script to make running these types of tests a little bit nicer and to allow concurrent execution of config tests. Added a test for the predict module, that fails on prediction of unknown types: ``` error: /usr/local/lib/kdns_modules/predict.lua:34: 'struct rr_type' has no member named 'TYPE65535' ``` --- diff --git a/daemon/lua/config.lua b/daemon/lua/config.lua index 91babeb5b..f8769017b 100644 --- a/daemon/lua/config.lua +++ b/daemon/lua/config.lua @@ -1,5 +1,5 @@ -- Listen on localhost -if not next(net.list()) then +if not next(net.list()) and not env.TEST then local ok, err = pcall(net.listen, '127.0.0.1') if not ok then error('bind to 127.0.0.1@53 '..err) diff --git a/tests/config/predict/test.cfg b/tests/config/predict/test.cfg new file mode 100644 index 000000000..c0c1069ba --- /dev/null +++ b/tests/config/predict/test.cfg @@ -0,0 +1,18 @@ +dofile('./test_utils.lua') -- load test utilities + +-- setup resolver +modules = { 'predict' } + +-- test if prediction of non-standard types works +function test_predict_drain_typex() + predict.queue_len = 1 + predict.queue['TYPE65535 example.com'] = 1 + predict.drain() +end + +-- run test after processed config file +-- default config will be used and we can test it. +event.after(0, function (ev) + test(test_predict_drain_typex) + quit() +end) diff --git a/tests/config/runtest.sh b/tests/config/runtest.sh new file mode 100755 index 000000000..158793192 --- /dev/null +++ b/tests/config/runtest.sh @@ -0,0 +1,11 @@ +#!/bin/sh -e +export TMP_RUNDIR=`mktemp -d` +function finish { + rm -rf ${TMP_RUNDIR} +} +trap finish EXIT + +echo "config-test: ${2}" +cp tests/config/${2}/* ${TMP_RUNDIR}/ +cp tests/config/test_utils.lua ${TMP_RUNDIR}/ +TEST=1 ${1} -f 1 -c test.cfg ${TMP_RUNDIR} \ No newline at end of file diff --git a/tests/config/test_config.mk b/tests/config/test_config.mk index 86822f121..4f26dc982 100644 --- a/tests/config/test_config.mk +++ b/tests/config/test_config.mk @@ -5,19 +5,17 @@ # Run kresd in temp directory and use config test.cfg # Check return code of kresd. Passed test have to call quit(). -tests_lua := \ - hints +tests_config := \ + hints \ + predict -check-config: check-install-precond - $(foreach test,$(tests_lua), \ - @echo "config-test: $(test)" ;\ - export TMP_RUNDIR=`mktemp -d` ;\ - cp "tests/config/$(test)"/* $${TMP_RUNDIR} ;\ - cp tests/config/test_utils.lua $${TMP_RUNDIR} ;\ - $(preload_syms) $(DEBUGGER) $(abspath $(SBINDIR)/kresd) -c test.cfg $${TMP_RUNDIR} > /dev/null ;\ - export retval=$$? ;\ - rm -rf $${TMP_RUNDIR} ;\ - test $${retval} -eq 0 ;\ - ) +define make_config_test +test-config-$(1): tests/config/$(1)/test.cfg check-install-precond + @$(preload_syms) $(DEBUGGER) ./tests/config/runtest.sh $(abspath $(SBINDIR)/kresd) $(1) +.PHONY: test-$(1) +endef + +$(foreach test,$(tests_config),$(eval $(call make_config_test,$(test)))) +check-config: $(foreach test,$(tests_config),test-config-$(test)) .PHONY: check-config diff --git a/tests/config/test_utils.lua b/tests/config/test_utils.lua index 5cdc1e538..b96a96788 100644 --- a/tests/config/test_utils.lua +++ b/tests/config/test_utils.lua @@ -1,21 +1,30 @@ function fail(fmt, ...) - io.stderr:write(string.format(fmt..'\n', ...)) + io.stderr:write(string.format(fmt..'\n', ...)) os.exit(2) end +function test(f, ...) + local res, exception = pcall(f, ...) + if not res then + local trace = debug.getinfo(2) + fail('%s:%d %s', trace.source, trace.currentline, exception) + end + return res +end + function table_keys_to_lower(table) - res = {} - for k, v in pairs(table) do - res[k:lower()] = v - end + local res = {} + for k, v in pairs(table) do + res[k:lower()] = v + end return res end function contains(table, value) - for k,v in pairs(table) do + for _, v in pairs(table) do if v == value then return true end end - return false + return false end