This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
160 lines (150 loc) · 4.32 KB
/
main_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"io/ioutil"
"os"
"strings"
"testing"
"time"
)
func TestNewDNAQuery(t *testing.T) {
_, err := NewDNAQuery(&Configuration{})
if err == nil {
t.Errorf("should error if 0 Apps in config")
}
c1 := App{Name: "App 1"}
cfg := &Configuration{
Apps: []App{c1},
}
_, err = NewDNAQuery(cfg)
if err == nil {
t.Errorf("should error if no log directory is specified")
}
cfg, _ = NewConfiguration("example.toml")
dna, _ := NewDNAQuery(cfg)
if dna == nil {
t.Error("dna should not be nil")
}
}
func TestCleanupFiles(t *testing.T) {
// make a few files
f1Path := "file1"
f2Path := "file2"
f3Path := "file3" // this file is never created
f1, err := os.Create(f1Path)
if err != nil {
t.Fatalf("Couldn't create test file (%s): %s", f1Path, err.Error())
}
f1.Close()
f2, err := os.Create(f2Path)
if err != nil {
t.Fatalf("Couldn't create test file (%s): %s", f1Path, err.Error())
}
f2.Close()
cleanupFiles(f1Path, f2Path, f3Path)
if _, err := os.Stat(f1Path); !os.IsNotExist(err) {
t.Errorf("test file (%s) still exists", f1Path)
}
if _, err := os.Stat(f2Path); !os.IsNotExist(err) {
t.Errorf("test file (%s) still exists", f2Path)
}
}
func TestProcessLine(t *testing.T) {
c1 := App{
Name: "app1",
Regex: `^([\d.]+) \[([^\]]*)\] - "([^"]*)" (\d+)`,
TimeGroup: 2,
TimeFormat: "2/Jan/2006:15:04:05 -0700",
}
c2 := App{
Name: "app2",
Regex: `^([\d.]+) - \[([^\]]*)\] - "([^"]*)" (\d+)`,
TimeGroup: 2,
TimeFormat: "2006-01-02T15:04:05-0700",
Excludes: []Exclude{{Group: 3, Contains: "ping"}},
}
// invalid configuration, should have code to detect this at start up, for now
// make sure code handles it
c3 := App{
Name: "app3",
Regex: `^([\d.]+)`,
TimeGroup: 2,
TimeFormat: "2006-01-02T15:04:05-0700",
Excludes: []Exclude{{Group: 3, Contains: "ping"}},
}
cfg := &Configuration{
Apps: []App{c1, c2, c3},
Storage: Storage{LogDirectory: "/tmp/"},
}
dna, err := NewDNAQuery(cfg)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
err = dna.processLine("", nil)
if err == nil {
t.Fatal("Expected error for bad filename")
}
ch := make(chan [2]string)
outfile := "output.csv"
go func(outfile string, ch chan [2]string) {
// regular, expect normal operation
ch <- [2]string{"app1", `123.123.123.123 [13/Nov/2017:13:23:01 -0000] - "GET view.json" 200`}
ch <- [2]string{"app1", `123.123.123.123 [13/Nov/2017:13:23:04 -0000] - "GET ping.json" 200`}
ch <- [2]string{"app2", `2.1.5.3 - [2017-12-03T13:23:04-0500] - "GET ping.json" 200`}
ch <- [2]string{"app2", `2.1.5.3 - [2017-12-03T13:23:04-0500] - "GET view.json" 200`}
// case where exclusion and time groups are more then regex
ch <- [2]string{"app3", `2.1.5.3`}
// case where there is no app registered
ch <- [2]string{"app", `2.1.5.3 - [2017-12-03T13:23:04-0500] - "GET view.json" 200`}
// case where the log line doesn't match regex
ch <- [2]string{"app1", `error`}
// case where time format does not match, should just skip over
ch <- [2]string{"app1", `123.123.123.123 [13-Nov-2017:13:23:01 -0000] - "GET view.json" 200`}
close(ch)
}(outfile, ch)
err = dna.processLine(outfile, ch)
// sleep a bit for goroutine to finish up once channel is closed
time.Sleep(500 * time.Millisecond)
dat, err := ioutil.ReadFile(outfile)
if err != nil {
t.Fatalf("unexpected error opening outfile, %v", err)
}
lines := strings.Split(string(dat), "\n")
expectedLines := 5 // 4 logs + 1 empty line
if len(lines) != expectedLines {
t.Errorf("expected %d lines, found %d lines", expectedLines, len(lines))
}
cleanupFiles(outfile)
}
func TestReadLine(t *testing.T) {
c1 := App{
Name: "ingress-lb",
Regex: `^.*$`,
}
cfg := &Configuration{
Apps: []App{c1},
Storage: Storage{LogDirectory: "/tmp/"},
}
dna, err := NewDNAQuery(cfg)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
ch, err := dna.readLine("test_data/testlog")
if err == nil {
t.Error("expected error for invalid file")
}
ch, err = dna.readLine("main.go")
if err == nil {
t.Error("expected error for invalid gzip file")
}
ch, err = dna.readLine("test_data/testlog.json.gz")
if err != nil {
t.Fatalf("unexpected error in readLine, %v", err)
}
count := 0
for _ = range ch {
count += 1
}
if count != 6 {
t.Errorf("Unexpected # of logs found based on config, expected 6 found %v", count)
}
}