-
Notifications
You must be signed in to change notification settings - Fork 144
/
opentracing_test.go
132 lines (114 loc) · 3.77 KB
/
opentracing_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package client
import (
"bytes"
"context"
"io"
"testing"
"github.com/go-openapi/strfmt"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-openapi/runtime"
)
type tres struct {
}
func (r tres) Code() int {
return 490
}
func (r tres) Message() string {
return "the message"
}
func (r tres) GetHeader(_ string) string {
return "the header"
}
func (r tres) GetHeaders(_ string) []string {
return []string{"the headers", "the headers2"}
}
func (r tres) Body() io.ReadCloser {
return io.NopCloser(bytes.NewBufferString("the content"))
}
type mockRuntime struct {
req runtime.TestClientRequest
}
func (m *mockRuntime) Submit(operation *runtime.ClientOperation) (interface{}, error) {
_ = operation.Params.WriteToRequest(&m.req, nil)
_, _ = operation.Reader.ReadResponse(&tres{}, nil)
return map[string]interface{}{}, nil
}
func testOperation(ctx context.Context) *runtime.ClientOperation {
return &runtime.ClientOperation{
ID: "getCluster",
Method: "GET",
PathPattern: "/kubernetes-clusters/{cluster_id}",
ProducesMediaTypes: []string{"application/json"},
ConsumesMediaTypes: []string{"application/json"},
Schemes: []string{schemeHTTPS},
Reader: runtime.ClientResponseReaderFunc(func(runtime.ClientResponse, runtime.Consumer) (interface{}, error) {
return nil, nil
}),
Params: runtime.ClientRequestWriterFunc(func(req runtime.ClientRequest, reg strfmt.Registry) error {
return nil
}),
AuthInfo: PassThroughAuth,
Context: ctx,
}
}
func Test_TracingRuntime_submit(t *testing.T) {
t.Parallel()
tracer := mocktracer.New()
_, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "op")
testSubmit(t, testOperation(ctx), tracer, 1)
}
func Test_TracingRuntime_submit_nilAuthInfo(t *testing.T) {
t.Parallel()
tracer := mocktracer.New()
_, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "op")
operation := testOperation(ctx)
operation.AuthInfo = nil
testSubmit(t, operation, tracer, 1)
}
func Test_TracingRuntime_submit_nilContext(t *testing.T) {
t.Parallel()
tracer := mocktracer.New()
_, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "op")
operation := testOperation(ctx)
operation.Context = nil
testSubmit(t, operation, tracer, 0) // just don't panic
}
func testSubmit(t *testing.T, operation *runtime.ClientOperation, tracer *mocktracer.MockTracer, expectedSpans int) {
header := map[string][]string{}
r := newOpenTracingTransport(&mockRuntime{runtime.TestClientRequest{Headers: header}},
"remote_host",
[]opentracing.StartSpanOption{opentracing.Tag{
Key: string(ext.PeerService),
Value: "service",
}})
_, err := r.Submit(operation)
require.NoError(t, err)
assert.Len(t, tracer.FinishedSpans(), expectedSpans)
if expectedSpans == 1 {
span := tracer.FinishedSpans()[0]
assert.Equal(t, "getCluster", span.OperationName)
assert.Equal(t, map[string]interface{}{
"component": "go-openapi",
"http.method": "GET",
"http.path": "/kubernetes-clusters/{cluster_id}",
"http.status_code": uint16(490),
"peer.hostname": "remote_host",
"peer.service": "service",
"span.kind": ext.SpanKindRPCClientEnum,
"error": true,
}, span.Tags())
}
}
func Test_injectSpanContext(t *testing.T) {
t.Parallel()
tracer := mocktracer.New()
_, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "op")
header := map[string][]string{}
createClientSpan(testOperation(ctx), header, "", nil)
// values are random - just check that something was injected
assert.Len(t, header, 3)
}