forked from logstash-plugins/logstash-patterns-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_helper.rb
63 lines (53 loc) · 1.57 KB
/
spec_helper.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
require "logstash/devutils/rspec/spec_helper"
require 'rspec/expectations'
# running the grok code outside a logstash package means
# LOGSTASH_HOME will not be defined, so let's set it here
# before requiring the grok filter
unless LogStash::Environment.const_defined?(:LOGSTASH_HOME)
LogStash::Environment::LOGSTASH_HOME = File.expand_path("../../", __FILE__)
end
# temporary fix to have the spec pass for an urgen mass-publish requirement.
# cut & pasted from the same tmp fix in the grok spec
# see https://github.com/logstash-plugins/logstash-filter-grok/issues/72
# this needs to be refactored and properly fixed
module LogStash::Environment
# also :pattern_path method must exist so we define it too
unless self.method_defined?(:pattern_path)
def pattern_path(path)
::File.join(LOGSTASH_HOME, "patterns", path)
end
end
end
require "logstash/filters/grok"
module GrokHelpers
def grok_match(label, message)
grok = build_grok(label)
event = build_event(message)
grok.filter(event)
event.to_hash
end
def build_grok(label)
grok = LogStash::Filters::Grok.new("match" => ["message", "%{#{label}}"])
grok.register
grok
end
def build_event(message)
LogStash::Event.new("message" => message)
end
end
RSpec.configure do |c|
c.include GrokHelpers
end
RSpec::Matchers.define :pass do |expected|
match do |actual|
!actual.include?("tags")
end
end
RSpec::Matchers.define :match do |value|
match do |grok|
grok = build_grok(grok)
event = build_event(value)
grok.filter(event)
!event.include?("tags")
end
end