Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Imron Alston committed Mar 31, 2015
1 parent 0264c4d commit dc7a904
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.1
0.0.2
8 changes: 3 additions & 5 deletions fluent-plugin-scalyr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions lib/fluent/plugin/out_scalyr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand Down
69 changes: 69 additions & 0 deletions test/test_out_scalyr.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dc7a904

Please sign in to comment.