forked from google/mtail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_test.go
176 lines (164 loc) · 3.9 KB
/
ex_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2011 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package main
import (
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/mtail/metrics"
"github.com/google/mtail/mtail"
"github.com/google/mtail/testdata"
"github.com/google/mtail/watcher"
"github.com/spf13/afero"
)
var exampleProgramTests = []struct {
programfile string // Example program file.
logfile string // Sample log input.
goldenfile string // Expected metrics after processing.
}{
{
"examples/rsyncd.mtail",
"testdata/rsyncd.log",
"testdata/rsyncd.golden",
},
{
"examples/sftp.mtail",
"testdata/sftp_chroot.log",
"testdata/sftp_chroot.golden",
},
{
"examples/dhcpd.mtail",
"testdata/anonymised_dhcpd_log",
"testdata/anonymised_dhcpd_log.golden",
},
{
"examples/ntpd.mtail",
"testdata/ntp4",
"testdata/ntp4.golden",
},
{
"examples/ntpd_peerstats.mtail",
"testdata/xntp3_peerstats",
"testdata/xntp3_peerstats.golden",
},
{
"examples/otherwise.mtail",
"testdata/otherwise.log",
"testdata/otherwise.golden",
},
{
"examples/else.mtail",
"testdata/else.log",
"testdata/else.golden",
},
{
"examples/types.mtail",
"testdata/types.log",
"testdata/types.golden",
},
{
"examples/filename.mtail",
"testdata/else.log",
"testdata/filename.golden",
},
{
"examples/logical.mtail",
"testdata/logical.log",
"testdata/logical.golden",
},
{
"examples/strcat.mtail",
"testdata/strcat.log",
"testdata/strcat.golden",
},
{
"examples/add_assign_float.mtail",
"testdata/add_assign_float.log",
"testdata/add_assign_float.golden",
},
{
"examples/typed-comparison.mtail",
"testdata/typed-comparison.log",
"testdata/typed-comparison.golden",
},
{
"examples/match-expression.mtail",
"testdata/match-expression.log",
"testdata/match-expression.golden",
},
}
func TestExamplePrograms(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
for _, tc := range exampleProgramTests {
t.Run(fmt.Sprintf("%s on %s", tc.programfile, tc.logfile), func(t *testing.T) {
w := watcher.NewFakeWatcher()
store := metrics.NewStore()
fs := &afero.OsFs{}
logs := []string{tc.logfile}
o := mtail.Options{Progs: tc.programfile, LogPathPatterns: logs, W: w, FS: fs, Store: store}
o.OneShot = true
o.OmitMetricSource = true
o.DumpAstTypes = true
o.DumpBytecode = true
mtail, err := mtail.New(o)
if err != nil {
t.Fatalf("create mtail failed: %s", err)
}
err = mtail.StartTailing()
if err != nil {
t.Fatalf("Start tailling failed: %s", err)
}
g, err := os.Open(tc.goldenfile)
if err != nil {
t.Fatalf("could not open golden file: %s", err)
}
defer g.Close()
golden_store := metrics.NewStore()
testdata.ReadTestData(g, tc.programfile, golden_store)
err = mtail.Close()
if err != nil {
t.Error(err)
}
diff := cmp.Diff(golden_store, store, cmpopts.IgnoreUnexported(sync.RWMutex{}))
if diff != "" {
t.Error(diff)
t.Logf(" Golden metrics: %s", golden_store.Metrics)
t.Logf("Program metrics: %s", store.Metrics)
}
})
}
}
// This test only compiles examples, but has coverage over all examples
// provided. This ensures we ship at least syntactically correct examples.
func TestCompileExamplePrograms(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
matches, err := filepath.Glob("examples/*.mtail")
if err != nil {
t.Fatal(err)
}
for _, tc := range matches {
t.Run(tc, func(t *testing.T) {
w := watcher.NewFakeWatcher()
s := metrics.NewStore()
fs := &afero.OsFs{}
o := mtail.Options{Progs: tc, W: w, FS: fs, Store: s}
o.CompileOnly = true
o.OmitMetricSource = true
o.DumpAstTypes = true
o.DumpBytecode = true
mtail, err := mtail.New(o)
if err != nil {
t.Fatal(err)
}
mtail.Close()
})
}
}