Skip to content

Commit

Permalink
Pull request: 2639 use testify require vol.2
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 2639-testify-require-2 to master

Updates AdguardTeam#2639.

Squashed commit of the following:

commit 31cc29a
Author: Eugene Burkov <[email protected]>
Date:   Tue Feb 9 18:48:31 2021 +0300

    all: deal with t.Run

commit 484f477
Author: Eugene Burkov <[email protected]>
Date:   Tue Feb 9 17:44:02 2021 +0300

    all: fix readability, imp tests

commit 1231a82
Author: Eugene Burkov <[email protected]>
Date:   Tue Feb 9 16:06:29 2021 +0300

    all: imp tests
  • Loading branch information
EugeneOne1 authored and heyxkhoa committed Mar 17, 2023
1 parent caa2e33 commit c7514a8
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 274 deletions.
45 changes: 45 additions & 0 deletions internal/aghtest/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package aghtest

import (
"io/ioutil"
"os"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// PrepareTestDir returns the full path to temporary created directory and
// registers the appropriate cleanup for *t.
func PrepareTestDir(t *testing.T) (dir string) {
t.Helper()

wd, err := os.Getwd()
require.Nil(t, err)

dir, err = ioutil.TempDir(wd, "agh-test")
require.Nil(t, err)
require.NotEmpty(t, dir)

t.Cleanup(func() {
// TODO(e.burkov): Replace with t.TempDir methods after updating
// go version to 1.15.
start := time.Now()
for {
err := os.RemoveAll(dir)
if err == nil {
break
}

if runtime.GOOS != "windows" || time.Since(start) >= 500*time.Millisecond {
break
}
time.Sleep(5 * time.Millisecond)
}
assert.Nil(t, err)
})

return dir
}
43 changes: 4 additions & 39 deletions internal/querylog/qlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package querylog

import (
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"runtime"
"sort"
"testing"
"time"
Expand All @@ -24,38 +21,6 @@ func TestMain(m *testing.M) {
aghtest.DiscardLogOutput(m)
}

func prepareTestDir(t *testing.T) string {
t.Helper()

wd, err := os.Getwd()
require.Nil(t, err)

dir, err := ioutil.TempDir(wd, "agh-tests")
require.Nil(t, err)
require.NotEmpty(t, dir)

t.Cleanup(func() {
// TODO(e.burkov): Replace with t.TempDir methods after updating
// go version to 1.15.
start := time.Now()
for {
err := os.RemoveAll(dir)
if err == nil {
break
}

if runtime.GOOS != "windows" || time.Since(start) >= 500*time.Millisecond {
break
}
time.Sleep(5 * time.Millisecond)
}

assert.Nil(t, err)
})

return dir
}

// TestQueryLog tests adding and loading (with filtering) entries from disk and
// memory.
func TestQueryLog(t *testing.T) {
Expand All @@ -64,7 +29,7 @@ func TestQueryLog(t *testing.T) {
FileEnabled: true,
Interval: 1,
MemSize: 100,
BaseDir: prepareTestDir(t),
BaseDir: aghtest.PrepareTestDir(t),
})

// Add disk entries.
Expand Down Expand Up @@ -166,7 +131,7 @@ func TestQueryLogOffsetLimit(t *testing.T) {
Enabled: true,
Interval: 1,
MemSize: 100,
BaseDir: prepareTestDir(t),
BaseDir: aghtest.PrepareTestDir(t),
})

const (
Expand Down Expand Up @@ -240,7 +205,7 @@ func TestQueryLogMaxFileScanEntries(t *testing.T) {
FileEnabled: true,
Interval: 1,
MemSize: 100,
BaseDir: prepareTestDir(t),
BaseDir: aghtest.PrepareTestDir(t),
})

const entNum = 10
Expand Down Expand Up @@ -268,7 +233,7 @@ func TestQueryLogFileDisabled(t *testing.T) {
FileEnabled: false,
Interval: 1,
MemSize: 2,
BaseDir: prepareTestDir(t),
BaseDir: aghtest.PrepareTestDir(t),
})

addEntry(l, "example1.org", net.IPv4(1, 1, 1, 1), net.IPv4(2, 2, 2, 1))
Expand Down
17 changes: 12 additions & 5 deletions internal/querylog/qlogfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ import (
"testing"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// prepareTestFiles prepares several test query log files, each with the
// specified lines count.
func prepareTestFiles(t *testing.T, dir string, filesNum, linesNum int) []string {
func prepareTestFiles(t *testing.T, filesNum, linesNum int) []string {
t.Helper()

if filesNum == 0 {
return []string{}
}

const strV = "\"%s\""
const nl = "\n"
const format = `{"IP":` + strV + `,"T":` + strV + `,` +
Expand All @@ -31,6 +36,8 @@ func prepareTestFiles(t *testing.T, dir string, filesNum, linesNum int) []string
lineTime, _ := time.Parse(time.RFC3339Nano, "2020-02-18T22:36:35.920973+03:00")
lineIP := uint32(0)

dir := aghtest.PrepareTestDir(t)

files := make([]string, filesNum)
for j := range files {
f, err := ioutil.TempFile(dir, "*.txt")
Expand All @@ -56,18 +63,18 @@ func prepareTestFiles(t *testing.T, dir string, filesNum, linesNum int) []string

// prepareTestFile prepares a test query log file with the specified number of
// lines.
func prepareTestFile(t *testing.T, dir string, linesCount int) string {
func prepareTestFile(t *testing.T, linesCount int) string {
t.Helper()

return prepareTestFiles(t, dir, 1, linesCount)[0]
return prepareTestFiles(t, 1, linesCount)[0]
}

// newTestQLogFile creates new *QLogFile for tests and registers the required
// cleanup functions.
func newTestQLogFile(t *testing.T, linesNum int) (file *QLogFile) {
t.Helper()

testFile := prepareTestFile(t, prepareTestDir(t), linesNum)
testFile := prepareTestFile(t, linesNum)

// Create the new QLogFile instance.
file, err := NewQLogFile(testFile)
Expand Down Expand Up @@ -275,7 +282,7 @@ func TestQLogFile(t *testing.T) {
}

func NewTestQLogFileData(t *testing.T, data string) (file *QLogFile) {
f, err := ioutil.TempFile(prepareTestDir(t), "*.txt")
f, err := ioutil.TempFile(aghtest.PrepareTestDir(t), "*.txt")
require.Nil(t, err)
t.Cleanup(func() {
assert.Nil(t, f.Close())
Expand Down
2 changes: 1 addition & 1 deletion internal/querylog/qlogreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func newTestQLogReader(t *testing.T, filesNum, linesNum int) (reader *QLogReader) {
t.Helper()

testFiles := prepareTestFiles(t, prepareTestDir(t), filesNum, linesNum)
testFiles := prepareTestFiles(t, filesNum, linesNum)

// Create the new QLogReader instance.
reader, err := NewQLogReader(testFiles)
Expand Down
130 changes: 62 additions & 68 deletions internal/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
Expand All @@ -34,28 +35,30 @@ func TestStats(t *testing.T) {
Filename: "./stats.db",
LimitDays: 1,
}

s, err := createObject(conf)
require.Nil(t, err)
t.Cleanup(func() {
s.clear()
s.Close()
assert.Nil(t, os.Remove(conf.Filename))
})

s, _ := createObject(conf)

e := Entry{}

e.Domain = "domain"
e.Client = "127.0.0.1"
e.Result = RFiltered
e.Time = 123456
s.Update(e)

e.Domain = "domain"
e.Client = "127.0.0.1"
e.Result = RNotFiltered
e.Time = 123456
s.Update(e)
s.Update(Entry{
Domain: "domain",
Client: "127.0.0.1",
Result: RFiltered,
Time: 123456,
})
s.Update(Entry{
Domain: "domain",
Client: "127.0.0.1",
Result: RNotFiltered,
Time: 123456,
})

d, ok := s.getData()
assert.True(t, ok)
require.True(t, ok)

a := []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}
assert.True(t, UIntArrayEquals(d.DNSQueries, a))
Expand All @@ -70,12 +73,15 @@ func TestStats(t *testing.T) {
assert.True(t, UIntArrayEquals(d.ReplacedParental, a))

m := d.TopQueried
require.NotEmpty(t, m)
assert.EqualValues(t, 1, m[0]["domain"])

m = d.TopBlocked
require.NotEmpty(t, m)
assert.EqualValues(t, 1, m[0]["domain"])

m = d.TopClients
require.NotEmpty(t, m)
assert.EqualValues(t, 2, m[0]["127.0.0.1"])

assert.EqualValues(t, 2, d.NumDNSQueries)
Expand All @@ -86,81 +92,69 @@ func TestStats(t *testing.T) {
assert.EqualValues(t, 0.123456, d.AvgProcessingTime)

topClients := s.GetTopClientsIP(2)
require.NotEmpty(t, topClients)
assert.True(t, net.IP{127, 0, 0, 1}.Equal(topClients[0]))

s.clear()
s.Close()
}

func TestLargeNumbers(t *testing.T) {
var hour int32 = 1
var hour int32 = 0
newID := func() uint32 {
// use "atomic" to make Go race detector happy
// Use "atomic" to make go race detector happy.
return uint32(atomic.LoadInt32(&hour))
}

// log.SetLevel(log.DEBUG)
conf := Config{
Filename: "./stats.db",
LimitDays: 1,
UnitID: newID,
}
s, err := createObject(conf)
require.Nil(t, err)
t.Cleanup(func() {
s.Close()
assert.Nil(t, os.Remove(conf.Filename))
})

s, _ := createObject(conf)
e := Entry{}

n := 1000 // number of distinct clients and domains every hour
for h := 0; h != 12; h++ {
if h != 0 {
atomic.AddInt32(&hour, 1)
}
for i := 0; i != n; i++ {
e.Domain = fmt.Sprintf("domain%d", i)
ip := net.IP{127, 0, 0, 1}
ip[2] = byte((i & 0xff00) >> 8)
ip[3] = byte(i & 0xff)
e.Client = ip.String()
e.Result = RNotFiltered
e.Time = 123456
s.Update(e)
// Number of distinct clients and domains every hour.
const n = 1000

for h := 0; h < 12; h++ {
atomic.AddInt32(&hour, 1)
for i := 0; i < n; i++ {
s.Update(Entry{
Domain: fmt.Sprintf("domain%d", i),
Client: net.IP{
127,
0,
byte((i & 0xff00) >> 8),
byte(i & 0xff),
}.String(),
Result: RNotFiltered,
Time: 123456,
})
}
}

d, ok := s.getData()
assert.True(t, ok)
assert.EqualValues(t, int(hour)*n, d.NumDNSQueries)

s.Close()
require.True(t, ok)
assert.EqualValues(t, hour*n, d.NumDNSQueries)
}

// this code is a chunk copied from getData() that generates aggregate data per day
func aggregateDataPerDay(firstID uint32) int {
firstDayID := (firstID + 24 - 1) / 24 * 24 // align_ceil(24)
a := []uint64{}
var sum uint64
id := firstDayID
nextDayID := firstDayID + 24
for i := firstDayID - firstID; int(i) != 720; i++ {
sum++
if id == nextDayID {
a = append(a, sum)
sum = 0
nextDayID += 24
}
id++
}
if id <= nextDayID {
a = append(a, sum)
func TestStatsCollector(t *testing.T) {
ng := func(_ *unitDB) uint64 {
return 0
}
return len(a)
}
units := make([]*unitDB, 720)

func TestAggregateDataPerTimeUnit(t *testing.T) {
for i := 0; i != 25; i++ {
alen := aggregateDataPerDay(uint32(i))
assert.Equalf(t, 30, alen, "i=%d", i)
}
t.Run("hours", func(t *testing.T) {
statsData := statsCollector(units, 0, Hours, ng)
assert.Len(t, statsData, 720)
})

t.Run("days", func(t *testing.T) {
for i := 0; i != 25; i++ {
statsData := statsCollector(units, uint32(i), Days, ng)
require.Lenf(t, statsData, 30, "i=%d", i)
}
})
}
Loading

0 comments on commit c7514a8

Please sign in to comment.