require_relative '../helper' require 'fluent/configurable' require 'fluent/config/element' require 'fluent/config/section' require 'fluent/system_config' module Fluent::Config class FakeLoggerInitializer attr_accessor :level def initialize @level = nil end end class FakeSupervisor attr_writer :log_level def initialize(**opt) @system_config = nil @cl_opt = { wokers: nil, root_dir: nil, log: FakeLoggerInitializer.new, log_level: Fluent::Log::LEVEL_INFO, suppress_interval: nil, suppress_config_dump: nil, suppress_repeated_stacktrace: nil, log_event_label: nil, log_event_verbose: nil, without_source: nil, emit_error_log_interval: nil, file_permission: nil, dir_permission: nil, }.merge(opt) end def for_system_config opt = {} # this is copy from Supervisor#build_system_config Fluent::SystemConfig::SYSTEM_CONFIG_PARAMETERS.each do |param| if @cl_opt.key?(param) && !@cl_opt[param].nil? if param == :log_level && @cl_opt[:log_level] == Fluent::Log::LEVEL_INFO # info level can't be specified via command line option. # log_level is info here, it is default value and 's log_level should be applied if exists. next end opt[param] = @cl_opt[param] end end opt end end class TestSystemConfig < ::Test::Unit::TestCase TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/tmp/system_config/#{ENV['TEST_ENV_NUMBER']}") def parse_text(text) basepath = File.expand_path(File.dirname(__FILE__) + '/../../') Fluent::Config.parse(text, '(test)', basepath, true).elements.find { |e| e.name == 'system' } end test 'should not override default configurations when no parameters' do conf = parse_text(<<-EOS) EOS s = FakeSupervisor.new sc = Fluent::SystemConfig.new(conf) sc.overwrite_variables(s.for_system_config) assert_equal(1, sc.workers) assert_nil(sc.root_dir) assert_nil(sc.log_level) assert_nil(sc.suppress_repeated_stacktrace) assert_nil(sc.emit_error_log_interval) assert_nil(sc.suppress_config_dump) assert_nil(sc.without_source) assert_equal(:text, sc.log.format) assert_equal('%Y-%m-%d %H:%M:%S %z', sc.log.time_format) end data( 'workers' => ['workers', 3], 'root_dir' => ['root_dir', File.join(TMP_DIR, 'root')], 'log_level' => ['log_level', 'error'], 'suppress_repeated_stacktrace' => ['suppress_repeated_stacktrace', true], 'log_event_verbose' => ['log_event_verbose', true], 'suppress_config_dump' => ['suppress_config_dump', true], 'without_source' => ['without_source', true], ) test "accepts parameters" do |(k, v)| conf = parse_text(<<-EOS) #{k} #{v} EOS s = FakeSupervisor.new sc = Fluent::SystemConfig.new(conf) sc.overwrite_variables(s.for_system_config) if k == 'log_level' assert_equal(Fluent::Log::LEVEL_ERROR, sc.__send__(k)) else assert_equal(v, sc.__send__(k)) end end test "log parameters" do conf = parse_text(<<-EOS) format json time_format %Y EOS s = FakeSupervisor.new sc = Fluent::SystemConfig.new(conf) sc.overwrite_variables(s.for_system_config) assert_equal(:json, sc.log.format) assert_equal('%Y', sc.log.time_format) end # info is removed because info level can't be specified via command line data('trace' => Fluent::Log::LEVEL_TRACE, 'debug' => Fluent::Log::LEVEL_DEBUG, 'warn' => Fluent::Log::LEVEL_WARN, 'error' => Fluent::Log::LEVEL_ERROR, 'fatal' => Fluent::Log::LEVEL_FATAL) test 'log_level is ignored when log_level related command line option is passed' do |level| conf = parse_text(<<-EOS) log_level info EOS s = FakeSupervisor.new(log_level: level) sc = Fluent::SystemConfig.new(conf) sc.overwrite_variables(s.for_system_config) assert_equal(level, sc.log_level) end end end