[iptables PATCH 5/7] tests: xlate-test: Exit non-zero on error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If a test fails, return a non-zero exit code. To do so, propagate the
pass/fail statistics up to main() for evaluation. While being at it,
move the statistics printing into there as well and get rid of that
redundant assignment to 'test_passed'.

Signed-off-by: Phil Sutter <phil@xxxxxx>
---
 xlate-test.py | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/xlate-test.py b/xlate-test.py
index bb7a447dc799e..4a56e798b9587 100755
--- a/xlate-test.py
+++ b/xlate-test.py
@@ -61,7 +61,6 @@ def run_test(name, payload):
                     result.append(magenta("src: ") + line.rstrip(" \n"))
                     result.append(magenta("exp: ") + expected)
                     result.append(magenta("res: ") + translation + "\n")
-                    test_passed = False
                 else:
                     passed += 1
             else:
@@ -76,10 +75,7 @@ def run_test(name, payload):
         print(name + ": " + green("OK"))
     if not test_passed:
         print("\n".join(result), file=sys.stderr)
-    if args.test:
-        print("1 test file, %d tests, %d tests passed, %d tests failed, %d errors" % (tests, passed, failed, errors))
-    else:
-        return tests, passed, failed, errors
+    return tests, passed, failed, errors
 
 
 def load_test_files():
@@ -93,10 +89,9 @@ def load_test_files():
                 total_passed += passed
                 total_failed += failed
                 total_error += errors
+    return (test_files, total_tests, total_passed, total_failed, total_error)
 
 
-    print("%d test files, %d tests, %d tests passed, %d tests failed, %d errors" % (test_files, total_tests, total_passed, total_failed, total_error))
-
 def main():
     global xtables_nft_multi
     if not args.host:
@@ -104,16 +99,27 @@ def main():
         xtables_nft_multi = os.path.abspath(os.path.curdir) \
                             + '/iptables/' + xtables_nft_multi
 
+    files = tests = passed = failed = errors = 0
     if args.test:
         if not args.test.endswith(".txlate"):
             args.test += ".txlate"
         try:
             with open(args.test, "r") as payload:
-                run_test(args.test, payload)
+                files = 1
+                tests, passed, failed, errors = run_test(args.test, payload)
         except IOError:
             print(red("Error: ") + "test file does not exist", file=sys.stderr)
+            return -1
+    else:
+        files, tests, passed, failed, errors = load_test_files()
+
+    if files > 1:
+        file_word = "files"
     else:
-        load_test_files()
+        file_word = "file"
+    print("%d test %s, %d tests, %d tests passed, %d tests failed, %d errors"
+            % (files, file_word, tests, passed, failed, errors))
+    return passed - tests
 
 
 parser = argparse.ArgumentParser()
@@ -121,4 +127,4 @@ parser.add_argument('-H', '--host', action='store_true',
                     help='Run tests against installed binaries')
 parser.add_argument("test", nargs="?", help="run only the specified test file")
 args = parser.parse_args()
-main()
+sys.exit(main())
-- 
2.33.0




[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux