-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalogview_test.go
95 lines (85 loc) · 2.5 KB
/
alogview_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
package main
import (
"strings"
"testing"
)
const testLog = `10-24 22:14:41.150 123 123 D TestTag1: first test message
10-24 22:14:41.150 123 123 D TestTag2: second test message
10-24 22:14:41.150 456 456 D TestTag1: third test message
10-24 22:14:41.150 456 456 D TestTag2: fourth test message`
func runFilters(data string, filters []filter) []*logLine {
r := strings.NewReader(data)
acc := []*logLine{}
rawlines := make(chan *logLine)
filtered := startFilters(filters, rawlines)
done := make(chan int)
go func() {
for {
select {
case line := <-filtered:
acc = append(acc, line)
case <-done:
done <- 1
}
}
}()
parseLogs(r, rawlines)
done <- 1
<-done
return acc
}
func createPackageFilter() *packageFilter {
return &packageFilter{
packages: map[string]bool{"foo": true},
pids: map[int]bool{123: true},
}
}
func TestProcessFiltering(t *testing.T) {
filters := []filter{createPackageFilter()}
acc := runFilters(testLog, filters)
// TODO: this is racy
if len(acc) != 2 {
t.Errorf("expected two log entries, got %d instead", len(acc))
}
assertProcLine(t, acc[0], 123, "first test message")
assertProcLine(t, acc[1], 123, "second test message")
}
func assertProcLine(t *testing.T, line *logLine, pid int, msg string) {
if line.pid != pid {
t.Errorf("invalid PID: expected: \"%d\" actual: \"%d\"", pid, line.pid)
}
if line.message != msg {
t.Errorf("invalid message: expected: \"%s\" actual: \"%s\"", msg, line.message)
}
}
func createTagFilter() *tagFilter {
return &tagFilter{
tags: map[string]bool{"TestTag1": true},
}
}
func TestTagFiltering(t *testing.T) {
filters := []filter{createTagFilter()}
acc := runFilters(testLog, filters)
if len(acc) != 2 {
t.Errorf("expected two log entries, got %d instead", len(acc))
}
assertTagLine(t, acc[0], "TestTag1", "first test message")
assertTagLine(t, acc[1], "TestTag1", "third test message")
}
func assertTagLine(t *testing.T, line *logLine, tag string, msg string) {
if line.tag != tag {
t.Errorf("invalid tag: expected: \"%s\" actual: \"%s\"", tag, line.tag)
}
if line.message != msg {
t.Errorf("invalid message: expected: \"%s\" actual: \"%s\"", msg, line.message)
}
}
func TestProcessAndTagFiltering(t *testing.T) {
filters := []filter{createPackageFilter(), createTagFilter()}
acc := runFilters(testLog, filters)
if len(acc) != 1 {
t.Errorf("Expected a single log entry, got %d instead", len(acc))
}
assertProcLine(t, acc[0], 123, "first test message")
assertTagLine(t, acc[0], "TestTag1", "first test message")
}