Pull request: 3845 hosts fatality

Merge in DNS/adguard-home from 3845-hosts-fatality to master

Updates #3845.

Squashed commit of the following:

commit 1447efcc4066e0226feaebde01fcc632cb7b7432
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Nov 17 17:14:35 2021 +0300

    home: imp readability

commit e934499072e983e1111b6c976eb93e1d6017981b
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Nov 17 13:35:10 2021 +0300

    aghnet: imp more

commit ed9995ee52bd9ec3fa130f3f56989619184a6669
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Nov 17 13:05:56 2021 +0300

    all: imp docs, code

commit 7b0718a1a4a58a4fd5f1ba24c33792b0610c334f
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Nov 16 20:32:24 2021 +0300

    all: reduce hosts container fatality
This commit is contained in:
Eugene Burkov
2021-11-17 17:21:10 +03:00
parent 4a4b4715ca
commit 9bac4b3db2
6 changed files with 171 additions and 83 deletions

View File

@@ -24,14 +24,6 @@ const (
sp = " "
)
const closeCalled errors.Error = "close method called"
// fsWatcherOnCloseStub is a stub implementation of the Close method of
// aghos.FSWatcher.
func fsWatcherOnCloseStub() (err error) {
return closeCalled
}
func TestNewHostsContainer(t *testing.T) {
const dirname = "dir"
const filename = "file1"
@@ -43,30 +35,25 @@ func TestNewHostsContainer(t *testing.T) {
}
testCases := []struct {
name string
paths []string
wantErr error
wantPatterns []string
wantErr error
name string
paths []string
}{{
name: "one_file",
paths: []string{p},
wantErr: nil,
wantPatterns: []string{p},
wantErr: nil,
name: "one_file",
paths: []string{p},
}, {
name: "no_files",
paths: []string{},
wantErr: errNoPaths,
wantPatterns: nil,
wantErr: ErrNoHostsPaths,
name: "no_files",
paths: []string{},
}, {
name: "non-existent_file",
paths: []string{path.Join(dirname, filename+"2")},
wantErr: fs.ErrNotExist,
wantPatterns: nil,
wantErr: ErrNoHostsPaths,
name: "non-existent_file",
paths: []string{path.Join(dirname, filename+"2")},
}, {
name: "whole_dir",
paths: []string{dirname},
wantErr: nil,
wantPatterns: []string{path.Join(dirname, "*")},
wantErr: nil,
name: "whole_dir",
paths: []string{dirname},
}}
for _, tc := range testCases {
@@ -88,7 +75,7 @@ func TestNewHostsContainer(t *testing.T) {
hc, err := NewHostsContainer(testFS, &aghtest.FSWatcher{
OnEvents: onEvents,
OnAdd: onAdd,
OnClose: fsWatcherOnCloseStub,
OnClose: func() (err error) { panic("not implemented") },
}, tc.paths...)
if tc.wantErr != nil {
require.ErrorIs(t, err, tc.wantErr)
@@ -99,13 +86,8 @@ func TestNewHostsContainer(t *testing.T) {
}
require.NoError(t, err)
t.Cleanup(func() {
require.ErrorIs(t, hc.Close(), closeCalled)
})
require.NotNil(t, hc)
assert.Equal(t, tc.wantPatterns, hc.patterns)
assert.NotNil(t, <-hc.Upd())
eventsCh <- struct{}{}
@@ -178,12 +160,11 @@ func TestHostsContainer_Refresh(t *testing.T) {
return nil
},
OnClose: fsWatcherOnCloseStub,
OnClose: func() (err error) { panic("not implemented") },
}
hc, err := NewHostsContainer(testFS, w, dirname)
require.NoError(t, err)
t.Cleanup(func() { require.ErrorIs(t, hc.Close(), closeCalled) })
checkRefresh := func(t *testing.T, wantHosts *stringutil.Set) {
upd, ok := <-hc.Upd()
@@ -257,10 +238,9 @@ func TestHostsContainer_MatchRequest(t *testing.T) {
return nil
},
OnClose: fsWatcherOnCloseStub,
OnClose: func() (err error) { panic("not implemented") },
}, filename)
require.NoError(t, err)
t.Cleanup(func() { require.ErrorIs(t, hc.Close(), closeCalled) })
testCase := []struct {
name string
@@ -398,7 +378,7 @@ func TestHostsContainer_PathsToPatterns(t *testing.T) {
paths: []string{fp1, path.Join(dir0, dir1)},
}, {
name: "non-existing",
wantErr: fs.ErrNotExist,
wantErr: nil,
want: nil,
paths: []string{path.Join(dir0, "file_3")},
}}
@@ -417,6 +397,19 @@ func TestHostsContainer_PathsToPatterns(t *testing.T) {
assert.Equal(t, tc.want, patterns)
})
}
t.Run("bad_file", func(t *testing.T) {
const errStat errors.Error = "bad file"
badFS := &aghtest.StatFS{
OnStat: func(name string) (fs.FileInfo, error) {
return nil, errStat
},
}
_, err := pathsToPatterns(badFS, []string{""})
assert.ErrorIs(t, err, errStat)
})
}
func TestUniqueRules_AddPair(t *testing.T) {