Skip to content

Commit

Permalink
iptables: put tests in the same package as the code
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 ded79f8 commit 6a5f503
Showing 1 changed file with 13 additions and 14 deletions.
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"
)

Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6a5f503

Please sign in to comment.