2020-02-18 10:02:59 +00:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
2020-02-20 11:11:33 +00:00
|
|
|
"encoding/binary"
|
2021-02-09 12:17:02 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-02-18 10:02:59 +00:00
|
|
|
"io"
|
2020-02-20 11:11:33 +00:00
|
|
|
"io/ioutil"
|
2020-02-21 09:57:12 +00:00
|
|
|
"math"
|
2020-02-20 11:11:33 +00:00
|
|
|
"net"
|
2020-02-18 10:02:59 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-02-20 11:11:33 +00:00
|
|
|
"time"
|
2020-02-18 10:02:59 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 12:17:02 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-02-18 10:02:59 +00:00
|
|
|
)
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// prepareTestFiles prepares several test query log files, each with the
|
|
|
|
// specified lines count.
|
|
|
|
func prepareTestFiles(t *testing.T, dir string, filesNum, linesNum int) []string {
|
|
|
|
t.Helper()
|
2020-02-20 16:38:11 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
const strV = "\"%s\""
|
|
|
|
const nl = "\n"
|
|
|
|
const format = `{"IP":` + strV + `,"T":` + strV + `,` +
|
|
|
|
`"QH":"example.org","QT":"A","QC":"IN",` +
|
|
|
|
`"Answer":"AAAAAAABAAEAAAAAB2V4YW1wbGUDb3JnAAABAAEHZXhhbXBsZQNvcmcAAAEAAQAAAAAABAECAwQ=",` +
|
|
|
|
`"Result":{},"Elapsed":0,"Upstream":"upstream"}` + nl
|
2020-02-20 16:38:11 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
lineTime, _ := time.Parse(time.RFC3339Nano, "2020-02-18T22:36:35.920973+03:00")
|
|
|
|
lineIP := uint32(0)
|
2020-02-20 16:38:11 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
files := make([]string, filesNum)
|
|
|
|
for j := range files {
|
|
|
|
f, err := ioutil.TempFile(dir, "*.txt")
|
|
|
|
require.Nil(t, err)
|
|
|
|
files[filesNum-j-1] = f.Name()
|
2020-02-20 11:11:33 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
for i := 0; i < linesNum; i++ {
|
|
|
|
lineIP++
|
|
|
|
lineTime = lineTime.Add(time.Second)
|
2020-02-20 16:38:11 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
ip := make(net.IP, 4)
|
|
|
|
binary.BigEndian.PutUint32(ip, lineIP)
|
2020-02-20 16:38:11 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
line := fmt.Sprintf(format, ip, lineTime.Format(time.RFC3339Nano))
|
2020-02-20 16:38:11 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
_, err = f.WriteString(line)
|
|
|
|
require.Nil(t, err)
|
2020-02-20 16:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
return files
|
2020-02-20 11:11:33 +00:00
|
|
|
}
|
2020-02-18 10:02:59 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// prepareTestFile prepares a test query log file with the specified number of
|
|
|
|
// lines.
|
|
|
|
func prepareTestFile(t *testing.T, dir string, linesCount int) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
return prepareTestFiles(t, dir, 1, linesCount)[0]
|
2020-02-20 16:38:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// 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)
|
|
|
|
|
|
|
|
// Create the new QLogFile instance.
|
|
|
|
file, err := NewQLogFile(testFile)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.NotNil(t, file)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
assert.Nil(t, file.Close())
|
|
|
|
})
|
|
|
|
|
|
|
|
return file
|
2020-02-20 16:38:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 12:17:02 +00: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()
|
|
|
|
require.Nil(t, err)
|
|
|
|
var expPos int64
|
|
|
|
if expPos = fileInfo.Size(); expPos > 0 {
|
|
|
|
expPos--
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to the start.
|
|
|
|
pos, err := q.SeekStart()
|
|
|
|
require.Nil(t, err)
|
|
|
|
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 16:38:11 +00:00
|
|
|
|
2021-02-09 12:17:02 +00: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)
|
|
|
|
require.Nil(t, err)
|
|
|
|
ts := readQLogTimestamp(line)
|
|
|
|
assert.NotEqualValues(t, 0, ts)
|
|
|
|
|
|
|
|
// Try seeking to that line now.
|
|
|
|
pos, _, err := q.SeekTS(ts)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.NotEqualValues(t, 0, pos)
|
|
|
|
|
|
|
|
testLine, err := q.ReadNext()
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, line, testLine)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 16:38:11 +00:00
|
|
|
|
2021-02-09 12:17:02 +00: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)
|
|
|
|
require.Nil(t, err)
|
|
|
|
testCases[2].ts = readQLogTimestamp(line) - 1
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
assert.NotEqualValues(t, 0, tc.ts)
|
|
|
|
|
|
|
|
_, depth, err := q.SeekTS(tc.ts)
|
|
|
|
assert.NotEmpty(t, l.num)
|
|
|
|
require.NotNil(t, err)
|
|
|
|
if tc.leq {
|
|
|
|
assert.LessOrEqual(t, depth, int(math.Log2(float64(l.num))+3))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 16:38:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
func getQLogFileLine(q *QLogFile, lineNumber int) (line string, err error) {
|
|
|
|
if _, err = q.SeekStart(); err != nil {
|
|
|
|
return line, err
|
2020-02-20 16:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < lineNumber; i++ {
|
2021-02-09 12:17:02 +00:00
|
|
|
if _, err = q.ReadNext(); err != nil {
|
|
|
|
return line, err
|
2020-02-20 16:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 12:17:02 +00:00
|
|
|
|
2020-02-20 16:38:11 +00:00
|
|
|
return q.ReadNext()
|
2020-02-20 11:11:33 +00:00
|
|
|
}
|
2020-02-18 10:02:59 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// Check adding and loading (with filtering) entries from disk and memory.
|
2020-02-20 11:11:33 +00:00
|
|
|
func TestQLogFile(t *testing.T) {
|
2021-02-09 12:17:02 +00:00
|
|
|
// Create the new QLogFile instance.
|
|
|
|
q := newTestQLogFile(t, 2)
|
2020-02-18 10:02:59 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// Seek to the start.
|
2020-02-18 10:02:59 +00:00
|
|
|
pos, err := q.SeekStart()
|
2021-02-09 12:17:02 +00:00
|
|
|
require.Nil(t, err)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.Greater(t, pos, int64(0))
|
2020-02-18 10:02:59 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// Read first line.
|
2020-02-18 10:02:59 +00:00
|
|
|
line, err := q.ReadNext()
|
2021-02-09 12:17:02 +00:00
|
|
|
require.Nil(t, err)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.Contains(t, line, "0.0.0.2")
|
2020-02-18 10:02:59 +00:00
|
|
|
assert.True(t, strings.HasPrefix(line, "{"), line)
|
|
|
|
assert.True(t, strings.HasSuffix(line, "}"), line)
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// Read second line.
|
2020-02-18 10:02:59 +00:00
|
|
|
line, err = q.ReadNext()
|
2021-02-09 12:17:02 +00:00
|
|
|
require.Nil(t, err)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.EqualValues(t, 0, q.position)
|
|
|
|
assert.Contains(t, line, "0.0.0.1")
|
2020-02-18 10:02:59 +00:00
|
|
|
assert.True(t, strings.HasPrefix(line, "{"), line)
|
|
|
|
assert.True(t, strings.HasSuffix(line, "}"), line)
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// Try reading again (there's nothing to read anymore).
|
2020-02-18 10:02:59 +00:00
|
|
|
line, err = q.ReadNext()
|
2021-02-09 12:17:02 +00:00
|
|
|
require.Equal(t, io.EOF, err)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.Empty(t, line)
|
2020-02-18 10:02:59 +00:00
|
|
|
}
|
2020-02-20 11:11:33 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
func NewTestQLogFileData(t *testing.T, data string) (file *QLogFile) {
|
|
|
|
f, err := ioutil.TempFile(prepareTestDir(t), "*.txt")
|
|
|
|
require.Nil(t, err)
|
2020-11-10 16:00:55 +00:00
|
|
|
t.Cleanup(func() {
|
2021-02-09 12:17:02 +00:00
|
|
|
assert.Nil(t, f.Close())
|
2020-11-10 16:00:55 +00:00
|
|
|
})
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
_, err = f.WriteString(data)
|
|
|
|
require.Nil(t, err)
|
2020-11-10 16:00:55 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
file, err = NewQLogFile(f.Name())
|
|
|
|
require.Nil(t, err)
|
2020-11-19 09:53:31 +00:00
|
|
|
t.Cleanup(func() {
|
2021-02-09 12:17:02 +00:00
|
|
|
assert.Nil(t, file.Close())
|
2020-11-19 09:53:31 +00:00
|
|
|
})
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
return file
|
|
|
|
}
|
2020-11-19 09:53:31 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
func TestQLog_Seek(t *testing.T) {
|
|
|
|
const nl = "\n"
|
|
|
|
const strV = "%s"
|
|
|
|
const recs = `{"T":"` + strV + `","QH":"wfqvjymurpwegyv","QT":"A","QC":"IN","CP":"","Answer":"","Result":{},"Elapsed":66286385,"Upstream":"tls://dns-unfiltered.adguard.com:853"}` + nl +
|
|
|
|
`{"T":"` + strV + `"}` + nl +
|
|
|
|
`{"T":"` + strV + `"}` + nl
|
|
|
|
timestamp, _ := time.Parse(time.RFC3339Nano, "2020-08-31T18:44:25.376690873+03:00")
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
delta int
|
|
|
|
wantErr error
|
|
|
|
wantDepth int
|
|
|
|
}{{
|
|
|
|
name: "ok",
|
|
|
|
delta: 0,
|
|
|
|
wantErr: nil,
|
|
|
|
wantDepth: 2,
|
|
|
|
}, {
|
|
|
|
name: "too_late",
|
|
|
|
delta: 2,
|
|
|
|
wantErr: ErrTSTooLate,
|
|
|
|
wantDepth: 2,
|
|
|
|
}, {
|
|
|
|
name: "too_early",
|
|
|
|
delta: -2,
|
|
|
|
wantErr: ErrTSTooEarly,
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
|
|
|
|
q := NewTestQLogFileData(t, data)
|
|
|
|
|
|
|
|
_, depth, err := q.SeekTS(timestamp.Add(time.Second * time.Duration(tc.delta)).UnixNano())
|
|
|
|
require.Truef(t, errors.Is(err, tc.wantErr), "%v", err)
|
|
|
|
assert.Equal(t, tc.wantDepth, depth)
|
|
|
|
})
|
|
|
|
}
|
2020-11-19 09:53:31 +00:00
|
|
|
}
|