Skip to content

Commit

Permalink
Add tests for AllowDevs in trafpol
Browse files Browse the repository at this point in the history
Signed-off-by: hwipl <33433250+hwipl@users.noreply.github.com>
  • Loading branch information
hwipl committed Sep 21, 2022
1 parent 5581c5a commit 0256cc2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
68 changes: 68 additions & 0 deletions internal/trafpol/allowdevs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package trafpol

import (
"reflect"
"testing"
)

// TestAllowDevsAdd tests Add of AllowDevs
func TestAllowDevsAdd(t *testing.T) {
a := NewAllowDevs()

got := []string{}
runNft = func(s string) {
got = append(got, s)
}

// test adding
want := []string{
"add element inet oc-daemon-filter allowdevs { eth3 }",
}
a.Add("eth3")
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}

// test adding again
// should not change anything
a.Add("eth3")
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}

// TestAllowDevsRemove tests Remove of AllowDevs
func TestAllowDevsRemove(t *testing.T) {
a := NewAllowDevs()

got := []string{}
runNft = func(s string) {
got = append(got, s)
}

// test removing device
a.Add("eth3")
want := []string{
"delete element inet oc-daemon-filter allowdevs { eth3 }",
}
got = []string{}
a.Remove("eth3")
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}

// test removing again (not existing device)
// should not change anything
a.Remove("eth3")
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}

// TestNewAllowDevs tests NewAllowDevs
func TestNewAllowDevs(t *testing.T) {
a := NewAllowDevs()
if a.m == nil {
t.Errorf("got nil, want != nil")
}
}
2 changes: 1 addition & 1 deletion internal/trafpol/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// runNft runs nft and passes s to it via stdin
func runNft(s string) {
var runNft = func(s string) {
cmd := "nft -f -"
c := exec.Command("bash", "-c", cmd)
c.Stdin = bytes.NewBufferString(s)
Expand Down

0 comments on commit 0256cc2

Please sign in to comment.