changed assess_jruby! to use block param, added specs & doc

This commit is contained in:
Colin Surprenant 2014-04-09 10:10:41 -04:00
parent d306b0c1af
commit 3337c910d2
3 changed files with 47 additions and 9 deletions

View file

@ -7,6 +7,8 @@ module LogStash
LOGSTASH_HOME = ::File.expand_path(::File.join(::File.dirname(__FILE__), "/../..")) LOGSTASH_HOME = ::File.expand_path(::File.join(::File.dirname(__FILE__), "/../.."))
JAR_DIR = ::File.join(LOGSTASH_HOME, "/vendor/jar") JAR_DIR = ::File.join(LOGSTASH_HOME, "/vendor/jar")
# loads currenly embedded elasticsearch jars
# @raise LogStash::EnvironmentError if not runnig under JRuby or if no jar files found
def load_elasticsearch_jars! def load_elasticsearch_jars!
assess_jruby! assess_jruby!
@ -22,8 +24,17 @@ module LogStash
end end
end end
def assess_jruby!(exception_class = nil, message = nil) # @yield execute optional block if not currently running under JRuby
raise(exception_class || LogStash::EnvironmentError, message || "JRuby is required") unless jruby? # @yieldreturn [Exception] exception to raise if Exception class returned otherwise raise default exception
# @raise [Exception] yielded exception or default if not runnig under JRuby
def assess_jruby!
unless jruby?
# grab return value from block if present, use default exception if not an exception class
exception = block_given? ? yield : nil
exception = LogStash::EnvironmentError.new("JRuby is required") unless exception.is_a?(Exception)
raise(exception)
end
end end
def jruby? def jruby?

View file

@ -197,7 +197,7 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base
if ["node", "transport"].include?(@protocol) if ["node", "transport"].include?(@protocol)
# Node or TransportClient; requires JRuby # Node or TransportClient; requires JRuby
LogStash::Environment.assess_jruby!(LogStash::PluginLoadingError, "This configuration requires JRuby. If you are not using JRuby, you must set 'protocol' to 'http'. For example: output { elasticsearch { protocol => \"http\" } }") LogStash::Environment.assess_jruby!{LogStash::PluginLoadingError.new("This configuration requires JRuby. If you are not using JRuby, you must set 'protocol' to 'http'. For example: output { elasticsearch { protocol => \"http\" } }")}
LogStash::Environment.load_elasticsearch_jars! LogStash::Environment.load_elasticsearch_jars!
# setup log4j properties for Elasticsearch # setup log4j properties for Elasticsearch
@ -241,7 +241,7 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base
:protocol => @protocol) :protocol => @protocol)
if @embedded if @embedded
LogStash::Environment.assess_jruby!(LogStash::ConfigurationError, "The 'embedded => true' setting is only valid for the elasticsearch output under JRuby. You are running #{RUBY_DESCRIPTION}") LogStash::Environment.assess_jruby!{LogStash::ConfigurationError.new("The 'embedded => true' setting is only valid for the elasticsearch output under JRuby. You are running #{RUBY_DESCRIPTION}")}
LogStash::Environment.load_elasticsearch_jars! LogStash::Environment.load_elasticsearch_jars!
# Default @host with embedded to localhost. This should help avoid # Default @host with embedded to localhost. This should help avoid

View file

@ -2,12 +2,39 @@ require "logstash/environment"
describe LogStash::Environment do describe LogStash::Environment do
it "should load elasticsarch jars" do describe "load_elasticsearch_jars!" do
expect {LogStash::Environment.load_elasticsearch_jars!}.to_not raise_error
it "should load elasticsarch jars" do
expect{LogStash::Environment.load_elasticsearch_jars!}.to_not raise_error
end
it "should raise when cannot find elasticsarch jars" do
stub_const("LogStash::Environment::JAR_DIR", "/some/invalid/path")
expect{LogStash::Environment.load_elasticsearch_jars!}.to raise_error(LogStash::EnvironmentError)
end
end end
it "should raise when cannot find elasticsarch jars" do describe "assess_jruby!" do
stub_const("LogStash::Environment::JAR_DIR", "/some/invalid/path")
expect {LogStash::Environment.load_elasticsearch_jars!}.to raise_error(LogStash::EnvironmentError) it "should not raise when jruby" do
expect(LogStash::Environment).to receive(:jruby?).twice.and_return(true)
expect{LogStash::Environment.assess_jruby!}.to_not raise_error
expect{LogStash::Environment.assess_jruby!{StandardError.new}}.to_not raise_error
end
it "should raise default exception" do
expect(LogStash::Environment).to receive(:jruby?).once.and_return(false)
expect{LogStash::Environment.assess_jruby!}.to raise_error(LogStash::EnvironmentError)
end
it "should yield to block and raise returned exception" do
expect(LogStash::Environment).to receive(:jruby?).once.and_return(false)
expect{LogStash::Environment.assess_jruby!{StandardError.new}}.to raise_error(StandardError)
end
it "should yield to block and raise default exception if exception not returned" do
expect(LogStash::Environment).to receive(:jruby?).once.and_return(false)
expect{LogStash::Environment.assess_jruby!{nil}}.to raise_error(LogStash::EnvironmentError)
end
end end
end end