-
Notifications
You must be signed in to change notification settings - Fork 7
/
handler_test.go
141 lines (113 loc) · 3.5 KB
/
handler_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
133
134
135
136
137
138
139
140
141
package otaws
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/awstesting/mock"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/mocktracer"
"strings"
"testing"
)
func TestNonGlobalTracer(t *testing.T) {
tracer := mocktracer.New()
client := mock.NewMockClient(&aws.Config{
Region: aws.String("us-west-2"),
})
AddOTHandlers(client, WithTracer(tracer))
req := client.NewRequest(&request.Operation{
Name: "Test Operation",
HTTPMethod: "POST",
}, nil, nil)
err := req.Send()
if err != nil {
t.Fatal("Expected request to succeed but failed:", err)
}
spans := tracer.FinishedSpans()
if numSpans := len(spans); numSpans != 1 {
t.Fatalf("Expected 1 span but found %d spans", numSpans)
}
span := spans[0]
if span.OperationName != "Test Operation" {
t.Errorf("Expected span to have operation name 'Test Operation' but was '%s'", span.OperationName)
}
}
// Test requires running local instance of DynamoDB
func TestAWS(t *testing.T) {
tracer := mocktracer.New()
opentracing.InitGlobalTracer(tracer)
client := mock.NewMockClient(&aws.Config{
Region: aws.String("us-west-2"),
})
AddOTHandlers(client)
req := client.NewRequest(&request.Operation{
Name: "Test Operation",
HTTPMethod: "POST",
HTTPPath: "/foo/bar",
}, nil, nil)
err := req.Send()
if err != nil {
t.Fatal("Expected request to succeed but failed:", err)
}
spans := tracer.FinishedSpans()
if numSpans := len(spans); numSpans != 1 {
t.Fatalf("Expected 1 span but found %d spans", numSpans)
}
span := spans[0]
if span.OperationName != "Test Operation" {
t.Errorf("Expected span to have operation name 'Test Operation' but was '%s'", span.OperationName)
}
expectedTags := map[string]interface{}{
"span.kind": ext.SpanKindRPCClientEnum,
"component": "go-aws",
"http.method": "POST",
"http.status_code": uint16(200),
"peer.service": "Mock",
}
for tag, expected := range expectedTags {
if actual := span.Tag(tag); actual != expected {
t.Errorf("Expected tag '%s' to have value '%v' but was '%v'", tag, expected, actual)
}
}
url, ok := span.Tag("http.url").(string)
if !ok {
t.Errorf("Expected span to have tag 'http.url' of type string")
}
if !strings.HasSuffix(url, "/foo/bar") {
t.Error("Expected tag 'http.url' to end with '/foo/bar' but was", url)
}
}
func TestNilResponse(t *testing.T) {
tracer := mocktracer.New()
opentracing.InitGlobalTracer(tracer)
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-2"),
Credentials: credentials.NewCredentials(credentials.ErrorProvider{
Err: errors.New("error credentials for test"),
ProviderName: "test error provider",
}),
})
if err != nil {
t.Fatal("Failed to instantiate session:", err)
}
dbClient := dynamodb.New(sess)
AddOTHandlers(dbClient.Client)
_, err = dbClient.ListTables(&dynamodb.ListTablesInput{})
if err == nil {
t.Fatal("Expected error but request succeeded")
}
spans := tracer.FinishedSpans()
if len(spans) != 1 {
t.Fatalf("Expected 1 span but saw %d spans", len(spans))
}
errTag, ok := spans[0].Tag("error").(bool)
if !ok {
t.Fatal("Expected span to have an 'error' tag of type bool")
} else if errTag != true {
t.Fatal("Expected span's 'error' tag to be true but was false")
}
}