Skip to content

Commit 562b604

Browse files
committed
adds missing case
1 parent 678eb1e commit 562b604

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

pkg/iptables/iptables_manager_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,44 @@ func TestVerifyRules(t *testing.T) {
685685
}
686686
}
687687

688+
func TestVerifyRules_MissingDefaultActionInDryRun(t *testing.T) {
689+
mockIpt := newMockIPTables()
690+
691+
// Make sure the chain exists
692+
chainName := "TEST_CHAIN"
693+
mockIpt.chains[chainName] = true
694+
695+
// Add some rules that are missing the final default lines
696+
// (The code expects: -A TEST_CHAIN -j LOG --log-prefix "DROP_<logIdentifier> "
697+
// plus a trailing -A TEST_CHAIN -j ACCEPT)
698+
// We'll omit them so the code path returns "default rule not found"
699+
mockIpt.rules[chainName] = []string{
700+
// Possibly also no user rules at all, or partial user rules, doesn't matter
701+
`-A TEST_CHAIN -d 192.168.1.1 -p tcp --dport 80 -j LOG --log-prefix "DROP_TEST123 "`,
702+
// or we can just have an empty slice if we want
703+
}
704+
705+
manager := &IPTablesManager{
706+
ipt: mockIpt,
707+
mainChainName: "CNI-OUTBOUND",
708+
defaultAction: "DROP",
709+
logIdentifier: "TEST123",
710+
dryRun: true, // CRUCIAL
711+
}
712+
713+
// No user OutboundRules in this example
714+
userRules := []OutboundRule{}
715+
716+
// Now call VerifyRules, expecting it to fail with "default rule not found"
717+
err := manager.VerifyRules(chainName, userRules)
718+
if err == nil {
719+
t.Fatalf("Expected an error about missing default rule but got nil")
720+
}
721+
if !strings.Contains(err.Error(), "default rule not found") {
722+
t.Errorf("Unexpected error: %v", err)
723+
}
724+
}
725+
688726
func TestVerifyRulesListError(t *testing.T) {
689727
mockIpt := newMockIPTables()
690728
manager := &IPTablesManager{

0 commit comments

Comments
 (0)