Skip to content

Commit

Permalink
Merge pull request #1497 from joker1007/improve-placeholder-validatio…
Browse files Browse the repository at this point in the history
…n-message

Add placeholder validation target to error message
  • Loading branch information
repeatedly authored Mar 13, 2017
2 parents acab7f4 + 40be737 commit b08e313
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
14 changes: 7 additions & 7 deletions lib/fluent/plugin/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -589,24 +589,24 @@ def validate_time!
example = @argument[:example]
timekey = @argument[:timekey]
if !sec && timekey
raise Fluent::ConfigError, "Parameter '#{name}' doesn't have timestamp placeholders for timekey #{timekey.to_i}"
raise Fluent::ConfigError, "Parameter '#{name}: #{string}' doesn't have timestamp placeholders for timekey #{timekey.to_i}"
end
if sec && !timekey
raise Fluent::ConfigError, "Parameter '#{name}' has timestamp placeholders, but chunk key 'time' is not configured"
raise Fluent::ConfigError, "Parameter '#{name}: #{string}' has timestamp placeholders, but chunk key 'time' is not configured"
end
if sec && timekey && timekey < sec
raise Fluent::ConfigError, "Parameter '#{@name}' doesn't have timestamp placeholder for #{title}('#{example}') for timekey #{timekey.to_i}"
raise Fluent::ConfigError, "Parameter '#{name}: #{string}' doesn't have timestamp placeholder for #{title}('#{example}') for timekey #{timekey.to_i}"
end
end

def validate_tag!
parts = @argument[:parts]
tagkey = @argument[:tagkey]
if tagkey && parts.empty?
raise Fluent::ConfigError, "Parameter '#{@name}' doesn't have tag placeholder"
raise Fluent::ConfigError, "Parameter '#{name}: #{string}' doesn't have tag placeholder"
end
if !tagkey && !parts.empty?
raise Fluent::ConfigError, "Parameter '#{@name}' has tag placeholders, but chunk key 'tag' is not configured"
raise Fluent::ConfigError, "Parameter '#{name}: #{string}' has tag placeholders, but chunk key 'tag' is not configured"
end
end

Expand All @@ -615,11 +615,11 @@ def validate_keys!
chunk_keys = @argument[:chunkkeys]
if (chunk_keys - keys).size > 0
not_specified = (chunk_keys - keys).sort
raise Fluent::ConfigError, "Parameter '#{@name}' doesn't have enough placeholders for keys #{not_specified.join(',')}"
raise Fluent::ConfigError, "Parameter '#{name}: #{string}' doesn't have enough placeholders for keys #{not_specified.join(',')}"
end
if (keys - chunk_keys).size > 0
not_satisfied = (keys - chunk_keys).sort
raise Fluent::ConfigError, "Parameter '#{@name}' has placeholders, but chunk keys doesn't have keys #{not_satisfied.join(',')}"
raise Fluent::ConfigError, "Parameter '#{name}: #{string}' has placeholders, but chunk keys doesn't have keys #{not_satisfied.join(',')}"
end
end
end
Expand Down
30 changes: 15 additions & 15 deletions test/plugin/test_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def waiting(seconds)
validators = @i.placeholder_validators(:path, "/my/path/file.%Y-%m-%d.log")
assert_equal 1, validators.size
assert_equal 1, validators.select(&:time?).size
assert_raise Fluent::ConfigError.new("Parameter 'path' has timestamp placeholders, but chunk key 'time' is not configured") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.%Y-%m-%d.log' has timestamp placeholders, but chunk key 'time' is not configured") do
validators.first.validate!
end
end
Expand All @@ -325,7 +325,7 @@ def waiting(seconds)
validators = @i.placeholder_validators(:path, "/my/path/to/file.log")
assert_equal 1, validators.size
assert_equal 1, validators.select(&:time?).size
assert_raise Fluent::ConfigError.new("Parameter 'path' doesn't have timestamp placeholders for timekey 30") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/to/file.log' doesn't have timestamp placeholders for timekey 30") do
validators.first.validate!
end
end
Expand All @@ -335,7 +335,7 @@ def waiting(seconds)
validators = @i.placeholder_validators(:path, "/my/path/${tag}/file.log")
assert_equal 1, validators.size
assert_equal 1, validators.select(&:tag?).size
assert_raise Fluent::ConfigError.new("Parameter 'path' has tag placeholders, but chunk key 'tag' is not configured") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/${tag}/file.log' has tag placeholders, but chunk key 'tag' is not configured") do
validators.first.validate!
end
end
Expand All @@ -345,7 +345,7 @@ def waiting(seconds)
validators = @i.placeholder_validators(:path, "/my/path/file.log")
assert_equal 1, validators.size
assert_equal 1, validators.select(&:tag?).size
assert_raise Fluent::ConfigError.new("Parameter 'path' doesn't have tag placeholder") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.log' doesn't have tag placeholder") do
validators.first.validate!
end
end
Expand All @@ -355,7 +355,7 @@ def waiting(seconds)
validators = @i.placeholder_validators(:path, "/my/path/${username}/file.${group}.log")
assert_equal 1, validators.size
assert_equal 1, validators.select(&:keys?).size
assert_raise Fluent::ConfigError.new("Parameter 'path' has placeholders, but chunk keys doesn't have keys group,username") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/${username}/file.${group}.log' has placeholders, but chunk keys doesn't have keys group,username") do
validators.first.validate!
end
end
Expand All @@ -365,7 +365,7 @@ def waiting(seconds)
validators = @i.placeholder_validators(:path, "/my/path/file.log")
assert_equal 1, validators.size
assert_equal 1, validators.select(&:keys?).size
assert_raise Fluent::ConfigError.new("Parameter 'path' doesn't have enough placeholders for keys group,username") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.log' doesn't have enough placeholders for keys group,username") do
validators.first.validate!
end
end
Expand All @@ -374,14 +374,14 @@ def waiting(seconds)
sub_test_case '#placeholder_validate!' do
test 'raises configuration error for a templace when timestamp placeholders exist but time key is missing' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')]))
assert_raise Fluent::ConfigError.new("Parameter 'path' has timestamp placeholders, but chunk key 'time' is not configured") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /path/without/timestamp/file.%Y%m%d-%H%M.log' has timestamp placeholders, but chunk key 'time' is not configured") do
@i.placeholder_validate!(:path, "/path/without/timestamp/file.%Y%m%d-%H%M.log")
end
end

test 'raises configuration error for a template without timestamp placeholders when timekey is configured' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'time', {"timekey" => 180})]))
assert_raise Fluent::ConfigError.new("Parameter 'path' doesn't have timestamp placeholders for timekey 180") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.log' doesn't have timestamp placeholders for timekey 180") do
@i.placeholder_validate!(:path, "/my/path/file.log")
end
assert_nothing_raised do
Expand All @@ -391,7 +391,7 @@ def waiting(seconds)

test 'raises configuration error for a template with timestamp placeholders when plugin is configured more fine timekey' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'time', {"timekey" => 180})]))
assert_raise Fluent::ConfigError.new("Parameter 'path' doesn't have timestamp placeholder for hour('%H') for timekey 180") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.%Y%m%d_%H.log' doesn't have timestamp placeholder for hour('%H') for timekey 180") do
@i.placeholder_validate!(:path, "/my/path/file.%Y%m%d_%H.log")
end
assert_nothing_raised do
Expand All @@ -401,14 +401,14 @@ def waiting(seconds)

test 'raises configuration error for a template when tag placeholders exist but tag key is missing' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')]))
assert_raise Fluent::ConfigError.new("Parameter 'path' has tag placeholders, but chunk key 'tag' is not configured") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/${tag}/file.${tag[2]}.log' has tag placeholders, but chunk key 'tag' is not configured") do
@i.placeholder_validate!(:path, "/my/path/${tag}/file.${tag[2]}.log")
end
end

test 'raises configuration error for a template without tag placeholders when tagkey is configured' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'tag')]))
assert_raise Fluent::ConfigError.new("Parameter 'path' doesn't have tag placeholder") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.log' doesn't have tag placeholder") do
@i.placeholder_validate!(:path, "/my/path/file.log")
end
assert_nothing_raised do
Expand All @@ -418,14 +418,14 @@ def waiting(seconds)

test 'raises configuration error for a template when variable key placeholders exist but chunk keys are missing' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')]))
assert_raise Fluent::ConfigError.new("Parameter 'path' has placeholders, but chunk keys doesn't have keys service,username") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/${service}/file.${username}.log' has placeholders, but chunk keys doesn't have keys service,username") do
@i.placeholder_validate!(:path, "/my/path/${service}/file.${username}.log")
end
end

test 'raises configuration error for a template without variable key placeholders when chunk keys are configured' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'username,service')]))
assert_raise Fluent::ConfigError.new("Parameter 'path' doesn't have enough placeholders for keys service,username") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.log' doesn't have enough placeholders for keys service,username") do
@i.placeholder_validate!(:path, "/my/path/file.log")
end
assert_nothing_raised do
Expand All @@ -435,10 +435,10 @@ def waiting(seconds)

test 'raise configuration error for a template and configuration with keys mismatch' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'username,service')]))
assert_raise Fluent::ConfigError.new("Parameter 'path' doesn't have enough placeholders for keys service") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/file.${username}.log' doesn't have enough placeholders for keys service") do
@i.placeholder_validate!(:path, "/my/path/file.${username}.log")
end
assert_raise Fluent::ConfigError.new("Parameter 'path' doesn't have enough placeholders for keys username") do
assert_raise Fluent::ConfigError.new("Parameter 'path: /my/path/${service}/file.log' doesn't have enough placeholders for keys username") do
@i.placeholder_validate!(:path, "/my/path/${service}/file.log")
end
assert_nothing_raised do
Expand Down

0 comments on commit b08e313

Please sign in to comment.