Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve parser tests by using go-cmp/cmp #10497

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 37 additions & 76 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"reflect"
"runtime"
"strings"
"sync"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -411,33 +414,21 @@ func TestConfig_ParserInterfaceNewFormat(t *testing.T) {
param: map[string]interface{}{
"HeaderRowCount": cfg.CSVHeaderRowCount,
},
mask: []string{"TimeFunc", "Log"},
},
"json_v2": {
mask: []string{"Log"},
mask: []string{"TimeFunc"},
},
"logfmt": {
mask: []string{"Now"},
},
"xml": {
mask: []string{"Log"},
},
"xpath_json": {
mask: []string{"Log"},
},
"xpath_msgpack": {
mask: []string{"Log"},
powersj marked this conversation as resolved.
Show resolved Hide resolved
},
"xpath_protobuf": {
cfg: &parsers.Config{
MetricName: "parser_test_new",
XPathProtobufFile: "testdata/addressbook.proto",
XPathProtobufType: "addressbook.AddressBook",
},
param: map[string]interface{}{
"ProtobufMessageDef": "testdata/addressbook.proto",
"ProtobufMessageType": "addressbook.AddressBook",
},
mask: []string{"Log"},
},
}

Expand Down Expand Up @@ -510,33 +501,24 @@ func TestConfig_ParserInterfaceNewFormat(t *testing.T) {
require.Len(t, actual, len(formats))

for i, format := range formats {
// Determine the underlying type of the parser
stype := reflect.Indirect(reflect.ValueOf(expected[i])).Interface()
// Ignore all unexported fields and fields not relevant for functionality
options := []cmp.Option{
cmpopts.IgnoreUnexported(stype),
cmpopts.IgnoreTypes(sync.Mutex{}),
cmpopts.IgnoreInterfaces(struct{ telegraf.Logger }{}),
}
if settings, found := override[format]; found {
a := reflect.Indirect(reflect.ValueOf(actual[i]))
e := reflect.Indirect(reflect.ValueOf(expected[i]))
g := reflect.Indirect(reflect.ValueOf(generated[i]))
for _, key := range settings.mask {
af := a.FieldByName(key)
ef := e.FieldByName(key)
gf := g.FieldByName(key)

v := reflect.Zero(ef.Type())
af.Set(v)
ef.Set(v)
gf.Set(v)
}
options = append(options, cmpopts.IgnoreFields(stype, settings.mask...))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the above set of code, can you briefly explain what is going on?

It looks like we are taking parser type and ignoring out various unexported fields that the parser has?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to ignore all unexported fields and telegraf.Logger, and add the fields that are needed to check specifically by name in the override structure.

}

// We need special handling for same parsers as they internally contain pointers
// to other structs that inherently differ between instances
switch format {
case "dropwizard", "grok", "influx", "wavefront":
// At least check if we have the same type
require.IsType(t, expected[i], actual[i])
require.IsType(t, expected[i], generated[i])
continue
}
require.EqualValuesf(t, expected[i], actual[i], "in SetParser() for %q", format)
require.EqualValuesf(t, expected[i], generated[i], "in SetParserFunc() for %q", format)
// Do a manual comparision as require.EqualValues will also work on unexported fields
// that cannot be cleared or ignored.
diff := cmp.Diff(expected[i], actual[i], options...)
require.Emptyf(t, diff, "Difference in SetParser() for %q", format)
diff = cmp.Diff(expected[i], generated[i], options...)
require.Emptyf(t, diff, "Difference in SetParserFunc() for %q", format)
}
}

Expand Down Expand Up @@ -581,33 +563,21 @@ func TestConfig_ParserInterfaceOldFormat(t *testing.T) {
param: map[string]interface{}{
"HeaderRowCount": cfg.CSVHeaderRowCount,
},
mask: []string{"TimeFunc", "Log"},
},
"json_v2": {
mask: []string{"Log"},
mask: []string{"TimeFunc"},
},
"logfmt": {
mask: []string{"Now"},
},
"xml": {
mask: []string{"Log"},
},
"xpath_json": {
mask: []string{"Log"},
},
"xpath_msgpack": {
mask: []string{"Log"},
},
"xpath_protobuf": {
cfg: &parsers.Config{
MetricName: "parser_test_new",
XPathProtobufFile: "testdata/addressbook.proto",
XPathProtobufType: "addressbook.AddressBook",
},
param: map[string]interface{}{
"ProtobufMessageDef": "testdata/addressbook.proto",
"ProtobufMessageType": "addressbook.AddressBook",
},
mask: []string{"Log"},
},
}

Expand Down Expand Up @@ -680,33 +650,24 @@ func TestConfig_ParserInterfaceOldFormat(t *testing.T) {
require.Len(t, actual, len(formats))

for i, format := range formats {
// Determine the underlying type of the parser
stype := reflect.Indirect(reflect.ValueOf(expected[i])).Interface()
// Ignore all unexported fields and fields not relevant for functionality
options := []cmp.Option{
cmpopts.IgnoreUnexported(stype),
cmpopts.IgnoreTypes(sync.Mutex{}),
cmpopts.IgnoreInterfaces(struct{ telegraf.Logger }{}),
}
if settings, found := override[format]; found {
a := reflect.Indirect(reflect.ValueOf(actual[i]))
e := reflect.Indirect(reflect.ValueOf(expected[i]))
g := reflect.Indirect(reflect.ValueOf(generated[i]))
for _, key := range settings.mask {
af := a.FieldByName(key)
ef := e.FieldByName(key)
gf := g.FieldByName(key)

v := reflect.Zero(ef.Type())
af.Set(v)
ef.Set(v)
gf.Set(v)
}
options = append(options, cmpopts.IgnoreFields(stype, settings.mask...))
}

// We need special handling for same parsers as they internally contain pointers
// to other structs that inherently differ between instances
switch format {
case "dropwizard", "grok", "influx", "wavefront":
// At least check if we have the same type
require.IsType(t, expected[i], actual[i])
require.IsType(t, expected[i], generated[i])
continue
}
require.EqualValuesf(t, expected[i], actual[i], "in SetParser() for %q", format)
require.EqualValuesf(t, expected[i], generated[i], "in SetParserFunc() for %q", format)
// Do a manual comparision as require.EqualValues will also work on unexported fields
// that cannot be cleared or ignored.
diff := cmp.Diff(expected[i], actual[i], options...)
require.Emptyf(t, diff, "Difference in SetParser() for %q", format)
diff = cmp.Diff(expected[i], generated[i], options...)
require.Emptyf(t, diff, "Difference in SetParserFunc() for %q", format)
}
}

Expand Down