/
instrumentation_tests.rb
335 lines (280 loc) · 8.63 KB
/
instrumentation_tests.rb
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
require 'active_support'
require 'securerandom'
class SimpleInstrumentor
class << self
attr_accessor :events, :blocks
def instrument(name, params = {}, &block)
@events << name
@blocks << name if block_given?
yield if block_given?
end
def reset!
@events = []
@blocks = []
end
end
end
Shindo.tests('Excon instrumentation') do
after do
ActiveSupport::Notifications.unsubscribe("excon")
ActiveSupport::Notifications.unsubscribe("excon.request")
ActiveSupport::Notifications.unsubscribe("excon.response")
ActiveSupport::Notifications.unsubscribe("excon.retry")
ActiveSupport::Notifications.unsubscribe("excon.error")
ActiveSupport::Notifications.unsubscribe("gug")
ActiveSupport::Notifications.unsubscribe("gug.request")
ActiveSupport::Notifications.unsubscribe("gug.response")
ActiveSupport::Notifications.unsubscribe("gug.retry")
ActiveSupport::Notifications.unsubscribe("gug.error")
Delorean.back_to_the_present
Excon.stubs.clear
end
before do
SimpleInstrumentor.reset!
end
def subscribe(match)
@events = []
ActiveSupport::Notifications.subscribe(match) do |*args|
@events << ActiveSupport::Notifications::Event.new(*args)
end
end
def make_request(params = {})
connection = Excon.new(
'http://127.0.0.1:9292',
:instrumentor => ActiveSupport::Notifications,
:mock => true
)
connection.get(params)
end
REQUEST_DELAY_SECONDS = 30
def stub_success
Excon.stub({:method => :get}) { |params|
Delorean.jump REQUEST_DELAY_SECONDS
{:body => params[:body], :headers => params[:headers], :status => 200}
}
end
def stub_retries
run_count = 0
Excon.stub({:method => :get}) { |params|
run_count += 1
if run_count <= 3 # First 3 calls fail.
raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
else
{:body => params[:body], :headers => params[:headers], :status => 200}
end
}
end
def stub_failure
Excon.stub({:method => :get}) { |params|
raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
}
end
tests('basic notification').returns(['excon.request', 'excon.response']) do
subscribe(/excon/)
stub_success
make_request
@events.map(&:name)
end
tests('captures scheme, host, port, and path').returns([:host, :path, :port, :scheme]) do
subscribe(/excon/)
stub_success
make_request
[:host, :path, :port, :scheme].select {|k| @events.first.payload.has_key? k}
end
tests('params in request overwrite those in constructor').returns('/cheezburger') do
subscribe(/excon/)
stub_success
make_request(:path => '/cheezburger')
@events.first.payload[:path]
end
tests('notify on retry').returns(3) do
subscribe(/excon/)
stub_retries
make_request(idempotent: true)
@events.count{|e| e.name =~ /retry/}
end
tests('notify on error').returns(true) do
subscribe(/excon/)
stub_failure
raises(Excon::Errors::SocketError) do
make_request
end
@events.any?{|e| e.name =~ /error/}
end
tests('filtering').returns(['excon.request', 'excon.error']) do
subscribe(/excon.request/)
subscribe(/excon.error/)
stub_failure
raises(Excon::Errors::SocketError) do
make_request(idempotent: true)
end
@events.map(&:name)
end
tests('more filtering').returns(['excon.retry', 'excon.retry', 'excon.retry']) do
subscribe(/excon.retry/)
stub_failure
raises(Excon::Errors::SocketError) do
make_request(idempotent: true)
end
@events.map(&:name)
end
tests('indicates duration').returns(true) do
subscribe(/excon/)
stub_success
make_request
(@events.first.duration/1000 - REQUEST_DELAY_SECONDS).abs < 1
end
tests('standard instrumentor') do
tests('success').returns(
['excon.request', 'excon.retry', 'excon.retry', 'excon.retry', 'excon.error']) do
begin
original_stderr = $stderr
$stderr = captured_stderr = StringIO.new
stub_failure
connection = Excon.new(
'http://127.0.0.1:9292',
:instrumentor => Excon::StandardInstrumentor,
:mock => true
)
raises(Excon::Errors::SocketError) do
connection.get(:idempotent => true)
end
captured_stderr.string.split("\n").reject {|line| line =~ %r{^ }}.map {|event| event.split(' ').first}
ensure
$stderr = original_stderr
end
end
tests('authorization header REDACT') do
@auth_header = 'Basic dXNlcjpwYXNz'
begin
original_stderr = $stderr
$stderr = @captured_stderr = StringIO.new
stub_failure
raises(Excon::Errors::SocketError) do
@connection = Excon.new(
'http://user:pass@127.0.0.1:9292',
:headers => {
'Authorization' => @auth_header
},
:instrumentor => Excon::StandardInstrumentor,
:mock => true
)
@connection.get(:idempotent => true)
end
ensure
$stderr = original_stderr
end
test('does not appear in response') do
!@captured_stderr.string.include?(@auth_header)
end
test('does not mutate Authorization value') do
@connection.data[:headers]['Authorization'] == @auth_header
end
end
tests('password REDACT') do
begin
original_stderr = $stderr
$stderr = @captured_stderr = StringIO.new
stub_failure
raises(Excon::Errors::SocketError) do
@connection = Excon.new(
'http://user:pass@127.0.0.1:9292',
:instrumentor => Excon::StandardInstrumentor,
:mock => true
)
@connection.get(:idempotent => true)
end
ensure
$stderr = original_stderr
end
@password_param = '"pass"'
test('does not appear in response') do
!@captured_stderr.string.include?(@password_param)
end
test('does not mutate password value') do
@connection.data[:password] == "pass"
end
end
tests('proxy password REDACT') do
begin
original_stderr = $stderr
$stderr = @captured_stderr = StringIO.new
stub_failure
raises(Excon::Errors::SocketError) do
@connection = Excon.new(
'http://user:pass@127.0.0.1:9292',
:instrumentor => Excon::StandardInstrumentor,
:mock => true,
:proxy => { :password => "pass" }
)
@connection.get(:idempotent => true)
end
ensure
$stderr = original_stderr
end
@proxy_password_param = '"pass"'
test('does not appear in response') do
!@captured_stderr.string.include?(@password_param)
end
test('does not mutate password value') do
@connection.data[:proxy][:password] == "pass"
end
end
end
tests('use our own instrumentor').returns(
['excon.request', 'excon.retry', 'excon.retry', 'excon.retry', 'excon.error']) do
stub_failure
connection = Excon.new(
'http://127.0.0.1:9292',
:instrumentor => SimpleInstrumentor,
:mock => true
)
raises(Excon::Errors::SocketError) do
connection.get(:idempotent => true)
end
SimpleInstrumentor.events
end
tests('always passes the block').returns(
['excon.request', 'excon.response']) do
stub_success
connection = Excon.new(
'http://127.0.0.1:9292',
:instrumentor => SimpleInstrumentor,
:mock => true
)
connection.get(:idempotent => true)
SimpleInstrumentor.blocks
end
tests('does not generate events when not provided').returns(0) do
subscribe(/excon/)
stub_success
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
connection.get(:idempotent => true)
@events.count
end
tests('allows setting the prefix').returns(
['gug.request', 'gug.retry', 'gug.retry','gug.retry', 'gug.error']) do
subscribe(/gug/)
stub_failure
raises(Excon::Errors::SocketError) do
make_request(idempotent: true, instrumentor_name: 'gug')
end
@events.map(&:name)
end
tests('allows setting the prefix when not idempotent').returns(
['gug.request', 'gug.error']) do
subscribe(/gug/)
stub_failure
raises(Excon::Errors::SocketError) do
make_request(instrumentor_name: 'gug')
end
@events.map(&:name)
end
with_rackup('basic.ru') do
tests('works unmocked').returns(['excon.request', 'excon.response']) do
subscribe(/excon/)
make_request(:mock => false)
@events.map(&:name)
end
end
end