From eab77e21dd96dd3533ab0f5fc3a26368f0a58caf Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 19 Oct 2021 22:37:56 +0800 Subject: [PATCH] test: add more tests (#1147) * test: add more tests * test: add more tests --- core/trace/attributes.go | 2 +- core/trace/attributes_test.go | 12 +++++ core/trace/tracer_test.go | 18 ++++++-- core/trace/utils.go | 4 +- core/trace/utils_test.go | 83 +++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 core/trace/attributes_test.go diff --git a/core/trace/attributes.go b/core/trace/attributes.go index a0c9dfd3..6da2e682 100644 --- a/core/trace/attributes.go +++ b/core/trace/attributes.go @@ -34,7 +34,7 @@ var ( RPCMessageTypeReceived = RPCMessageTypeKey.String("RECEIVED") ) -// StatusCodeAttr returns a attribute.KeyValue that represents the give c. +// StatusCodeAttr returns an attribute.KeyValue that represents the give c. func StatusCodeAttr(c gcodes.Code) attribute.KeyValue { return GRPCStatusCodeKey.Int64(int64(c)) } diff --git a/core/trace/attributes_test.go b/core/trace/attributes_test.go new file mode 100644 index 00000000..67c0097c --- /dev/null +++ b/core/trace/attributes_test.go @@ -0,0 +1,12 @@ +package trace + +import ( + "testing" + + "github.com/stretchr/testify/assert" + gcodes "google.golang.org/grpc/codes" +) + +func TestStatusCodeAttr(t *testing.T) { + assert.Equal(t, GRPCStatusCodeKey.Int(int(gcodes.DataLoss)), StatusCodeAttr(gcodes.DataLoss)) +} diff --git a/core/trace/tracer_test.go b/core/trace/tracer_test.go index 8f0085ff..bb77d384 100644 --- a/core/trace/tracer_test.go +++ b/core/trace/tracer_test.go @@ -157,7 +157,8 @@ func TestExtractValidTraceContext(t *testing.T) { }), }, } - otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( + propagation.TraceContext{}, propagation.Baggage{})) propagator := otel.GetTextMapPropagator() for _, tt := range tests { @@ -242,7 +243,8 @@ func TestExtractInvalidTraceContext(t *testing.T) { header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-", }, } - otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( + propagation.TraceContext{}, propagation.Baggage{})) propagator := otel.GetTextMapPropagator() for _, tt := range tests { @@ -308,7 +310,8 @@ func TestInjectValidTraceContext(t *testing.T) { }), }, } - otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( + propagation.TraceContext{}, propagation.Baggage{})) propagator := otel.GetTextMapPropagator() for _, tt := range tests { @@ -325,6 +328,11 @@ func TestInjectValidTraceContext(t *testing.T) { md := metadata.MD{} Inject(ctx, propagator, &md) assert.Equal(t, want, md) + + mm := &metadataSupplier{ + metadata: &md, + } + assert.NotEmpty(t, mm.Keys()) }) } } @@ -334,7 +342,8 @@ func TestInvalidSpanContextDropped(t *testing.T) { require.False(t, invalidSC.IsValid()) ctx := trace.ContextWithRemoteSpanContext(context.Background(), invalidSC) - otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( + propagation.TraceContext{}, propagation.Baggage{})) propagator := otel.GetTextMapPropagator() md := metadata.MD{} @@ -342,5 +351,6 @@ func TestInvalidSpanContextDropped(t *testing.T) { mm := &metadataSupplier{ metadata: &md, } + assert.Empty(t, mm.Keys()) assert.Equal(t, "", mm.Get("traceparent"), "injected invalid SpanContext") } diff --git a/core/trace/utils.go b/core/trace/utils.go index 9477375e..b039f875 100644 --- a/core/trace/utils.go +++ b/core/trace/utils.go @@ -15,7 +15,7 @@ const localhost = "127.0.0.1" // PeerFromCtx returns the peer from ctx. func PeerFromCtx(ctx context.Context) string { p, ok := peer.FromContext(ctx) - if !ok { + if !ok || p == nil { return "" } @@ -55,7 +55,7 @@ func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue) { func PeerAttr(addr string) []attribute.KeyValue { host, port, err := net.SplitHostPort(addr) if err != nil { - return []attribute.KeyValue(nil) + return nil } if len(host) == 0 { diff --git a/core/trace/utils_test.go b/core/trace/utils_test.go index e6bb9590..d5edeeb1 100644 --- a/core/trace/utils_test.go +++ b/core/trace/utils_test.go @@ -1,13 +1,53 @@ package trace import ( + "context" + "net" "testing" "github.com/stretchr/testify/assert" "go.opentelemetry.io/otel/attribute" semconv "go.opentelemetry.io/otel/semconv/v1.4.0" + "google.golang.org/grpc/peer" ) +func TestPeerFromContext(t *testing.T) { + addrs, err := net.InterfaceAddrs() + assert.Nil(t, err) + assert.NotEmpty(t, addrs) + tests := []struct { + name string + ctx context.Context + empty bool + }{ + { + name: "empty", + ctx: context.Background(), + empty: true, + }, + { + name: "nil", + ctx: peer.NewContext(context.Background(), nil), + empty: true, + }, + { + name: "with value", + ctx: peer.NewContext(context.Background(), &peer.Peer{ + Addr: addrs[0], + }), + }, + } + + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + t.Parallel() + addr := PeerFromCtx(test.ctx) + assert.Equal(t, test.empty, len(addr) == 0) + }) + } +} + func TestParseFullMethod(t *testing.T) { tests := []struct { fullMethod string @@ -68,3 +108,46 @@ func TestParseFullMethod(t *testing.T) { assert.Equal(t, test.attr, a) } } + +func TestSpanInfo(t *testing.T) { + val, kvs := SpanInfo("/fullMethod", "remote") + assert.Equal(t, "fullMethod", val) + assert.NotEmpty(t, kvs) +} + +func TestPeerAttr(t *testing.T) { + tests := []struct { + name string + addr string + expect []attribute.KeyValue + }{ + { + name: "empty", + }, + { + name: "port only", + addr: ":8080", + expect: []attribute.KeyValue{ + semconv.NetPeerIPKey.String(localhost), + semconv.NetPeerPortKey.String("8080"), + }, + }, + { + name: "port only", + addr: "192.168.0.2:8080", + expect: []attribute.KeyValue{ + semconv.NetPeerIPKey.String("192.168.0.2"), + semconv.NetPeerPortKey.String("8080"), + }, + }, + } + + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + t.Parallel() + kvs := PeerAttr(test.addr) + assert.EqualValues(t, test.expect, kvs) + }) + } +}