mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 06:25:44 +03:00
143616ca6e
Merge in DNS/adguard-home from 4299-querylog-stats-api to master Updates #1717. Updates #4299. Squashed commit of the following: commit 5b706b7997a536bc4fd2c532fb89ca5ab3536848 Merge: 48b62b0f306c1983
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 22 13:53:09 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 48b62b0f1882f1ad120c6cdd90cd7dd8cb8a7738 Author: Vladislav Abdulmyanov <v.abdulmyanov@adguard.com> Date: Wed Mar 22 12:25:04 2023 +0200 client: fix styles, add titles and descrs commit 97e31cff70d05b51bd0e5ea2d20e8e7a251a7e41 Author: Vladislav Abdulmyanov <v.abdulmyanov@adguard.com> Date: Tue Mar 21 18:38:12 2023 +0200 client: add ignored domains for querylog commit 24d75c4376382205ae6b8f731b1cd23d517772c9 Author: Vladislav Abdulmyanov <v.abdulmyanov@adguard.com> Date: Tue Mar 21 18:21:13 2023 +0200 client: add ignore domains for stats commit eefc3891d01f90af79fdac9ba8eea06d4d54a0bc Merge: 978675ea1daabb97
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 21 10:53:35 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 978675ea2c07bf248b4c8f26ebdf78cf59a12ef5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 21 10:53:11 2023 +0300 openapi: fix chlog commit 2ed33007aade115d38b0ca582206cc10678b084c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 17:49:07 2023 +0300 home: fix tests commit 6af11520c164553ee9fce8f214ea169672188d7e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 17:40:16 2023 +0300 home: fix typo commit 56acdfde5b1ee8d16b232c1293b91affbe319ad1 Merge: 319da34d48431f8b
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 17:32:58 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 319da34de41ec84310b23bba2ad79c8a3a4c14ff Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 3 17:34:38 2023 +0300 querylog: fix docs commit d5a8f24d5b336e7bdbbca18069f6ede8c96bcc2c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 3 11:42:00 2023 +0300 stats: fix docs commit e0cbfc1c4078180a05835ce7587e9f45484adc81 Merge: 4743c810012e5beb
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 18:45:17 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 4743c81038052b9e0ca29ae5f1565021d36ca1ef Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 18:14:16 2023 +0300 all: imp code; fix time conversion commit 34310cffd7e331d098c535590245387051674fa8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 12:34:11 2023 +0300 chlog: restore order commit cadd864a66655242948f1cb16e6d4945c0235d7e Merge: 2f3e25bebb226434
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 12:26:06 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 2f3e25bee56d2c6ddcf4aa2fc6a1dc51ed9b06e1 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 12:25:14 2023 +0300 all: fix fmt commit d54022baa6c8a3d0d3c308a9b6b1a6a9dc6ac7b6 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 28 16:16:40 2023 +0300 all: imp code; fix chlog commit df22de91f59a51194c55e7bcbe5bc3fcc60cb8e3 Merge: e1ea4797a772212d
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Feb 27 17:24:09 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit e1ea4797af974c36f06683ffc6eaaae917921a43 Merge: d7db0a5abb80a7c2
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Feb 27 17:23:20 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit d7db0a5af1e1f49f6174c1c42e6d9306f2381d16 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Feb 27 17:12:20 2023 +0300 all: imp docs ... and 15 more commits
199 lines
4.8 KiB
Go
199 lines
4.8 KiB
Go
package stats_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testutil.DiscardLogOutput(m)
|
|
}
|
|
|
|
// constUnitID is the UnitIDGenFunc which always return 0.
|
|
func constUnitID() (id uint32) { return 0 }
|
|
|
|
func assertSuccessAndUnmarshal(t *testing.T, to any, handler http.Handler, req *http.Request) {
|
|
t.Helper()
|
|
|
|
require.NotNil(t, handler)
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
require.Equal(t, http.StatusOK, rw.Code)
|
|
|
|
data := rw.Body.Bytes()
|
|
if to == nil {
|
|
assert.Empty(t, data)
|
|
|
|
return
|
|
}
|
|
|
|
err := json.Unmarshal(data, to)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestStats(t *testing.T) {
|
|
cliIP := netutil.IPv4Localhost()
|
|
cliIPStr := cliIP.String()
|
|
|
|
handlers := map[string]http.Handler{}
|
|
conf := stats.Config{
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
Limit: timeutil.Day,
|
|
Enabled: true,
|
|
UnitID: constUnitID,
|
|
HTTPRegister: func(_, url string, handler http.HandlerFunc) {
|
|
handlers[url] = handler
|
|
},
|
|
}
|
|
|
|
s, err := stats.New(conf)
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
t.Run("data", func(t *testing.T) {
|
|
const reqDomain = "domain"
|
|
|
|
entries := []stats.Entry{{
|
|
Domain: reqDomain,
|
|
Client: cliIPStr,
|
|
Result: stats.RFiltered,
|
|
Time: 123456,
|
|
}, {
|
|
Domain: reqDomain,
|
|
Client: cliIPStr,
|
|
Result: stats.RNotFiltered,
|
|
Time: 123456,
|
|
}}
|
|
|
|
wantData := &stats.StatsResp{
|
|
TimeUnits: "hours",
|
|
TopQueried: []map[string]uint64{0: {reqDomain: 1}},
|
|
TopClients: []map[string]uint64{0: {cliIPStr: 2}},
|
|
TopBlocked: []map[string]uint64{0: {reqDomain: 1}},
|
|
DNSQueries: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
|
|
},
|
|
BlockedFiltering: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
|
},
|
|
ReplacedSafebrowsing: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
},
|
|
ReplacedParental: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
},
|
|
NumDNSQueries: 2,
|
|
NumBlockedFiltering: 1,
|
|
NumReplacedSafebrowsing: 0,
|
|
NumReplacedSafesearch: 0,
|
|
NumReplacedParental: 0,
|
|
AvgProcessingTime: 0.123456,
|
|
}
|
|
|
|
for _, e := range entries {
|
|
s.Update(e)
|
|
}
|
|
|
|
data := &stats.StatsResp{}
|
|
req := httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
|
|
assert.Equal(t, wantData, data)
|
|
})
|
|
|
|
t.Run("tops", func(t *testing.T) {
|
|
topClients := s.TopClientsIP(2)
|
|
require.NotEmpty(t, topClients)
|
|
|
|
assert.Equal(t, cliIP, topClients[0])
|
|
})
|
|
|
|
t.Run("reset", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodPost, "/control/stats_reset", nil)
|
|
assertSuccessAndUnmarshal(t, nil, handlers["/control/stats_reset"], req)
|
|
|
|
_24zeroes := [24]uint64{}
|
|
emptyData := &stats.StatsResp{
|
|
TimeUnits: "hours",
|
|
TopQueried: []map[string]uint64{},
|
|
TopClients: []map[string]uint64{},
|
|
TopBlocked: []map[string]uint64{},
|
|
DNSQueries: _24zeroes[:],
|
|
BlockedFiltering: _24zeroes[:],
|
|
ReplacedSafebrowsing: _24zeroes[:],
|
|
ReplacedParental: _24zeroes[:],
|
|
}
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
data := &stats.StatsResp{}
|
|
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
assert.Equal(t, emptyData, data)
|
|
})
|
|
}
|
|
|
|
func TestLargeNumbers(t *testing.T) {
|
|
var curHour uint32 = 1
|
|
handlers := map[string]http.Handler{}
|
|
|
|
conf := stats.Config{
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
Limit: timeutil.Day,
|
|
Enabled: true,
|
|
UnitID: func() (id uint32) { return atomic.LoadUint32(&curHour) },
|
|
HTTPRegister: func(_, url string, handler http.HandlerFunc) { handlers[url] = handler },
|
|
}
|
|
|
|
s, err := stats.New(conf)
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
const (
|
|
hoursNum = 12
|
|
cliNumPerHour = 1000
|
|
)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
|
|
for h := 0; h < hoursNum; h++ {
|
|
atomic.AddUint32(&curHour, 1)
|
|
|
|
for i := 0; i < cliNumPerHour; i++ {
|
|
ip := net.IP{127, 0, byte((i & 0xff00) >> 8), byte(i & 0xff)}
|
|
e := stats.Entry{
|
|
Domain: fmt.Sprintf("domain%d.hour%d", i, h),
|
|
Client: ip.String(),
|
|
Result: stats.RNotFiltered,
|
|
Time: 123456,
|
|
}
|
|
s.Update(e)
|
|
}
|
|
}
|
|
|
|
data := &stats.StatsResp{}
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
assert.Equal(t, hoursNum*cliNumPerHour, int(data.NumDNSQueries))
|
|
}
|