2020-02-18 13:02:59 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
2020-02-20 14:11:33 +03:00
|
|
|
"encoding/binary"
|
2021-02-09 15:17:02 +03:00
|
|
|
"fmt"
|
2020-02-18 13:02:59 +03:00
|
|
|
"io"
|
2020-02-21 12:57:12 +03:00
|
|
|
"math"
|
2020-02-20 14:11:33 +03:00
|
|
|
"net"
|
2021-05-21 14:55:42 +03:00
|
|
|
"os"
|
2020-02-18 13:02:59 +03:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-02-20 14:11:33 +03:00
|
|
|
"time"
|
2020-02-18 13:02:59 +03:00
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-10-22 11:58:18 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2020-02-18 13:02:59 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 15:17:02 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-02-18 13:02:59 +03:00
|
|
|
)
|
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
// prepareTestFile prepares one test query log file with the specified lines
|
|
|
|
// count.
|
|
|
|
func prepareTestFile(t *testing.T, dir string, linesNum int) (name string) {
|
2021-02-09 15:17:02 +03:00
|
|
|
t.Helper()
|
2020-02-20 19:38:11 +03:00
|
|
|
|
2021-05-21 14:55:42 +03:00
|
|
|
f, err := os.CreateTemp(dir, "*.txt")
|
2021-03-29 11:40:04 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
// Use defer and not t.Cleanup to make sure that the file is closed
|
|
|
|
// after this function is done.
|
|
|
|
defer func() {
|
|
|
|
derr := f.Close()
|
|
|
|
require.NoError(t, derr)
|
|
|
|
}()
|
2021-02-09 19:38:31 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
const ans = `"AAAAAAABAAEAAAAAB2V4YW1wbGUDb3JnAAABAAEHZXhhbXBsZQNvcmcAAAEAAQAAAAAABAECAwQ="`
|
|
|
|
const format = `{"IP":%q,"T":%q,"QH":"example.org","QT":"A","QC":"IN",` +
|
|
|
|
`"Answer":` + ans + `,"Result":{},"Elapsed":0,"Upstream":"upstream"}` + "\n"
|
2020-02-20 19:38:11 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
var lineIP uint32
|
|
|
|
lineTime := time.Date(2020, 2, 18, 19, 36, 35, 920973000, time.UTC)
|
2024-05-15 13:34:12 +03:00
|
|
|
for range linesNum {
|
2021-03-29 11:40:04 +03:00
|
|
|
lineIP++
|
|
|
|
lineTime = lineTime.Add(time.Second)
|
2020-02-20 19:38:11 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
ip := make(net.IP, 4)
|
|
|
|
binary.BigEndian.PutUint32(ip, lineIP)
|
2021-02-09 19:38:31 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
line := fmt.Sprintf(format, ip, lineTime.Format(time.RFC3339Nano))
|
2020-02-20 14:11:33 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
_, err = f.WriteString(line)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-02-20 19:38:11 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
return f.Name()
|
|
|
|
}
|
2020-02-20 19:38:11 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
// prepareTestFiles prepares several test query log files, each with the
|
|
|
|
// specified lines count.
|
|
|
|
func prepareTestFiles(t *testing.T, filesNum, linesNum int) []string {
|
|
|
|
t.Helper()
|
2020-02-20 19:38:11 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
if filesNum == 0 {
|
|
|
|
return []string{}
|
2020-02-20 19:38:11 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
dir := t.TempDir()
|
2020-02-18 13:02:59 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
files := make([]string, filesNum)
|
|
|
|
for i := range files {
|
|
|
|
files[filesNum-i-1] = prepareTestFile(t, dir, linesNum)
|
|
|
|
}
|
2021-02-09 15:17:02 +03:00
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
return files
|
2020-02-20 19:38:11 +03:00
|
|
|
}
|
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
// newTestQLogFile creates new *qLogFile for tests and registers the required
|
2021-02-09 15:17:02 +03:00
|
|
|
// cleanup functions.
|
2023-06-07 20:04:01 +03:00
|
|
|
func newTestQLogFile(t *testing.T, linesNum int) (file *qLogFile) {
|
2021-02-09 15:17:02 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2021-03-29 11:40:04 +03:00
|
|
|
testFile := prepareTestFiles(t, 1, linesNum)[0]
|
2021-02-09 15:17:02 +03:00
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
// Create the new qLogFile instance.
|
|
|
|
file, err := newQLogFile(testFile)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
assert.NotNil(t, file)
|
2021-10-22 11:58:18 +03:00
|
|
|
testutil.CleanupAndRequireSuccess(t, file.Close)
|
2021-02-09 15:17:02 +03:00
|
|
|
|
|
|
|
return file
|
2020-02-20 19:38:11 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
func TestQLogFile_ReadNext(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
linesNum int
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
linesNum: 0,
|
|
|
|
}, {
|
|
|
|
name: "large",
|
|
|
|
linesNum: 50000,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
q := newTestQLogFile(t, tc.linesNum)
|
|
|
|
|
|
|
|
// Calculate the expected position.
|
|
|
|
fileInfo, err := q.file.Stat()
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-09 15:17:02 +03:00
|
|
|
var expPos int64
|
|
|
|
if expPos = fileInfo.Size(); expPos > 0 {
|
|
|
|
expPos--
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to the start.
|
|
|
|
pos, err := q.SeekStart()
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-09 15:17:02 +03:00
|
|
|
require.EqualValues(t, expPos, pos)
|
|
|
|
|
|
|
|
var read int
|
|
|
|
var line string
|
|
|
|
for err == nil {
|
|
|
|
line, err = q.ReadNext()
|
|
|
|
if err == nil {
|
|
|
|
assert.NotEmpty(t, line)
|
|
|
|
read++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, io.EOF, err)
|
|
|
|
assert.Equal(t, tc.linesNum, read)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 19:38:11 +03:00
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
func TestQLogFile_SeekTS_good(t *testing.T) {
|
|
|
|
linesCases := []struct {
|
|
|
|
name string
|
|
|
|
num int
|
|
|
|
}{{
|
|
|
|
name: "large",
|
|
|
|
num: 10000,
|
|
|
|
}, {
|
|
|
|
name: "small",
|
|
|
|
num: 10,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, l := range linesCases {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
linesNum int
|
|
|
|
line int
|
|
|
|
}{{
|
|
|
|
name: "not_too_old",
|
|
|
|
line: 2,
|
|
|
|
}, {
|
|
|
|
name: "old",
|
|
|
|
line: l.num - 2,
|
|
|
|
}, {
|
|
|
|
name: "first",
|
|
|
|
line: 0,
|
|
|
|
}, {
|
|
|
|
name: "last",
|
|
|
|
line: l.num,
|
|
|
|
}}
|
|
|
|
|
|
|
|
q := newTestQLogFile(t, l.num)
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(l.name+"_"+tc.name, func(t *testing.T) {
|
|
|
|
line, err := getQLogFileLine(q, tc.line)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-09 15:17:02 +03:00
|
|
|
ts := readQLogTimestamp(line)
|
|
|
|
assert.NotEqualValues(t, 0, ts)
|
|
|
|
|
|
|
|
// Try seeking to that line now.
|
2021-12-13 13:55:41 +03:00
|
|
|
pos, _, err := q.seekTS(ts)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-09 15:17:02 +03:00
|
|
|
assert.NotEqualValues(t, 0, pos)
|
|
|
|
|
|
|
|
testLine, err := q.ReadNext()
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-09 15:17:02 +03:00
|
|
|
assert.Equal(t, line, testLine)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 19:38:11 +03:00
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
func TestQLogFile_SeekTS_bad(t *testing.T) {
|
|
|
|
linesCases := []struct {
|
|
|
|
name string
|
|
|
|
num int
|
|
|
|
}{{
|
|
|
|
name: "large",
|
|
|
|
num: 10000,
|
|
|
|
}, {
|
|
|
|
name: "small",
|
|
|
|
num: 10,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, l := range linesCases {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
ts int64
|
|
|
|
leq bool
|
|
|
|
}{{
|
|
|
|
name: "non-existent_long_ago",
|
|
|
|
}, {
|
|
|
|
name: "non-existent_far_ahead",
|
|
|
|
}, {
|
|
|
|
name: "almost",
|
|
|
|
leq: true,
|
|
|
|
}}
|
|
|
|
|
|
|
|
q := newTestQLogFile(t, l.num)
|
|
|
|
testCases[0].ts = 123
|
|
|
|
|
|
|
|
lateTS, _ := time.Parse(time.RFC3339, "2100-01-02T15:04:05Z07:00")
|
|
|
|
testCases[1].ts = lateTS.UnixNano()
|
|
|
|
|
|
|
|
line, err := getQLogFileLine(q, l.num/2)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-09 15:17:02 +03:00
|
|
|
testCases[2].ts = readQLogTimestamp(line) - 1
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
assert.NotEqualValues(t, 0, tc.ts)
|
|
|
|
|
2021-03-11 20:36:54 +03:00
|
|
|
var depth int
|
2021-12-13 13:55:41 +03:00
|
|
|
_, depth, err = q.seekTS(tc.ts)
|
2021-02-09 15:17:02 +03:00
|
|
|
assert.NotEmpty(t, l.num)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
if tc.leq {
|
|
|
|
assert.LessOrEqual(t, depth, int(math.Log2(float64(l.num))+3))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 19:38:11 +03:00
|
|
|
}
|
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
func getQLogFileLine(q *qLogFile, lineNumber int) (line string, err error) {
|
2021-02-09 15:17:02 +03:00
|
|
|
if _, err = q.SeekStart(); err != nil {
|
|
|
|
return line, err
|
2020-02-20 19:38:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < lineNumber; i++ {
|
2021-02-09 15:17:02 +03:00
|
|
|
if _, err = q.ReadNext(); err != nil {
|
|
|
|
return line, err
|
2020-02-20 19:38:11 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 15:17:02 +03:00
|
|
|
|
2020-02-20 19:38:11 +03:00
|
|
|
return q.ReadNext()
|
2020-02-20 14:11:33 +03:00
|
|
|
}
|
2020-02-18 13:02:59 +03:00
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
// Check adding and loading (with filtering) entries from disk and memory.
|
2020-02-20 14:11:33 +03:00
|
|
|
func TestQLogFile(t *testing.T) {
|
2023-06-07 20:04:01 +03:00
|
|
|
// Create the new qLogFile instance.
|
2021-02-09 15:17:02 +03:00
|
|
|
q := newTestQLogFile(t, 2)
|
2020-02-18 13:02:59 +03:00
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
// Seek to the start.
|
2020-02-18 13:02:59 +03:00
|
|
|
pos, err := q.SeekStart()
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Greater(t, pos, int64(0))
|
2020-02-18 13:02:59 +03:00
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
// Read first line.
|
2020-02-18 13:02:59 +03:00
|
|
|
line, err := q.ReadNext()
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Contains(t, line, "0.0.0.2")
|
2020-02-18 13:02:59 +03:00
|
|
|
assert.True(t, strings.HasPrefix(line, "{"), line)
|
|
|
|
assert.True(t, strings.HasSuffix(line, "}"), line)
|
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
// Read second line.
|
2020-02-18 13:02:59 +03:00
|
|
|
line, err = q.ReadNext()
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.EqualValues(t, 0, q.position)
|
|
|
|
assert.Contains(t, line, "0.0.0.1")
|
2020-02-18 13:02:59 +03:00
|
|
|
assert.True(t, strings.HasPrefix(line, "{"), line)
|
|
|
|
assert.True(t, strings.HasSuffix(line, "}"), line)
|
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
// Try reading again (there's nothing to read anymore).
|
2020-02-18 13:02:59 +03:00
|
|
|
line, err = q.ReadNext()
|
2021-02-09 15:17:02 +03:00
|
|
|
require.Equal(t, io.EOF, err)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Empty(t, line)
|
2020-02-18 13:02:59 +03:00
|
|
|
}
|
2020-02-20 14:11:33 +03:00
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
func newTestQLogFileData(t *testing.T, data string) (file *qLogFile) {
|
2021-05-21 14:55:42 +03:00
|
|
|
f, err := os.CreateTemp(t.TempDir(), "*.txt")
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, f.Close)
|
2020-11-10 19:00:55 +03:00
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
_, err = f.WriteString(data)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2020-11-10 19:00:55 +03:00
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
file, err = newQLogFile(f.Name())
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, file.Close)
|
2020-11-19 12:53:31 +03:00
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
return file
|
|
|
|
}
|
2020-11-19 12:53:31 +03:00
|
|
|
|
2021-02-09 15:17:02 +03:00
|
|
|
func TestQLog_Seek(t *testing.T) {
|
|
|
|
const nl = "\n"
|
|
|
|
const strV = "%s"
|
2022-07-28 20:18:07 +03:00
|
|
|
const recs = `{"T":"` + strV + `","QH":"wfqvjymurpwegyv","QT":"A","QC":"IN","CP":"","Answer":"","Result":{},"Elapsed":66286385,"Upstream":"tls://unfiltered.adguard-dns.com:853"}` + nl +
|
2021-02-09 15:17:02 +03:00
|
|
|
`{"T":"` + strV + `"}` + nl +
|
|
|
|
`{"T":"` + strV + `"}` + nl
|
|
|
|
timestamp, _ := time.Parse(time.RFC3339Nano, "2020-08-31T18:44:25.376690873+03:00")
|
|
|
|
|
|
|
|
testCases := []struct {
|
2023-06-07 20:04:01 +03:00
|
|
|
wantErr error
|
2021-02-09 15:17:02 +03:00
|
|
|
name string
|
|
|
|
delta int
|
|
|
|
wantDepth int
|
|
|
|
}{{
|
|
|
|
name: "ok",
|
|
|
|
delta: 0,
|
|
|
|
wantErr: nil,
|
|
|
|
wantDepth: 2,
|
|
|
|
}, {
|
|
|
|
name: "too_late",
|
|
|
|
delta: 2,
|
2023-06-07 20:04:01 +03:00
|
|
|
wantErr: errTSTooLate,
|
2021-02-09 15:17:02 +03:00
|
|
|
wantDepth: 2,
|
|
|
|
}, {
|
|
|
|
name: "too_early",
|
|
|
|
delta: -2,
|
2023-06-07 20:04:01 +03:00
|
|
|
wantErr: errTSTooEarly,
|
2021-02-09 15:17:02 +03:00
|
|
|
wantDepth: 1,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
data := fmt.Sprintf(recs,
|
|
|
|
timestamp.Add(-time.Second).Format(time.RFC3339Nano),
|
|
|
|
timestamp.Format(time.RFC3339Nano),
|
|
|
|
timestamp.Add(time.Second).Format(time.RFC3339Nano),
|
|
|
|
)
|
|
|
|
|
2023-06-07 20:04:01 +03:00
|
|
|
q := newTestQLogFileData(t, data)
|
2021-02-09 15:17:02 +03:00
|
|
|
|
2021-12-13 13:55:41 +03:00
|
|
|
_, depth, err := q.seekTS(timestamp.Add(time.Second * time.Duration(tc.delta)).UnixNano())
|
2021-02-09 15:17:02 +03:00
|
|
|
require.Truef(t, errors.Is(err, tc.wantErr), "%v", err)
|
|
|
|
assert.Equal(t, tc.wantDepth, depth)
|
|
|
|
})
|
|
|
|
}
|
2020-11-19 12:53:31 +03:00
|
|
|
}
|