Skip to content

Commit 324a6ad

Browse files
author
Mathew
committed
improve test harness
1 parent c6e6d73 commit 324a6ad

3 files changed

Lines changed: 124 additions & 33 deletions

File tree

IPTables.Net.Tests/IptablesLibraryTest.cs

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,56 @@ public void TestStartup()
6262
}
6363

6464
var binary = GetBinary();
65-
Process.Start(binary, "-N test2").WaitForExit();
66-
Process.Start(binary, "-N test").WaitForExit();
67-
Process.Start(binary, "-A test -j ACCEPT").WaitForExit();
68-
Process.Start(binary, "-N test3").WaitForExit();
69-
Process.Start(binary, "-A test3 -p tcp -m tcp --dport 80 -j ACCEPT").WaitForExit();
65+
CleanupTestChains(binary);
66+
RequireSuccess(binary, "-N test2");
67+
RequireSuccess(binary, "-N test");
68+
RequireSuccess(binary, "-A test -j ACCEPT");
69+
RequireSuccess(binary, "-N test3");
70+
RequireSuccess(binary, "-A test3 -p tcp -m tcp --dport 80 -j ACCEPT");
71+
}
72+
}
73+
74+
private int Execute(string binary, string args, bool logOutput = true)
75+
{
76+
using (var process = Process.Start(new ProcessStartInfo(binary, args)
77+
{
78+
RedirectStandardError = true,
79+
RedirectStandardOutput = true,
80+
UseShellExecute = false
81+
}))
82+
{
83+
var standardOutput = process.StandardOutput.ReadToEnd();
84+
var standardError = process.StandardError.ReadToEnd();
85+
process.WaitForExit();
86+
87+
if (logOutput)
88+
{
89+
if (!string.IsNullOrWhiteSpace(standardOutput))
90+
{
91+
Console.WriteLine(standardOutput);
92+
}
93+
94+
if (!string.IsNullOrWhiteSpace(standardError))
95+
{
96+
Console.Error.WriteLine(standardError);
97+
}
98+
}
99+
100+
return process.ExitCode;
101+
}
102+
}
103+
104+
private void RequireSuccess(string binary, string args)
105+
{
106+
Assert.That(Execute(binary, args), Is.EqualTo(0), "Command should succeed: " + binary + " " + args);
107+
}
108+
109+
private void CleanupTestChains(string binary)
110+
{
111+
foreach (var chain in new[] {"test", "test2", "test3"})
112+
{
113+
Execute(binary, "-F " + chain, false);
114+
Execute(binary, "-X " + chain, false);
70115
}
71116
}
72117

@@ -81,13 +126,9 @@ public void TestDestroy()
81126
}
82127

83128
var binary = GetBinary();
84-
Process.Start(binary, "-F test").WaitForExit();
85-
Process.Start(binary, "-X test").WaitForExit();
86-
Process.Start(binary, "-F test2").WaitForExit();
87-
//Process.Start(binary, "-X test2").WaitForExit();
88-
Process.Start(binary, "-F test3").WaitForExit();
89-
Process.Start(binary, "-X test3").WaitForExit();
90-
} }
129+
CleanupTestChains(binary);
130+
}
131+
}
91132

92133
[Test]
93134
public void TestRuleOutput()

IPTables.Net.Tests/IptcInterfaceTest.cs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,57 @@ public void TestStartup()
6161
Console.WriteLine("Test Startup");
6262

6363
var binary = GetBinary();
64-
Execute(binary, "-F test");
64+
CleanupTestChains(binary);
65+
RequireSuccess(binary, "-N test2");
66+
RequireSuccess(binary, "-N test");
67+
RequireSuccess(binary, "-A test -j ACCEPT");
68+
RequireSuccess(binary, "-N test3");
69+
RequireSuccess(binary, "-A test3 -p tcp -m tcp --dport 80 -j ACCEPT");
70+
}
71+
}
6572

66-
Execute(binary, "-N test2");
67-
Process.Start(binary, "-N test").WaitForExit();
68-
Process.Start(binary, "-A test -j ACCEPT").WaitForExit();
73+
private int Execute(string binary, string args, bool logOutput = true)
74+
{
75+
using (var process = Process.Start(new ProcessStartInfo(binary, args)
76+
{
77+
RedirectStandardError = true,
78+
RedirectStandardOutput = true,
79+
UseShellExecute = false
80+
}))
81+
{
82+
var standardOutput = process.StandardOutput.ReadToEnd();
83+
var standardError = process.StandardError.ReadToEnd();
84+
process.WaitForExit();
6985

70-
Process.Start(binary, "-N test3").WaitForExit();
71-
Process.Start(binary, "-A test3 -p tcp -m tcp --dport 80 -j ACCEPT").WaitForExit();
86+
if (logOutput)
87+
{
88+
if (!string.IsNullOrWhiteSpace(standardOutput))
89+
{
90+
Console.WriteLine(standardOutput);
91+
}
92+
93+
if (!string.IsNullOrWhiteSpace(standardError))
94+
{
95+
Console.Error.WriteLine(standardError);
96+
}
97+
}
98+
99+
return process.ExitCode;
72100
}
73101
}
74102

75-
private void Execute(string binary, string args)
103+
private void RequireSuccess(string binary, string args)
104+
{
105+
Assert.That(Execute(binary, args), Is.EqualTo(0), "Command should succeed: " + binary + " " + args);
106+
}
107+
108+
private void CleanupTestChains(string binary)
76109
{
77-
var process = Process.Start(new ProcessStartInfo(binary, args){RedirectStandardError = true, RedirectStandardOutput = true});
78-
Console.WriteLine(process.StandardOutput.ReadToEnd());
79-
Console.Error.WriteLine(process.StandardError.ReadToEnd());
80-
process.WaitForExit();
110+
foreach (var chain in new[] {"test", "test2", "test3"})
111+
{
112+
Execute(binary, "-F " + chain, false);
113+
Execute(binary, "-X " + chain, false);
114+
}
81115
}
82116

83117
[OneTimeTearDown]
@@ -87,17 +121,11 @@ public void TestDestroy()
87121
{
88122
if (Environment.GetEnvironmentVariable("SKIP_SYSTEM_TESTS") == "1")
89123
{
90-
Assert.Ignore();
124+
return;
91125
}
92126
Console.WriteLine("Test Done");
93127
var binary = GetBinary();
94-
Process.Start(binary, "-D test -j ACCEPT").WaitForExit();
95-
Process.Start(binary, "-F test").WaitForExit();
96-
Process.Start(binary, "-X test").WaitForExit();
97-
Process.Start(binary, "-F test2").WaitForExit();
98-
//Process.Start(binary, "-X test2").WaitForExit();
99-
Process.Start(binary, "-F test3").WaitForExit();
100-
Process.Start(binary, " - X test3").WaitForExit();
128+
CleanupTestChains(binary);
101129
}
102130
}
103131

test.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,39 @@ load_kernel_modules() {
8080
done
8181
}
8282

83+
cleanup_test_chains_for_binary() {
84+
local binary="$1"
85+
local chain
86+
87+
if ! command_exists "$binary"; then
88+
return
89+
fi
90+
91+
for chain in test test2 test3; do
92+
run_as_root "$binary" -F "$chain" >/dev/null 2>&1 || true
93+
run_as_root "$binary" -X "$chain" >/dev/null 2>&1 || true
94+
done
95+
}
96+
97+
cleanup_test_chains() {
98+
cleanup_test_chains_for_binary iptables
99+
cleanup_test_chains_for_binary ip6tables
100+
}
101+
83102
run_full_tests() {
84103
local results_dir
85104
local trap_command
86105
local -a effective_test_args=("${DOTNET_TEST_ARGS[@]}")
87106
results_dir="$(mktemp -d)"
88-
trap_command="$(printf 'rm -rf -- %q; restore_iptables_backend' "$results_dir")"
107+
trap_command="$(printf 'rm -rf -- %q; cleanup_test_chains; restore_iptables_backend' "$results_dir")"
89108
trap "$trap_command" EXIT
90109

91110
switch_to_legacy_backend_if_available
92111
load_kernel_modules
93112

94113
command_exists iptables || die "The iptables binary is required for full system tests."
95114
command_exists ip6tables || die "The ip6tables binary is required for full system tests."
115+
cleanup_test_chains
96116

97117
if [[ "$RUN_UNSTABLE_SYSTEM_TESTS" != "1" ]] && ! has_explicit_test_filter; then
98118
effective_test_args+=("--filter" "TestCategory!=NotWorkingOnTravis")
@@ -104,7 +124,7 @@ run_full_tests() {
104124
return
105125
fi
106126

107-
run_as_root env \
127+
run_and_check_core_dumps run_as_root env \
108128
"PATH=$PATH" \
109129
"DOTNET_ROOT=${DOTNET_ROOT}" \
110130
"DOTNET_CLI_HOME=/root/.dotnet-cli" \
@@ -118,6 +138,8 @@ run_full_tests() {
118138
--no-build \
119139
--no-restore \
120140
--nologo \
141+
-m:1 \
142+
-p:UseSharedCompilation=false \
121143
--results-directory "$results_dir" \
122144
"${effective_test_args[@]}"
123145
}

0 commit comments

Comments
 (0)