forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.rb
156 lines (136 loc) · 4.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
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
156
require "insist"
require "logstash/agent"
require "logstash/event"
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
if ENV["TEST_DEBUG"]
Cabin::Channel.get.level = :debug
Cabin::Channel.get.subscribe(STDOUT)
end
module LogStash
module RSpec
def config(configstr)
@config_str = configstr
end # def config
def config_yaml(configstr)
@config_str = configstr
@is_yaml = true
end
def type(default_type)
@default_type = default_type
end
def tags(*tags)
@default_tags = tags
puts "Setting default tags: #{@default_tags}"
end
def sample(event, &block)
default_type = @default_type || "default"
default_tags = @default_tags || []
config = get_config
agent = LogStash::Agent.new
@inputs, @filters, @outputs = agent.instance_eval { parse_config(config) }
[@inputs, @filters, @outputs].flatten.each do |plugin|
plugin.logger = Cabin::Channel.get
plugin.register
end
multiple_events = event.is_a?(Array)
filters = @filters
name = event.to_s
name = name[0..50] + "..." if name.length > 50
describe "\"#{name}\"" do
before :all do
# Coerce to an array of LogStash::Event
event = [event] unless event.is_a?(Array)
event = event.collect do |e|
if e.is_a?(String)
LogStash::Event.new("@message" => e, "@type" => default_type,
"@tags" => default_tags)
else
LogStash::Event.new(e)
end
end
results = []
event.each do |e|
filters.each do |filter|
next if e.cancelled?
filter.filter(e)
end
results << e unless e.cancelled?
end
# do any flushing.
filters.each_with_index do |filter, i|
if filter.respond_to?(:flush)
# get any event from flushing
list = filter.flush
if list
list.each do |e|
filters[i+1 .. -1].each do |f|
f.filter(e)
end
results << e unless e.cancelled?
end
end # if list
end # filter.respond_to?(:flush)
end # filters.each_with_index
@results = results
end # before :all
if multiple_events
subject { @results }
else
subject { @results.first }
end
it("when processed", &block)
end
end # def sample
def input(&block)
config = get_config
agent = LogStash::Agent.new
it "looks good" do
inputs, filters, outputs = agent.instance_eval { parse_config(config) }
block.call(inputs)
end
end # def input
def get_config
if @is_yaml
require "logstash/config/file/yaml"
config = LogStash::Config::File::Yaml.new(nil, @config_str)
else
require "logstash/config/file"
config = LogStash::Config::File.new(nil, @config_str)
end
end # def get_config
def agent(&block)
@agent_count ||= 0
require "logstash/agent"
# scoping is hard, let's go shopping!
config_str = @config_str
describe "agent(#{@agent_count}) #{caller[1]}" do
before :each do
start = ::Time.now
@agent = LogStash::Agent.new
@agent.run(["-e", config_str])
@agent.wait
@duration = ::Time.now - start
end
it("looks good", &block)
end
@agent_count += 1
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