-
Notifications
You must be signed in to change notification settings - Fork 23
/
client_test.go
251 lines (220 loc) · 7.5 KB
/
client_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package thousandeyes
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
// client is the PagerDuty client being tested.
client *Client
// server is a test HTTP server used to provide mock API responses.
server *httptest.Server
)
func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
var authToken = "foo"
var accountGroup = "bar"
clientOpts := ClientOptions{AuthToken: authToken, AccountID: accountGroup}
client = NewClient(&clientOpts)
}
func teardown() {
server.Close()
}
func Test_ClientAccountGroup(t *testing.T) {
setup()
out := `{"agents": []}`
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo", AccountGroupID: "test"}
mux.HandleFunc("/agents.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "test", r.URL.Query().Get("aid"))
_, _ = w.Write([]byte(out))
})
_, _ = client.GetAgents()
}
func Test_ClientAccountGroupNone(t *testing.T) {
setup()
out := `{"agents": []}`
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/agents.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "", r.URL.Query().Get("aid"))
_, _ = w.Write([]byte(out))
})
_, _ = client.GetAgents()
}
func Test_ClientUserAgentHeader(t *testing.T) {
setup()
// test default user-agent
clientOpts := ClientOptions{
AuthToken: "foo",
APIEndpoint: server.URL,
}
client = NewClient(&clientOpts)
mux.HandleFunc("/agents.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "ThousandEyes Go SDK", r.Header.Get("user-agent"))
_, _ = w.Write([]byte(`{"agents": []}`))
})
_, _ = client.GetAgents()
// test custom user-agent
clientOpts = ClientOptions{
AuthToken: "foo",
APIEndpoint: server.URL,
UserAgent: "porto",
}
client = NewClient(&clientOpts)
mux.HandleFunc("/alert-rules.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "porto", r.Header.Get("user-agent"))
_, _ = w.Write([]byte(`{"alert-rules": []}`))
})
_, _ = client.GetAlertRules()
}
func Test_ClientAPIEndpoint(t *testing.T) {
setup()
// by default if the APIEndpoint field in the client options is not set
// then the client defaults to "https://api.thousandeyes.com/v6"
clientOpts := ClientOptions{
AuthToken: "foo",
AccountID: "bar",
}
client = NewClient(&clientOpts)
assert.Equal(t, defaultAPIEndpoint, client.APIEndpoint)
// test overriding the APIEndpoint field of the client
overrideAPIEndpoint := "https://api.millioneyes.com/v9"
clientOpts = ClientOptions{
AuthToken: "foo",
AccountID: "bar",
APIEndpoint: overrideAPIEndpoint,
}
client = NewClient(&clientOpts)
assert.Equal(t, overrideAPIEndpoint, client.APIEndpoint)
}
func Test_setDelay(t *testing.T) {
setup()
now := time.Now()
var delay time.Duration
var req *http.Request
var resp *http.Response
orgRate = RateLimit{}
instantTestRate = RateLimit{}
req, _ = http.NewRequest("GET", "https://api.thousandeyes.com/v6/agents.json", nil)
resp = &http.Response{}
resp.Header = make(map[string][]string)
// Test initial requests, for which rate limit data is not available
orgRate = RateLimit{}
delay = setDelay(req, nil, now)
assert.Equal(t, time.Duration(0), delay)
// Test subsequent requests with rate limit data
// Old concurrent messages should be purged.
orgRate = RateLimit{
Limit: 240,
Remaining: 100,
Reset: now.Add(30 * time.Second).Unix(),
LastRemaining: 101,
ConcurrentMessages: []time.Time{
now.Add(-1000 * time.Millisecond),
now.Add(-750 * time.Millisecond),
now,
now.Add(250 * time.Millisecond),
},
}
delay = setDelay(req, nil, now)
assert.Equal(t, 750*time.Millisecond, delay)
// All complications from valid state:
orgRate = RateLimit{
Limit: 240,
Remaining: 100,
Reset: now.Add(30 * time.Second).Unix(),
LastRemaining: 104,
ConcurrentMessages: []time.Time{
now.Add(1000 * time.Millisecond),
now.Add(750 * time.Millisecond),
now.Add(500 * time.Millisecond),
now.Add(250 * time.Millisecond),
},
}
instantTestRate = orgRate // Use state to test instant test below
delay = setDelay(req, nil, now)
assert.Equal(t, 2*time.Second, delay)
// Same result should be obtained for an instant test
req, _ = http.NewRequest("GET", "https://api.thousandeyes.com/v6/instant/agent-to-server.json", nil)
delay = setDelay(req, nil, now)
assert.Equal(t, 2*time.Second, delay)
req, _ = http.NewRequest("GET", "https://api.thousandeyes.com/v6/agents.json", nil)
// LastTime over the minimum delay time should result in the minimum delay time if there
// are no concurrent messages and last remaining has not decreased by more than 1.
orgRate.LastRemaining = 101
orgRate.ConcurrentMessages = []time.Time{}
delay = setDelay(req, nil, now)
assert.Equal(t, time.Duration(250*time.Millisecond), delay)
// A passed response means we should delay, as this is presently only done
// in response to a 429
resp.StatusCode = 429
delay = setDelay(req, resp, now)
assert.Equal(t, 31*time.Second, delay)
// Remaining messages being under the minimum should also result in waiting
// until reset
orgRate.Remaining = 1
delay = setDelay(req, nil, now)
assert.Equal(t, 31*time.Second, delay)
// Test conflicting or invalid states
// After reset, LastRemaining may be larger than Remaining
orgRate = RateLimit{
Limit: 240,
Remaining: 240,
Reset: now.Add(30 * time.Second).Unix(),
LastRemaining: 2,
ConcurrentMessages: []time.Time{},
}
delay = setDelay(req, nil, now)
assert.Equal(t, 250*time.Millisecond, delay)
// Delays over one minute should be shortened to one minute
orgRate.Remaining = 0
orgRate.Reset = now.Add(120 * time.Second).Unix()
delay = setDelay(req, nil, now)
assert.Equal(t, 1*time.Minute, delay)
}
func Test_storeLimits(t *testing.T) {
setup()
now := time.Now()
destRate := RateLimit{
Limit: 240,
Remaining: 2,
Reset: 120,
}
var req *http.Request
var resp *http.Response
orgRate = RateLimit{}
instantTestRate = RateLimit{}
req, _ = http.NewRequest("GET", "https://api.thousandeyes.com/v6/agents.json", nil)
resp = &http.Response{}
resp.Header = make(map[string][]string)
resp.Header.Add("X-Organization-Rate-Limit-Limit", "240")
resp.Header.Add("X-Organization-Rate-Limit-Remaining", "2")
resp.Header.Add("X-Organization-Rate-Limit-Reset", "120")
storeLimits(req, resp, now)
assert.Equal(t, destRate, orgRate)
assert.Equal(t, RateLimit{}, instantTestRate)
orgRate = RateLimit{}
instantTestRate = RateLimit{}
req, _ = http.NewRequest("GET", "https://api.thousandeyes.com/v6/instant/agent-to-server.json", nil)
resp = &http.Response{}
resp.Header = make(map[string][]string)
resp.Header.Add("X-Instant-Test-Rate-Limit-Limit", "240")
resp.Header.Add("X-Instant-Test-Rate-Limit-Remaining", "2")
resp.Header.Add("X-Instant-Test-Rate-Limit-Reset", "120")
storeLimits(req, resp, now)
assert.Equal(t, destRate, instantTestRate)
assert.Equal(t, RateLimit{}, orgRate)
}
func Test_isInstantTest(t *testing.T) {
var req *http.Request
req, _ = http.NewRequest("GET", "https://api.thousandeyes.com/v6/instant/agent-to-server.json", nil)
assert.Equal(t, true, isInstantTest(req))
req, _ = http.NewRequest("GET", "https://api.thousandeyes.com/v6/endpoint-instant/agent-to-server.json", nil)
assert.Equal(t, true, isInstantTest(req))
req, _ = http.NewRequest("GET", "https://api.thousandeyes.com/v6/agents.json", nil)
assert.Equal(t, false, isInstantTest(req))
}