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