Browse Source

test: add more tests (#1147)

* test: add more tests

* test: add more tests
Kevin Wan 3 years ago
parent
commit
eab77e21dd

+ 1 - 1
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))
 }

+ 12 - 0
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))
+}

+ 14 - 4
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")
 }

+ 2 - 2
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 {

+ 83 - 0
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)
+		})
+	}
+}