-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_out_irc.rb
More file actions
261 lines (211 loc) · 6.94 KB
/
Copy pathtest_out_irc.rb
File metadata and controls
261 lines (211 loc) · 6.94 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require 'helper'
require 'socket'
class IRCOutputTest < Test::Unit::TestCase
include Fluent::Test::Helpers
TAG = "test"
PORT = 6667
CHANNEL = "fluentd"
NICK = "fluentd"
USER = "fluentd"
REAL = "fluentd"
COMMAND = :notice
MESSAGE = "notice: %s [%s] %s"
TIME_FORMAT = "%Y/%m/%d %H:%M:%S"
def setup
Fluent::Test.setup
Fluent::Engine.now = Time.now
end
def config(options = {})
data = %[
type irc
host localhost
port #{options.fetch(:port, PORT)}
channel #{options.fetch(:channel, CHANNEL)}
channel_keys #{options.fetch(:channel_keys, "")}
nick #{NICK}
user #{USER}
real #{REAL}
command #{options.fetch(:command, COMMAND.to_s)}
message #{MESSAGE}
out_keys tag,time,msg
time_key time
time_format #{TIME_FORMAT}
tag_key tag
send_queue_limit 10
send_interval 0.5
]
command_keys = options.fetch(:command_keys, nil)
data += %[command_keys #{command_keys}] if command_keys
data
end
def create_driver(conf = config)
Fluent::Test::Driver::Output.new(Fluent::Plugin::IRCOutput).configure(conf)
end
def test_configure
d = create_driver
assert_equal "localhost", d.instance.host
assert_equal PORT, d.instance.port
assert_equal "##{CHANNEL}", d.instance.channel
assert_equal NICK, d.instance.nick
assert_equal USER, d.instance.user
assert_equal REAL, d.instance.real
assert_equal COMMAND, d.instance.command
assert_equal MESSAGE, d.instance.message
assert_equal ["tag","time","msg"], d.instance.out_keys
assert_equal "time", d.instance.time_key
assert_equal TIME_FORMAT, d.instance.time_format
assert_equal "tag", d.instance.tag_key
assert_equal 10, d.instance.send_queue_limit
assert_equal 0.5, d.instance.send_interval
end
def test_configure_channel_keys
d = create_driver(config({channel:"%s", channel_keys:"channel"}))
assert_equal "#%s", d.instance.channel
assert_equal ["channel"], d.instance.channel_keys
end
def test_configure_command_keys
d = create_driver(config({command:"%s", command_keys:"command"}))
assert_equal "%s", d.instance.command
assert_equal ["command"], d.instance.command_keys
end
def test_configure_command
assert_raise Fluent::ConfigError do
create_driver(config({command: 'foo'}))
end
assert_nothing_raised { create_driver(config({command: 'priv_msg'})) }
assert_nothing_raised { create_driver(config({command: 'privmsg'})) }
assert_nothing_raised { create_driver(config({command: 'notice'})) }
end
def test_emit
msg = "test"
msgs = [{"msg" => msg}]
body = MESSAGE % [TAG, Time.at(Fluent::Engine.now).strftime(TIME_FORMAT), msg]
m = {}
emit_test(msgs) do |socket|
socket.set_encoding("utf-8")
s = IRCParser.parse(socket.gets)
m[s.class.to_sym] = s
s = IRCParser.parse(socket.gets)
m[s.class.to_sym] = s
s = IRCParser.parse(socket.gets)
m[s.class.to_sym] = s
s = IRCParser.parse(socket.gets&.force_encoding("utf-8"))
m[s.class.to_sym] = s
assert_equal ["##{CHANNEL}"], m[:join].channels
assert_equal "##{CHANNEL}", m[COMMAND].target
assert_equal body, m[COMMAND].body
assert_equal NICK, m[:nick].nick
assert_equal USER, m[:user].user
assert_equal REAL, m[:user].postfix
assert_nil socket.gets # expects EOF
end
end
def test_dynamic_channel
omit("Unstable with v0.14 test driver. How to fix it? :<") if ENV["CI"]
msgs = [
{"msg" => "test", "channel" => "chan1"},
{"msg" => "test", "channel" => "chan2"},
{"msg" => "test", "channel" => "chan1"},
]
extra_config = {
channel: "%s",
channel_keys: "channel",
}
emit_test(msgs, extra_config: extra_config) do |socket|
socket.gets # ignore NICK
socket.gets # ignore USER
m = IRCParser.parse(socket.gets)
assert_equal :join, m.class.to_sym, :join
assert_equal ["#chan1"], m.channels
m = IRCParser.parse(socket.gets)
assert_equal COMMAND, m.class.to_sym
assert_equal "#chan1", m.target
m = IRCParser.parse(socket.gets)
assert_equal :join, m.class.to_sym
assert_equal ["#chan2"], m.channels
m = IRCParser.parse(socket.gets)
assert_equal COMMAND, m.class.to_sym
assert_equal "#chan2", m.target
m = IRCParser.parse(socket.gets)
assert_equal COMMAND, m.class.to_sym
assert_equal "#chan1", m.target
assert_nil socket.gets # expects EOF
end
end
def test_dynamic_command
omit("Unstable with v0.14 test driver. How to fix it? :<") if ENV["CI"]
msgs = [
{"msg" => "test", "command" => "privmsg"},
{"msg" => "test", "command" => "priv_msg"},
{"msg" => "test", "command" => "notice"},
{"msg" => "test", "command" => "something_wrong"},
]
extra_config = {
command: "%s",
command_keys: "command",
}
emit_test(msgs, extra_config: extra_config) do |socket|
m = IRCParser.parse(socket.gets)
assert_equal :join, m.class.to_sym
m = IRCParser.parse(socket.gets)
assert_equal :priv_msg, m.class.to_sym
m = IRCParser.parse(socket.gets)
assert_equal :priv_msg, m.class.to_sym
m = IRCParser.parse(socket.gets)
assert_equal :notice, m.class.to_sym
m = IRCParser.parse(socket.gets)
assert_equal :priv_msg, m.class.to_sym # replaced by default priv_msg
socket.gets # ignore NICK
socket.gets # ignore USER
assert_nil socket.gets # expects EOF
end
end
def test_fallback_err_nick_name_in_use
msgs = [
{"msg" => "test", "command" => "privmsg"},
]
emit_test(msgs) do |socket, d|
socket.gets # ignore NICK
socket.gets # ignore USER
socket.gets # ignore join
socket.gets # ignore priv_msg
# imitate to receive :err_nick_name_in_use
conn = d.instance.instance_variable_get(:@conn)
IRCParser.message(:err_nick_name_in_use) do |m|
conn.on_read(m.to_s)
end
sleep 1
# test to use `#{NICK}_` instead
m = IRCParser.parse(socket.gets)
assert_equal :nick, m.class.to_sym
assert_equal "#{NICK}_", m.nick
end
end
private
def emit_test(msgs, extra_config: {}, &block)
TCPServer.open(0) do |serv|
port = serv.addr[1]
d = create_driver(config({port: port}.merge(extra_config)))
thread = Thread.new do
begin
s = serv.accept
block.call(s, d)
s.close
rescue IOError
# Suppress stream closed in another thread (IOError)
end
end
d.run(default_tag: TAG) do
msgs.each do |m|
d.feed(event_time, m)
channel = d.instance.on_timer
d.instance.conn.joined[channel] = true # pseudo join
end
# How to remove sleep?
# It is necessary to ensure that no data remains in Cool.io write buffer before detach.
sleep 1.5
end
thread.join
end
end
end