Commit 22791635 authored by Daniel Salzman's avatar Daniel Salzman

func-test: add support for setting of test/case to run

parent 60bdb71f
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse, importlib, os, sys, tempfile, time, traceback import argparse, importlib, os, re, sys, tempfile, time, traceback
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/tools") sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/tools")
from dnstest import log, err from dnstest import log, err
import params import params
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", \ parser.add_argument("-d", dest="debug", action="store_true", \
help="enable exception traceback on stdout", \ help="enable exception traceback on stdout")
action='store_true') parser.add_argument("tests", metavar="[:]test[/case]", nargs="*", \
help="([exclude] | run) specific (test set | [test case])")
args = parser.parse_args() args = parser.parse_args()
if args.debug:
params.debug = True
outs_dir = tempfile.mkdtemp(prefix="knottest-%s-" % int(time.time())) outs_dir = tempfile.mkdtemp(prefix="knottest-%s-" % int(time.time()))
tests_dir = "tests" tests_dir = "tests"
test_cnt = 0 test_cnt = 0
fail_cnt = 0 fail_cnt = 0
log("Starting Knot test suite %s" % outs_dir) excluded = dict()
included = dict()
for item in args.tests:
if re.match(":", item):
item = item[1:]
storage = excluded
else:
storage = included
parts = item.split("/")
if len(parts) == 1:
case = list()
elif len(parts) == 2:
case = [parts[1]]
else:
print("Invalid argument %s" % item)
exit(1)
test = parts[0]
if test in storage:
storage[test].append(case)
else:
storage[test] = case
# List all tests if nothing was specified.
if not included:
for i in sorted(os.listdir("./" + tests_dir)):
included[i] = list()
def save_traceback(outdir): def save_traceback(outdir):
file = open(params.out_dir + "/traceback", mode="a") file = open(params.out_dir + "/traceback", mode="a")
traceback.print_exc(file=file) traceback.print_exc(file=file)
file.close() file.close()
for test in sorted(os.listdir("./" + tests_dir)): log("Starting Knot test suite %s" % outs_dir)
for test in sorted(included):
# Skip excluded test set.
if test in excluded and not excluded[test]:
continue
# Check test directory.
test_dir = "./%s/%s" % (tests_dir, test) test_dir = "./%s/%s" % (tests_dir, test)
if not os.path.isdir(test_dir): if not os.path.isdir(test_dir):
log("Invalid test name %s (ignored)" % test)
continue continue
log("Test \'%s\'" % test) log("Test \'%s\'" % test)
for case in sorted(os.listdir(test_dir)): # Set test cases to run.
if not included[test]:
# List all test cases.
cases = sorted(os.listdir(test_dir))
else:
cases = included[test]
for case in cases:
# Skip excluded cases.
if test in excluded and case in excluded[test]:
continue
test_cnt += 1
case_dir = test_dir + "/" + case case_dir = test_dir + "/" + case
test_file = case_dir + "/test.py" test_file = case_dir + "/test.py"
if not os.path.isfile(test_file): if not os.path.isfile(test_file):
log(" * case \'%s\': NOT EXECUTABLE!" % case)
fail_cnt += 1
continue continue
log(" * case \'%s\'" % case) log(" * case \'%s\'" % case)
test_cnt += 1
try: try:
out_dir = outs_dir + "/" + test + "/" + case out_dir = outs_dir + "/" + test + "/" + case
...@@ -59,12 +107,12 @@ for test in sorted(os.listdir("./" + tests_dir)): ...@@ -59,12 +107,12 @@ for test in sorted(os.listdir("./" + tests_dir)):
params.err = True params.err = True
params.errmsg = format(exc) params.errmsg = format(exc)
save_traceback(params.out_dir) save_traceback(params.out_dir)
if params.debug: if args.debug:
traceback.print_exc() traceback.print_exc()
except BaseException as exc: except BaseException as exc:
log("Interrupted") log("Interrupted")
save_traceback(params.out_dir) save_traceback(params.out_dir)
if params.debug: if args.debug:
traceback.print_exc() traceback.print_exc()
exit(1) exit(1)
...@@ -77,10 +125,10 @@ for test in sorted(os.listdir("./" + tests_dir)): ...@@ -77,10 +125,10 @@ for test in sorted(os.listdir("./" + tests_dir)):
if params.test: if params.test:
params.test.stop() params.test.stop()
log("TEST CASES: %i, FAILED: %i" % (test_cnt, fail_cnt))
if fail_cnt: if fail_cnt:
log("Failed test cases: %i/%i" % (fail_cnt, test_cnt))
exit(1) exit(1)
else: else:
log("All test cases passed")
exit(0) exit(0)
...@@ -15,5 +15,3 @@ test = None ...@@ -15,5 +15,3 @@ test = None
err = False err = False
# Error message text. # Error message text.
errmsg = "" errmsg = ""
# Enable exception traceback.
debug = False
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment