blob: dbba1d673ec9f6d35154db594ddde9e0d35751c2 [file] [log] [blame]
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +02001#!/usr/bin/env python3
2# encoding: utf-8
3
4import os
5import sys
6import shlex
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +02007import argparse
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +02008from subprocess import Popen, PIPE
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +02009
10keywords = ("iptables-translate", "ip6tables-translate")
11
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020012if sys.stdout.isatty():
13 colors = {"magenta": "\033[95m", "green": "\033[92m", "yellow": "\033[93m",
14 "red": "\033[91m", "end": "\033[0m"}
15else:
16 colors = {"magenta": "", "green": "", "yellow": "", "red": "", "end": ""}
17
18
19def magenta(string):
20 return colors["magenta"] + string + colors["end"]
21
22
23def red(string):
24 return colors["red"] + string + colors["end"]
25
26
27def yellow(string):
28 return colors["yellow"] + string + colors["end"]
29
30
31def green(string):
32 return colors["green"] + string + colors["end"]
33
34
35def run_test(name, payload):
36 test_passed = True
Harsha Sharma4bd51772017-10-22 18:19:09 +053037 tests = passed = failed = errors = 0
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020038 result = []
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020039
40 for line in payload:
41 if line.startswith(keywords):
Harsha Sharma4bd51772017-10-22 18:19:09 +053042 tests += 1
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +020043 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 Sharma4bd51772017-10-22 18:19:09 +053049 test_passed = False
50 failed += 1
51 result.append(name + ": " + red("Fail"))
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +020052 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 Sharma4bd51772017-10-22 18:19:09 +053056 else:
57 passed += 1
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +020058 else:
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020059 test_passed = False
Harsha Sharma4bd51772017-10-22 18:19:09 +053060 errors += 1
61 result.append(name + ": " + red("Error: ") + "iptables-translate failure")
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +020062 result.append(error.decode("utf-8"))
Harsha Sharma4bd51772017-10-22 18:19:09 +053063 if (passed == tests) and not args.test:
64 print(name + ": " + green("OK"))
65 if not test_passed:
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020066 print("\n".join(result))
Harsha Sharma4bd51772017-10-22 18:19:09 +053067 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 Garay340105f2017-03-31 14:34:38 +020071
72
73def load_test_files():
Harsha Sharma4bd51772017-10-22 18:19:09 +053074 test_files = total_tests = total_passed = total_error = total_failed = 0
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020075 for test in sorted(os.listdir("extensions")):
76 if test.endswith(".txlate"):
77 with open("extensions/" + test, "r") as payload:
Harsha Sharma4bd51772017-10-22 18:19:09 +053078 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 Garay340105f2017-03-31 14:34:38 +020084
85
Harsha Sharma4bd51772017-10-22 18:19:09 +053086 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 Garay340105f2017-03-31 14:34:38 +020088def main():
Florian Westphal255e55b2018-01-26 23:31:48 +010089 if args.test:
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020090 if not args.test.endswith(".txlate"):
91 args.test += ".txlate"
92 try:
Harsha Sharma4bd51772017-10-22 18:19:09 +053093 with open(args.test, "r") as payload:
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020094 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
101parser = argparse.ArgumentParser()
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +0200102parser.add_argument("test", nargs="?", help="run only the specified test file")
103args = parser.parse_args()
104main()