diff --git a/lib/logstash/runner.rb b/lib/logstash/runner.rb index a705e856e1e..260508faaf0 100644 --- a/lib/logstash/runner.rb +++ b/lib/logstash/runner.rb @@ -56,6 +56,10 @@ module Cabin::Mixins::Logger class LogStash::Runner include LogStash::Program + def initialize + @runners = [] + end + def main(args) require "logstash/util" require "stud/trap" @@ -74,7 +78,6 @@ def main(args) args = [nil] if args.empty? - @runners = [] while args != nil && !args.empty? args = run(args) end @@ -187,15 +190,16 @@ def wait agent = LogStash::Agent.new($0) begin agent.parse(args) - @runners << Stud::Task.new { agent.execute } rescue Clamp::HelpWanted => e - puts e.command.help + show_help(e.command) + return [] rescue Clamp::UsageError => e # If 'too many arguments' then give the arguments to # the next command. Otherwise it's a real error. raise if e.message != "too many arguments" remaining = agent.remaining_arguments end + @runners << Stud::Task.new { agent.execute } return remaining end @@ -229,8 +233,17 @@ def wait return args end # def run + def self.valid_caller? + caller.none?{|entry| entry =~ /rspec/} + end + + private + + def show_help(command) + puts command.help + end end # class LogStash::Runner -if $0 == __FILE__ +if $0 == __FILE__ && LogStash::Runner.valid_caller? LogStash::Runner.new.main(ARGV) end diff --git a/spec/runner_spec.rb b/spec/runner_spec.rb new file mode 100644 index 00000000000..5250747f354 --- /dev/null +++ b/spec/runner_spec.rb @@ -0,0 +1,42 @@ +require "logstash/runner" +require "logstash/agent" +require "logstash/kibana" +require "stud/task" + +class NullRunner + def run(args); end +end + +describe LogStash::Runner do + + context "argument parsing" do + it "should run agent" do + expect(Stud::Task).to receive(:new).once.and_return(nil) + args = ["agent", "-e", ""] + expect(subject.run(args)).to eq(nil) + end + + it "should run agent help" do + expect(subject).to receive(:show_help).once.and_return(nil) + args = ["agent", "-h"] + expect(subject.run(args)).to eq([]) + end + + it "should run agent help and not run following commands" do + expect(subject).to receive(:show_help).once.and_return(nil) + args = ["agent", "-h", "web"] + expect(subject.run(args)).to eq([]) + end + + it "should run agent and web" do + expect(Stud::Task).to receive(:new).once + args = ["agent", "-e", "", "web"] + args = subject.run(args) + expect(args).to eq(["web"]) + + expect(LogStash::Kibana::Runner).to receive(:new).once.and_return(NullRunner.new) + args = subject.run(args) + expect(args).to eq(nil) + end + end +end