Skip to content

Commit

Permalink
Fix small nits in pdata tests, add missing compatibility tests (#4931)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Mar 1, 2022
1 parent 5d0a8dd commit 4493a5e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
58 changes: 47 additions & 11 deletions model/pdata/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,37 @@ package pdata
import (
"testing"

gogoproto "github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
goproto "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"

"go.opentelemetry.io/collector/model/internal"
otlplogs "go.opentelemetry.io/collector/model/internal/data/protogen/logs/v1"
)

func TestLogRecordCount(t *testing.T) {
md := NewLogs()
assert.EqualValues(t, 0, md.LogRecordCount())
logs := NewLogs()
assert.EqualValues(t, 0, logs.LogRecordCount())

rl := md.ResourceLogs().AppendEmpty()
assert.EqualValues(t, 0, md.LogRecordCount())
rl := logs.ResourceLogs().AppendEmpty()
assert.EqualValues(t, 0, logs.LogRecordCount())

ill := rl.InstrumentationLibraryLogs().AppendEmpty()
assert.EqualValues(t, 0, md.LogRecordCount())
assert.EqualValues(t, 0, logs.LogRecordCount())

ill.LogRecords().AppendEmpty()
assert.EqualValues(t, 1, md.LogRecordCount())
assert.EqualValues(t, 1, logs.LogRecordCount())

rms := md.ResourceLogs()
rms := logs.ResourceLogs()
rms.EnsureCapacity(3)
rms.AppendEmpty().InstrumentationLibraryLogs().AppendEmpty()
illl := rms.AppendEmpty().InstrumentationLibraryLogs().AppendEmpty().LogRecords()
for i := 0; i < 5; i++ {
illl.AppendEmpty()
}
// 5 + 1 (from rms.At(0) initialized first)
assert.EqualValues(t, 6, md.LogRecordCount())
assert.EqualValues(t, 6, logs.LogRecordCount())
}

func TestLogRecordCountWithEmpty(t *testing.T) {
Expand Down Expand Up @@ -74,9 +77,42 @@ func TestLogRecordCountWithEmpty(t *testing.T) {

func TestToFromLogProto(t *testing.T) {
wrapper := internal.LogsFromOtlp(&otlplogs.LogsData{})
ld := LogsFromInternalRep(wrapper)
assert.EqualValues(t, NewLogs(), ld)
assert.EqualValues(t, &otlplogs.LogsData{}, ld.orig)
logs := LogsFromInternalRep(wrapper)
assert.EqualValues(t, NewLogs(), logs)
assert.EqualValues(t, &otlplogs.LogsData{}, logs.orig)
}

func TestResourceLogsWireCompatibility(t *testing.T) {
// This test verifies that OTLP ProtoBufs generated using goproto lib in
// opentelemetry-proto repository OTLP ProtoBufs generated using gogoproto lib in
// this repository are wire compatible.

// Generate ResourceLogs as pdata struct.
logs := generateTestResourceLogs()

// Marshal its underlying ProtoBuf to wire.
wire1, err := gogoproto.Marshal(logs.orig)
assert.NoError(t, err)
assert.NotNil(t, wire1)

// Unmarshal from the wire to OTLP Protobuf in goproto's representation.
var goprotoMessage emptypb.Empty
err = goproto.Unmarshal(wire1, &goprotoMessage)
assert.NoError(t, err)

// Marshal to the wire again.
wire2, err := goproto.Marshal(&goprotoMessage)
assert.NoError(t, err)
assert.NotNil(t, wire2)

// Unmarshal from the wire into gogoproto's representation.
var gogoprotoRS2 otlplogs.ResourceLogs
err = gogoproto.Unmarshal(wire2, &gogoprotoRS2)
assert.NoError(t, err)

// Now compare that the original and final ProtoBuf messages are the same.
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible.
assert.EqualValues(t, logs.orig, &gogoprotoRS2)
}

func TestLogsClone(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions model/pdata/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func TestResourceMetricsWireCompatibility(t *testing.T) {
// this repository are wire compatible.

// Generate ResourceMetrics as pdata struct.
pdataRM := generateTestResourceMetrics()
metrics := generateTestResourceMetrics()

// Marshal its underlying ProtoBuf to wire.
wire1, err := gogoproto.Marshal(pdataRM.orig)
wire1, err := gogoproto.Marshal(metrics.orig)
assert.NoError(t, err)
assert.NotNil(t, wire1)

Expand All @@ -80,7 +80,7 @@ func TestResourceMetricsWireCompatibility(t *testing.T) {

// Now compare that the original and final ProtoBuf messages are the same.
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible.
assert.True(t, assert.EqualValues(t, pdataRM.orig, &gogoprotoRM))
assert.True(t, assert.EqualValues(t, metrics.orig, &gogoprotoRM))
}

func TestMetricCount(t *testing.T) {
Expand Down
28 changes: 14 additions & 14 deletions model/pdata/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ import (
)

func TestSpanCount(t *testing.T) {
md := NewTraces()
assert.EqualValues(t, 0, md.SpanCount())
traces := NewTraces()
assert.EqualValues(t, 0, traces.SpanCount())

rs := md.ResourceSpans().AppendEmpty()
assert.EqualValues(t, 0, md.SpanCount())
rs := traces.ResourceSpans().AppendEmpty()
assert.EqualValues(t, 0, traces.SpanCount())

ils := rs.InstrumentationLibrarySpans().AppendEmpty()
assert.EqualValues(t, 0, md.SpanCount())
assert.EqualValues(t, 0, traces.SpanCount())

ils.Spans().AppendEmpty()
assert.EqualValues(t, 1, md.SpanCount())
assert.EqualValues(t, 1, traces.SpanCount())

rms := md.ResourceSpans()
rms := traces.ResourceSpans()
rms.EnsureCapacity(3)
rms.AppendEmpty().InstrumentationLibrarySpans().AppendEmpty()
ilss := rms.AppendEmpty().InstrumentationLibrarySpans().AppendEmpty().Spans()
for i := 0; i < 5; i++ {
ilss.AppendEmpty()
}
// 5 + 1 (from rms.At(0) initialized first)
assert.EqualValues(t, 6, md.SpanCount())
assert.EqualValues(t, 6, traces.SpanCount())
}

func TestSpanCountWithEmpty(t *testing.T) {
Expand Down Expand Up @@ -76,9 +76,9 @@ func TestSpanCountWithEmpty(t *testing.T) {

func TestToFromOtlp(t *testing.T) {
otlp := &otlptrace.TracesData{}
td := TracesFromInternalRep(internal.TracesFromOtlp(otlp))
assert.EqualValues(t, NewTraces(), td)
assert.EqualValues(t, otlp, internal.TracesToOtlp(td.InternalRep()))
traces := TracesFromInternalRep(internal.TracesFromOtlp(otlp))
assert.EqualValues(t, NewTraces(), traces)
assert.EqualValues(t, otlp, internal.TracesToOtlp(traces.InternalRep()))
// More tests in ./tracedata/traces_test.go. Cannot have them here because of
// circular dependency.
}
Expand All @@ -89,10 +89,10 @@ func TestResourceSpansWireCompatibility(t *testing.T) {
// this repository are wire compatible.

// Generate ResourceSpans as pdata struct.
pdataRS := generateTestResourceSpans()
traces := generateTestResourceSpans()

// Marshal its underlying ProtoBuf to wire.
wire1, err := gogoproto.Marshal(pdataRS.orig)
wire1, err := gogoproto.Marshal(traces.orig)
assert.NoError(t, err)
assert.NotNil(t, wire1)

Expand All @@ -113,7 +113,7 @@ func TestResourceSpansWireCompatibility(t *testing.T) {

// Now compare that the original and final ProtoBuf messages are the same.
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible.
assert.EqualValues(t, pdataRS.orig, &gogoprotoRS2)
assert.EqualValues(t, traces.orig, &gogoprotoRS2)
}

func TestTracesClone(t *testing.T) {
Expand Down

0 comments on commit 4493a5e

Please sign in to comment.