diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..6bc8c27 --- /dev/null +++ b/Rakefile @@ -0,0 +1,11 @@ +require 'bundler' +Bundler::GemHelper.install_tasks + +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs << "test" << "lib" + t.pattern = 'test/**/test_*.rb' +end + +task :default => [:build] diff --git a/VERSION b/VERSION index 8acdd82..4e379d2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.1 +0.0.2 diff --git a/fluent-plugin-scalyr.gemspec b/fluent-plugin-scalyr.gemspec index ee0cedb..5350da4 100644 --- a/fluent-plugin-scalyr.gemspec +++ b/fluent-plugin-scalyr.gemspec @@ -15,9 +15,7 @@ Gem::Specification.new do |gem| gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } gem.require_paths = ['lib'] gem.add_dependency "fluentd", [">= 0.10.49", "< 2"] - gem.add_dependency "yajl-ruby", "~> 1.0" - gem.add_dependency "fluent-mixin-config-placeholders", ">= 0.3.0" - gem.add_development_dependency "rake", ">= 0.9.2" - gem.add_development_dependency "flexmock", ">= 1.2.0" - gem.add_development_dependency "test-unit", ">= 3.0.8" + gem.add_development_dependency "rake", "~> 0.9.2" + gem.add_development_dependency "flexmock", "~> 1.2.0" + gem.add_development_dependency "bundler", "~> 1.9.1" end diff --git a/lib/fluent/plugin/out_scalyr.rb b/lib/fluent/plugin/out_scalyr.rb index 75a7763..be8387a 100644 --- a/lib/fluent/plugin/out_scalyr.rb +++ b/lib/fluent/plugin/out_scalyr.rb @@ -7,7 +7,7 @@ require 'thread' module Scalyr - class FluentLogger < Fluent::BufferedOutput + class ScalyrOut < Fluent::BufferedOutput Fluent::Plugin.register_output( 'scalyr', self ) config_param :api_write_token, :string @@ -105,20 +105,21 @@ def handle_response( response ) #make sure the JSON reponse has a "status" field if !response_hash.key? "status" $log.debug "JSON response does not contain status message" - raise Scalyr::ServerError + raise Scalyr::ServerError.new "JSON response does not contain status message" end status = response_hash["status"] if status.start_with? "error" if status =~ %r"/client/"i - raise Scalyr::ClientError + raise Scalyr::ClientError.new status else #don't check specifically for server, we assume all non-client errors are server errors - raise Scalyr::ServerError + raise Scalyr::ServerError.new status end elsif !response.code.include? "200" #response code is a string not an int raise Scalyr::ServerError end + end def build_add_events_body( chunk ) diff --git a/test/test_out_scalyr.rb b/test/test_out_scalyr.rb new file mode 100644 index 0000000..14e1824 --- /dev/null +++ b/test/test_out_scalyr.rb @@ -0,0 +1,69 @@ +require 'flexmock/test_unit' +require 'fluent/test' +require 'fluent/plugin/out_scalyr' + +class ScalyrOutTest < Test::Unit::TestCase + def setup + Fluent::Test.setup + end + + def create_driver + Fluent::Test::BufferedOutputTestDriver.new( Scalyr::ScalyrOut ) + end + + def test_handle_response_missing_status + d = create_driver + response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "message":"An invalid message" }' ) + exception = assert_raise( Scalyr::ServerError, "Server error not raised for missing status" ) { + d.instance.handle_response( response ) + } + + assert_equal( "JSON response does not contain status message", exception.message ) + end + + def test_handle_response_unknown_error + d = create_driver + response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "message":"An invalid message", "status":"error/other" }' ) + exception = assert_raise( Scalyr::ServerError, "Server error not raised for error status" ) { + d.instance.handle_response( response ) + } + assert_equal( "error/other", exception.message ) + end + + def test_handle_response_client_error + d = create_driver + response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "message":"An invalid message", "status":"error/client/test" }' ) + exception = assert_raise( Scalyr::ClientError, "Client error not raised for error status" ) { + d.instance.handle_response( response ) + } + assert_equal( "error/client/test", exception.message ) + end + + def test_handle_response_server_error + d = create_driver + response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "message":"An invalid message", "status":"error/server/test" }' ) + exception = assert_raise( Scalyr::ServerError, "Server error not raised for error status" ) { + d.instance.handle_response( response ) + } + assert_equal( "error/server/test", exception.message ) + end + + def test_handle_response_code_not_200 + d = create_driver + response = flexmock( Net::HTTPResponse, :code => '404', :body =>'{ "status":"error/server/fileNotFound" }' ) + exception = assert_raise( Scalyr::ServerError, Scalyr::ClientError, "Error raised on success" ) { + d.instance.handle_response( response ) + } + assert_equal( "error/server/fileNotFound", exception.message ) + end + + def test_handle_response_code_200 + d = create_driver + response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' ) + exception = assert_nothing_raised( Scalyr::ServerError, Scalyr::ClientError, "Error raised on success" ) { + d.instance.handle_response( response ) + } + end + +end +