Skip to content

Commit

Permalink
Replace reflection-based struct assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
axw authored and kruskall committed Jun 15, 2023
1 parent f3e81f5 commit f5f5a16
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/elastic/apm-data/input/elasticapm/internal/modeldecoder/nullable"
"github.com/elastic/apm-data/model"
Expand Down Expand Up @@ -131,7 +132,7 @@ func SetStructValues(in interface{}, values *Values, opts ...SetStructValuesOpti
switch fKind := f.Kind(); fKind {
case reflect.String:
fieldVal = reflect.ValueOf(values.Str)
case reflect.Int, reflect.Int64:
case reflect.Int, reflect.Int32, reflect.Int64:
fieldVal = reflect.ValueOf(values.Int).Convert(f.Type())
case reflect.Float64:
fieldVal = reflect.ValueOf(values.Float).Convert(f.Type())
Expand Down Expand Up @@ -265,10 +266,6 @@ func SetZeroStructValue(i interface{}, callback func(string)) {
// that values are equal to expected values
func AssertStructValues(t *testing.T, i interface{}, isException func(string) bool,
values *Values) {
if true {
// TODO FIX no op protobuf support
return
}
IterateStruct(i, func(f reflect.Value, key string) {
if isException(key) {
return
Expand Down Expand Up @@ -308,7 +305,7 @@ func AssertStructValues(t *testing.T, i interface{}, isException func(string) bo
case *int:
newVal = &values.Int
case int32:
newVal = values.Int
newVal = int32(values.Int)
case uint32:
newVal = uint32(values.Int)
case *uint32:
Expand All @@ -327,8 +324,8 @@ func AssertStructValues(t *testing.T, i interface{}, isException func(string) bo
newVal = &values.Bool
case http.Header:
newVal = values.HTTPHeader
case time.Time:
newVal = values.Time
case *timestamppb.Timestamp:
newVal = timestamppb.New(values.Time)
case time.Duration:
newVal = values.Duration
case modelpb.MetricType:
Expand Down
13 changes: 10 additions & 3 deletions input/elasticapm/internal/modeldecoder/rumv3/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"

"github.com/elastic/apm-data/input/elasticapm/internal/decoder"
"github.com/elastic/apm-data/input/elasticapm/internal/modeldecoder"
Expand Down Expand Up @@ -54,9 +56,14 @@ func TestDecodeNestedError(t *testing.T) {
require.NoError(t, DecodeNestedError(dec, &input, &batch))
require.Len(t, batch, 1)
require.NotNil(t, batch[0].Error)
defaultValues := modeldecodertest.DefaultValues()
defaultValues.Update(time.Unix(1599996822, 281000000).UTC())
modeldecodertest.AssertStructValues(t, &batch[0], metadataExceptions(), defaultValues)
assert.Equal(t, time.Unix(1599996822, 281000000).UTC(), batch[0].Timestamp.AsTime())
assert.Empty(t, cmp.Diff(&modelpb.Error{
Id: "a-b-c",
Log: &modelpb.ErrorLog{
Message: "abc",
LoggerName: "default",
},
}, batch[0].Error, protocmp.Transform()))

// if no timestamp is provided, leave base event timestamp unmodified
input = modeldecoder.Input{Base: eventBase}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,6 @@ func TestDecodeNestedTransaction(t *testing.T) {
assert.Equal(t, "1", batch[2].Trace.Id)
assert.Equal(t, "100", batch[2].Parent.Id)

for _, event := range batch {
modeldecodertest.AssertStructValues(
t, &event,
metadataExceptions("Timestamp"), // timestamp checked above
modeldecodertest.DefaultValues(),
)
}

err := DecodeNestedTransaction(decoder.NewJSONDecoder(strings.NewReader(`malformed`)), &input, &batch)
require.Error(t, err)
assert.Contains(t, err.Error(), "decode")
Expand Down
10 changes: 7 additions & 3 deletions input/elasticapm/internal/modeldecoder/v2/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"

"github.com/elastic/apm-data/input/elasticapm/internal/decoder"
"github.com/elastic/apm-data/input/elasticapm/internal/modeldecoder"
Expand Down Expand Up @@ -56,9 +58,11 @@ func TestDecodeNestedError(t *testing.T) {
require.NoError(t, DecodeNestedError(dec, &input, &batch))
require.Len(t, batch, 1)
require.NotNil(t, batch[0].Error)

defaultVal.Update(time.Unix(1599996822, 281000000).UTC())
modeldecodertest.AssertStructValues(t, &batch[0], isMetadataException, defaultVal)
assert.Equal(t, time.Unix(1599996822, 281000000).UTC(), batch[0].Timestamp.AsTime())
assert.Empty(t, cmp.Diff(&modelpb.Error{
Id: "a-b-c",
Log: &modelpb.ErrorLog{Message: "abc"},
}, batch[0].Error, protocmp.Transform()))

str = `{"error":{"id":"a-b-c","log":{"message":"abc"},"context":{"experimental":"exp"}}}`
dec = decoder.NewJSONDecoder(strings.NewReader(str))
Expand Down
11 changes: 8 additions & 3 deletions input/elasticapm/internal/modeldecoder/v2/metricset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ func TestDecodeNestedMetricset(t *testing.T) {
require.NoError(t, DecodeNestedMetricset(dec, &input, &batch))
require.Len(t, batch, 1)
require.NotNil(t, batch[0].Metricset)
assert.Equal(t, []*modelpb.MetricsetSample{{Name: "a.b", Value: 2048}}, batch[0].Metricset.Samples)
defaultVal.Update(time.Unix(1599996822, 281000000).UTC())
modeldecodertest.AssertStructValues(t, &batch[0], isMetadataException, defaultVal)
assert.Equal(t, time.Unix(1599996822, 281000000).UTC(), batch[0].Timestamp.AsTime())
assert.Empty(t, cmp.Diff(&modelpb.Metricset{
Name: "app",
Samples: []*modelpb.MetricsetSample{{
Name: "a.b",
Value: 2048,
}},
}, batch[0].Metricset, protocmp.Transform()))

// invalid type
err := DecodeNestedMetricset(decoder.NewJSONDecoder(strings.NewReader(`malformed`)), &input, &batch)
Expand Down
14 changes: 12 additions & 2 deletions input/elasticapm/internal/modeldecoder/v2/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/testing/protocmp"

"github.com/elastic/apm-data/input/elasticapm/internal/decoder"
"github.com/elastic/apm-data/input/elasticapm/internal/modeldecoder"
Expand Down Expand Up @@ -57,8 +59,16 @@ func TestDecodeNestedSpan(t *testing.T) {
require.NoError(t, DecodeNestedSpan(dec, &input, &batch))
require.Len(t, batch, 1)
require.NotNil(t, batch[0].Span)
defaultVal.Update(time.Time{}.Add(143 * time.Millisecond))
modeldecodertest.AssertStructValues(t, &batch[0], isMetadataException, defaultVal)
assert.Equal(t, time.Time{}.Add(143*time.Millisecond), batch[0].Timestamp.AsTime())
assert.Equal(t, 100*time.Millisecond, batch[0].Event.Duration.AsDuration())
assert.Equal(t, &modelpb.Parent{Id: "parent-123"}, batch[0].Parent, protocmp.Transform())
assert.Equal(t, &modelpb.Trace{Id: "trace-ab"}, batch[0].Trace, protocmp.Transform())
assert.Empty(t, cmp.Diff(&modelpb.Span{
Name: "s",
Type: "db",
Id: "a-b-c",
RepresentativeCount: 1,
}, batch[0].Span, protocmp.Transform()))

err := DecodeNestedSpan(decoder.NewJSONDecoder(strings.NewReader(`malformed`)), &input, &batch)
require.Error(t, err)
Expand Down

0 comments on commit f5f5a16

Please sign in to comment.