Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iptables mock #2721

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pkg/iptableswrapper/mocks/iptables_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mock_iptableswrapper
import (
"fmt"
"reflect"
"slices"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -44,7 +45,12 @@ func (ipt *MockIptables) Insert(table, chain string, pos int, rulespec ...string
if ipt.DataplaneState[table] == nil {
ipt.DataplaneState[table] = map[string][][]string{}
}
ipt.DataplaneState[table][chain] = append(ipt.DataplaneState[table][chain], rulespec)
if len(ipt.DataplaneState[table][chain]) == pos-1 {
ipt.DataplaneState[table][chain] = append(ipt.DataplaneState[table][chain], rulespec)
} else {
ipt.DataplaneState[table][chain] = append(ipt.DataplaneState[table][chain][:pos], ipt.DataplaneState[table][chain][pos-1:]...)
ipt.DataplaneState[table][chain][pos] = rulespec
}
return nil
}

Expand Down Expand Up @@ -91,6 +97,10 @@ func (ipt *MockIptables) List(table, chain string) ([]string, error) {
var chains []string
chainContents := ipt.DataplaneState[table][chain]
for _, ruleSpec := range chainContents {
if slices.Contains(ruleSpec, "-N") {
chains = append(chains, strings.Join(ruleSpec, " "))
continue
}
sanitizedRuleSpec := []string{"-A", chain}
for _, item := range ruleSpec {
if strings.Contains(item, " ") {
Expand All @@ -101,10 +111,15 @@ func (ipt *MockIptables) List(table, chain string) ([]string, error) {
chains = append(chains, strings.Join(sanitizedRuleSpec, " "))
}
return chains, nil

}

func (ipt *MockIptables) NewChain(table, chain string) error {
exists, _ := ipt.ChainExists(table, chain)
if exists {
return errors.New("Chain already exists")
}
// Creating a new chain adds a -N chain rule to iptables
ipt.Append(table, chain, "-N", chain)
jchen6585 marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

Expand All @@ -113,6 +128,12 @@ func (ipt *MockIptables) ClearChain(table, chain string) error {
}

func (ipt *MockIptables) DeleteChain(table, chain string) error {
// More than just the create chain rule
if len(ipt.DataplaneState[table][chain]) > 1 {
err := fmt.Sprintf("Chain %s is not empty", chain)
jdn5126 marked this conversation as resolved.
Show resolved Hide resolved
return errors.New(err)
}
delete(ipt.DataplaneState[table], chain)
return nil
}

Expand Down
Loading
Loading