- clean up lots of tests

- 'syntax check' test now loads it into ruby instead of using
  system("ruby -c ... ")
- generalized the plugin_path testing improvements by crankycoder
  to run the main test suite as well.
This commit is contained in:
Jordan Sissel 2011-10-28 01:12:40 -07:00
parent 46b89d59c3
commit 8bade5da2f
13 changed files with 98 additions and 121 deletions

View file

@ -8,7 +8,7 @@ gem "cabin", "0.1.3" # for logging. apache 2 license
gem "bunny" # for amqp support, MIT-style license
gem "uuidtools" # for naming amqp queues, License ???
gem "filewatch", "0.3.0" # for file tailing, BSD License
gem "jls-grok", "0.9.1" # for grok filter, BSD License
gem "jls-grok", "0.9.2" # for grok filter, BSD License
jruby? and gem "jruby-elasticsearch", "0.0.11" # BSD License
gem "stomp" # for stomp protocol, Apache 2.0 License
gem "json" # Ruby license

View file

@ -9,15 +9,16 @@ GEM
cabin (0.1.3)
json
columnize (0.3.4)
ffi-rzmq (0.8.2)
ffi-rzmq (0.9.0)
filewatch (0.3.0)
gelf (1.1.3)
json
gelfd (0.1.0)
gmetric (0.1.3)
haml (3.1.3)
jls-grok (0.9.1)
jruby-elasticsearch (0.0.12)
jls-grok (0.9.2)
cabin
jruby-elasticsearch (0.0.11)
jruby-openssl (0.7.4)
bouncy-castle-java
json (1.6.1)
@ -35,7 +36,6 @@ GEM
rack (1.3.4)
rack-protection (1.1.4)
rack
rake (0.9.2)
rbx-require-relative (0.0.5)
redis (2.2.2)
ruby-debug (0.10.4)
@ -63,27 +63,26 @@ DEPENDENCIES
awesome_print
bunny
cabin (= 0.1.3)
ffi-rzmq
filewatch (~> 0.3.0)
ffi-rzmq (= 0.9.0)
filewatch (= 0.3.0)
gelf
gelfd (~> 0.1.0)
gmetric (~> 0.1.3)
gelfd (= 0.1.0)
gmetric (= 0.1.3)
haml
jls-grok (= 0.9.0)
jruby-elasticsearch (~> 0.0.11)
jls-grok (= 0.9.2)
jruby-elasticsearch (= 0.0.11)
jruby-openssl
json
minitest
mizuno
mocha
mocha (= 0.10.0)
mongo
rack
rake
redis
ruby-debug
ruby-debug (= 0.10.4)
sass
sinatra
statsd-ruby (~> 0.3.0)
statsd-ruby (= 0.3.0)
stomp
uuidtools
xmpp4r (~> 0.5)
xmpp4r (= 0.5)

View file

@ -43,10 +43,10 @@ class LogStash::Config::File
plugin = registry[o[:plugin]]
if type.nil?
puts "Unknown config #{o[:type]}/#{o[:plugin]}"
@logger.info("Unknown plugin", :type => o[:type], :plugin => o[:plugin])
end
yield :type => type, :plugin => plugin, :parameters => o[:parameters]
end
end # def parse

View file

@ -149,7 +149,7 @@ class LogStash::Filters::Grok < LogStash::Filters::Base
if !@patterns.include?(field)
@patterns[field] = Grok::Pile.new
@patterns[field].logger = @logger
#@patterns[field].logger = @logger
add_patterns_from_files(@patternfiles, @patterns[field])
end

View file

@ -21,6 +21,7 @@ class LogStash::Plugin
def initialize(params=nil)
@params = params
@logger = LogStash::Logger.new(STDOUT)
@logger.level = :warn
end
# This method is called when someone or something wants this plugin to shut

View file

@ -18,6 +18,7 @@ class LogStash::Search::ElasticSearch < LogStash::Search::Base
@bind_host = (settings[:bind_host] || nil)
@type = (settings[:type] || :node)
@logger = LogStash::Logger.new(STDOUT)
@logger.level = :warn
@client = ElasticSearch::Client.new(:host => @host, :port => @port,
:cluster => @cluster,
:bind_host => @bind_host,

View file

@ -1,4 +1,4 @@
require "em-http-request"
#require "em-http-request"
require "logstash/namespace"
require "logstash/logging"
require "logstash/event"
@ -9,6 +9,7 @@ require "logstash/search/facetresult"
require "logstash/search/facetresult/histogram"
# TODO(sissel): This won't work anymore (we don't use EM right now)
# TODO(sissel): Update this to be functional again?
class LogStash::Search::Twitter < LogStash::Search::Base
public

View file

@ -1,21 +1,24 @@
require "rubygems"
require "optparse"
# TODO(sissel): Are these necessary anymore?
$:.unshift "#{File.dirname(__FILE__)}/../lib"
$:.unshift "#{File.dirname(__FILE__)}/../test"
require "logstash/namespace"
require "logstash/loadlibs"
require "logstash/logging"
require "ruby-debug"
class LogStash::Test
public
def initialize
log_to(STDERR)
# initialize to an empty pluginpath
@verbose = 0
@plugin_paths = []
end
public
def initialize
log_to(STDERR)
# This is lib/logstash/test.rb, so go up 2 directories for the plugin path
@plugin_paths = [ File.join(File.dirname(__FILE__), "..", "..") ]
@verbose = 0
end # def initialize
public
def log_to(target)
@ -34,11 +37,11 @@ class LogStash::Test
"#{optional ? " if you want this library" : ""}. #{message}"
return { :optional => optional, :found => false }
end
end
end # def check_lib
def report_ruby_version
puts "Running #{RUBY_VERSION}p#{RUBY_PATCHLEVEL} on #{RUBY_PLATFORM}"
end
end # def report_ruby_version
def check_libraries
results = [
@ -79,114 +82,88 @@ class LogStash::Test
end
return true
end
def run_tests(args)
require "logstash_test_runner"
return MiniTest::Unit.new.run(args)
end # def run_tests
end # def check_libraries
# Parse options.
private
def extend_pluginpath(args)
def options(args)
# strip out the pluginpath argument if it exists and
# extend the LOAD_PATH for the ruby runtime
opts = OptionParser.new
opts.on("-v", "Increase verbosity") do
@verbose += 1
if @verbose >= 3 # Uber debugging.
@logger.level = :debug
$DEBUG = true
elsif @verbose == 2 # logstash debug logs
@logger.level = :debug
elsif @verbose == 1 # logstash info logs
@logger.level = :info
else # Default log level
@logger.level = :warn
end
end
# Step one is to add test flags.
opts.on("--pluginpath PLUGINPATH",
"Load plugins and test from a pluginpath") do |path|
@plugin_paths << path
@plugin_paths.each do |p|
@logger.debug("Adding to ruby load path", :path => p)
runner = PluginTestRunner.new p
$:.unshift p
runner.load_tests()
puts "Added to ruby load :path = [#{p}]"
end
end # --pluginpath PLUGINPATH
begin
remainder = opts.parse(args)
rescue OptionParser::InvalidOption => e
@logger.info("Invalid option", :exception => e)
raise e
end
return remainder
end # def extend_pluginpath
end # def options
public
def run(args)
remainder = options(args)
args = extend_pluginpath(args)
if @verbose >= 3 # Uber debugging.
@logger.level = :debug
$DEBUG = true
elsif @verbose == 2 # logstash debug logs
@logger.level = :debug
elsif @verbose == 1 # logstash info logs
@logger.level = :info
else # Default log level
@logger.level = :warn
end
@success = true
@thread = Thread.new do
report_ruby_version
# TODO(sissel): Add a way to call out specific things to test, like
# logstash-web, elasticsearch, mongodb, syslog, etc.
if !check_libraries
puts "Library check failed."
@success = false
# TODO(sissel): Rewrite this into a proper test?
#if !check_libraries
#puts "Library check failed."
#@success = false
#end
@plugin_paths.each do |path|
load_tests(path)
end
if !run_tests(args)
puts "Test suite failed."
@success = false
end
end
return args
require "minitest/spec"
@status = MiniTest::Unit.new.run(ARGV)
end # the runner thread
return remainder
end # def run
def wait
@thread.join
return @success ? 0 : 1
return @status
end # def wait
# Find tests in a given path. Tests must be in the plugin path +
# "/test/.../test_*.rb"
def each_test(basepath, &block)
glob_path = File.join(basepath, "test", "**", "test_*.rb")
@logger.info("Searching for tests", :path => glob_path)
Dir.glob(glob_path).each do |path|
block.call(path)
end
end # def each_test
def load_tests(path)
each_test(path) do |test|
@logger.info("Loading test", :test => test)
require test
end
end # def load_tests
end # class LogStash::Test
class PluginTestRunner
def initialize(rootpath)
@rootpath = rootpath
end
def _discover_tests()
glob_path = File.join(@rootpath, "**", "test_*.rb")
puts "Searching [#{glob_path}]"
Dir.glob(glob_path).each do|f|
yield f
end
end
def load_tests()
_discover_tests() do |path|
path_parts = path.split(File::SEPARATOR).map {|x| x=="" ? File::SEPARATOR : x}
test_module = File.join(path_parts.slice(1, path_parts.length + 1))
test_module = test_module.sub(".rb", '')
puts "Loading test module: #{test_module}"
require test_module
puts "Loaded : [#{test_module}]"
end
end
end

View file

@ -1,3 +1,5 @@
require "sinatra/base" # gem sinatra
class LogStash::Web::Server < Sinatra::Base
get '/search' do
# TODO(sissel): Refactor this to use the api_search

View file

@ -1,3 +1,5 @@
require "sinatra/base"
class LogStash::Web::Server < Sinatra::Base
# Mizuno can't serve static files from a jar
# https://github.com/matadon/mizuno/issues/9

View file

@ -9,9 +9,7 @@ require "logstash/event"
require "timeout"
describe LogStash::Filters::Date do
# These tests assume a given timezone.
before do
ENV["TZ"] = "PST8PDT"
@typename = "foozle"
end
@ -60,17 +58,15 @@ describe LogStash::Filters::Date do
end # testing ISO8601
test "parsing with java SimpleDateFormat syntax" do
#config "field1" => "%b %e %H:%M:%S"
config "field1" => "MMM dd HH:mm:ss"
config "field1" => "MMM dd HH:mm:ss Z"
now = Time.now
now += now.gmt_offset
year = now.year
require 'java'
skip("this test assumes pacific time - won't work anywhere else")
times = {
"Nov 24 01:29:01" => "#{year}-11-24T09:29:01.000Z",
"Nov 24 01:29:01 -0800" => "#{year}-11-24T09:29:01.000Z",
}
event = LogStash::Event.new
@ -83,15 +79,14 @@ describe LogStash::Filters::Date do
end # SimpleDateFormat tests
test "performance" do
config "field1" => "MMM dd HH:mm:ss"
config "field1" => "MMM dd HH:mm:ss Z"
iterations = 50000
start = Time.now
gmt_now = start + start.gmt_offset
year = gmt_now.year
skip("This test assumes PDT")
input = "Nov 24 01:29:01"
input = "Nov 24 01:29:01 -0800"
output = "#{year}-11-24T09:29:01.000Z"
event = LogStash::Event.new

View file

@ -7,10 +7,10 @@ require "logstash"
# Autorun if the caller script is the name of the process.
# Sort of like 'if $0 == __FILE__' but works with require()
parent = caller.collect {
|c| c.gsub(/:[0-9]+(:.*)$/, "")
}.find { |c| c != __FILE__ }
require "minitest/autorun" if parent == $0 or ENV["AUTORUN"]
#parent = caller.collect {
#|c| c.gsub(/:[0-9]+(:.*)$/, "")
#}.find { |c| c != __FILE__ }
#require "minitest/autorun" if parent == $0 or ENV["AUTORUN"]
# I don't really like monkeypatching, but whatever, this is probably better
# than overriding the 'describe' method.

View file

@ -3,7 +3,6 @@ $:.unshift File.dirname(__FILE__) + "/../../lib"
$:.unshift File.dirname(__FILE__) + "/../"
require "minitest/spec"
require "minitest/autorun" if $0 == __FILE__
describe "syntax check" do
source = File.join(File.dirname(__FILE__), "..", "..", "lib", "logstash", "**", "*.rb")
@ -13,12 +12,12 @@ describe "syntax check" do
# We could use 'load' here but that implies a bunch more than just syntax
# checking. Most especially it will fail if we try to use java libraries
# not currently in the classpath.
#begin
#load path
#rescue LoadError => e
#flunk("Error loading #{path}: #{e.inspect}")
#end
assert(system("ruby", "-c", path), "Error parsing #{path}")
begin
load path
rescue LoadError => e
flunk("Error loading #{path}: #{e.inspect}")
end
#assert(system("ruby", "-c", path), "Error parsing #{path}")
end # syntax check a file
end # find all ruby files
end # syntax check