spec/inputs/exec_spec.rb in logstash-input-exec-3.3.3 vs spec/inputs/exec_spec.rb in logstash-input-exec-3.4.0
- old
+ new
@@ -1,77 +1,106 @@
# encoding: utf-8
require "timecop"
require "time"
require_relative "../spec_helper"
+require "logstash/devutils/rspec/shared_examples"
+require 'logstash/plugin_mixins/ecs_compatibility_support/spec_helper'
-describe LogStash::Inputs::Exec do
+describe LogStash::Inputs::Exec, :ecs_compatibility_support do
- context "when register" do
- let(:input) { described_class.new("command" => "ls", "interval" => 0) }
- it "should not raise error if config is valid" do
- # register will try to load jars and raise if it cannot find jars or if org.apache.log4j.spi.LoggingEvent class is not present
- expect {input.register}.to_not raise_error
+ ecs_compatibility_matrix(:disabled, :v1, :v8) do |ecs_select|
+
+ before(:each) do
+ allow_any_instance_of(described_class).to receive(:ecs_compatibility).and_return(ecs_compatibility)
end
- context "with an invalid config" do
- let(:input) { described_class.new("command" => "ls") }
- it "should raise error" do
- expect {input.register}.to raise_error(LogStash::ConfigurationError)
+
+ context "when register" do
+ let(:input) { described_class.new("command" => "ls", "interval" => 0) }
+
+ it "should not raise error if config is valid" do
+ # register will try to load jars and raise if it cannot find jars or if org.apache.log4j.spi.LoggingEvent class is not present
+ expect { input.register }.to_not raise_error
end
+
+ context "with an invalid config" do
+ let(:input) { described_class.new("command" => "ls") }
+ it "should raise error" do
+ expect { input.register }.to raise_error(LogStash::ConfigurationError)
+ end
+ end
end
- end
- context "when operating normally" do
- let(:input) { described_class.new("command" => "ls", "interval" => 0) }
- let(:queue) { [] }
- let(:loggr) { double('loggr') }
+ context "when operating normally" do
+ let(:input) { described_class.new("command" => "echo 'hi!'", "interval" => 0) }
+ let(:queue) { [] }
- before :each do
- expect(described_class).to receive(:logger).and_return(loggr).exactly(7).times
- allow(loggr).to receive(:info)
- allow(loggr).to receive(:info?)
- allow(loggr).to receive(:warn)
- allow(loggr).to receive(:warn?)
- allow(loggr).to receive(:debug)
- allow(loggr).to receive(:debug?)
- end
+ before :each do
+ input.register
+ end
- it "enqueues some events" do
- input.register
- expect(loggr).not_to receive(:error)
+ it "enqueues some events" do
+ expect(input.logger).not_to receive(:error)
- input.execute(queue)
+ input.execute(queue)
- expect(queue.size).not_to be_zero
+ expect(queue.size).not_to be_zero
+ end
end
- end
- context "when a command runs normally" do
- let(:input) { described_class.new("command" => "/bin/sh -c 'sleep 1; /bin/echo -n two; exit 3'", "interval" => 0) }
- let(:queue) { [] }
+ context "when a command runs normally" do
+ let(:command) { "/bin/sh -c 'sleep 1; /bin/echo -n two; exit 3'" }
+ let(:input) { described_class.new("command" => command, "interval" => 0) }
+ let(:queue) { [] }
- before do
- input.register
- input.execute(queue)
- end
+ before do
+ input.register
+ input.execute(queue)
+ end
- after do
- input.stop
- end
+ after do
+ input.stop
+ end
- it "has duration tracked" do
- expect(queue.pop.get('[@metadata][duration]')).to be > 1
+ it "has duration (in seconds)" do
+ duration = queue.pop.get('[@metadata][duration]')
+ expect(duration).to be_a Float
+ expect(duration).to be > 1
+ expect(duration).to be < 3
+ end if ecs_select.active_mode == :disabled
+
+ it "reports process elapsed time (in nanos)" do
+ elapsed_time = queue.pop.get('[@metadata][input][exec][process][elapsed_time]')
+ expect(elapsed_time).to be_a Integer
+ expect(elapsed_time).to be > 1 * 1_000_000
+ expect(elapsed_time).to be < 3 * 1_000_000
+ end if ecs_select.active_mode != :disabled
+
+ it "has output as expected" do
+ expect(queue.pop.get('message')).to eq "two"
+ end
+
+ it "reports process command_line " do
+ if ecs_select.active_mode == :disabled
+ expect(queue.pop.get('command')).to eql command
+ else
+ expect(queue.pop.get('[process][command_line]')).to eql command
+ end
+ end
+
+ it "reports process exit_code" do
+ if ecs_select.active_mode == :disabled
+ expect(queue.pop.get('[@metadata][exit_status]')).to eq 3
+ else
+ expect(queue.pop.get('[process][exit_code]')).to eq 3
+ end
+ end
+
end
- it "has output as expected" do
- expect(queue.pop.get('message')).to eq "two"
- end
- it "has exit_status tracked" do
- expect(queue.pop.get('[@metadata][exit_status]')).to eq 3
- end
end
context "when scheduling" do
- let(:input) { described_class.new("command" => "ls", "schedule" => "* * * * * UTC") }
+ let(:input) { described_class.new("command" => "ls --help", "schedule" => "* * * * * UTC") }
let(:queue) { [] }
before do
input.register
end