iptables-test: fix python3
PATCH 1/1 iptables-test: fix python3
This patch converts the 'iptables-test.py' file (iptables/iptables-test.py) to run on both python 2 and python3.
--- iptables-test.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-)
iptables-test.py +22 -21
@@ -10,6 +10,7 @@ # This software has been sponsored by Sophos Astaro <http://www.sophos.com> # +from __future__ import print_function import sys import os import subprocess
@@ -45,8 +46,8 @@ def print_error(reason, filename=None, lineno=None): ''' Prints an error with nice colors, indicating file and line number. ''' - print (filename + ": " + Colors.RED + "ERROR" + - Colors.ENDC + ": line %d (%s)" % (lineno, reason)) + print(filename + ": " + Colors.RED + "ERROR" + + Colors.ENDC + ": line {} ({})".format(lineno, reason)) def delete_rule(iptables, rule, filename, lineno):
@@ -79,7 +80,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns): cmd = iptables + " -A " + rule if netns: - cmd = "ip netns exec ____iptables-container-test " + EXECUTEABLE + " " + cmd + cmd = "ip netns exec ____iptables-container-test " + EXECUTEABLE + " {}".format(cmd) ret = execute_cmd(cmd, filename, lineno)
@@ -88,7 +89,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns): # if ret: if res == "OK": - reason = "cannot load: " + cmd + reason = "cannot load: {}".format(cmd) print_error(reason, filename, lineno) return -1 else:
@@ -96,7 +97,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns): return 0 else: if res == "FAIL": - reason = "should fail: " + cmd + reason = "should fail: {}".format(cmd) print_error(reason, filename, lineno) delete_rule(iptables, rule, filename, lineno) return -1
@@ -119,10 +120,10 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns): command = EBTABLES_SAVE path = os.path.abspath(os.path.curdir) + "/iptables/" + EXECUTEABLE - command = path + " " + command + command = path + " {}".format(command) if netns: - command = "ip netns exec ____iptables-container-test " + command + command = "ip netns exec ____iptables-container-test {}".format(command) args = splitted[1:] proc = subprocess.Popen(command, shell=True,
@@ -134,7 +135,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns): # check for segfaults # if proc.returncode == -11: - reason = "iptables-save segfaults: " + cmd + reason = "iptables-save segfaults: {}".format(cmd) print_error(reason, filename, lineno) delete_rule(iptables, rule, filename, lineno) return -1
@@ -142,7 +143,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns): # find the rule matching = out.find(rule_save) if matching < 0: - reason = "cannot find: " + iptables + " -I " + rule + reason = "cannot find: {}".format(iptables) + " -I {}".format(rule) print_error(reason, filename, lineno) delete_rule(iptables, rule, filename, lineno) return -1
@@ -164,16 +165,16 @@ def execute_cmd(cmd, filename, lineno): ''' global log_file if cmd.startswith('iptables ') or cmd.startswith('ip6tables ') or cmd.startswith('ebtables ') or cmd.startswith('arptables '): - cmd = os.path.abspath(os.path.curdir) + "/iptables/" + EXECUTEABLE + " " + cmd + cmd = os.path.abspath(os.path.curdir) + "/iptables/" + EXECUTEABLE + " {}".format(cmd) - print >> log_file, "command: %s" % cmd + print("command: {}".format(cmd), file=log_file) ret = subprocess.call(cmd, shell=True, universal_newlines=True, stderr=subprocess.STDOUT, stdout=log_file) log_file.flush() # generic check for segfaults if ret == -11: - reason = "command segfaults: " + cmd + reason = "command segfaults: {}".format(cmd) print_error(reason, filename, lineno) return ret
@@ -232,7 +233,7 @@ def run_test_file(filename, netns): if line[0] == "@": external_cmd = line.rstrip()[1:] if netns: - external_cmd = "ip netns exec ____iptables-container-test " + external_cmd + external_cmd = "ip netns exec ____iptables-container-test {}".format(external_cmd) execute_cmd(external_cmd, filename, lineno) continue
@@ -240,7 +241,7 @@ def run_test_file(filename, netns): if line[0] == "%": external_cmd = line.rstrip()[1:] if netns: - external_cmd = "ip netns exec ____iptables-container-test " + EXECUTEABLE + " " + external_cmd + external_cmd = "ip netns exec ____iptables-container-test {}".format(EXECUTEABLE) + " {}".format(external_cmd) execute_cmd(external_cmd, filename, lineno) continue
@@ -249,7 +250,7 @@ def run_test_file(filename, netns): continue if len(chain_array) == 0: - print "broken test, missing chain, leaving" + print("broken test, missing chain, leaving") sys.exit() test_passed = True
@@ -282,7 +283,7 @@ def run_test_file(filename, netns): if netns: execute_cmd("ip netns del ____iptables-container-test", filename, 0) if total_test_passed: - print filename + ": " + Colors.GREEN + "OK" + Colors.ENDC + print(filename + ": " + Colors.GREEN + "OK" + Colors.ENDC) f.close() return tests, passed
@@ -302,7 +303,7 @@ def show_missing(): missing = [test_name(i) for i in libfiles if not test_name(i) in testfiles] - print '\n'.join(missing) + print('\n'.join(missing)) #
@@ -336,7 +337,7 @@ def main(): EXECUTEABLE = "xtables-nft-multi" 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 os.putenv("XTABLES_LIBDIR", os.path.abspath(EXTENSIONS_PATH))
@@ -351,7 +352,7 @@ def main(): try: log_file = open(LOGFILE, 'w') except IOError: - print "Couldn't open log file %s" % LOGFILE + print("Couldn't open log file {}".format(LOGFILE)) return file_list = [os.path.join(EXTENSIONS_PATH, i)
@@ -365,9 +366,9 @@ def main(): passed += file_passed test_files += 1 - print ("%d test files, %d unit tests, %d passed" % - (test_files, tests, passed)) + print("{} test files, {} unit tests, {} passed".format(test_files, tests, passed)) if __name__ == '__main__': main() +
Signature · 1 line
2.21.0.windows.1
Quoted 13 lines · Shekhar Sharma
This patch converts the '
iptables-test.py' file (iptables/iptables-test.py) to run on both python 2 and python3.Signed-off-by Shekhar Sharma <[email protected]>--- iptables-test.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-)iptables-test.py +0
-0
[..]
iptables-test.py def run_test(iptables, rule, rule_save, res, filename, lineno, netns): · line 83
Quoted 6 lines
iptables-test.py +1
-1@@ -79,7 +80,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns): cmd = iptables + " -A " + rule if netns: - cmd = "ip netns exec ____iptables-container-test " + EXECUTEABLE + " " + cmd + cmd = "ip netns exec ____iptables-container-test " + EXECUTEABLE + " {}".format(cmd)
This is a bogus change. No reason to switch to format() when we're just concatenating strings. Many occurrences of this in the patch.
I think you only need to fix the print statements.
Quoted 3 lines
iptables-test.py +0
-0ret = execute_cmd(cmd, filename, lineno)
[..]
iptables-test.py def main(): · line 374
Quoted 12 lines
iptables-test.py +2
-2@@ -365,9 +366,9 @@ def main(): passed += file_passed test_files += 1 - print ("%d test files, %d unit tests, %d passed" % - (test_files, tests, passed)) + print("{} test files, {} unit tests, {} passed".format(test_files, tests, passed)) if __name__ == '__main__': main() +
Bogus new line.
tests/py/nft-test.py
Quoted 29 lines · Eric Garver
Sun, Jun 09, 2019 at 11:48:49PM +0530, Shekhar Sharma wrote:
Quoted 10 lines · Shekhar Sharma
This patch converts the '
iptables-test.py' file (iptables/iptables-test.py) to run on both python 2 and python3.Signed-off-by Shekhar Sharma <[email protected]>--- iptables-test.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-)Sun, Jun 09, 2019 at 11:48:49PM +0530, Shekhar Sharma wrote: renamed from On +0
-0This patch adds the netns feature to the '
nft-test.py' file.Signed-off-by Shekhar Sharma <[email protected]>---
The version history of the patch is :
v1: add the netns feature
v2: useformat()method to simplify print statements.
v3: updated the shebang
v4: resent the same with small changestests/py/nft-test.py | 98 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 18 deletions(-)tests/py/nft-test.py +0
-0
[..]iptables-test.py b/iptables-test.py
Quoted 14 lines
Quoted 3 lines
index 532dee7..8018b65 100755
iptables-test.py +0
-0[..]
iptables-test.py def run_test(iptables, rule, rule_save, res, filename, lineno, netns): · line 83
Quoted 6 lines
iptables-test.py +1
-1@@ -79,7 +80,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns): cmd = iptables + " -A " + rule if netns: - cmd = "ip netns exec ____iptables-container-test " + EXECUTEABLE + " " + cmd + cmd = "ip netns exec ____iptables-container-test " + EXECUTEABLE + " {}".format(cmd)This is a bogus change. No reason to switch to
format()when we're just concatenating strings. Many occurrences of this in the patch.
Quoted 2 lines
I think you only need to fix the print statements.
Okay, i will change it and resend the patch.
Quoted 13 lines
Quoted 3 lines
iptables-test.py +0
-0ret = execute_cmd(cmd, filename, lineno)[..]
Quoted 9 lines
iptables-test.py +1
-2@@ -365,9 +366,9 @@ def main(): passed += file_passed test_files += 1 - print ("%d test files, %d unit tests, %d passed" % - (test_files, tests, passed)) + print("{} test files, {} unit tests, {} passed".format(test_files, tests, passed))
Quoted 5 lines
iptables-test.py def main(): · line 374
Quoted 3 lines
iptables-test.py +1
-0if __name__ == '__main__': main() +Bogus new line.
Should i change the shebang to this here as well?
#!/usr/bin/env python
Thanks!
Shekhar
Quoted 63 lines · shekhar sharma
tests/py/nft-test.py
Quoted 29 lines · Eric Garver
Sun, Jun 09, 2019 at 11:48:49PM +0530, Shekhar Sharma wrote:
Quoted 10 lines · Shekhar Sharma
This patch converts the '
iptables-test.py' file (iptables/iptables-test.py) to run on both python 2 and python3.Signed-off-by Shekhar Sharma <[email protected]>--- iptables-test.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-)Sun, Jun 09, 2019 at 11:48:49PM +0530, Shekhar Sharma wrote: renamed from On +0
-0This patch adds the netns feature to the '
nft-test.py' file.Signed-off-by Shekhar Sharma <[email protected]>---
The version history of the patch is :
v1: add the netns feature
v2: useformat()method to simplify print statements.
v3: updated the shebang
v4: resent the same with small changestests/py/nft-test.py | 98 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 18 deletions(-)tests/py/nft-test.py +0
-0[..]
iptables-test.pyb/iptables-test.pyQuoted 14 lines
Quoted 3 lines
index 532dee7..8018b65 100755
iptables-test.py +0
-0[..]
iptables-test.py def run_test(iptables, rule, rule_save, res, filename, lineno, netns): · line 83
Quoted 6 lines
iptables-test.py +1
-1@@ -79,7 +80,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns): cmd = iptables + " -A " + rule if netns: - cmd = "ip netns exec ____iptables-container-test " + EXECUTEABLE + " " + cmd + cmd = "ip netns exec ____iptables-container-test " + EXECUTEABLE + " {}".format(cmd)This is a bogus change. No reason to switch to
format()when we're just concatenating strings. Many occurrences of this in the patch.Quoted 2 lines
I think you only need to fix the print statements.
Okay, i will change it and resend the patch.
Quoted 13 lines
Quoted 3 lines
iptables-test.py +0
-0ret = execute_cmd(cmd, filename, lineno)[..]
Quoted 9 lines
iptables-test.py +1
-2@@ -365,9 +366,9 @@ def main(): passed += file_passed test_files += 1 - print ("%d test files, %d unit tests, %d passed" % - (test_files, tests, passed)) + print("{} test files, {} unit tests, {} passed".format(test_files, tests, passed))
Quoted 8 lines
Quoted 5 lines
iptables-test.py def main(): · line 374
Quoted 3 lines
iptables-test.py +1
-0if __name__ == '__main__': main() +Bogus new line.
Should i change the shebang to this here as well?
#!/usr/bin/env python
Yes. Might as well since you already have to reroll the patch.