chore(parsers.json_v2): Cleanup test by reusing testutil function (#11627)
This commit is contained in:
parent
75e8640a26
commit
401d2b2a28
|
|
@ -1,9 +1,9 @@
|
||||||
package json_v2_test
|
package json_v2_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -24,94 +24,72 @@ func TestMultipleConfigs(t *testing.T) {
|
||||||
// Make sure testdata contains data
|
// Make sure testdata contains data
|
||||||
require.Greater(t, len(folders), 0)
|
require.Greater(t, len(folders), 0)
|
||||||
|
|
||||||
expectedErrors := []struct {
|
// Setup influx parser for parsing the expected metrics
|
||||||
Name string
|
parser := &influx.Parser{}
|
||||||
Error string
|
require.NoError(t, parser.Init())
|
||||||
}{
|
|
||||||
{
|
inputs.Add("file", func() telegraf.Input {
|
||||||
Name: "wrong_path",
|
return &file.File{}
|
||||||
Error: "wrong",
|
})
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, f := range folders {
|
for _, f := range folders {
|
||||||
|
testdataPath := filepath.Join("testdata", f.Name())
|
||||||
|
configFilename := filepath.Join(testdataPath, "telegraf.conf")
|
||||||
|
expectedFilename := filepath.Join(testdataPath, "expected.out")
|
||||||
|
expectedErrorFilename := filepath.Join(testdataPath, "expected.err")
|
||||||
|
|
||||||
t.Run(f.Name(), func(t *testing.T) {
|
t.Run(f.Name(), func(t *testing.T) {
|
||||||
// Process the telegraf config file for the test
|
// Read the expected output
|
||||||
buf, err := os.ReadFile(fmt.Sprintf("testdata/%s/telegraf.conf", f.Name()))
|
expected, err := testutil.ParseMetricsFromFile(expectedFilename, parser)
|
||||||
require.NoError(t, err)
|
|
||||||
inputs.Add("file", func() telegraf.Input {
|
|
||||||
return &file.File{}
|
|
||||||
})
|
|
||||||
cfg := config.NewConfig()
|
|
||||||
err = cfg.LoadConfigData(buf)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Gather the metrics from the input file configure
|
// Read the expected errors if any
|
||||||
acc := testutil.Accumulator{}
|
var expectedErrors []string
|
||||||
for _, input := range cfg.Inputs {
|
if _, err := os.Stat(expectedErrorFilename); err == nil {
|
||||||
err = input.Init()
|
var err error
|
||||||
|
expectedErrors, err = testutil.ParseLinesFromFile(expectedErrorFilename)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
err = input.Gather(&acc)
|
require.NotEmpty(t, expectedErrors)
|
||||||
// If the test has an expected error then require one was received
|
}
|
||||||
var expectedError bool
|
|
||||||
for _, e := range expectedErrors {
|
// Configure the plugin
|
||||||
if e.Name == f.Name() {
|
cfg := config.NewConfig()
|
||||||
require.Contains(t, err.Error(), e.Error)
|
require.NoError(t, cfg.LoadConfig(configFilename))
|
||||||
expectedError = true
|
|
||||||
break
|
// Gather the metrics from the input file configure
|
||||||
}
|
var acc testutil.Accumulator
|
||||||
}
|
var actualErrorMsgs []string
|
||||||
if !expectedError {
|
for _, input := range cfg.Inputs {
|
||||||
require.NoError(t, err)
|
require.NoError(t, input.Init())
|
||||||
|
if err := input.Gather(&acc); err != nil {
|
||||||
|
actualErrorMsgs = append(actualErrorMsgs, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the test has expected error(s) then compare them
|
||||||
|
if len(expectedErrors) > 0 {
|
||||||
|
sort.Strings(actualErrorMsgs)
|
||||||
|
sort.Strings(expectedErrors)
|
||||||
|
for i, msg := range expectedErrors {
|
||||||
|
require.Contains(t, actualErrorMsgs[i], msg)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
require.Empty(t, actualErrorMsgs)
|
||||||
|
}
|
||||||
|
|
||||||
// Process expected metrics and compare with resulting metrics
|
// Process expected metrics and compare with resulting metrics
|
||||||
expectedOutputs, err := readMetricFile(t, fmt.Sprintf("testdata/%s/expected.out", f.Name()))
|
actual := acc.GetTelegrafMetrics()
|
||||||
require.NoError(t, err)
|
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
|
||||||
resultingMetrics := acc.GetTelegrafMetrics()
|
|
||||||
testutil.RequireMetricsEqual(t, expectedOutputs, resultingMetrics, testutil.IgnoreTime())
|
|
||||||
|
|
||||||
// Folder with timestamp prefixed will also check for matching timestamps to make sure they are parsed correctly
|
// Folder with timestamp prefixed will also check for matching timestamps to make sure they are parsed correctly
|
||||||
// The milliseconds weren't matching, seemed like a rounding difference between the influx parser
|
// The milliseconds weren't matching, seemed like a rounding difference between the influx parser
|
||||||
// Compares each metrics times separately and ignores milliseconds
|
// Compares each metrics times separately and ignores milliseconds
|
||||||
if strings.HasPrefix(f.Name(), "timestamp") {
|
if strings.HasPrefix(f.Name(), "timestamp") {
|
||||||
require.Equal(t, len(expectedOutputs), len(resultingMetrics))
|
require.Equal(t, len(expected), len(actual))
|
||||||
for i, m := range resultingMetrics {
|
for i, m := range actual {
|
||||||
require.Equal(t, expectedOutputs[i].Time().Truncate(time.Second), m.Time().Truncate(time.Second))
|
require.Equal(t, expected[i].Time().Truncate(time.Second), m.Time().Truncate(time.Second))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func readMetricFile(t *testing.T, path string) ([]telegraf.Metric, error) {
|
|
||||||
var metrics []telegraf.Metric
|
|
||||||
expectedFile, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return metrics, err
|
|
||||||
}
|
|
||||||
defer expectedFile.Close()
|
|
||||||
|
|
||||||
parser := &influx.Parser{}
|
|
||||||
require.NoError(t, parser.Init())
|
|
||||||
scanner := bufio.NewScanner(expectedFile)
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
|
||||||
if line != "" {
|
|
||||||
m, err := parser.ParseLine(line)
|
|
||||||
// The timezone needs to be UTC to match the timestamp test results
|
|
||||||
m.SetTime(m.Time().UTC())
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to parse metric in %q failed: %v", line, err)
|
|
||||||
}
|
|
||||||
metrics = append(metrics, m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = expectedFile.Close()
|
|
||||||
if err != nil {
|
|
||||||
return metrics, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return metrics, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
the path wrong doesn't exist
|
||||||
|
the path wrong doesn't exist
|
||||||
|
the path wrong doesn't exist
|
||||||
|
The timestamp path wrong returned NULL
|
||||||
|
the path wrong doesn't exist
|
||||||
|
the path wrong doesn't exist
|
||||||
Loading…
Reference in New Issue