require 'helper'
class ConfigExpanderTest < Test::Unit::TestCase
def setup
@m = Fluent::Config::Expander
end
def test_replace
assert_equal nil, @m.replace(nil, {})
assert_equal "", @m.replace("", {})
assert_equal "", @m.replace("", {'foo' => 'bar'})
assert_equal "barbar", @m.replace("foobar", {'foo' => 'bar'})
assert_equal "foofoo", @m.replace("foobar", {'bar' => 'foo'})
assert_equal "foobar", @m.replace("foobar", {'hoge' => 'moge'})
assert_equal "xxbar", @m.replace("foofoobar", {'foo' => 'x'})
end
def test_expand
e1 = Fluent::Config::Element.new('config', '', {'attr_first'=>'value_first', 'attr_second'=>'__NUM__'}, [], [])
assert_equal "\n attr_first value_first\n attr_second __NUM__\n\n", e1.to_s
assert_equal "\n attr_first value_first\n attr_second 24224\n\n", @m.expand(e1, {'__NUM__' => '24224'}).to_s
e2 = Fluent::Config::Element.new('server', '', {'host'=> 'test', 'port'=>'__PORT__', 'ext'=>'__PORT__', 'num__PORT__'=>'1'}, [], [])
assert_equal "\n host test\n port __PORT__\n ext __PORT__\n num__PORT__ 1\n\n", e2.to_s
assert_equal "\n host test\n port 24224\n ext 24224\n num24224 1\n\n", @m.expand(e2, {'__PORT__' => '24224'}).to_s
e3 = Fluent::Config::Element.new('config', 'for __PORT__', {'attr_first'=>'value_first', 'attr_second'=>'__PORT__'}, [e2], [])
exconf1 = <
attr_first value_first
attr_second 24224
host test
port 24224
ext 24224
num24224 1
EOL
assert_equal exconf1, @m.expand(e3, {'__PORT__' => '24224'}).to_s
nonexconf2 = <
host node__nodenum__.local
port __portnum__
EOL
conf2 = Fluent::Config.parse(nonexconf2, 'hoge').elements.first
assert_equal nonexconf2, conf2.to_s
exconf2 = <
host node__nodenum__.local
port 24221
host node__nodenum__.local
port 24222
host node__nodenum__.local
port 24223
host node__nodenum__.local
port 24224
EOL
assert_equal exconf2, @m.expand(conf2, {}).to_s
nonexconf3 = <
type forward
flush_interval 1s
host node__nodenum__.local
port __portnum__
EOL
conf3 = Fluent::Config.parse(nonexconf3, 'hoge').elements.first
assert_equal nonexconf3, conf3.to_s
exconf3 = <
type forward
flush_interval 1s
host node01.local
port 24221
host node01.local
port 24222
host node01.local
port 24223
host node01.local
port 24224
host node02.local
port 24221
host node02.local
port 24222
host node02.local
port 24223
host node02.local
port 24224
EOL
assert_equal exconf3, @m.expand(conf3, {}).to_s
end
end