Skip to content

Commit acb1860

Browse files
committed
Add unit tests for the HTTP request creation with v2 routes.
1 parent 3a9f935 commit acb1860

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

fluent-plugin-datadog.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ Gem::Specification.new do |spec|
3030
spec.add_development_dependency "test-unit", '~> 3.1'
3131
spec.add_development_dependency "rake", "~> 12.0"
3232
spec.add_development_dependency "yajl-ruby", "~> 1.2"
33-
spec.add_development_dependency 'webmock', "~> 3.5.0"
33+
spec.add_development_dependency 'webmock', "~> 3.6.0"
3434
end

test/plugin/test_out_datadog.rb

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require "fluent/test/helpers"
33
require "fluent/test/driver/output"
44
require "fluent/plugin/out_datadog"
5+
require "fluent/plugin/version"
56
require 'webmock/test_unit'
67

78
class FluentDatadogTest < Test::Unit::TestCase
@@ -210,31 +211,60 @@ def create_valid_subject
210211
end
211212
end
212213

213-
sub_test_case "http connection errors" do
214+
# v1 routes
215+
sub_test_case "http connection errors (v1 routes)" do
214216
test "should retry when server is returning 5XX" do
215217
api_key = 'XXX'
216218
stub_dd_request_with_return_code(api_key, 500)
217219
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
219221
assert_raise(Fluent::DatadogOutput::RetryableError) do
220222
client.send(payload)
221223
end
222224
end
223225

224-
test "should not retry when server is returning 4XX" do
226+
test "should not retry when server is returning 4XX (v1 routes)" do
225227
api_key = 'XXX'
226228
stub_dd_request_with_return_code(api_key, 400)
227229
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 = '{}'
228253
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, false, api_key
229254
assert_nothing_raised do
230255
client.send(payload)
231256
end
232257
end
233258
end
234259

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
238268
end
239269

240270
def stub_dd_request_with_error(api_key, error)
@@ -255,4 +285,21 @@ def stub_dd_request(api_key)
255285
'User-Agent' => 'Ruby'
256286
})
257287
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
258305
end

0 commit comments

Comments
 (0)