From dfff109a72b894213a7b982e162c47876205beae Mon Sep 17 00:00:00 2001 From: Takuro Ashie Date: Thu, 14 Nov 2019 12:59:18 +0900 Subject: [PATCH] Also parse bool value strictly on strict_config_value = true Should raise ConfigError on unrecognized strings or any other value types to notify mistakes in user's config as early as possible. Signed-off-by: Takuro Ashie --- lib/fluent/config/types.rb | 2 ++ test/config/test_types.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/fluent/config/types.rb b/lib/fluent/config/types.rb index 40ab5fde07..6b8f2b384a 100644 --- a/lib/fluent/config/types.rb +++ b/lib/fluent/config/types.rb @@ -64,6 +64,8 @@ def self.bool_value(str, opts = {}, name = nil) # parser should pass empty string in this case but changing behaviour may break existing environment so keep parser behaviour. Just ignore comment value in boolean handling for now. if str.respond_to?('start_with?') && str.start_with?('#') true + elsif opts[:strict] + raise Fluent::ConfigError, "#{name}: invalid bool value: #{str}" else nil end diff --git a/test/config/test_types.rb b/test/config/test_types.rb index dccc9f5f5b..cf714cdab0 100644 --- a/test/config/test_types.rb +++ b/test/config/test_types.rb @@ -95,6 +95,21 @@ class TestConfigTypes < ::Test::Unit::TestCase test 'not assumed case' do |(expected, val)| assert_equal(expected, Config.bool_value(val)) end + + data("true" => [true, true], + "false" => [false, false], + "hoge" => [Fluent::ConfigError.new("name1: invalid bool value: hoge"), "hoge"], + "nill" => [nil, nil], + "integer" => [Fluent::ConfigError.new("name1: invalid bool value: 10"), 10]) + test 'not assumed case with strict' do |(expected, val)| + if expected.kind_of? Exception + assert_raise(expected) do + Config.bool_value(val, { strict: true }, "name1") + end + else + assert_equal(expected, Config.bool_value(val, { strict: true }, "name1")) + end + end end sub_test_case 'Config.regexp_value' do