Skip to content

Commit

Permalink
Move reading config test
Browse files Browse the repository at this point in the history
Signed-off-by: Yuta Iwama <[email protected]>
  • Loading branch information
ganmacs committed Dec 11, 2019
1 parent af37887 commit 620e7b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 81 deletions.
32 changes: 27 additions & 5 deletions test/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ def test_check_not_fetchd
assert_equal before_size, match_conf.unused.size
end

def write_config(path, data)
def write_config(path, data, encoding: 'utf-8')
FileUtils.mkdir_p(File.dirname(path))
File.open(path, "w") {|f| f.write data }
File.open(path, "w:#{encoding}:utf-8") {|f| f.write data }
end

def test_inline
prepare_config
opts = {
:config_path => "#{TMP_DIR}/config_test_1.conf",
:inline_config => "<source>\n type http\n port 2222\n </source>"
:config_path => "#{TMP_DIR}/config_test_1.conf",
:inline_config => "<source>\n type http\n port 2222\n </source>"
}
assert_nothing_raised do
Fluent::Supervisor.new(opts)
Expand All @@ -175,5 +175,27 @@ def create_warn_dummy_logger
logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
$log = Fluent::Log.new(logger)
end
end

sub_test_case '.build'do
test 'read config' do
write_config("#{TMP_DIR}/build/config_build.conf", 'key value')
c = Fluent::Config.build(config_path: "#{TMP_DIR}/build/config_build.conf")
assert_equal('value', c['key'])
end

test 'read config with encoding' do
write_config("#{TMP_DIR}/build/config_build2.conf", "#てすと\nkey value", encoding: 'shift_jis')

c = Fluent::Config.build(config_path: "#{TMP_DIR}/build/config_build2.conf", encoding: 'shift_jis')
assert_equal('value', c['key'])
end

test 'read config with additional_config' do
write_config("#{TMP_DIR}/build/config_build2.conf", "key value")

c = Fluent::Config.build(config_path: "#{TMP_DIR}/build/config_build2.conf", additional_config: 'key2 value2')
assert_equal('value', c['key'])
assert_equal('value2', c['key2'])
end
end
end
77 changes: 1 addition & 76 deletions test/test_supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,81 +30,6 @@ def write_config(path, data)
File.open(path, "w") {|f| f.write data }
end

def test_read_config
create_info_dummy_logger

tmp_dir = "#{TMP_DIR}/dir/test_read_config.conf"
conf_str = %[
<source>
@type forward
@id forward_input
</source>
<match debug.**>
@type stdout
@id stdout_output
</match>
]
write_config tmp_dir, conf_str
opts = Fluent::Supervisor.default_options
sv = Fluent::Supervisor.new(opts)

use_v1_config = {}
use_v1_config['use_v1_config'] = true

sv.instance_variable_set(:@config_path, tmp_dir)
sv.instance_variable_set(:@use_v1_config, use_v1_config)

conf = sv.__send__(:read_config)
elem = conf.elements.find { |e| e.name == 'source' }
assert_equal "forward", elem['@type']
assert_equal "forward_input", elem['@id']

elem = conf.elements.find { |e| e.name == 'match' }
assert_equal "debug.**", elem.arg
assert_equal "stdout", elem['@type']
assert_equal "stdout_output", elem['@id']

$log.out.reset
end

def test_read_config_with_multibyte_string
tmp_path = "#{TMP_DIR}/dir/test_multibyte_config.conf"
conf_str = %[
<source>
@type forward
@id forward_input
@label @INPUT
</source>
<label @INPUT>
<filter>
@type record_transformer
<record>
message こんにちは. ${record["name"]} has made a order of ${record["item"]} just now.
</record>
</filter>
<match>
@type stdout
</match>
</label>
]
FileUtils.mkdir_p(File.dirname(tmp_path))
File.open(tmp_path, "w:utf-8") {|file| file.write(conf_str) }

opts = Fluent::Supervisor.default_options
sv = Fluent::Supervisor.new(opts)

use_v1_config = {}
use_v1_config['use_v1_config'] = true

sv.instance_variable_set(:@config_path, tmp_path)
sv.instance_variable_set(:@use_v1_config, use_v1_config)

conf = sv.__send__(:read_config)
label = conf.elements.detect {|e| e.name == "label" }
filter = label.elements.detect {|e| e.name == "filter" }
record_transformer = filter.elements.detect {|e| e.name = "record_transformer" }
assert_equal(Encoding::UTF_8, record_transformer["message"].encoding)
end

def test_system_config
opts = Fluent::Supervisor.default_options
Expand Down Expand Up @@ -416,7 +341,7 @@ def test_inline_config

inline_config = '<match *>\n@type stdout\n</match>'
stub(STDIN).read { inline_config }
stub(sv).read_config # to skip
stub(Fluent::Config).build # to skip
stub(sv).build_system_config { Fluent::SystemConfig.new } # to skip

sv.configure
Expand Down

0 comments on commit 620e7b0

Please sign in to comment.