Class: LogStash::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/test.rb

Instance Method Summary collapse

Constructor Details

#initializeTest

Returns a new instance of Test.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/logstash/test.rb', line 15

def initialize
  log_to(STDERR)

  # This is lib/logstash/test.rb, so go up 2 directories for the plugin path
  if jarred?(__FILE__)
    @plugin_paths = [ File.dirname(File.dirname(__FILE__)) ]
  else
    @plugin_paths = [ File.dirname(File.dirname(File.dirname(__FILE__))) ]
  end 
  @verbose = 0
end

Instance Method Details

#check_lib(lib, provider, is = :optional, message = nil) ⇒ Object

def log_to



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/logstash/test.rb', line 37

def check_lib(lib, provider, is=:optional, message=nil)
  optional = (is == :optional)
  begin
    require lib
    puts "+ Found #{optional ? "optional" : "required"} library '#{lib}'"
    return { :optional => optional, :found => true }
  rescue LoadError => e
    puts "- Missing #{optional ? "optional" : "required"} library '#{lib}'" \
         "- try 'gem install #{provider}'" \
         "#{optional ? " if you want this library" : ""}. #{message}"
    return { :optional => optional, :found => false }
  end
end

#check_librariesObject

def report_ruby_version



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/logstash/test.rb', line 55

def check_libraries
  results = [
    # main agent
    check_lib("grok-pure", "jls-grok", :optional, "needed for the grok filter."),
    check_lib("bunny", "bunny", :optional, "needed for AMQP input and output"),
    check_lib("uuidtools", "uuidtools", :required,
              "needed for AMQP input and output"),
    check_lib("ap", "awesome_print", :optional, "improve debug logging output"),
    check_lib("json", "json", :required, "required for logstash to function"),
    check_lib("filewatch/tail", "filewatch", :optional,
              "required for file input"),
    check_lib("jruby-elasticsearch", "jruby-elasticsearch", :optional,
              "required for elasticsearch output and for logstash web"),
    check_lib("stomp", "stomp", :optional,
              "required for stomp input and output"),
    check_lib("mongo", "mongo", :optional, "required for mongo output"),
    check_lib("redis", "redis", :optional,
              "required for stomp input and output"),
    check_lib("gelf", "gelf", :optional, "required for gelf (graylog2) output"),
    check_lib("statsd", "statsd-ruby", :optional, "required for statsd output"),

    # logstash web
    check_lib("ftw", "ftw", :required, "needed for logstash web"),
    check_lib("rack", "rack", :required, "needed for logstash web"),
    check_lib("sinatra", "sinatra", :required, "needed for logstash web"),
    check_lib("sass", "sass", :required, "needed for logstash web"),
    check_lib("haml", "haml", :required, "needed for logstash web"),
  ]

  missing_required = results.count { |r| !r[:optional] and !r[:found] }
  if missing_required == 0
    puts "All required libraries found :)"
  else
    suffix = (missing_required > 1) ? "ies" : "y"
    puts "FATAL: Missing #{missing_required} required librar#{suffix}"
    return false
  end

  return true
end

#each_test(basepath, &block) ⇒ Object

Find tests in a given path. Tests must be in the plugin path + “/test/…/test_*.rb”



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/logstash/test.rb', line 164

def each_test(basepath, &block)
  if jarred?(basepath)
    # No test/logstash/... hierarchy in the jar, not right now anyway.
    glob_path = File.join(basepath, "logstash", "**", "test_*.rb")
  else
    glob_path = File.join(basepath, "test", "**", "test_*.rb")
  end
  @logger.info("Searching for tests", :path => glob_path)
  Dir.glob(glob_path).each do |path|
    block.call(path)
  end
end

#load_tests(path) ⇒ Object

def each_test



177
178
179
180
181
182
# File 'lib/logstash/test.rb', line 177

def load_tests(path)
  each_test(path) do |test|
    @logger.info("Loading test", :test => test)
    require test
  end
end

#log_to(target) ⇒ Object



33
34
35
# File 'lib/logstash/test.rb', line 33

def log_to(target)
  @logger = LogStash::Logger.new(target)
end

#report_ruby_versionObject

def check_lib



51
52
53
# File 'lib/logstash/test.rb', line 51

def report_ruby_version
  puts "Running #{RUBY_VERSION}p#{RUBY_PATCHLEVEL} on #{RUBY_PLATFORM}"
end

#run(args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/logstash/test.rb', line 123

def run(args)
  remainder = options(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): 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

    require "minitest/spec"
    @status = MiniTest::Unit.new.run(ARGV)
  end # the runner thread
  return remainder
end

#waitObject

def run



157
158
159
160
# File 'lib/logstash/test.rb', line 157

def wait
  @thread.join
  return @status
end