mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 06:37:19 -04:00
- Refactor test harness so to make writing logstash tests easier.
- Add inputs/syslog test - Refactor inputs/file test
This commit is contained in:
parent
c20f562033
commit
06aeb47398
4 changed files with 136 additions and 23 deletions
|
@ -1,35 +1,30 @@
|
|||
#!/usr/bin/env ruby
|
||||
require 'rubygems'
|
||||
$:.unshift File.dirname(__FILE__) + "/../../lib"
|
||||
$:.unshift File.dirname(__FILE__) + "/../../../lib"
|
||||
$:.unshift File.dirname(__FILE__) + "/../../"
|
||||
require "test/unit"
|
||||
require "logstash"
|
||||
require "logstash/filters"
|
||||
require "logstash/event"
|
||||
require "tempfile"
|
||||
require "socket"
|
||||
require "logstash/testcase"
|
||||
require "logstash/agent"
|
||||
|
||||
|
||||
# TODO(sissel): refactor this so we can more easily specify tests.
|
||||
class TestInputFile < Test::Unit::TestCase
|
||||
class TestInputFile < LogStash::TestCase
|
||||
def em_setup
|
||||
@tmpfile = Tempfile.new(self.class.name)
|
||||
@type = "default"
|
||||
@hostname = Socket.gethostname
|
||||
|
||||
config = YAML.load <<-"YAML"
|
||||
inputs:
|
||||
#{@type}:
|
||||
- file://#{@tmpfile.path}
|
||||
outputs:
|
||||
- internal:///
|
||||
YAML
|
||||
config = {
|
||||
"inputs" => {
|
||||
@type => [
|
||||
"file://#{@tmpfile.path}"
|
||||
],
|
||||
},
|
||||
"outputs" => [
|
||||
"internal:///"
|
||||
]
|
||||
} # config
|
||||
|
||||
@output = EventMachine::Channel.new
|
||||
@agent = LogStash::Agent.new(config)
|
||||
@agent.register
|
||||
@agent.outputs[0].callback do |event|
|
||||
@output.push(event)
|
||||
end
|
||||
super(config)
|
||||
end
|
||||
|
||||
def test_simple
|
||||
|
@ -51,8 +46,8 @@ class TestInputFile < Test::Unit::TestCase
|
|||
|
||||
# Write to the file periodically
|
||||
timer = EM::PeriodicTimer.new(0.2) do
|
||||
a = data.shift((rand * 3).to_i + 1).join("\n")
|
||||
@tmpfile.puts a
|
||||
out = data.shift((rand * 3).to_i + 1).join("\n")
|
||||
@tmpfile.puts out
|
||||
@tmpfile.flush
|
||||
timer.cancel if data.length == 0
|
||||
end
|
||||
|
|
77
test/logstash/inputs/test_syslog.rb
Normal file
77
test/logstash/inputs/test_syslog.rb
Normal file
|
@ -0,0 +1,77 @@
|
|||
|
||||
$:.unshift File.dirname(__FILE__) + "/../../../lib"
|
||||
$:.unshift File.dirname(__FILE__) + "/../../"
|
||||
|
||||
require "logstash/testcase"
|
||||
require "logstash/agent"
|
||||
|
||||
class TestInputSyslog < LogStash::TestCase
|
||||
def em_setup
|
||||
config = {
|
||||
"inputs" => {
|
||||
@type => [
|
||||
]
|
||||
},
|
||||
"outputs" => [
|
||||
"internal:///"
|
||||
]
|
||||
}
|
||||
|
||||
done = false
|
||||
# TODO(sissel): refactor this into something reusable?
|
||||
1.upto(30) do
|
||||
begin
|
||||
# Grab a a random port to listen on.
|
||||
#@port = (rand * 30000 + 20000).to_i
|
||||
@port = 12345
|
||||
config["inputs"][@type] = ["syslog://127.0.0.1:#{@port}"]
|
||||
super(config)
|
||||
done = true
|
||||
break
|
||||
rescue => e
|
||||
# Verified working with EventMachine 0.12.10
|
||||
if e.is_a?(RuntimeError) && e.message == "no acceptor"
|
||||
# ignore, it's likely we tried to listen on a port already in use.
|
||||
else
|
||||
raise e
|
||||
end
|
||||
end # rescue
|
||||
end # loop for an ephemeral port
|
||||
|
||||
if !done
|
||||
raise "Couldn't find a port to bind on."
|
||||
end
|
||||
|
||||
# Override input.
|
||||
@connection = EventMachine::connect("127.0.0.1", @port)
|
||||
@input = EventMachine::Channel.new
|
||||
@input.subscribe do |message|
|
||||
@connection.send_data(message)
|
||||
end
|
||||
end # def em_setup
|
||||
|
||||
def test_syslog_normal
|
||||
inputs = [
|
||||
"<1>Dec 19 12:30:48 snack nagios3: Auto-save of retention data completed successfully.",
|
||||
"<2>Dec 19 11:35:32 carrera sshd[28882]: Failed password for invalid user PlcmSpIp from 121.9.210.245 port 48846 ssh2"
|
||||
]
|
||||
|
||||
EventMachine.run do
|
||||
em_setup
|
||||
|
||||
expected_messages = inputs.clone
|
||||
@output.subscribe do |event|
|
||||
expect = expected_messages.shift
|
||||
#assert_equal(expect, event.message)
|
||||
assert_equal(expect.split(" ", 5)[-1], event.message)
|
||||
if expected_messages.size == 0
|
||||
@agent.stop
|
||||
end
|
||||
end
|
||||
|
||||
EM::PeriodicTimer.new(0.3) do
|
||||
@input.push inputs.shift + "\n" if inputs.size > 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end # class TestInputSyslog
|
39
test/logstash/testcase.rb
Normal file
39
test/logstash/testcase.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
|
||||
require 'rubygems'
|
||||
$:.unshift File.dirname(__FILE__) + "/../../lib"
|
||||
|
||||
require "test/unit"
|
||||
require "socket"
|
||||
require "logstash/namespace"
|
||||
require "logstash/outputs/internal"
|
||||
require "logstash/inputs/internal"
|
||||
|
||||
class LogStash::TestCase < Test::Unit::TestCase
|
||||
def setup
|
||||
super
|
||||
@type = "default"
|
||||
@hostname = Socket.gethostname
|
||||
end
|
||||
|
||||
def em_setup(config = {})
|
||||
@agent = LogStash::Agent.new(config)
|
||||
@agent.register
|
||||
|
||||
@output = EventMachine::Channel.new
|
||||
output = @agent.outputs.find { |o| o.is_a?(LogStash::Outputs::Internal) }
|
||||
|
||||
if output
|
||||
output.callback do |event|
|
||||
@output.push(event)
|
||||
end
|
||||
end
|
||||
|
||||
input = @agent.inputs.find { |o| o.is_a?(LogStash::Inputs::Internal) }
|
||||
@input = input.channel if input
|
||||
end # def em_setup
|
||||
|
||||
# We have to include at least one test here, otherwise Test::Unit barfs about
|
||||
# not tests for this class, even though it's just a superclass for real test
|
||||
# cases.
|
||||
def test_ok; end
|
||||
end # class LogStash::TestCase
|
|
@ -5,3 +5,5 @@ require "logstash/test_syntax"
|
|||
require "logstash/filters/test_date"
|
||||
require "logstash/filters/test_grep"
|
||||
require "logstash/filters/test_multiline"
|
||||
require "logstash/inputs/test_file"
|
||||
require "logstash/inputs/test_syslog"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue