forked from grafadruid/go-druid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
druid_test.go
163 lines (142 loc) · 5.06 KB
/
druid_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package druid
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSetBaseURLWithSuffix(t *testing.T) {
assert := assert.New(t)
d, err := NewClient("localhost:8082")
assert.Nil(err, "error should be nil")
assert.NotNil(d, "client should not be nil")
wantBaseURL, _ := url.ParseRequestURI("/")
err = d.setBaseURL("")
assert.Nil(err, "error should be nil")
assert.Equal(d.baseURL, wantBaseURL, "they should not be equal")
}
func TestNewClientWithSkipVerify(t *testing.T) {
assert := assert.New(t)
var druidOpts []ClientOption
druidOpts = append(druidOpts, WithSkipTLSVerify())
d, err := NewClient("localhost:8082", druidOpts...)
assert.Nil(err, "error should be nil")
assert.NotNil(d, "client should not be nil")
assert.True(d.http.HTTPClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, "insecure skip verify should be true")
}
// TODO: at some point use https://golang.org/src/crypto/tls/example_test.go this to create server with bad cert and test
func TestDefaultRetry(t *testing.T) {
ctx := context.TODO()
var b string
var expectedErr error
resp := buildMockResp(200, b)
retry, err := defaultRetry(ctx, &resp, nil)
assert.Nil(t, err)
assert.False(t, retry)
b = `{
"error": "SQL parse failed", "errorMessage" : "Something bad happened."
}`
expectedErr = fmt.Errorf("failed to query Druid: {Error:SQL parse failed ErrorMessage:Something bad happened. ErrorClass: Host:}")
resp = buildMockResp(400, b)
retry, err = defaultRetry(ctx, &resp, nil)
assert.NotNil(t, err)
assert.Equal(t, expectedErr, err)
assert.False(t, retry)
b = `{
"error": "Plan validation failed", "errorMessage" : "Something bad happened."
}`
expectedErr = fmt.Errorf("failed to query Druid: {Error:Plan validation failed ErrorMessage:Something bad happened. ErrorClass: Host:}")
resp = buildMockResp(400, b)
retry, err = defaultRetry(ctx, &resp, nil)
assert.NotNil(t, err)
assert.Equal(t, expectedErr, err)
assert.False(t, retry)
b = `{
"error": "Resource limit exceeded", "errorMessage" : "Something bad happened."
}`
expectedErr = fmt.Errorf("error response from Druid: {Error:Resource limit exceeded ErrorMessage:Something bad happened. ErrorClass: Host:}")
resp = buildMockResp(400, b)
retry, err = defaultRetry(ctx, &resp, nil)
assert.NotNil(t, err)
assert.Equal(t, expectedErr, err)
assert.True(t, retry)
b = `{
"error": "Query capacity exceeded", "errorMessage" : "Something bad happened."
}`
expectedErr = fmt.Errorf("error response from Druid: {Error:Query capacity exceeded ErrorMessage:Something bad happened. ErrorClass: Host:}")
resp = buildMockResp(429, b)
retry, err = defaultRetry(ctx, &resp, nil)
assert.NotNil(t, err)
assert.Equal(t, expectedErr, err)
assert.True(t, retry)
b = `{
"error": "Unsupported operation", "errorMessage" : "Something bad happened."
}`
expectedErr = fmt.Errorf("failed to query Druid: {Error:Unsupported operation ErrorMessage:Something bad happened. ErrorClass: Host:}")
resp = buildMockResp(501, b)
retry, err = defaultRetry(ctx, &resp, nil)
assert.NotNil(t, err)
assert.Equal(t, expectedErr, err)
assert.False(t, retry)
b = `{
"error": "Query timeout", "errorMessage" : "Something bad happened."
}`
expectedErr = fmt.Errorf("error response from Druid: {Error:Query timeout ErrorMessage:Something bad happened. ErrorClass: Host:}")
resp = buildMockResp(504, b)
retry, err = defaultRetry(ctx, &resp, nil)
assert.NotNil(t, err)
assert.Equal(t, expectedErr, err)
assert.True(t, retry)
b = `{
"error": "Query cancelled", "errorMessage" : "Something bad happened."
}`
expectedErr = fmt.Errorf("failed to query Druid: {Error:Query cancelled ErrorMessage:Something bad happened. ErrorClass: Host:}")
resp = buildMockResp(500, b)
retry, err = defaultRetry(ctx, &resp, nil)
assert.NotNil(t, err)
assert.Equal(t, expectedErr, err)
assert.False(t, retry)
b = `{
"error": "Unknown exception", "errorMessage" : "Something bad happened."
}`
expectedErr = fmt.Errorf("failed to query Druid: {Error:Unknown exception ErrorMessage:Something bad happened. ErrorClass: Host:}")
resp = buildMockResp(500, b)
retry, err = defaultRetry(ctx, &resp, nil)
assert.NotNil(t, err)
assert.Equal(t, expectedErr, err)
assert.False(t, retry)
b = `invalid json`
expectedErr = fmt.Errorf("failed to read the response from Druid: invalid character 'i' looking for beginning of value")
resp = buildMockResp(500, b)
retry, err = defaultRetry(ctx, &resp, nil)
assert.NotNil(t, err)
assert.Equal(t, expectedErr.Error(), err.Error())
assert.True(t, retry)
}
func buildMockResp(statusCode int, body string) http.Response {
var st string
switch statusCode {
case 200:
st = "200 OK"
case 400:
st = "400 Bad Request"
case 429:
st = "429 Too Many Requests"
case 500:
st = "500 Internal Server Error"
case 501:
st = "Not Implemented"
case 504:
st = "Gateway Timeout"
default:
panic(fmt.Errorf("Unsupported mock status code: %d", statusCode))
}
return http.Response{
Status: st, StatusCode: statusCode,
Body: ioutil.NopCloser(strings.NewReader(body)),
}
}