Skip to content

Commit

Permalink
aghnet: cover arpdb
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Mar 23, 2022
1 parent 5cba78a commit 0fb8d9e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions internal/aghnet/arpdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
"github.com/stretchr/testify/require"
)

func TestNewARPDB(t *testing.T) {
var a ARPDB
require.NotPanics(t, func() {
a = NewARPDB()
})

assert.NotNil(t, a)
}

// TestARPDB is the mock implementation of ARPDB to use in tests.
type TestARPDB struct {
OnRefresh func() (err error)
Expand Down Expand Up @@ -166,3 +175,52 @@ func TestCmdARPDB_arpa(t *testing.T) {
testutil.AssertErrorMsg(t, "cmd arpdb: running command: can't run", err)
})
}

type badReader struct{}

var _ io.Reader = badReader{}

func (badReader) Read(p []byte) (n int, err error) {
return 0, errors.Error("can't read")
}

func TestCmdARPDB_errors(t *testing.T) {
badReaderRunCmd := runCmdFunc(func() (r io.Reader, err error) {
return badReader{}, nil
})

a := &cmdARPDB{
runcmd: badReaderRunCmd,
parse: parseArpA,
ns: &neighs{
mu: &sync.RWMutex{},
ns: make([]Neighbor, 0),
},
}

const wantErrMsg = "cmd arpdb: scanning the output: can't read"

testutil.AssertErrorMsg(t, wantErrMsg, a.Refresh())
}

func TestEmptyARPDB(t *testing.T) {
a := EmptyARPDB{}

t.Run("refresh", func(t *testing.T) {
var err error
require.NotPanics(t, func() {
err = a.Refresh()
})

assert.NoError(t, err)
})

t.Run("neighbors", func(t *testing.T) {
var ns []Neighbor
require.NotPanics(t, func() {
ns = a.Neighbors()
})

assert.Empty(t, ns)
})
}

0 comments on commit 0fb8d9e

Please sign in to comment.