Skip to content

Commit 5a4b90a

Browse files
committed
skip shot line
1 parent a0a328b commit 5a4b90a

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

pkg/iptables/iptables_manager_test.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,34 +2126,28 @@ func TestCreateContainerChain_FailDryRunAcceptAppend(t *testing.T) {
21262126

21272127
func TestRemoveJumpRuleByTargetChain_SkipShortLines(t *testing.T) {
21282128
mockIpt := newMockIPTables()
2129+
2130+
// We'll have a few normal lines plus one short line
2131+
mockIpt.rules["CNI-OUTBOUND"] = []string{
2132+
"-A CNI-OUTBOUND -s 10.0.0.1 -j TARGET_CHAIN", // normal line
2133+
"-A", // short line => tokens < 2 => skip
2134+
}
2135+
21292136
manager := &IPTablesManager{
21302137
ipt: mockIpt,
21312138
mainChainName: "CNI-OUTBOUND",
21322139
defaultAction: "DROP",
21332140
}
21342141

2135-
// Some "normal" lines
2136-
mockIpt.rules["CNI-OUTBOUND"] = []string{
2137-
"-A CNI-OUTBOUND -s 10.0.0.1 -j SOME_CHAIN",
2138-
"-A CNI-OUTBOUND -s 10.0.0.2 -j ANOTHER_CHAIN",
2139-
}
2140-
// And a short line with fewer than 2 tokens
2141-
mockIpt.rules["CNI-OUTBOUND"] = append(mockIpt.rules["CNI-OUTBOUND"], "-A")
2142+
err := manager.RemoveJumpRuleByTargetChain("TARGET_CHAIN")
2143+
assert.NoError(t, err, "Expected no error removing jump rule")
21422144

2143-
// We remove jump rule by target chain => won't find it, but also won't fail due to short line
2144-
err := manager.RemoveJumpRuleByTargetChain("SOME_CHAIN")
2145-
assert.NoError(t, err, "We expect removal to succeed for SOME_CHAIN")
2146-
2147-
// Confirm it did remove the line referencing "SOME_CHAIN"
2145+
// The short line is skipped. The normal line is removed successfully.
21482146
rules, _ := mockIpt.List("filter", "CNI-OUTBOUND")
2149-
found := false
2150-
for _, r := range rules {
2151-
if strings.Contains(r, "SOME_CHAIN") {
2152-
found = true
2153-
break
2154-
}
2147+
if len(rules) != 1 {
2148+
t.Errorf("Expected 1 line left after removal, got %d: %v", len(rules), rules)
21552149
}
2156-
if found {
2157-
t.Errorf("Expected jump rule to be removed, but it's still found in: %v", rules)
2150+
if strings.Contains(rules[0], "TARGET_CHAIN") {
2151+
t.Errorf("Expected jump rule referencing TARGET_CHAIN to be removed, but it still exists: %v", rules[0])
21582152
}
21592153
}

0 commit comments

Comments
 (0)