1919require_relative "../../framework/settings"
2020require_relative "../../services/logstash_service"
2121require_relative "../../framework/helpers"
22+ require_relative "pluginmanager_spec_helper"
2223require "logstash/devutils/rspec/spec_helper"
2324require "stud/temporary"
2425require "fileutils"
@@ -29,23 +30,32 @@ def gem_in_lock_file?(pattern, lock_file)
2930 content . match ( pattern )
3031end
3132
33+ def plugin_filename_re ( name , version )
34+ %Q(\b #{ Regexp . escape name } -#{ Regexp . escape version } (-java)?\b )
35+ end
36+
3237# Bundler can mess up installation successful output: https://github.com/elastic/logstash/issues/15801
3338INSTALL_SUCCESS_RE = /IB?nstall successful/
3439INSTALLATION_SUCCESS_RE = /IB?nstallation successful/
3540
41+ INSTALLATION_ABORTED_RE = /Installation aborted/
42+
3643describe "CLI > logstash-plugin install" do
37- before ( :all ) do
44+ before ( :each ) do
3845 @fixture = Fixture . new ( __FILE__ )
3946 @logstash = @fixture . get_service ( "logstash" )
4047 @logstash_plugin = @logstash . plugin_cli
41- @pack_directory = File . expand_path ( File . join ( File . dirname ( __FILE__ ) , ".." , ".." , "fixtures" , "logstash-dummy-pack" ) )
4248 end
4349
4450 shared_examples "install from a pack" do
4551 let ( :pack ) { "file://#{ File . join ( @pack_directory , "logstash-dummy-pack.zip" ) } " }
4652 let ( :install_command ) { "bin/logstash-plugin install" }
4753 let ( :change_dir ) { true }
4854
55+ before ( :all ) do
56+ @pack_directory = File . expand_path ( File . join ( File . dirname ( __FILE__ ) , ".." , ".." , "fixtures" , "logstash-dummy-pack" ) )
57+ end
58+
4959 # When you are on anything by linux we won't disable the internet with seccomp
5060 if RbConfig ::CONFIG [ "host_os" ] == "linux"
5161 context "without internet connection (linux seccomp wrapper)" do
@@ -152,4 +162,92 @@ def gem_in_lock_file?(pattern, lock_file)
152162 end
153163 end
154164 end
165+
166+ context "rubygems hosted plugin" do
167+ include_context "pluginmanager validation helpers"
168+ shared_examples ( "overwriting existing" ) do
169+ before ( :each ) do
170+ aggregate_failures ( "precheck" ) do
171+ expect ( "#{ plugin_name } -#{ existing_plugin_version } " ) . to_not be_installed_gem
172+ expect ( "#{ plugin_name } -#{ specified_plugin_version } " ) . to_not be_installed_gem
173+ end
174+ aggregate_failures ( "setup" ) do
175+ execute = @logstash_plugin . install ( plugin_name , version : existing_plugin_version )
176+
177+ expect ( execute . stderr_and_stdout ) . to match ( INSTALLATION_SUCCESS_RE )
178+ expect ( execute . exit_code ) . to eq ( 0 )
179+
180+ expect ( "#{ plugin_name } -#{ existing_plugin_version } " ) . to be_installed_gem
181+ expect ( "#{ plugin_name } -#{ specified_plugin_version } " ) . to_not be_installed_gem
182+ end
183+ end
184+ it "installs the specified version and removes the pre-existing one" do
185+ execute = @logstash_plugin . install ( plugin_name , version : specified_plugin_version )
186+
187+ aggregate_failures ( "command execution" ) do
188+ expect ( execute . stderr_and_stdout ) . to match ( INSTALLATION_SUCCESS_RE )
189+ expect ( execute . exit_code ) . to eq ( 0 )
190+ end
191+
192+ installed = @logstash_plugin . list ( plugin_name , verbose : true )
193+ expect ( installed . stderr_and_stdout ) . to match ( /#{ Regexp . escape plugin_name } [(]#{ Regexp . escape ( specified_plugin_version ) } [)]/ )
194+
195+ expect ( "#{ plugin_name } -#{ existing_plugin_version } " ) . to_not be_installed_gem
196+ expect ( "#{ plugin_name } -#{ specified_plugin_version } " ) . to be_installed_gem
197+ end
198+ end
199+
200+ context "when installing over an older version" do
201+ let ( :plugin_name ) { "logstash-filter-qatest" }
202+ let ( :existing_plugin_version ) { "0.1.0" }
203+ let ( :specified_plugin_version ) { "0.1.1" }
204+
205+ include_examples "overwriting existing"
206+ end
207+
208+ context "when installing over a newer version" do
209+ let ( :plugin_name ) { "logstash-filter-qatest" }
210+ let ( :existing_plugin_version ) { "0.1.0" }
211+ let ( :specified_plugin_version ) { "0.1.1" }
212+
213+ include_examples "overwriting existing"
214+ end
215+
216+ context "installing plugin that isn't present" do
217+ it "installs the plugin" do
218+ aggregate_failures ( "prevalidation" ) do
219+ expect ( "logstash-filter-qatest" ) . to_not be_installed_gem
220+ end
221+
222+ execute = @logstash_plugin . install ( "logstash-filter-qatest" )
223+
224+ expect ( execute . stderr_and_stdout ) . to match ( INSTALLATION_SUCCESS_RE )
225+ expect ( execute . exit_code ) . to eq ( 0 )
226+
227+ installed = @logstash_plugin . list ( "logstash-filter-qatest" )
228+ expect ( installed . stderr_and_stdout ) . to match ( /logstash-filter-qatest/ )
229+ expect ( installed . exit_code ) . to eq ( 0 )
230+
231+ expect ( gem_in_lock_file? ( /logstash-filter-qatest/ , @logstash . lock_file ) ) . to be_truthy
232+
233+ expect ( "logstash-filter-qatest" ) . to be_installed_gem
234+ end
235+ end
236+ context "installing plugin that doesn't exist on rubygems" do
237+ it "doesn't install anything" do
238+ execute = @logstash_plugin . install ( "logstash-filter-404-no-exist" )
239+
240+ expect ( execute . stderr_and_stdout ) . to match ( INSTALLATION_ABORTED_RE )
241+ expect ( execute . exit_code ) . to eq ( 1 )
242+ end
243+ end
244+ context "installing gem that isn't a plugin" do
245+ it "doesn't install anything" do
246+ execute = @logstash_plugin . install ( "dummy_gem" )
247+
248+ expect ( execute . stderr_and_stdout ) . to match ( INSTALLATION_ABORTED_RE )
249+ expect ( execute . exit_code ) . to eq ( 1 )
250+ end
251+ end
252+ end
155253end
0 commit comments