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: refactor helper, add RuleSet #373

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
iptables: put tests in the same package as the code
nadiamoe committed Nov 10, 2023
commit 43dea2efb37ecbbe6ee18846dadc8f87e080ef05
27 changes: 13 additions & 14 deletions pkg/iptables/iptables_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package iptables_test
package iptables

import (
"errors"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/grafana/xk6-disruptor/pkg/iptables"
"github.com/grafana/xk6-disruptor/pkg/runtime"
)

@@ -16,15 +15,15 @@ func Test_Iptables(t *testing.T) {

for _, tc := range []struct {
name string
testFunc func(iptables.Iptables) error
testFunc func(Iptables) error
execError error
expectedCommands []string
expectedError error
}{
{
name: "Adds rule",
testFunc: func(i iptables.Iptables) error {
return i.Add(iptables.Rule{
testFunc: func(i Iptables) error {
return i.Add(Rule{
Table: "some",
Chain: "ECHO",
Args: "foo -t bar -w xx",
@@ -36,8 +35,8 @@ func Test_Iptables(t *testing.T) {
},
{
name: "Removes rule",
testFunc: func(i iptables.Iptables) error {
return i.Remove(iptables.Rule{
testFunc: func(i Iptables) error {
return i.Remove(Rule{
Table: "some",
Chain: "ECHO",
Args: "foo -t bar -w xx",
@@ -49,8 +48,8 @@ func Test_Iptables(t *testing.T) {
},
{
name: "Propagates error",
testFunc: func(i iptables.Iptables) error {
return i.Remove(iptables.Rule{
testFunc: func(i Iptables) error {
return i.Remove(Rule{
Table: "some",
Chain: "ECHO",
Args: "foo -t bar -w xx",
@@ -69,7 +68,7 @@ func Test_Iptables(t *testing.T) {
t.Parallel()

fakeExec := runtime.NewFakeExecutor(nil, tc.execError)
ipt := iptables.New(fakeExec)
ipt := New(fakeExec)
err := tc.testFunc(ipt)
if !errors.Is(err, tc.expectedError) {
t.Fatalf("Expected error to be %v, got %v", tc.expectedError, err)
@@ -87,17 +86,17 @@ func Test_RulesetAddsRemovesRules(t *testing.T) {
t.Parallel()

exec := runtime.NewFakeExecutor(nil, nil)
ruleset := iptables.NewRuleSet(iptables.New(exec))
ruleset := NewRuleSet(New(exec))

// Add two rules
err := ruleset.Add(iptables.Rule{
err := ruleset.Add(Rule{
Table: "table1", Chain: "CHAIN1", Args: "--foo foo --bar bar",
})
if err != nil {
t.Fatalf("error adding rule: %v", err)
}

err = ruleset.Add(iptables.Rule{
err = ruleset.Add(Rule{
Table: "table2", Chain: "CHAIN2", Args: "--boo boo --baz baz",
})
if err != nil {
@@ -135,7 +134,7 @@ func Test_RulesetAddsRemovesRules(t *testing.T) {
exec.Reset()

// After removing the rules, add a new one.
err = ruleset.Add(iptables.Rule{
err = ruleset.Add(Rule{
Table: "table3", Chain: "CHAIN3", Args: "--zoo zoo --zap zap",
})
if err != nil {