summaryrefslogtreecommitdiffstats
path: root/xlate-test.py
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2022-10-21 10:28:09 +0200
committerPhil Sutter <phil@nwl.cc>2022-11-11 19:14:28 +0100
commit0d652b8b9ed45d2cfb2e246033cc074faf8aad16 (patch)
treeb06ca2026b3254a9ccc8eb5f6e3ee37bc5350ebe /xlate-test.py
parentf30c5edce0413b2b2346c7f58e801f10f6e9bc5a (diff)
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 <phil@nwl.cc>
Diffstat (limited to 'xlate-test.py')
-rwxr-xr-xxlate-test.py67
1 files 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: