forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.rb
125 lines (107 loc) · 3.11 KB
/
test_utils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
if ENV['COVERAGE']
require 'simplecov'
require 'coveralls'
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
SimpleCov.start do
add_filter 'spec/'
add_filter 'vendor/'
end
end
require "insist"
require "logstash/agent"
require "logstash/pipeline"
require "logstash/event"
require "logstash/logging"
require "insist"
require "stud/try"
$TESTING = true
if RUBY_VERSION < "1.9.2"
$stderr.puts "Ruby 1.9.2 or later is required. (You are running: " + RUBY_VERSION + ")"
$stderr.puts "Options for fixing this: "
$stderr.puts " * If doing 'ruby bin/logstash ...' add --1.9 flag to 'ruby'"
$stderr.puts " * If doing 'java -jar ... ' add -Djruby.compat.version=RUBY1_9 to java flags"
raise LoadError
end
$logger = LogStash::Logger.new(STDOUT)
if ENV["TEST_DEBUG"]
$logger.level = :debug
else
$logger.level = :error
end
module LogStash
module RSpec
def config(configstr)
let(:config) { configstr }
end # def config
def type(default_type)
let(:default_type) { default_type }
end
def tags(*tags)
let(:default_tags) { tags }
puts "Setting default tags: #{@default_tags}"
end
def sample(sample_event, &block)
name = sample_event.is_a?(String) ? sample_event : sample_event.to_json
name = name[0..50] + "..." if name.length > 50
describe "\"#{name}\"" do
extend LogStash::RSpec
let(:pipeline) { LogStash::Pipeline.new(config) }
let(:event) do
sample_event = [sample_event] unless sample_event.is_a?(Array)
next sample_event.collect do |e|
e = { "message" => e } if e.is_a?(String)
next LogStash::Event.new(e)
end
end
let(:results) do
results = []
count = 0
pipeline.instance_eval { @filters.each(&:register) }
event.each do |e|
extra = []
pipeline.filter(e) do |new_event|
extra << new_event
end
results << e if !e.cancelled?
results += extra.reject(&:cancelled?)
end
# TODO(sissel): pipeline flush needs to be implemented.
#results += pipeline.flush
next results
end
subject { results.length > 1 ? results: results.first }
it("when processed", &block)
end
end # def sample
def input(&block)
it "inputs" do
pipeline = LogStash::Pipeline.new(config)
queue = Queue.new
pipeline.instance_eval do
@output_func = lambda { |event| queue << event }
end
block.call(pipeline, queue)
pipeline.shutdown
end
end # def input
def agent(&block)
require "logstash/pipeline"
it("agent(#{caller[0].gsub(/ .*/, "")}) runs") do
pipeline = LogStash::Pipeline.new(config)
pipeline.run
block.call
end
end # def agent
end # module RSpec
end # module LogStash
class Shiftback
def initialize(&block)
@block = block
end
def <<(event)
@block.call(event)
end
end # class Shiftback