Skip to content

Commit 409cde8

Browse files
committed
Add test for output plugin
1 parent 086a9f6 commit 409cde8

File tree

4 files changed

+82
-31
lines changed

4 files changed

+82
-31
lines changed

Diff for: Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
require "bundler/gem_tasks"
22

3+
require 'rake/testtask'
4+
Rake::TestTask.new(:test) do |test|
5+
test.libs << "lib" << "test"
6+
test.pattern = "test/plugin/*.rb"
7+
test.verbose = true
8+
end
9+
10+
task :default => :test

Diff for: lib/fluent/plugin/out_remote_syslog.rb

+33-31
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
11
require "fluent/mixin/config_placeholders"
22

3-
class RemoteSyslogOutput < Fluent::Output
4-
Fluent::Plugin.register_output("remote_syslog", self)
3+
module Fluent
4+
class RemoteSyslogOutput < Fluent::Output
5+
Fluent::Plugin.register_output("remote_syslog", self)
56

6-
config_param :hostname, :string, :default => ""
7+
config_param :hostname, :string, :default => ""
78

8-
config_param :key_name, :string, :default => "message"
9+
config_param :key_name, :string, :default => "message"
910

10-
config_param :remote_hostname, :string
11-
config_param :port, :integer, :default => 25
11+
config_param :remote_hostname, :string
12+
config_param :port, :integer, :default => 25
1213

13-
config_param :facility, :string, :default => "user"
14-
config_param :severity, :string, :default => "notice"
15-
config_param :tag, :string, :default => "fluentd"
14+
config_param :facility, :string, :default => "user"
15+
config_param :severity, :string, :default => "notice"
16+
config_param :tag, :string, :default => "fluentd"
1617

17-
include Fluent::Mixin::ConfigPlaceholders
18+
include Fluent::Mixin::ConfigPlaceholders
1819

19-
def initialize
20-
super
21-
require "remote_syslog_logger"
22-
end
20+
def initialize
21+
super
22+
require "remote_syslog_logger"
23+
end
2324

24-
def configure(conf)
25-
super
26-
@logger = RemoteSyslogLogger::UdpSender.new(@remote_hostname,
27-
@port,
28-
facility: @facility,
29-
severity: @severity,
30-
program: @tag,
31-
local_hostname: @hostname)
32-
end
25+
def configure(conf)
26+
super
27+
@logger = RemoteSyslogLogger::UdpSender.new(@remote_hostname,
28+
@port,
29+
facility: @facility,
30+
severity: @severity,
31+
program: @tag,
32+
local_hostname: @hostname)
33+
end
3334

34-
def shutdown
35-
super
36-
@logger.close if @logger
37-
end
35+
def shutdown
36+
super
37+
@logger.close if @logger
38+
end
3839

39-
def emit(tag, es, chain)
40-
chain.next
41-
es.each do |time, record|
42-
@logger.transmit record[@key_name]
40+
def emit(tag, es, chain)
41+
chain.next
42+
es.each do |time, record|
43+
@logger.transmit record[@key_name]
44+
end
4345
end
4446
end
4547
end

Diff for: test/plugin/out_remote_syslog.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require "test_helper"
2+
require "fluent/plugin/out_remote_syslog"
3+
4+
class RemoteSyslogOutputTest < Test::Unit::TestCase
5+
def setup
6+
super
7+
Fluent::Test.setup
8+
end
9+
10+
CONFIG = %[
11+
type remote_syslog
12+
hostname foo.com
13+
remote_hostname example.com
14+
port 5566
15+
severity debug
16+
tag testunit
17+
]
18+
19+
def create_driver(conf = CONFIG)
20+
Fluent::Test::OutputTestDriver.new(Fluent::RemoteSyslogOutput) {}.configure(conf)
21+
end
22+
23+
def test_configure
24+
d = create_driver
25+
logger = d.instance.instance_variable_get(:@logger)
26+
27+
assert_not_nil logger
28+
assert_equal "example.com", logger.instance_variable_get(:@remote_hostname)
29+
assert_equal 5566, logger.instance_variable_get(:@remote_port)
30+
p = logger.instance_variable_get(:@packet)
31+
assert_equal "foo.com", p.hostname
32+
assert_equal 1, p.facility
33+
assert_equal "testunit", p.tag
34+
assert_equal 7, p.severity
35+
end
36+
end

Diff for: test/test_helper.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2+
$LOAD_PATH.unshift(File.dirname(__FILE__))
3+
4+
require 'test/unit'
5+
require 'fluent/test'

0 commit comments

Comments
 (0)