Skip to content

Commit

Permalink
iptables: add unit test for iptables.Iptables
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiamoe authored and Roberto Santalla committed Nov 10, 2023
1 parent 2e42b4c commit ded79f8
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions pkg/iptables/iptables_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,88 @@
package iptables_test

import (
"errors"
"testing"

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

func Test_Iptables(t *testing.T) {
t.Parallel()

anError := errors.New("an error occurred")

for _, tc := range []struct {
name string
testFunc func(iptables.Iptables) error
execError error
expectedCommands []string
expectedError error
}{
{
name: "Adds rule",
testFunc: func(i iptables.Iptables) error {
return i.Add(iptables.Rule{
Table: "some",
Chain: "ECHO",
Args: "foo -t bar -w xx",
})
},
expectedCommands: []string{
"iptables -t some -A ECHO foo -t bar -w xx",
},
},
{
name: "Removes rule",
testFunc: func(i iptables.Iptables) error {
return i.Remove(iptables.Rule{
Table: "some",
Chain: "ECHO",
Args: "foo -t bar -w xx",
})
},
expectedCommands: []string{
"iptables -t some -D ECHO foo -t bar -w xx",
},
},
{
name: "Propagates error",
testFunc: func(i iptables.Iptables) error {
return i.Remove(iptables.Rule{
Table: "some",
Chain: "ECHO",
Args: "foo -t bar -w xx",
})
},
execError: anError,
expectedCommands: []string{
"iptables -t some -D ECHO foo -t bar -w xx",
},
expectedError: anError,
},
} {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

fakeExec := runtime.NewFakeExecutor(nil, tc.execError)
ipt := iptables.New(fakeExec)
err := tc.testFunc(ipt)
if !errors.Is(err, tc.expectedError) {
t.Fatalf("Expected error to be %v, got %v", tc.expectedError, err)
}

commands := fakeExec.CmdHistory()
if diff := cmp.Diff(commands, tc.expectedCommands); diff != "" {
t.Fatalf("Ran commands do not match expected:\n%s", diff)
}
})
}
}

func Test_RulesetAddsRemovesRules(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit ded79f8

Please sign in to comment.