Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # encoding: utf-8 |
| 3 | |
| 4 | import os |
| 5 | import sys |
| 6 | import shlex |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 7 | import argparse |
Pablo M. Bermudo Garay | 3f92b25 | 2017-04-19 01:19:08 +0200 | [diff] [blame] | 8 | from subprocess import Popen, PIPE |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 9 | |
Florian Westphal | e67c088 | 2018-04-13 17:40:56 +0200 | [diff] [blame] | 10 | keywords = ("iptables-translate", "ip6tables-translate", "ebtables-translate") |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 11 | |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 12 | if sys.stdout.isatty(): |
| 13 | colors = {"magenta": "\033[95m", "green": "\033[92m", "yellow": "\033[93m", |
| 14 | "red": "\033[91m", "end": "\033[0m"} |
| 15 | else: |
| 16 | colors = {"magenta": "", "green": "", "yellow": "", "red": "", "end": ""} |
| 17 | |
| 18 | |
| 19 | def magenta(string): |
| 20 | return colors["magenta"] + string + colors["end"] |
| 21 | |
| 22 | |
| 23 | def red(string): |
| 24 | return colors["red"] + string + colors["end"] |
| 25 | |
| 26 | |
| 27 | def yellow(string): |
| 28 | return colors["yellow"] + string + colors["end"] |
| 29 | |
| 30 | |
| 31 | def green(string): |
| 32 | return colors["green"] + string + colors["end"] |
| 33 | |
| 34 | |
| 35 | def run_test(name, payload): |
| 36 | test_passed = True |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 37 | tests = passed = failed = errors = 0 |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 38 | result = [] |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 39 | |
| 40 | for line in payload: |
| 41 | if line.startswith(keywords): |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 42 | tests += 1 |
Pablo M. Bermudo Garay | 3f92b25 | 2017-04-19 01:19:08 +0200 | [diff] [blame] | 43 | process = Popen(shlex.split(line), stdout=PIPE, stderr=PIPE) |
| 44 | (output, error) = process.communicate() |
| 45 | if process.returncode == 0: |
| 46 | translation = output.decode("utf-8").rstrip(" \n") |
| 47 | expected = next(payload).rstrip(" \n") |
| 48 | if translation != expected: |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 49 | test_passed = False |
| 50 | failed += 1 |
| 51 | result.append(name + ": " + red("Fail")) |
Pablo M. Bermudo Garay | 3f92b25 | 2017-04-19 01:19:08 +0200 | [diff] [blame] | 52 | result.append(magenta("src: ") + line.rstrip(" \n")) |
| 53 | result.append(magenta("exp: ") + expected) |
| 54 | result.append(magenta("res: ") + translation + "\n") |
| 55 | test_passed = False |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 56 | else: |
| 57 | passed += 1 |
Pablo M. Bermudo Garay | 3f92b25 | 2017-04-19 01:19:08 +0200 | [diff] [blame] | 58 | else: |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 59 | test_passed = False |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 60 | errors += 1 |
| 61 | result.append(name + ": " + red("Error: ") + "iptables-translate failure") |
Pablo M. Bermudo Garay | 3f92b25 | 2017-04-19 01:19:08 +0200 | [diff] [blame] | 62 | result.append(error.decode("utf-8")) |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 63 | if (passed == tests) and not args.test: |
| 64 | print(name + ": " + green("OK")) |
| 65 | if not test_passed: |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 66 | print("\n".join(result)) |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 67 | if args.test: |
| 68 | print("1 test file, %d tests, %d tests passed, %d tests failed, %d errors" % (tests, passed, failed, errors)) |
| 69 | else: |
| 70 | return tests, passed, failed, errors |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 71 | |
| 72 | |
| 73 | def load_test_files(): |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 74 | test_files = total_tests = total_passed = total_error = total_failed = 0 |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 75 | for test in sorted(os.listdir("extensions")): |
| 76 | if test.endswith(".txlate"): |
| 77 | with open("extensions/" + test, "r") as payload: |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 78 | tests, passed, failed, errors = run_test(test, payload) |
| 79 | test_files += 1 |
| 80 | total_tests += tests |
| 81 | total_passed += passed |
| 82 | total_failed += failed |
| 83 | total_error += errors |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 84 | |
| 85 | |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 86 | print("%d test files, %d tests, %d tests passed, %d tests failed, %d errors" % (test_files, total_tests, total_passed, total_failed, total_error)) |
| 87 | |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 88 | def main(): |
Florian Westphal | 255e55b | 2018-01-26 23:31:48 +0100 | [diff] [blame] | 89 | if args.test: |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 90 | if not args.test.endswith(".txlate"): |
| 91 | args.test += ".txlate" |
| 92 | try: |
Harsha Sharma | 4bd5177 | 2017-10-22 18:19:09 +0530 | [diff] [blame] | 93 | with open(args.test, "r") as payload: |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 94 | run_test(args.test, payload) |
| 95 | except IOError: |
| 96 | print(red("Error: ") + "test file does not exist") |
| 97 | else: |
| 98 | load_test_files() |
| 99 | |
| 100 | |
| 101 | parser = argparse.ArgumentParser() |
Pablo M. Bermudo Garay | 340105f | 2017-03-31 14:34:38 +0200 | [diff] [blame] | 102 | parser.add_argument("test", nargs="?", help="run only the specified test file") |
| 103 | args = parser.parse_args() |
| 104 | main() |