Use the gem specification from the plugin to display the version notice to the user

Fixes #2331
This commit is contained in:
Pier-Hugues Pellerin 2015-01-07 17:35:35 -05:00 committed by Jordan Sissel
parent 232cf92178
commit 15eaeb57ca
3 changed files with 108 additions and 35 deletions

View file

@ -39,6 +39,8 @@ module LogStash::Config::Mixin
Regexp => 100, Regexp => 100,
} }
GEM_NAME_PREFIX = 'logstash'
# This method is called when someone does 'include LogStash::Config' # This method is called when someone does 'include LogStash::Config'
def self.included(base) def self.included(base)
# Add the DSL methods to the 'base' given. # Add the DSL methods to the 'base' given.
@ -121,13 +123,16 @@ module LogStash::Config::Mixin
return @config_name return @config_name
end end
# Deprecated: Declare the version of the plugin
# inside the gemspec.
def plugin_status(status=nil) def plugin_status(status=nil)
milestone(status) milestone(status)
end end
# Deprecated: Declare the version of the plugin
# inside the gemspec.
def milestone(m=nil) def milestone(m=nil)
@milestone = m if !m.nil? @logger.error(I18n.t('logstash.plugin.deprecated'))
return @milestone
end end
# Define a new configuration setting # Define a new configuration setting
@ -184,7 +189,7 @@ module LogStash::Config::Mixin
end end
end end
subclass.instance_variable_set("@config", subconfig) subclass.instance_variable_set("@config", subconfig)
@@milestone_notice_given = false @@version_notice_given = false
end # def inherited end # def inherited
def validate(params) def validate(params)
@ -193,7 +198,7 @@ module LogStash::Config::Mixin
@logger = Cabin::Channel.get(LogStash) @logger = Cabin::Channel.get(LogStash)
is_valid = true is_valid = true
is_valid &&= validate_milestone is_valid &&= validate_plugin_version
is_valid &&= validate_check_invalid_parameter_names(params) is_valid &&= validate_check_invalid_parameter_names(params)
is_valid &&= validate_check_required_parameter_names(params) is_valid &&= validate_check_required_parameter_names(params)
is_valid &&= validate_check_parameter_values(params) is_valid &&= validate_check_parameter_values(params)
@ -201,23 +206,36 @@ module LogStash::Config::Mixin
return is_valid return is_valid
end # def validate end # def validate
def validate_milestone def plugin_version
return true if @@milestone_notice_given specification = Gem::Specification.find_by_name(plugin_gem_name)
docmsg = "For more information about plugin milestones, see http://logstash.net/docs/#{LOGSTASH_VERSION}/plugin-milestones " major, minor, patch = specification.version.segments
plugin_type = ancestors.find { |a| a.name =~ /::Base$/ }.config_name
case @milestone Struct.new(:major, :minor, :patch)
when 0,1,2 .new(major, minor, patch)
@logger.warn(I18n.t("logstash.plugin.milestone.#{@milestone}", end
:type => plugin_type, :name => @config_name,
def plugin_gem_name
[GEM_NAME_PREFIX, @plugin_type, @plugin_name].join('-')
end
def validate_plugin_version
return true if @@version_notice_given
if plugin_version.major < 1
if plugin_version.minor == 9
@logger.warn(I18n.t("logstash.plugin.version.0-9-0",
:type => @plugin_type,
:name => @config_name,
:LOGSTASH_VERSION => LOGSTASH_VERSION)) :LOGSTASH_VERSION => LOGSTASH_VERSION))
when 3
# No message to log for milestone 3 plugins.
when nil
raise "#{@config_name} must set a milestone. #{docmsg}"
else else
raise "#{@config_name} set an invalid plugin status #{@milestone}. Valid values are 0, 1, 2, or 3. #{docmsg}" @logger.warn(I18n.t("logstash.plugin.version.0-1-0",
:type => @plugin_type,
:name => @config_name,
:LOGSTASH_VERSION => LOGSTASH_VERSION))
end
end end
@@milestone_notice_given = true
@@version_notice_given = true
return true return true
end end

View file

@ -34,23 +34,22 @@ en:
supported by this plugin. I will continue working as if you had not set supported by this plugin. I will continue working as if you had not set
this setting. this setting.
plugin: plugin:
milestone: deprecated_milestone: >-
"0": >- %{plugin} plugin is using the 'milestone' method to declare the version
Using milestone 0 %{type} plugin '%{name}'. This plugin isn't well of the plugin this method is deprecated in favor of declaring the
supported by the community and likely has no maintainer. For more version inside the gemspec.
information on plugin milestones, see version:
http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-milestones 0-9-0:
"1": >- Using version 0.9.x %{type} plugin '%{name}'. This plugin should but
Using milestone 1 %{type} plugin '%{name}'. This plugin should work, would benefit from use by folks like you. Please let us know if you
but would benefit from use by folks like you. Please let us know if you find bugs or have suggestions on how to improve this plugin. For more
find bugs or have suggestions on how to improve this plugin. For more information on plugin milestones, see
information on plugin milestones, see http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-version
http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-milestones 0-1-0: >-
"2": >- Using version 0.1.x %{type} plugin '%{name}'. This plugin isn't well
Using milestone 2 %{type} plugin '%{name}'. This plugin should be supported by the community and likely has no maintainer. For more
stable, but if you see strange behavior, please let us know! information on plugin milestones, see
For more information on plugin milestones, see http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-version
http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-milestones
agent: agent:
sighup: >- sighup: >-
SIGHUP received. SIGHUP received.

View file

@ -29,4 +29,60 @@ describe LogStash::Plugin do
end end
expect(LogStash::Plugin.lookup("filter", "lady_gaga")).to eq(LogStash::Filters::LadyGaga) expect(LogStash::Plugin.lookup("filter", "lady_gaga")).to eq(LogStash::Filters::LadyGaga)
end end
context "when validating the plugin version" do
let(:plugin_name) { 'logstash-filter-stromae' }
subject do
Class.new(LogStash::Filters::Base) do
config_name 'stromae'
end
end
it "doesn't warn the user if the version is superior or equal to 1.0.0" do
allow(Gem::Specification).to receive(:find_by_name)
.with(plugin_name)
.and_return(double(:version => Gem::Version.new('1.0.0')))
expect_any_instance_of(Cabin::Channel).not_to receive(:warn)
subject.validate({})
end
it 'warns the user if the plugin version is between 0.9.x and 1.0.0' do
allow(Gem::Specification).to receive(:find_by_name)
.with(plugin_name)
.and_return(double(:version => Gem::Version.new('0.9.1')))
expect_any_instance_of(Cabin::Channel).to receive(:warn)
.with(/Using version 0.9.x/)
subject.validate({})
end
it 'warns the user if the plugin version is inferior to 0.9.x' do
allow(Gem::Specification).to receive(:find_by_name)
.with(plugin_name)
.and_return(double(:version => Gem::Version.new('0.1.1')))
expect_any_instance_of(Cabin::Channel).to receive(:warn)
.with(/Using version 0.1.x/)
subject.validate({})
end
it "doesnt show the version notice more than once" do
class LogStash::Filters::Stromae < LogStash::Filters::Base
config_name "stromae"
end
allow(Gem::Specification).to receive(:find_by_name)
.with(plugin_name)
.and_return(double(:version => Gem::Version.new('0.1.1')))
expect_any_instance_of(Cabin::Channel).to receive(:warn)
.once
.with(/Using version 0.1.x/)
LogStash::Filters::Stromae.validate({})
LogStash::Filters::Stromae.validate({})
end
end
end end