| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/google/go-cmp/cmp" |
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
) |
| 14 |
|
| 15 |
func TestValidateSpanKind(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
in SpanKind |
| 18 |
want SpanKind |
| 19 |
}{ |
| 20 |
{ |
| 21 |
SpanKindUnspecified, |
| 22 |
SpanKindInternal, |
| 23 |
}, |
| 24 |
{ |
| 25 |
SpanKindInternal, |
| 26 |
SpanKindInternal, |
| 27 |
}, |
| 28 |
{ |
| 29 |
SpanKindServer, |
| 30 |
SpanKindServer, |
| 31 |
}, |
| 32 |
{ |
| 33 |
SpanKindClient, |
| 34 |
SpanKindClient, |
| 35 |
}, |
| 36 |
{ |
| 37 |
SpanKindProducer, |
| 38 |
SpanKindProducer, |
| 39 |
}, |
| 40 |
{ |
| 41 |
SpanKindConsumer, |
| 42 |
SpanKindConsumer, |
| 43 |
}, |
| 44 |
} |
| 45 |
for _, test := range tests { |
| 46 |
if got := ValidateSpanKind(test.in); got != test.want { |
| 47 |
t.Errorf("ValidateSpanKind(%#v) = %#v, want %#v", test.in, got, test.want) |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestSpanKindString(t *testing.T) { |
| 53 |
tests := []struct { |
| 54 |
in SpanKind |
| 55 |
want string |
| 56 |
}{ |
| 57 |
{ |
| 58 |
SpanKindUnspecified, |
| 59 |
"unspecified", |
| 60 |
}, |
| 61 |
{ |
| 62 |
SpanKindInternal, |
| 63 |
"internal", |
| 64 |
}, |
| 65 |
{ |
| 66 |
SpanKindServer, |
| 67 |
"server", |
| 68 |
}, |
| 69 |
{ |
| 70 |
SpanKindClient, |
| 71 |
"client", |
| 72 |
}, |
| 73 |
{ |
| 74 |
SpanKindProducer, |
| 75 |
"producer", |
| 76 |
}, |
| 77 |
{ |
| 78 |
SpanKindConsumer, |
| 79 |
"consumer", |
| 80 |
}, |
| 81 |
} |
| 82 |
for _, test := range tests { |
| 83 |
if got := test.in.String(); got != test.want { |
| 84 |
t.Errorf("%#v.String() = %#v, want %#v", test.in, got, test.want) |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestLinkFromContext(t *testing.T) { |
| 90 |
k1v1 := attribute.String("key1", "value1") |
| 91 |
spanCtx := SpanContext{traceID: TraceID([16]byte{1}), remote: true} |
| 92 |
|
| 93 |
receiverCtx := ContextWithRemoteSpanContext(t.Context(), spanCtx) |
| 94 |
link := LinkFromContext(receiverCtx, k1v1) |
| 95 |
|
| 96 |
if !assertSpanContextEqual(link.SpanContext, spanCtx) { |
| 97 |
t.Fatalf("LinkFromContext: Unexpected context created: %s", cmp.Diff(link.SpanContext, spanCtx)) |
| 98 |
} |
| 99 |
assert.Equal(t, link.Attributes[0], k1v1) |
| 100 |
} |