Pull request: 2846 cover aghnet vol.3

Merge in DNS/adguard-home from 2846-cover-aghnet-vol.3 to master

Updates #2846.

Squashed commit of the following:

commit cb22987c43c17bbc8d098e65639cc84e2284bc7b
Merge: cf995e9d f31ffcc5
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Mar 30 15:01:10 2022 +0300

    Merge branch 'master' into 2846-cover-aghnet-vol.3

commit cf995e9dce635f16e10406a61e2ab12f06407f1f
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Mar 28 18:29:50 2022 +0300

    aghnet: imp tests

commit bc225fe8800633b29216840bc7d5b82d7c2d2bfb
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Mar 28 18:03:06 2022 +0300

    aghnet: imp tests

commit a82eb6045495b94a2e81ced9a3ef5bfe65788e56
Merge: f8081249 0d562a7b
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Mar 28 17:39:13 2022 +0300

    Merge branch 'master' into 2846-cover-aghnet-vol.3

commit f80812490c49f69655d409c6f015b069affa2f19
Merge: edccaa79 3603b1fc
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Mar 28 17:29:20 2022 +0300

    Merge branch 'master' into 2846-cover-aghnet-vol.3

commit edccaa79fca061ffeea1985c293eed123b16a09c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Mar 28 13:53:40 2022 +0300

    aghnet: imp tests

commit 7c5028c92f0a6680516bda67c73e794182c9b825
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Fri Mar 25 18:01:28 2022 +0300

    aghnet: imp code & docs

commit 7897c6b13e9be340ae8a71947a8a0bab82c682eb
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Fri Mar 25 17:11:46 2022 +0300

    aghnet: imp coverage

commit 1eef110af3bf721a0275c695bf27c31815abff04
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Mar 23 21:10:29 2022 +0300

    all: return byte slice
This commit is contained in:
Eugene Burkov
2022-03-30 15:11:57 +03:00
parent f31ffcc5d1
commit 5e71f5df6a
21 changed files with 789 additions and 391 deletions

View File

@@ -1,12 +1,9 @@
package aghnet
import (
"io"
"net"
"strings"
"sync"
"testing"
"testing/iotest"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/testutil"
@@ -16,9 +13,7 @@ import (
func TestNewARPDB(t *testing.T) {
var a ARPDB
require.NotPanics(t, func() {
a = NewARPDB()
})
require.NotPanics(t, func() { a = NewARPDB() })
assert.NotNil(t, a)
}
@@ -135,11 +130,11 @@ func TestARPDBS(t *testing.T) {
assert.Equal(t, 1, succRefrCount)
assert.NotEmpty(t, a.Neighbors())
// Only the last succeeded ARPDB should be used.
// Unstable ARPDB should refresh successfully again.
err = a.Refresh()
require.NoError(t, err)
assert.Equal(t, 2, succRefrCount)
assert.Equal(t, 1, succRefrCount)
assert.NotEmpty(t, a.Neighbors())
})
@@ -153,6 +148,7 @@ func TestARPDBS(t *testing.T) {
func TestCmdARPDB_arpa(t *testing.T) {
a := &cmdARPDB{
cmd: "cmd",
parse: parseArpA,
ns: &neighs{
mu: &sync.RWMutex{},
@@ -161,7 +157,8 @@ func TestCmdARPDB_arpa(t *testing.T) {
}
t.Run("arp_a", func(t *testing.T) {
a.runcmd = func() (r io.Reader, err error) { return strings.NewReader(arpAOutput), nil }
sh := theOnlyCmd("cmd", 0, arpAOutput, nil)
substShell(t, sh.RunCmd)
err := a.Refresh()
require.NoError(t, err)
@@ -170,32 +167,20 @@ func TestCmdARPDB_arpa(t *testing.T) {
})
t.Run("runcmd_error", func(t *testing.T) {
a.runcmd = func() (r io.Reader, err error) { return nil, errors.Error("can't run") }
sh := theOnlyCmd("cmd", 0, "", errors.Error("can't run"))
substShell(t, sh.RunCmd)
err := a.Refresh()
testutil.AssertErrorMsg(t, "cmd arpdb: running command: can't run", err)
})
}
func TestCmdARPDB_errors(t *testing.T) {
const errRead errors.Error = "can't read"
t.Run("bad_code", func(t *testing.T) {
sh := theOnlyCmd("cmd", 1, "", nil)
substShell(t, sh.RunCmd)
badReaderRunCmd := runCmdFunc(func() (r io.Reader, err error) {
return iotest.ErrReader(errRead), nil
err := a.Refresh()
testutil.AssertErrorMsg(t, "cmd arpdb: running command: unexpected exit code 1", err)
})
a := &cmdARPDB{
runcmd: badReaderRunCmd,
parse: parseArpA,
ns: &neighs{
mu: &sync.RWMutex{},
ns: make([]Neighbor, 0),
},
}
const wantErrMsg string = "cmd arpdb: scanning the output: " + string(errRead)
testutil.AssertErrorMsg(t, wantErrMsg, a.Refresh())
}
func TestEmptyARPDB(t *testing.T) {