summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorShekhar Sharma <shekhar250198@gmail.com>2019-06-29 01:32:29 +0530
committerPablo Neira Ayuso <pablo@netfilter.org>2019-07-01 20:47:04 +0200
commit57ca420f02acb3cac948c43916076452bcb00734 (patch)
tree121c9836dffe82a7d3ef37c1c632cfb4a81d0bb0 /tests
parent16543a0136c00391ecb7e38a13878eb806648bf7 (diff)
tests: py: fix python3
This converts the nft-test.py file to run on both py2 and py3. Signed-off-by: Shekhar Sharma <shekhar250198@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'tests')
-rwxr-xr-xtests/py/nft-test.py66
1 files changed, 33 insertions, 33 deletions
diff --git a/tests/py/nft-test.py b/tests/py/nft-test.py
index 09d00dba..fcbd28ca 100755
--- a/tests/py/nft-test.py
+++ b/tests/py/nft-test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python2
+#!/usr/bin/env python
#
# (C) 2014 by Ana Rey Botello <anarey@gmail.com>
#
@@ -13,12 +13,14 @@
# Thanks to the Outreach Program for Women (OPW) for sponsoring this test
# infrastructure.
+from __future__ import print_function
import sys
import os
import argparse
import signal
import json
import traceback
+import tempfile
TESTS_PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(TESTS_PATH, '../../py/'))
@@ -486,12 +488,11 @@ def set_check_element(rule1, rule2):
'''
Check if element exists in anonymous sets.
'''
- ret = -1
pos1 = rule1.find("{")
pos2 = rule2.find("{")
- if (cmp(rule1[:pos1], rule2[:pos2]) != 0):
- return ret;
+ if (rule1[:pos1] != rule2[:pos2]):
+ return False
end1 = rule1.find("}")
end2 = rule2.find("}")
@@ -501,13 +502,12 @@ def set_check_element(rule1, rule2):
list2 = (rule2[pos2 + 1:end2].replace(" ", "")).split(",")
list1.sort()
list2.sort()
- if cmp(list1, list2) == 0:
- ret = 0
+ if list1 != list2:
+ return False
- if ret != 0:
- return ret
+ return rule1[end1:] == rule2[end2:]
- return cmp(rule1[end1:], rule2[end2:])
+ return False
def obj_add(o, test_result, filename, lineno):
@@ -770,7 +770,7 @@ def rule_add(rule, filename, lineno, force_all_family_option, filename_path):
unit_tests += 1
table_flush(table, filename, lineno)
- payload_log = os.tmpfile()
+ payload_log = tempfile.TemporaryFile(mode="w+")
# Add rule and check return code
cmd = "add rule %s %s %s" % (table, chain, rule[0])
@@ -840,8 +840,8 @@ def rule_add(rule, filename, lineno, force_all_family_option, filename_path):
if rule_output.rstrip() != teoric_exit.rstrip():
if rule[0].find("{") != -1: # anonymous sets
- if set_check_element(teoric_exit.rstrip(),
- rule_output.rstrip()) != 0:
+ if not set_check_element(teoric_exit.rstrip(),
+ rule_output.rstrip()):
warning += 1
retest_output = True
print_differences_warning(filename, lineno,
@@ -910,7 +910,7 @@ def rule_add(rule, filename, lineno, force_all_family_option, filename_path):
gotf.name, 1)
table_flush(table, filename, lineno)
- payload_log = os.tmpfile()
+ payload_log = tempfile.TemporaryFile(mode="w+")
# Add rule in JSON format
cmd = json.dumps({ "nftables": [{ "add": { "rule": {
@@ -1016,9 +1016,9 @@ def execute_cmd(cmd, filename, lineno, stdout_log=False, debug=False):
:param debug: temporarily set these debug flags
'''
global log_file
- print >> log_file, "command: %s" % cmd
+ print("command: {}".format(cmd), file=log_file)
if debug_option:
- print cmd
+ print(cmd)
if debug:
debug_old = nftables.get_debug()
@@ -1212,7 +1212,7 @@ def run_test_file(filename, force_all_family_option, specific_file):
sys.stdout.flush()
if signal_received == 1:
- print "\nSignal received. Cleaning up and Exitting..."
+ print("\nSignal received. Cleaning up and Exitting...")
cleanup_on_exit()
sys.exit(0)
@@ -1319,13 +1319,13 @@ def run_test_file(filename, force_all_family_option, specific_file):
if specific_file:
if force_all_family_option:
- print print_result_all(filename, tests, total_warning, total_error,
- total_unit_run)
+ print(print_result_all(filename, tests, total_warning, total_error,
+ total_unit_run))
else:
- print print_result(filename, tests, total_warning, total_error)
+ print(print_result(filename, tests, total_warning, total_error))
else:
if tests == passed and tests > 0:
- print filename + ": " + Colors.GREEN + "OK" + Colors.ENDC
+ print(filename + ": " + Colors.GREEN + "OK" + Colors.ENDC)
f.close()
del table_list[:]
@@ -1336,7 +1336,7 @@ def run_test_file(filename, force_all_family_option, specific_file):
def main():
- parser = argparse.ArgumentParser(description='Run nft tests', version='1.0')
+ parser = argparse.ArgumentParser(description='Run nft tests')
parser.add_argument('filenames', nargs='*', metavar='path/to/file.t',
help='Run only these tests')
@@ -1359,6 +1359,10 @@ def main():
dest='enable_schema',
help='verify json input/output against schema')
+ parser.add_argument('-v', '--version', action='version',
+ version='1.0',
+ help='Print the version information')
+
args = parser.parse_args()
global debug_option, need_fix_option, enable_json_option, enable_json_schema
debug_option = args.debug
@@ -1372,15 +1376,15 @@ def main():
signal.signal(signal.SIGTERM, signal_handler)
if os.getuid() != 0:
- print "You need to be root to run this, sorry"
+ print("You need to be root to run this, sorry")
return
# 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."
+ print("The nftables library does not exist. "
+ "You need to build the project.")
return
if args.enable_schema and not args.enable_json:
@@ -1434,19 +1438,15 @@ def main():
run_total += file_unit_run
if test_files == 0:
- print "No test files to run"
+ print("No test files to run")
else:
if not specific_file:
if force_all_family_option:
- print "%d test files, %d files passed, %d unit tests, " \
- "%d total executed, %d error, %d warning" \
- % (test_files, files_ok, tests, run_total, errors,
- warnings)
+ print("%d test files, %d files passed, %d unit tests, " % (test_files, files_ok, tests))
+ print("%d total executed, %d error, %d warning" % (run_total, errors,warnings))
else:
- print "%d test files, %d files passed, %d unit tests, " \
- "%d error, %d warning" \
- % (test_files, files_ok, tests, errors, warnings)
-
+ print("%d test files, %d files passed, %d unit tests, " % (test_files, files_ok, tests))
+ print("%d error, %d warning" % (errors, warnings))
if __name__ == '__main__':
main()