forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
75 lines (64 loc) · 1.99 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
package parquet
import (
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
test "github.com/influxdata/telegraf/testutil/plugin_input"
"github.com/stretchr/testify/require"
)
func TestCases(t *testing.T) {
folders, err := os.ReadDir("testcases")
require.NoError(t, err)
require.NotEmpty(t, folders)
for _, f := range folders {
testcasePath := filepath.Join("testcases", f.Name())
configFilename := filepath.Join(testcasePath, "telegraf.conf")
t.Run(f.Name(), func(t *testing.T) {
// Configure the plugin
cfg := config.NewConfig()
require.NoError(t, cfg.LoadConfig(configFilename))
require.NoError(t, err)
require.Len(t, cfg.Inputs, 1)
// Tune the test-plugin
plugin := cfg.Inputs[0].Input.(*test.Plugin)
plugin.Path = testcasePath
require.NoError(t, plugin.Init())
// Gather the metrics and check for potential 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{
cmpopts.EquateApprox(0, 1e-6),
testutil.SortMetrics(),
}
if plugin.ShouldIgnoreTimestamp {
options = append(options, testutil.IgnoreTime())
}
// Process expected metrics and compare with resulting metrics
actual := acc.GetTelegrafMetrics()
testutil.RequireMetricsEqual(t, plugin.Expected, actual, options...)
})
}
}
func BenchmarkParsing(b *testing.B) {
plugin := &Parser{}
benchmarkData, err := os.ReadFile("testcases/benchmark/input.parquet")
require.NoError(b, err)
b.ResetTimer()
for n := 0; n < b.N; n++ {
//nolint:errcheck // Benchmarking so skip the error check to avoid the unnecessary operations
plugin.Parse(benchmarkData)
}
}