require_relative '../helper'
require 'fluent/test/driver/input'
require 'fluent/plugin/in_exec'
require 'timecop'
class ExecInputTest < Test::Unit::TestCase
SCRIPT_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', 'scripts', 'exec_script.rb'))
TEST_TIME = "2011-01-02 13:14:15"
TEST_UNIX_TIME = Time.parse(TEST_TIME)
def setup
Fluent::Test.setup
@test_time = event_time()
end
def create_driver(conf)
Fluent::Test::Driver::Input.new(Fluent::Plugin::ExecInput).configure(conf)
end
DEFAULT_CONFIG_ONLY_WITH_KEYS = %[
command ruby #{SCRIPT_PATH} "#{TEST_TIME}" 0
run_interval 1s
tag "my.test.data"
keys ["k1", "k2", "k3"]
]
TSV_CONFIG = %[
command ruby #{SCRIPT_PATH} "#{TEST_TIME}" 0
run_interval 1s
@type tsv
keys time, tag, k1
tag_key tag
time_key time
time_type string
time_format %Y-%m-%d %H:%M:%S
]
JSON_CONFIG = %[
command ruby #{SCRIPT_PATH} #{TEST_UNIX_TIME.to_i} 1
run_interval 1s
@type json
tag_key tag
time_key time
time_type unixtime
]
MSGPACK_CONFIG = %[
command ruby #{SCRIPT_PATH} #{TEST_UNIX_TIME.to_i} 2
run_interval 1s
@type msgpack
tag_key tagger
time_key datetime
time_type unixtime
]
# here document for not de-quoting backslashes
REGEXP_CONFIG = %[
command ruby #{SCRIPT_PATH} "#{TEST_TIME}" 3
run_interval 1s
tag regex_tag
] + <<'EOC'
@type regexp
expression "(?
EOC
sub_test_case 'with configuration with sections' do
test 'configure with default tsv format without extract' do
d = create_driver DEFAULT_CONFIG_ONLY_WITH_KEYS
assert{ d.instance.parser.is_a? Fluent::Plugin::TSVParser }
assert_equal "my.test.data", d.instance.tag
assert_equal ["k1", "k2", "k3"], d.instance.parser.keys
end
test 'configure raises error if both of tag and extract.tag_key are missing' do
assert_raise Fluent::ConfigError.new("'tag' or 'tag_key' option is required on exec input") do
create_driver %[
command ruby -e 'puts "yay"'
keys y1
]
end
end
test 'configure for tsv' do
d = create_driver TSV_CONFIG
assert{ d.instance.parser.is_a? Fluent::Plugin::TSVParser }
assert_equal ["time", "tag", "k1"], d.instance.parser.keys
assert_equal "tag", d.instance.extract_config.tag_key
assert_equal "time", d.instance.extract_config.time_key
assert_equal :string, d.instance.extract_config.time_type
assert_equal "%Y-%m-%d %H:%M:%S", d.instance.extract_config.time_format
end
test 'configure for json' do
d = create_driver JSON_CONFIG
assert{ d.instance.parser.is_a? Fluent::Plugin::JSONParser }
assert_equal "tag", d.instance.extract_config.tag_key
assert_equal "time", d.instance.extract_config.time_key
assert_equal :unixtime, d.instance.extract_config.time_type
end
test 'configure for msgpack' do
d = create_driver MSGPACK_CONFIG
assert{ d.instance.parser.is_a? Fluent::Plugin::MessagePackParser }
assert_equal "tagger", d.instance.extract_config.tag_key
assert_equal "datetime", d.instance.extract_config.time_key
assert_equal :unixtime, d.instance.extract_config.time_type
end
test 'configure for regexp' do
d = create_driver REGEXP_CONFIG
assert{ d.instance.parser.is_a? Fluent::Plugin::RegexpParser }
assert_equal "regex_tag", d.instance.tag
expression = <<'EXP'.chomp
(?