-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaner_test.go
53 lines (42 loc) · 1.46 KB
/
cleaner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
/*
To generate test data:
<testdata/web.log go run cleaner.go \
-crawler-output ./testdata/expected-crawler.log \
-non-crawler-output ./testdata/expected-non-crawler.log \
-error-output ./testdata/expected-error.log \
-extra-crawler-agents-file testdata/extra-crawler-agents.txt
*/
import (
"bytes"
"io"
"os"
"testing"
)
func readFile(path string) []byte {
data, err := os.ReadFile(path)
if err != nil {
panic(err)
}
return data
}
var extraCrawlerAgentsBytes []byte = readFile("testdata/extra-crawler-agents.txt")
var rawLogBytes []byte = readFile("testdata/web.log")
var expectedNonCrawlerBytes []byte = readFile("testdata/expected-non-crawler.log")
var expectedCrawlerBytes []byte = readFile("testdata/expected-crawler.log")
var expectedErrorBytes []byte = readFile("testdata/expected-error.log")
func TestCleaner(t *testing.T) {
var nonCrawlerBytes, crawlerBytes, errorBytes bytes.Buffer
addExtraCrawlerAgents(bytes.NewReader(extraCrawlerAgentsBytes))
cleanCrawlers(defaultUserAgentKey, bytes.NewReader(rawLogBytes),
io.Writer(&nonCrawlerBytes), io.Writer(&crawlerBytes), io.Writer(&errorBytes))
if !bytes.Equal(expectedNonCrawlerBytes, nonCrawlerBytes.Bytes()) {
t.Fatal("Non crawler logs did not match expected")
}
if !bytes.Equal(expectedCrawlerBytes, crawlerBytes.Bytes()) {
t.Fatal("Crawler logs did not match expected")
}
if !bytes.Equal(expectedErrorBytes, errorBytes.Bytes()) {
t.Fatal("Error logs did not match expected")
}
}