test: add more tests (#1147)

* test: add more tests

* test: add more tests
This commit is contained in:
Kevin Wan
2021-10-19 22:37:56 +08:00
committed by GitHub
parent d41163f5c1
commit eab77e21dd
5 changed files with 112 additions and 7 deletions

View File

@@ -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)
})
}
}