diff options
Diffstat (limited to 'tests/json_echo/run-test.py')
-rwxr-xr-x | tests/json_echo/run-test.py | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/tests/json_echo/run-test.py b/tests/json_echo/run-test.py index a636d5f2..a6bdfc61 100755 --- a/tests/json_echo/run-test.py +++ b/tests/json_echo/run-test.py @@ -4,6 +4,7 @@ from __future__ import print_function import sys import os import json +import argparse TESTS_PATH = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(TESTS_PATH, '../../py/')) @@ -13,12 +14,26 @@ from nftables import Nftables # Change working directory to repository root os.chdir(TESTS_PATH + "/../..") -if not os.path.exists('src/.libs/libnftables.so'): - print("The nftables library does not exist. " - "You need to build the project.") +parser = argparse.ArgumentParser(description='Run JSON echo tests') +parser.add_argument('-H', '--host', action='store_true', + help='Run tests against installed libnftables.so.1') +parser.add_argument('-l', '--library', default=None, + help='Path to libntables.so, overrides --host') +args = parser.parse_args() + +check_lib_path = True +if args.library is None: + if args.host: + args.library = 'libnftables.so.1' + check_lib_path = False + else: + args.library = 'src/.libs/libnftables.so.1' + +if check_lib_path and not os.path.exists(args.library): + print("Library not found at '%s'." % args.library) sys.exit(1) -nftables = Nftables(sofile = 'src/.libs/libnftables.so') +nftables = Nftables(sofile = args.library) nftables.set_echo_output(True) # various commands to work with @@ -80,25 +95,25 @@ add_quota = { "add": { # helper functions def exit_err(msg): - print("Error: %s" %msg) + print("Error: %s" %msg, file=sys.stderr) sys.exit(1) def exit_dump(e, obj): - print("FAIL: {}".format(e)) - print("Output was:") - json.dumps(out, sort_keys = True, indent = 4, separators = (',', ': ')) - sys.exit(1) + msg = "{}\n".format(e) + msg += "Output was:\n" + msg += json.dumps(obj, sort_keys = True, indent = 4, separators = (',', ': ')) + exit_err(msg) def do_flush(): rc, out, err = nftables.json_cmd({ "nftables": [flush_ruleset] }) - if not rc is 0: + if rc != 0: exit_err("flush ruleset failed: {}".format(err)) def do_command(cmd): if not type(cmd) is list: cmd = [cmd] rc, out, err = nftables.json_cmd({ "nftables": cmd }) - if not rc is 0: + if rc != 0: exit_err("command failed: {}".format(err)) return out @@ -119,7 +134,7 @@ def get_handle(output, search): else: data = item - k = search.keys()[0] + k = list(search.keys())[0] if not k in data: continue |