spec/outputs/newrelic_spec.rb in logstash-output-newrelic-1.0.9 vs spec/outputs/newrelic_spec.rb in logstash-output-newrelic-1.1.1

- old
+ new

@@ -6,23 +6,62 @@ require "logstash/event" require "webmock/rspec" require "zlib" describe LogStash::Outputs::NewRelic do + let (:base_uri) { "https://testing-example-collector.com" } + let (:retry_seconds) { 0 } + # Don't sleep in tests, to keep tests fast. We have a test for the method that produces the sleep duration between retries. + let (:max_delay) { 0 } + let (:retries) { 3 } + let (:license_key) { 'cool-guy' } + let (:simple_config) { + { + "base_uri" => base_uri, + "license_key" => license_key + } + } + + + before(:each) do + @newrelic_output = LogStash::Plugin.lookup("output", "newrelic").new(simple_config) + @newrelic_output.register + end + + after(:each) do + if @newrelic_output + @newrelic_output.shutdown + end + end + context "license key tests" do + it "sets license key when given in the header" do + stub_request(:any, base_uri).to_return(status: 200) + + event = LogStash::Event.new({:message => "Test message" }) + @newrelic_output.multi_receive([event]) + + wait_for(a_request(:post, base_uri) + .with(headers: { + "X-License-Key" => license_key, + "X-Event-Source" => "logs", + "Content-Encoding" => "gzip", + })).to have_been_made + end + end +end + +describe LogStash::Outputs::NewRelic do let (:api_key) { "someAccountKey" } let (:base_uri) { "https://testing-example-collector.com" } let (:retry_seconds) { 0 } # Don't sleep in tests, to keep tests fast. We have a test for the method that produces the sleep duration between retries. let (:max_delay) { 0 } let (:retries) { 3 } let (:simple_config) { { "api_key" => api_key, "base_uri" => base_uri, - "retries" => retries, - "retry_seconds" => retry_seconds, - "max_delay" => max_delay, } } # An arbitrary time to use in these tests, with different representations class FixedTime @@ -69,12 +108,12 @@ context "validation of config" do it "requires api_key" do no_api_key_config = { } - - expect { LogStash::Plugin.lookup("output", "newrelic").new(no_api_key_config) }.to raise_error LogStash::ConfigurationError + output = LogStash::Plugin.lookup("output", "newrelic").new(no_api_key_config) + expect { output.register }.to raise_error LogStash::ConfigurationError end end context "request headers" do it "all present" do @@ -220,69 +259,9 @@ messages = multiple_gzipped_messages(request.body)[0]['logs'] messages.length == 2 && messages[0]['message'] == 'Test message 1' && messages[1]['message'] == 'Test message 2' }) .to have_been_made - end - end - - context "retry" do - it "sleep periods double each time up to max time" do - specific_config = simple_config.clone - # Use non-trivial times -- they can be big, since this test doesn't do any sleeping, just - # tests the sleep duration - specific_config["max_delay"] = 60 - specific_config["retry_seconds"] = 5 - - # Create a new plugin with this specific config that has longer retry sleep - # configuration than we normally want - @newrelic_output.shutdown - @newrelic_output = LogStash::Plugin.lookup("output", "newrelic").new(specific_config) - @newrelic_output.register - - expect(@newrelic_output.sleep_duration(0)).to equal(0) - expect(@newrelic_output.sleep_duration(1)).to equal(5) - expect(@newrelic_output.sleep_duration(2)).to equal(10) - expect(@newrelic_output.sleep_duration(3)).to equal(20) - expect(@newrelic_output.sleep_duration(4)).to equal(40) - expect(@newrelic_output.sleep_duration(5)).to equal(60) - expect(@newrelic_output.sleep_duration(6)).to equal(60) # Never gets bigger than this - end - - it "first call fails, should retry" do - stub_request(:any, base_uri) - .to_return(status: 500) - .to_return(status: 200) - - event = LogStash::Event.new({ "message" => "Test message" }) - @newrelic_output.multi_receive([event]) - - wait_for(a_request(:post, base_uri)).to have_been_made.times(2) - end - - it "first two calls fail, should retry" do - stub_request(:any, base_uri) - .to_return(status: 500) - .to_return(status: 500) - .to_return(status: 200) - - event = LogStash::Event.new({ "message" => "Test message" }) - @newrelic_output.multi_receive([event]) - - wait_for(a_request(:post, base_uri)).to have_been_made.times(3) - end - - it "all calls fails, should stop retrying at some point" do - stub_request(:any, base_uri) - .to_return(status: 500) - - event = LogStash::Event.new({ "message" => "Test message" }) - @newrelic_output.multi_receive([event]) - - # This may not fail if the wait_for is called exactly when there have been 'retries' calls. - # However, with zero sleep time (max_delay=0), on a laptop the POST was done 2000+ times by the - # time this was executed - wait_for(a_request(:post, base_uri)).to have_been_made.times(retries) end end context "error handling" do it "continues through errors, future calls should still succeed" do