]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
tests: fixed config tests locking up on error, added test for predict
authorMarek Vavruša <mvavrusa@cloudflare.com>
Thu, 23 Nov 2017 07:40:28 +0000 (23:40 -0800)
committerMarek Vavruša <mvavrusa@cloudflare.com>
Fri, 24 Nov 2017 03:30:29 +0000 (19:30 -0800)
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'
```

daemon/lua/config.lua
tests/config/predict/test.cfg [new file with mode: 0644]
tests/config/runtest.sh [new file with mode: 0755]
tests/config/test_config.mk
tests/config/test_utils.lua

index 91babeb5b503f837e2b60c39ab9935a624d1b166..f8769017ba2e9c7bf825cb62aded23233f6a809f 100644 (file)
@@ -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 (file)
index 0000000..c0c1069
--- /dev/null
@@ -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 (executable)
index 0000000..1587931
--- /dev/null
@@ -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
index 86822f121a0b79502fda1204fa19dd950f011a1f..4f26dc982687785fe3896ea940419e0417876d50 100644 (file)
@@ -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
index 5cdc1e53877bd7a45dff66003d60f801acaefbb7..b96a96788fa0c1ca96e4e79152fac95e0453142e 100644 (file)
@@ -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