2
2
require "fluent/test/helpers"
3
3
require "fluent/test/driver/output"
4
4
require "fluent/plugin/out_datadog"
5
+ require "fluent/plugin/version"
5
6
require 'webmock/test_unit'
6
7
7
8
class FluentDatadogTest < Test ::Unit ::TestCase
@@ -210,31 +211,60 @@ def create_valid_subject
210
211
end
211
212
end
212
213
213
- sub_test_case "http connection errors" do
214
+ # v1 routes
215
+ sub_test_case "http connection errors (v1 routes)" do
214
216
test "should retry when server is returning 5XX" do
215
217
api_key = 'XXX'
216
218
stub_dd_request_with_return_code ( api_key , 500 )
217
219
payload = '{}'
218
- client = Fluent ::DatadogOutput ::DatadogHTTPClient . new Logger . new ( STDOUT ) , false , false , "datadog.com" , 443 , 80 , nil , false , api_key
220
+ client = Fluent ::DatadogOutput ::DatadogHTTPClient . new Logger . new ( STDOUT ) , false , false , "datadog.com" , 443 , 80 , nil , false , api_key , true
219
221
assert_raise ( Fluent ::DatadogOutput ::RetryableError ) do
220
222
client . send ( payload )
221
223
end
222
224
end
223
225
224
- test "should not retry when server is returning 4XX" do
226
+ test "should not retry when server is returning 4XX (v1 routes) " do
225
227
api_key = 'XXX'
226
228
stub_dd_request_with_return_code ( api_key , 400 )
227
229
payload = '{}'
230
+ client = Fluent ::DatadogOutput ::DatadogHTTPClient . new Logger . new ( STDOUT ) , false , false , "datadog.com" , 443 , 80 , nil , false , api_key , true
231
+ assert_nothing_raised do
232
+ client . send ( payload )
233
+ end
234
+ end
235
+ end
236
+
237
+ # v2 routes
238
+ sub_test_case "http connection errors (v2 routes)" do
239
+ test "should retry when server is returning 5XX" do
240
+ api_key = 'XXX'
241
+ stub_dd_request_with_return_code ( api_key , 500 , true )
242
+ payload = '{}'
243
+ client = Fluent ::DatadogOutput ::DatadogHTTPClient . new Logger . new ( STDOUT ) , false , false , "datadog.com" , 443 , 80 , nil , false , api_key
244
+ assert_raise ( Fluent ::DatadogOutput ::RetryableError ) do
245
+ client . send ( payload )
246
+ end
247
+ end
248
+
249
+ test "should not retry when server is returning 4XX (v2 routes)" do
250
+ api_key = 'XXX'
251
+ stub_dd_request_with_return_code ( api_key , 400 , true )
252
+ payload = '{}'
228
253
client = Fluent ::DatadogOutput ::DatadogHTTPClient . new Logger . new ( STDOUT ) , false , false , "datadog.com" , 443 , 80 , nil , false , api_key
229
254
assert_nothing_raised do
230
255
client . send ( payload )
231
256
end
232
257
end
233
258
end
234
259
235
- def stub_dd_request_with_return_code ( api_key , return_code )
236
- stub_dd_request ( api_key ) .
237
- to_return ( status : return_code , body : "" , headers : { } )
260
+ def stub_dd_request_with_return_code ( api_key , return_code , v2_routes = false )
261
+ if v2_routes
262
+ stub_dd_request_v2_routes ( api_key ) .
263
+ to_return ( status : return_code , body : "" , headers : { } )
264
+ else
265
+ stub_dd_request ( api_key ) .
266
+ to_return ( status : return_code , body : "" , headers : { } )
267
+ end
238
268
end
239
269
240
270
def stub_dd_request_with_error ( api_key , error )
@@ -255,4 +285,21 @@ def stub_dd_request(api_key)
255
285
'User-Agent' => 'Ruby'
256
286
} )
257
287
end
288
+
289
+ def stub_dd_request_v2_routes ( api_key )
290
+ stub_request ( :post , "http://datadog.com/api/v2/logs" ) .
291
+ with (
292
+ body : "{}" ,
293
+ headers : {
294
+ 'Accept' => '*/*' ,
295
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' ,
296
+ 'Connection' => 'keep-alive' ,
297
+ 'Content-Type' => 'application/json' ,
298
+ 'Dd-Api-Key' => "#{ api_key } " ,
299
+ 'Dd-Evp-Origin' => 'fluent' ,
300
+ 'Dd-Evp-Origin-Version' => "#{ Datadog ::FluentPlugin ::GEM_VERSION } " ,
301
+ 'Keep-Alive' => '30' ,
302
+ 'User-Agent' => 'Ruby'
303
+ } )
304
+ end
258
305
end
0 commit comments