Skip to content

Commit c3a9867

Browse files
author
Mathew
committed
fix skipped teardown
1 parent 324a6ad commit c3a9867

3 files changed

Lines changed: 95 additions & 24 deletions

File tree

AGENTS.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
## Overview
44

5-
This repository contains a .NET 8 solution for working with Linux iptables from C#. The managed code lives under `IPTables.Net/`, the NUnit tests live under `IPTables.Net.Tests/`, and the native helper library used by the libiptc-based adapter lives under `ipthelper/`.
5+
This repository contains a .NET 10 solution for working with Linux iptables from C#. The managed code lives under `IPTables.Net/`, the NUnit tests live under `IPTables.Net.Tests/`, and the native helper library used by the libiptc-based adapter lives under `ipthelper/`.
66

77
## Build And Test
88

99
- Use `./build.sh` for a full solution build.
1010
- Use `./test.sh` to build and run tests.
1111
- Use `./test.sh --fast` for the managed-heavy test pass that sets `SKIP_SYSTEM_TESTS=1`.
1212
- Use `./test.sh --full` on Linux when root or passwordless `sudo` is available to run the stable native and system iptables tests.
13+
- Use `./test.sh --full --iptables-backend legacy|nft|current` to choose the iptables backend explicitly. Full mode defaults to `legacy`.
1314
- Set `RUN_UNSTABLE_SYSTEM_TESTS=1` with `./test.sh --full` to include tests marked `NotWorkingOnTravis`, including the conntrack coverage that can crash on some containerized hosts.
1415

1516
Both scripts will bootstrap a usable .NET SDK if `dotnet` is missing. By default they infer the needed SDK channel from the highest `TargetFramework` declared in the repo's `.csproj` files. On Linux they also build `libipthelper` and install missing native build dependencies through a supported package manager when needed.
@@ -19,7 +20,8 @@ Both scripts will bootstrap a usable .NET SDK if `dotnet` is missing. By default
1920
- `ipthelper/` builds `libipthelper.so`, which is required for `IPTablesLibAdapter`, `IptcInterface`, and the conntrack/native tests.
2021
- The helper links against the system iptables development libraries, `libnl3`, and `libpcap`.
2122
- The scripts first try a normal helper build and then retry with `-DOLD_IPTABLES` if the local iptables headers are older.
22-
- `test.sh --full` prefers the `iptables-legacy` / `ip6tables-legacy` alternatives when they are available because that matches the native test expectations more closely.
23+
- `test.sh --full` defaults to the `iptables-legacy` / `ip6tables-legacy` alternatives because that matches the native test expectations more closely.
24+
- Pass `--iptables-backend current` if you need to keep the host's existing backend, or `--iptables-backend nft` to exercise the nft variants explicitly.
2325

2426
## Testing Guidance
2527

IPTables.Net.Tests/IptablesLibraryTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void TestDestroy()
122122
{
123123
if (Environment.GetEnvironmentVariable("SKIP_SYSTEM_TESTS") == "1")
124124
{
125-
Assert.Ignore();
125+
return;
126126
}
127127

128128
var binary = GetBinary();

test.sh

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ TEST_MODE="${TEST_MODE:-auto}"
1010
DOTNET_TEST_ARGS=()
1111
ORIGINAL_IPTABLES_TARGET=""
1212
ORIGINAL_IP6TABLES_TARGET=""
13+
IPTABLES_BACKEND="${IPTABLES_BACKEND:-legacy}"
1314
RUN_UNSTABLE_SYSTEM_TESTS="${RUN_UNSTABLE_SYSTEM_TESTS:-0}"
1415

1516
has_explicit_test_filter() {
@@ -23,6 +24,32 @@ has_explicit_test_filter() {
2324
return 1
2425
}
2526

27+
normalize_iptables_backend() {
28+
local value="${1:-legacy}"
29+
30+
case "${value,,}" in
31+
legacy|nft|current)
32+
printf '%s\n' "${value,,}"
33+
;;
34+
*)
35+
die "Unsupported iptables backend '${value}'. Expected one of: legacy, nft, current."
36+
;;
37+
esac
38+
}
39+
40+
resolve_command_path() {
41+
local command_path
42+
43+
command_path="$(command -v "$1" 2>/dev/null)" || return 1
44+
45+
if command_exists readlink; then
46+
readlink -f "$command_path" 2>/dev/null || printf '%s\n' "$command_path"
47+
return
48+
fi
49+
50+
printf '%s\n' "$command_path"
51+
}
52+
2653
restore_iptables_backend() {
2754
if [[ -n "$ORIGINAL_IPTABLES_TARGET" ]]; then
2855
run_as_root update-alternatives --set iptables "$ORIGINAL_IPTABLES_TARGET" >/dev/null 2>&1 || true
@@ -33,39 +60,54 @@ restore_iptables_backend() {
3360
fi
3461
}
3562

36-
switch_to_legacy_backend_if_available() {
37-
if ! command_exists update-alternatives; then
63+
switch_to_selected_backend() {
64+
local backend="$1"
65+
local current_v4
66+
local current_v6
67+
local desired_v4
68+
local desired_v6
69+
local resolved_current_v4
70+
local resolved_current_v6
71+
local resolved_desired_v4
72+
local resolved_desired_v6
73+
74+
if [[ "$backend" == "current" ]]; then
75+
info "Using the current iptables backend for full-system tests"
3876
return
3977
fi
4078

41-
if ! command_exists iptables-legacy || ! command_exists ip6tables-legacy; then
79+
desired_v4="$(command -v "iptables-${backend}" 2>/dev/null)" || die "The iptables-${backend} binary is required for --iptables-backend ${backend}."
80+
desired_v6="$(command -v "ip6tables-${backend}" 2>/dev/null)" || die "The ip6tables-${backend} binary is required for --iptables-backend ${backend}."
81+
resolved_current_v4="$(resolve_command_path iptables)" || die "The iptables binary is required for full system tests."
82+
resolved_current_v6="$(resolve_command_path ip6tables)" || die "The ip6tables binary is required for full system tests."
83+
resolved_desired_v4="$(resolve_command_path "iptables-${backend}")"
84+
resolved_desired_v6="$(resolve_command_path "ip6tables-${backend}")"
85+
86+
if [[ "$resolved_current_v4" == "$resolved_desired_v4" && "$resolved_current_v6" == "$resolved_desired_v6" ]]; then
87+
info "Using iptables-${backend} for full-system tests"
4288
return
4389
fi
4490

45-
local current_v4
46-
local current_v6
47-
local legacy_v4
48-
local legacy_v6
91+
if ! command_exists update-alternatives; then
92+
die "Cannot switch to the ${backend} backend because update-alternatives is unavailable. Use --iptables-backend current to keep the existing backend."
93+
fi
4994

5095
current_v4="$(update-alternatives --query iptables 2>/dev/null | awk '/^Value: / { print $2 }')"
5196
current_v6="$(update-alternatives --query ip6tables 2>/dev/null | awk '/^Value: / { print $2 }')"
52-
legacy_v4="$(command -v iptables-legacy)"
53-
legacy_v6="$(command -v ip6tables-legacy)"
54-
5597
if [[ -z "$current_v4" || -z "$current_v6" ]]; then
56-
return
98+
die "Unable to determine the current iptables alternatives."
5799
fi
58100

59-
if [[ "$current_v4" != "$legacy_v4" ]]; then
101+
if [[ "$resolved_current_v4" != "$resolved_desired_v4" ]]; then
60102
ORIGINAL_IPTABLES_TARGET="$current_v4"
61-
info "Switching iptables to the legacy backend for native library tests"
62-
run_as_root update-alternatives --set iptables "$legacy_v4"
103+
info "Switching iptables to the ${backend} backend for full-system tests"
104+
run_as_root update-alternatives --set iptables "$desired_v4"
63105
fi
64106

65-
if [[ "$current_v6" != "$legacy_v6" ]]; then
107+
if [[ "$resolved_current_v6" != "$resolved_desired_v6" ]]; then
66108
ORIGINAL_IP6TABLES_TARGET="$current_v6"
67-
info "Switching ip6tables to the legacy backend for native library tests"
68-
run_as_root update-alternatives --set ip6tables "$legacy_v6"
109+
info "Switching ip6tables to the ${backend} backend for full-system tests"
110+
run_as_root update-alternatives --set ip6tables "$desired_v6"
69111
fi
70112
}
71113

@@ -95,8 +137,19 @@ cleanup_test_chains_for_binary() {
95137
}
96138

97139
cleanup_test_chains() {
98-
cleanup_test_chains_for_binary iptables
99-
cleanup_test_chains_for_binary ip6tables
140+
local binary
141+
local -a binaries=(
142+
iptables
143+
iptables-legacy
144+
iptables-nft
145+
ip6tables
146+
ip6tables-legacy
147+
ip6tables-nft
148+
)
149+
150+
for binary in "${binaries[@]}"; do
151+
cleanup_test_chains_for_binary "$binary"
152+
done
100153
}
101154

102155
run_full_tests() {
@@ -107,7 +160,7 @@ run_full_tests() {
107160
trap_command="$(printf 'rm -rf -- %q; cleanup_test_chains; restore_iptables_backend' "$results_dir")"
108161
trap "$trap_command" EXIT
109162

110-
switch_to_legacy_backend_if_available
163+
switch_to_selected_backend "$IPTABLES_BACKEND"
111164
load_kernel_modules
112165

113166
command_exists iptables || die "The iptables binary is required for full system tests."
@@ -157,19 +210,33 @@ while (($# > 0)); do
157210
[[ $# -gt 0 ]] || die "--configuration requires a value"
158211
CONFIGURATION="$(normalize_configuration "$1")"
159212
;;
213+
--iptables-backend)
214+
shift
215+
[[ $# -gt 0 ]] || die "--iptables-backend requires a value"
216+
IPTABLES_BACKEND="$(normalize_iptables_backend "$1")"
217+
;;
218+
--iptables-backend=*)
219+
IPTABLES_BACKEND="$(normalize_iptables_backend "${1#*=}")"
220+
;;
160221
--help|-h)
161222
cat <<'EOF'
162-
Usage: ./test.sh [--fast|--full] [--configuration <Debug|Release>] [dotnet test arguments...]
223+
Usage: ./test.sh [--fast|--full] [--iptables-backend <legacy|nft|current>] [--configuration <Debug|Release>] [dotnet test arguments...]
163224
164225
Modes:
165226
--fast Skip privileged/system iptables tests by setting SKIP_SYSTEM_TESTS=1.
166227
--full Run the full NUnit suite, including native helper and system iptables tests.
167228
229+
Backend selection:
230+
--iptables-backend legacy Use iptables-legacy/ip6tables-legacy for full-system tests. This is the default.
231+
--iptables-backend nft Use iptables-nft/ip6tables-nft for full-system tests.
232+
--iptables-backend current Keep the host's current iptables/ip6tables backend.
233+
168234
Default behavior:
169235
TEST_MODE=auto chooses --full on Linux when passwordless sudo/root is available,
170236
otherwise it falls back to --fast.
171237
172238
Environment:
239+
IPTABLES_BACKEND=legacy|nft|current sets the default full-test backend.
173240
RUN_UNSTABLE_SYSTEM_TESTS=1 includes tests marked NotWorkingOnTravis, such as
174241
the conntrack coverage that can crash on some containerized hosts.
175242
EOF
@@ -182,6 +249,8 @@ EOF
182249
shift
183250
done
184251

252+
IPTABLES_BACKEND="$(normalize_iptables_backend "$IPTABLES_BACKEND")"
253+
185254
if [[ "$TEST_MODE" == "auto" ]]; then
186255
if is_linux && can_run_privileged; then
187256
TEST_MODE="full"

0 commit comments

Comments
 (0)