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