spec/inputs/snmp_spec.rb in logstash-input-snmp-1.3.0 vs spec/inputs/snmp_spec.rb in logstash-input-snmp-1.3.1
- old
+ new
@@ -134,89 +134,172 @@
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
- before do
expect(LogStash::SnmpClient).to receive(:new).and_return(mock_client)
- expect(mock_client).to receive(:get).and_return({"foo" => "bar"})
# devutils in v6 calls close on the test pipelines while it does not in v7+
- expect(mock_client).to receive(:close).at_most(:once)
+ allow(mock_client).to receive(:close).at_most(:once)
end
- it "shoud add @metadata fields and add default host field" do
- config = <<-CONFIG
+ context 'mocked get' do
+
+ before do
+ expect(mock_client).to receive(:get).and_return({"foo" => "bar"})
+ end
+
+ it "shoud add @metadata fields and add default host field" do
+ config = <<-CONFIG
input {
snmp {
get => ["1.3.6.1.2.1.1.1.0"]
hosts => [{ host => "udp:127.0.0.1/161" community => "public" }]
}
}
- CONFIG
- event = input(config) { |_, queue| queue.pop }
+ CONFIG
+ event = input(config) { |_, queue| queue.pop }
- if ecs_select.active_mode == :disabled
- expect(event.get("[@metadata][host_protocol]")).to eq("udp")
- expect(event.get("[@metadata][host_address]")).to eq("127.0.0.1")
- expect(event.get("[@metadata][host_port]")).to eq("161")
- expect(event.get("[@metadata][host_community]")).to eq("public")
- expect(event.get("host")).to eql("127.0.0.1")
- else
- expect(event.get("[@metadata][input][snmp][host][protocol]")).to eq("udp")
- expect(event.get("[@metadata][input][snmp][host][address]")).to eq("127.0.0.1")
- expect(event.get("[@metadata][input][snmp][host][port]")).to eq('161')
- expect(event.get("[@metadata][input][snmp][host][community]")).to eq("public")
- expect(event.get("host")).to eql('ip' => "127.0.0.1")
+ if ecs_select.active_mode == :disabled
+ expect(event.get("[@metadata][host_protocol]")).to eq("udp")
+ expect(event.get("[@metadata][host_address]")).to eq("127.0.0.1")
+ expect(event.get("[@metadata][host_port]")).to eq("161")
+ expect(event.get("[@metadata][host_community]")).to eq("public")
+ expect(event.get("host")).to eql("127.0.0.1")
+ else
+ expect(event.get("[@metadata][input][snmp][host][protocol]")).to eq("udp")
+ expect(event.get("[@metadata][input][snmp][host][address]")).to eq("127.0.0.1")
+ expect(event.get("[@metadata][input][snmp][host][port]")).to eq('161')
+ expect(event.get("[@metadata][input][snmp][host][community]")).to eq("public")
+ expect(event.get("host")).to eql('ip' => "127.0.0.1")
+ end
end
- end
- it "should add custom host field (legacy metadata)" do
- config = <<-CONFIG
+ it "should add custom host field (legacy metadata)" do
+ config = <<-CONFIG
input {
snmp {
get => ["1.3.6.1.2.1.1.1.0"]
hosts => [{ host => "udp:127.0.0.1/161" community => "public" }]
add_field => { host => "%{[@metadata][host_protocol]}:%{[@metadata][host_address]}/%{[@metadata][host_port]},%{[@metadata][host_community]}" }
}
}
- CONFIG
- event = input(config) { |_, queue| queue.pop }
+ CONFIG
+ event = input(config) { |_, queue| queue.pop }
- expect(event.get("host")).to eq("udp:127.0.0.1/161,public")
- end if ecs_select.active_mode == :disabled
+ expect(event.get("host")).to eq("udp:127.0.0.1/161,public")
+ end if ecs_select.active_mode == :disabled
- it "should add custom host field (ECS mode)" do
- config = <<-CONFIG
+ it "should add custom host field (ECS mode)" do
+ config = <<-CONFIG
input {
snmp {
get => ["1.3.6.1.2.1.1.1.0"]
hosts => [{ host => "tcp:192.168.1.11/1161" }]
add_field => { "[host][formatted]" => "%{[@metadata][input][snmp][host][protocol]}://%{[@metadata][input][snmp][host][address]}:%{[@metadata][input][snmp][host][port]}" }
}
}
- CONFIG
- event = input(config) { |_, queue| queue.pop }
+ CONFIG
+ event = input(config) { |_, queue| queue.pop }
- expect(event.get("host")).to eq('formatted' => "tcp://192.168.1.11:1161")
- end if ecs_select.active_mode != :disabled
+ expect(event.get("host")).to eq('formatted' => "tcp://192.168.1.11:1161")
+ end if ecs_select.active_mode != :disabled
- it "should target event data" do
- config = <<-CONFIG
+ it "should target event data" do
+ config = <<-CONFIG
input {
snmp {
get => ["1.3.6.1.2.1.1.1.0"]
hosts => [{ host => "udp:127.0.0.1/161" community => "public" }]
target => "snmp_data"
}
}
- CONFIG
- event = input(config) { |_, queue| queue.pop }
+ CONFIG
+ event = input(config) { |_, queue| queue.pop }
- expect( event.include?('foo') ).to be false
- expect( event.get('[snmp_data]') ).to eql 'foo' => 'bar'
+ expect( event.include?('foo') ).to be false
+ expect( event.get('[snmp_data]') ).to eql 'foo' => 'bar'
+ end
+
end
+
+ context 'mocked nil get response' do
+
+ let(:config) do
+ {
+ 'get' => ["1.3.6.1.2.1.1.1.0"],
+ "hosts" => [{"host" => "udp:127.0.0.1/161", "community" => "public"}]
+ }
+ end
+
+ let(:logger) { double("Logger").as_null_object }
+
+ before do
+ expect(mock_client).to receive(:get).once.and_return(nil)
+ allow_any_instance_of(described_class).to receive(:logger).and_return(logger)
+ expect(logger).not_to receive(:error)
+ end
+
+ it 'generates no events when client returns no response' do
+ input = described_class.new(config).tap { |input| input.register }
+ input.poll_clients queue = Queue.new
+
+ expect( queue.size ).to eql 0
+ end
+ end
+
+ context 'mocked nil table response' do
+
+ let(:config) do
+ {
+ 'tables' => [
+ { 'name' => "a1Table", 'columns' => ["1.3.6.1.4.1.3375.2.2.5.2.3.1.1"] }
+ ],
+ "hosts" => [{"host" => "udp:127.0.0.1/161", "community" => "public"}]
+ }
+ end
+
+ let(:logger) { double("Logger").as_null_object }
+
+ before do
+ expect(mock_client).to receive(:table).once.and_return(nil)
+ allow_any_instance_of(described_class).to receive(:logger).and_return(logger)
+ expect(logger).not_to receive(:error)
+ end
+
+ it 'generates no events when client returns no response' do
+ input = described_class.new(config).tap { |input| input.register }
+ input.poll_clients queue = Queue.new
+
+ expect( queue.size ).to eql 0
+ end
+ end
+
+ context 'mocked nil walk response' do
+
+ let(:config) do
+ {
+ 'walk' => ["1.3.6.1.2.1.1"],
+ "hosts" => [{"host" => "udp:127.0.0.1/161", "community" => "public"}]
+ }
+ end
+
+ let(:logger) { double("Logger").as_null_object }
+
+ before do
+ expect(mock_client).to receive(:walk).once.and_return(nil)
+ allow_any_instance_of(described_class).to receive(:logger).and_return(logger)
+ expect(logger).not_to receive(:error)
+ end
+
+ it 'generates no events when client returns no response' do
+ input = described_class.new(config).tap { |input| input.register }
+ input.poll_clients queue = Queue.new
+
+ expect( queue.size ).to eql 0
+ end
+ end
+
end
context "StoppableIntervalRunner" do
let(:stop_holder) { Struct.new(:value).new(false) }