/
test_rewrite_tag_name.rb
executable file
·120 lines (110 loc) · 3.55 KB
/
test_rewrite_tag_name.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
require 'helper'
class RewriteTagNameMixinTest < Test::Unit::TestCase
def setup
Fluent::Test.setup
end
CONFIG = %[
tag rewrited.${tag}
]
def create_driver(conf=CONFIG,tag='test')
Fluent::Test::OutputTestDriver.new(Fluent::RewriteTagNameMixinOutput, tag).configure(conf)
end
def test_configure
assert_raise(Fluent::ConfigError) {
d = create_driver('')
}
assert_raise(Fluent::ConfigError) {
d = create_driver('unknown_keys')
}
d = create_driver(CONFIG)
assert_equal 'rewrited.${tag}', d.instance.config['tag']
end
def test_emit
d1 = create_driver(CONFIG, 'input.access')
d1.run do
d1.emit({'message' => 'foo'})
end
emits = d1.emits
assert_equal 1, emits.length
assert_equal 'rewrited.input.access', emits[0][0] # tag
assert_equal 'foo', emits[0][2]['message']
end
def test_emit_upcase
d1 = create_driver(%[
tag rewrited.__TAG__
remove_tag_prefix input.
], 'input.access')
d1.run do
d1.emit({'message' => 'foo'})
end
emits = d1.emits
assert_equal 1, emits.length
assert_equal 'rewrited.access', emits[0][0] # tag
assert_equal 'foo', emits[0][2]['message']
end
def test_emit_with_HandleTagNameMixin
d1 = create_driver(%[
tag rewrited.${tag}
remove_tag_prefix input.
], 'input.access')
d1.run do
d1.emit({'message' => 'foo'})
end
emits = d1.emits
assert_equal 1, emits.length
assert_equal 'rewrited.access', emits[0][0] # tag
assert_equal 'foo', emits[0][2]['message']
end
def test_emit_hostname
d1 = create_driver(%[
tag rewrited.${hostname}
], 'input.access')
d1.run do
d1.emit({'message' => 'foo'})
end
emits = d1.emits
assert_equal 1, emits.length
hostname = `hostname`.chomp
assert_equal "rewrited.#{hostname}", emits[0][0] # tag
assert_equal 'foo', emits[0][2]['message']
end
def test_emit_hostname_short
d1 = create_driver(%[
tag rewrited.${hostname}
hostname_command hostname -s
], 'input.access')
d1.run do
d1.emit({'message' => 'foo'})
end
emits = d1.emits
assert_equal 1, emits.length
hostname_command = d1.instance.config['hostname_command']
hostname = `#{hostname_command}`.chomp
assert_equal "rewrited.#{hostname}", emits[0][0] # tag
assert_equal 'foo', emits[0][2]['message']
end
def test_emit_tag_parts
tag = '0.1.2.3.4'
rules = [
{:conf=>'tag rewrited.${tag_parts[0]}', :expect_tag => 'rewrited.0'},
{:conf=>'tag rewrited.${tag_parts[1]}', :expect_tag => 'rewrited.1'},
{:conf=>'tag rewrited.${tag_parts[-1]}', :expect_tag => 'rewrited.4'},
{:conf=>'tag rewrited.${tag_parts[-2]}', :expect_tag => 'rewrited.3'},
{:conf=>'tag rewrited.${tag_parts[1..1000]}', :expect_tag => 'rewrited.1.2.3.4'},
{:conf=>'tag rewrited.${tag_parts[0..2]}', :expect_tag => 'rewrited.0.1.2'},
{:conf=>'tag rewrited.${tag_parts[0..-3]}', :expect_tag => 'rewrited.0.1.2'},
{:conf=>'tag rewrited.${tag_parts[1..-2]}', :expect_tag => 'rewrited.1.2.3'},
{:conf=>'tag rewrited.${tag_parts[-2..-1]}', :expect_tag => 'rewrited.3.4'},
]
rules.each do |rule|
d = create_driver(rule[:conf], tag)
d.run do
d.emit({'message' => 'foo'})
end
emits = d.emits
assert_equal 1, emits.length
assert_equal rule[:expect_tag], emits[0][0] # tag
assert_equal 'foo', emits[0][2]['message']
end
end
end