mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 22:57:16 -04:00
changed assess_jruby! to use block param, added specs & doc
This commit is contained in:
parent
d306b0c1af
commit
3337c910d2
3 changed files with 47 additions and 9 deletions
|
@ -7,6 +7,8 @@ module LogStash
|
|||
LOGSTASH_HOME = ::File.expand_path(::File.join(::File.dirname(__FILE__), "/../.."))
|
||||
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!
|
||||
assess_jruby!
|
||||
|
||||
|
@ -22,8 +24,17 @@ module LogStash
|
|||
end
|
||||
end
|
||||
|
||||
def assess_jruby!(exception_class = nil, message = nil)
|
||||
raise(exception_class || LogStash::EnvironmentError, message || "JRuby is required") unless jruby?
|
||||
# @yield execute optional block if not currently running under 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
|
||||
|
||||
def jruby?
|
||||
|
|
|
@ -197,7 +197,7 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base
|
|||
|
||||
if ["node", "transport"].include?(@protocol)
|
||||
# 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!
|
||||
|
||||
# setup log4j properties for Elasticsearch
|
||||
|
@ -241,7 +241,7 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base
|
|||
:protocol => @protocol)
|
||||
|
||||
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!
|
||||
|
||||
# Default @host with embedded to localhost. This should help avoid
|
||||
|
|
|
@ -2,12 +2,39 @@ require "logstash/environment"
|
|||
|
||||
describe LogStash::Environment do
|
||||
|
||||
describe "load_elasticsearch_jars!" do
|
||||
|
||||
it "should load elasticsarch jars" do
|
||||
expect {LogStash::Environment.load_elasticsearch_jars!}.to_not raise_error
|
||||
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)
|
||||
expect{LogStash::Environment.load_elasticsearch_jars!}.to raise_error(LogStash::EnvironmentError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "assess_jruby!" do
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue