From 0d652b8b9ed45d2cfb2e246033cc074faf8aad16 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Fri, 21 Oct 2022 10:28:09 +0200 Subject: tests: xlate-test: Cleanup file reading loop Put the actual translation test into a function to call from the loop and clean it up a bit. Preparation work for running a second test on the same data. Signed-off-by: Phil Sutter --- xlate-test.py | 67 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/xlate-test.py b/xlate-test.py index 03bef7e2..ee393349 100755 --- a/xlate-test.py +++ b/xlate-test.py @@ -33,6 +33,24 @@ def green(string): return colors["green"] + string + colors["end"] +def test_one_xlate(name, sourceline, expected, result): + process = Popen([ xtables_nft_multi ] + shlex.split(sourceline), stdout=PIPE, stderr=PIPE) + (output, error) = process.communicate() + if process.returncode != 0: + result.append(name + ": " + red("Error: ") + "iptables-translate failure") + result.append(error.decode("utf-8")) + return False + + translation = output.decode("utf-8").rstrip(" \n") + if translation != expected: + result.append(name + ": " + red("Fail")) + result.append(magenta("src: ") + sourceline.rstrip(" \n")) + result.append(magenta("exp: ") + expected) + result.append(magenta("res: ") + translation + "\n") + return False + + return True + def run_test(name, payload): global xtables_nft_multi test_passed = True @@ -41,37 +59,26 @@ def run_test(name, payload): line = payload.readline() while line: - if line.startswith(keywords): - sourceline = line - tests += 1 - process = Popen([ xtables_nft_multi ] + shlex.split(line), stdout=PIPE, stderr=PIPE) - (output, error) = process.communicate() - if process.returncode == 0: - translation = output.decode("utf-8").rstrip(" \n") - expected = payload.readline().rstrip(" \n") - next_expected = payload.readline() - if next_expected.startswith("nft"): - expected += "\n" + next_expected.rstrip(" \n") - line = payload.readline() - else: - line = next_expected - if translation != expected: - test_passed = False - failed += 1 - result.append(name + ": " + red("Fail")) - result.append(magenta("src: ") + sourceline.rstrip(" \n")) - result.append(magenta("exp: ") + expected) - result.append(magenta("res: ") + translation + "\n") - else: - passed += 1 - else: - test_passed = False - errors += 1 - result.append(name + ": " + red("Error: ") + "iptables-translate failure") - result.append(error.decode("utf-8")) - line = payload.readline() + if not line.startswith(keywords): + line = payload.readline() + continue + + sourceline = line + expected = payload.readline().rstrip(" \n") + next_expected = payload.readline() + if next_expected.startswith("nft"): + expected += "\n" + next_expected.rstrip(" \n") + line = payload.readline() + else: + line = next_expected + + tests += 1 + if test_one_xlate(name, sourceline, expected, result): + passed += 1 else: - line = payload.readline() + errors += 1 + test_passed = False + if (passed == tests) and not args.test: print(name + ": " + green("OK")) if not test_passed: -- cgit v1.2.3