forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
176 lines (151 loc) · 4.97 KB
/
parser_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
package prometheus
import (
"net/http"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/testutil"
test "github.com/influxdata/telegraf/testutil/plugin_input"
)
func TestCases(t *testing.T) {
// Get all directories in testcases
folders, err := os.ReadDir("testcases")
require.NoError(t, err)
// Make sure testdata contains data
require.NotEmpty(t, folders)
for _, f := range folders {
fname := f.Name()
testdataPath := filepath.Join("testcases", fname)
configFilename := filepath.Join(testdataPath, "telegraf.conf")
// Run tests as metric version 1
t.Run(fname+"_v1", func(t *testing.T) {
// Load the configuration
cfg := config.NewConfig()
require.NoError(t, cfg.LoadConfig(configFilename))
require.Len(t, cfg.Inputs, 1)
// Tune plugin
plugin := cfg.Inputs[0].Input.(*test.Plugin)
plugin.Path = testdataPath
plugin.UseTypeTag = "_type"
plugin.ExpectedFilename = "expected_v1.out"
parser := plugin.Parser.(*models.RunningParser).Parser.(*Parser)
parser.MetricVersion = 1
if raw, found := plugin.AdditionalParams["headers"]; found {
headers, ok := raw.(map[string]interface{})
require.Truef(t, ok, "unknown header type %T", raw)
parser.Header = make(http.Header)
for k, rv := range headers {
v, ok := rv.(string)
require.Truef(t, ok, "unknown header value type %T for %q", raw, k)
parser.Header.Add(k, v)
}
}
require.NoError(t, plugin.Init())
// Gather data and check errors
var acc testutil.Accumulator
err := plugin.Gather(&acc)
switch len(plugin.ExpectedErrors) {
case 0:
require.NoError(t, err)
case 1:
require.ErrorContains(t, err, plugin.ExpectedErrors[0])
default:
require.Contains(t, plugin.ExpectedErrors, err.Error())
}
// Determine checking options
options := []cmp.Option{
testutil.SortMetrics(),
}
if plugin.ShouldIgnoreTimestamp || parser.IgnoreTimestamp {
options = append(options, testutil.IgnoreTime())
}
// Check the resulting metrics
actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, plugin.Expected, actual, options...)
// Special checks
if parser.IgnoreTimestamp {
t.Log("testing ignore-timestamp case")
for i, m := range actual {
expected := plugin.Expected[i]
require.Greaterf(t, m.Time(), expected.Time(), "metric time not after prometheus value in %d", i)
}
}
})
// Run tests as metric version 2
t.Run(fname+"_v2", func(t *testing.T) {
// Load the configuration
cfg := config.NewConfig()
require.NoError(t, cfg.LoadConfig(configFilename))
require.Len(t, cfg.Inputs, 1)
// Tune plugin
plugin := cfg.Inputs[0].Input.(*test.Plugin)
plugin.Path = testdataPath
plugin.UseTypeTag = "_type"
plugin.ExpectedFilename = "expected_v2.out"
parser := plugin.Parser.(*models.RunningParser).Parser.(*Parser)
parser.MetricVersion = 2
if raw, found := plugin.AdditionalParams["headers"]; found {
headers, ok := raw.(map[string]interface{})
require.Truef(t, ok, "unknown header type %T", raw)
parser.Header = make(http.Header)
for k, rv := range headers {
v, ok := rv.(string)
require.Truef(t, ok, "unknown header value type %T for %q", raw, k)
parser.Header.Add(k, v)
}
}
require.NoError(t, plugin.Init())
// Gather data and check errors
var acc testutil.Accumulator
err := plugin.Gather(&acc)
switch len(plugin.ExpectedErrors) {
case 0:
require.NoError(t, err)
case 1:
require.ErrorContains(t, err, plugin.ExpectedErrors[0])
default:
require.Contains(t, plugin.ExpectedErrors, err.Error())
}
// Determine checking options
options := []cmp.Option{
testutil.SortMetrics(),
}
if plugin.ShouldIgnoreTimestamp || parser.IgnoreTimestamp {
options = append(options, testutil.IgnoreTime())
}
// Check the resulting metrics
actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, plugin.Expected, actual, options...)
// Special checks
if parser.IgnoreTimestamp {
t.Log("testing ignore-timestamp case")
for i, m := range actual {
expected := plugin.Expected[i]
require.Greaterf(t, m.Time(), expected.Time(), "metric time not after prometheus value in %d", i)
}
}
})
}
}
func BenchmarkParsingMetricVersion1(b *testing.B) {
plugin := &Parser{MetricVersion: 1}
benchmarkData, err := os.ReadFile(filepath.FromSlash("testcases/benchmark/input.txt"))
require.NoError(b, err)
require.NotEmpty(b, benchmarkData)
for n := 0; n < b.N; n++ {
_, _ = plugin.Parse(benchmarkData)
}
}
func BenchmarkParsingMetricVersion2(b *testing.B) {
plugin := &Parser{MetricVersion: 2}
benchmarkData, err := os.ReadFile(filepath.FromSlash("testcases/benchmark/input.txt"))
require.NoError(b, err)
require.NotEmpty(b, benchmarkData)
for n := 0; n < b.N; n++ {
_, _ = plugin.Parse(benchmarkData)
}
}