*: better tests for qlog
This commit is contained in:
parent
34e1147de3
commit
67dc7d7d88
|
@ -1,34 +1,38 @@
|
||||||
package querylog
|
package querylog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestQLogFileEmpty(t *testing.T) {
|
||||||
|
// TODO: test empty file
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQLogFileLarge(t *testing.T) {
|
||||||
|
// TODO: test reading large file
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQLogFileSeek(t *testing.T) {
|
||||||
|
// TODO: test seek method on a small file
|
||||||
|
}
|
||||||
|
|
||||||
// Check adding and loading (with filtering) entries from disk and memory
|
// Check adding and loading (with filtering) entries from disk and memory
|
||||||
func TestQLogFile(t *testing.T) {
|
func TestQLogFile(t *testing.T) {
|
||||||
conf := Config{
|
testDir := prepareTestDir()
|
||||||
Enabled: true,
|
defer func() { _ = os.RemoveAll(testDir) }()
|
||||||
Interval: 1,
|
testFile := prepareTestFile(testDir, 2)
|
||||||
MemSize: 100,
|
|
||||||
}
|
|
||||||
conf.BaseDir = prepareTestDir()
|
|
||||||
defer func() { _ = os.RemoveAll(conf.BaseDir) }()
|
|
||||||
l := newQueryLog(conf)
|
|
||||||
|
|
||||||
// add disk entries
|
|
||||||
addEntry(l, "example.org", "1.2.3.4", "0.1.2.4")
|
|
||||||
addEntry(l, "example.org", "1.2.3.4", "0.1.2.5")
|
|
||||||
|
|
||||||
// write to disk
|
|
||||||
_ = l.flushLogBuffer(true)
|
|
||||||
|
|
||||||
// create the new QLogFile instance
|
// create the new QLogFile instance
|
||||||
q, err := NewQLogFile(l.logFile)
|
q, err := NewQLogFile(testFile)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, q)
|
assert.NotNil(t, q)
|
||||||
|
|
||||||
|
@ -40,7 +44,7 @@ func TestQLogFile(t *testing.T) {
|
||||||
// read first line
|
// read first line
|
||||||
line, err := q.ReadNext()
|
line, err := q.ReadNext()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.True(t, strings.Contains(line, "0.1.2.5"), line)
|
assert.True(t, strings.Contains(line, "0.0.0.2"), line)
|
||||||
assert.True(t, strings.HasPrefix(line, "{"), line)
|
assert.True(t, strings.HasPrefix(line, "{"), line)
|
||||||
assert.True(t, strings.HasSuffix(line, "}"), line)
|
assert.True(t, strings.HasSuffix(line, "}"), line)
|
||||||
|
|
||||||
|
@ -48,7 +52,7 @@ func TestQLogFile(t *testing.T) {
|
||||||
line, err = q.ReadNext()
|
line, err = q.ReadNext()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, int64(0), q.position)
|
assert.Equal(t, int64(0), q.position)
|
||||||
assert.True(t, strings.Contains(line, "0.1.2.4"), line)
|
assert.True(t, strings.Contains(line, "0.0.0.1"), line)
|
||||||
assert.True(t, strings.HasPrefix(line, "{"), line)
|
assert.True(t, strings.HasPrefix(line, "{"), line)
|
||||||
assert.True(t, strings.HasSuffix(line, "}"), line)
|
assert.True(t, strings.HasSuffix(line, "}"), line)
|
||||||
|
|
||||||
|
@ -57,3 +61,30 @@ func TestQLogFile(t *testing.T) {
|
||||||
assert.Equal(t, io.EOF, err)
|
assert.Equal(t, io.EOF, err)
|
||||||
assert.Equal(t, "", line)
|
assert.Equal(t, "", line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prepareTestFile - prepares a test query log file with the specified number of lines
|
||||||
|
func prepareTestFile(dir string, linesCount int) string {
|
||||||
|
format := `{"IP":"${IP}","T":"${TIMESTAMP}","QH":"example.org","QT":"A","QC":"IN","Answer":"AAAAAAABAAEAAAAAB2V4YW1wbGUDb3JnAAABAAEHZXhhbXBsZQNvcmcAAAEAAQAAAAAABAECAwQ=","Result":{},"Elapsed":0,"Upstream":"upstream"}`
|
||||||
|
|
||||||
|
lineTime, _ := time.Parse(time.RFC3339Nano, "2020-02-18T22:36:35.920973+03:00")
|
||||||
|
lineIP := uint32(0)
|
||||||
|
|
||||||
|
f, _ := ioutil.TempFile(dir, "*.txt")
|
||||||
|
|
||||||
|
for i := 0; i < linesCount; i++ {
|
||||||
|
lineIP += 1
|
||||||
|
lineTime = lineTime.Add(time.Second)
|
||||||
|
|
||||||
|
ip := make(net.IP, 4)
|
||||||
|
binary.BigEndian.PutUint32(ip, lineIP)
|
||||||
|
|
||||||
|
line := format
|
||||||
|
line = strings.ReplaceAll(line, "${IP}", ip.String())
|
||||||
|
line = strings.ReplaceAll(line, "${TIMESTAMP}", lineTime.Format(time.RFC3339Nano))
|
||||||
|
|
||||||
|
_, _ = f.WriteString(line)
|
||||||
|
_, _ = f.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.Name()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue