diff --git a/app/models/fluentd/agent/common.rb b/app/models/fluentd/agent/common.rb index c403a35..c101261 100644 --- a/app/models/fluentd/agent/common.rb +++ b/app/models/fluentd/agent/common.rb @@ -160,7 +160,7 @@ class Fluentd when "label" label_content = header + scanner.scan_until(%r{^}) label, sections = parse_label_section(label_content, started) - contents["label:#{label}"] << { pos: started, sections: sections } + contents["label:#{label}"] << { label: label, pos: started, sections: sections } else raise TypeError, "Unknown section: #{started}: #{section_type}" end @@ -179,10 +179,43 @@ class Fluentd header = scanner.scan_until(/^\s*<(filter|match)/) type = scanner[1] source = header + scanner.scan_until(%r{^\s*}) - sections[type] << { pos: pos + offset, content: source.sub(/\n/, "") } + sections[type] << { label: label, pos: pos + offset, content: source.sub(/\n+/, "") } end return label, sections end + + def dump_parsed_config(parsed_config) + content = "".dup + sources = parsed_config["source"] || [] + filters = parsed_config["filter"] || [] + matches = parsed_config["match"] || [] + labels = parsed_config.select do |key, sections| + key.start_with?("label:") + end + labels = labels.values.flatten + sorted_sections = (sources + filters + matches + labels).sort_by do |section| + section[:pos] + end + sorted_sections.each do |section| + if section.key?(:label) + label = section[:label] + sub_filters = section.dig(:sections, "filter") || [] + sub_matches = section.dig(:sections, "match") || [] + sub_sections = (sub_filters + sub_matches).sort_by do |sub_section| + sub_section[:pos] + end + content << "\n\n" + else + content << section[:content] << "\n\n" + end + end + content.chomp + end end end end diff --git a/test/models/fluentd/agent_common_test.rb b/test/models/fluentd/agent_common_test.rb index 015f51b..7280ddd 100644 --- a/test/models/fluentd/agent_common_test.rb +++ b/test/models/fluentd/agent_common_test.rb @@ -42,7 +42,7 @@ class Fluentd { pos: 44, content: filter_section_content } ], "match" => [ - { pos: 84, content: match_section_content } + { pos: 85, content: match_section_content } ] } assert_equal(expected, actual) @@ -69,10 +69,10 @@ class Fluentd CONFIG sections = { "filter" => [ - { pos: 75, content: filter_section_content} + { label: "@INPUT", pos: 76, content: filter_section_content} ], "match" => [ - { pos: 121, content: match_section_content} + { label: "@INPUT", pos: 122, content: match_section_content} ] } expected = { @@ -80,7 +80,7 @@ class Fluentd { pos: 0, content: source_section_content } ], "label:@INPUT" => [ - { pos: 60, sections: sections } + { label: "@INPUT", pos: 60, sections: sections } ] } assert_equal(expected, actual) @@ -115,10 +115,10 @@ class Fluentd CONFIG input_sections = { "filter" => [ - { pos: 140, content: filter_section_content } + { label: "@INPUT", pos: 140, content: filter_section_content } ], "match" => [ - { pos: 187, content: match_section_content } + { label: "@INPUT", pos: 187, content: match_section_content } ] } filter_secion_content1 = <<-CONFIG.chomp @@ -143,12 +143,12 @@ class Fluentd CONFIG main_sections = { "filter" => [ - { pos: 274, content: filter_secion_content1 }, - { pos: 321, content: filter_secion_content2 } + { label: "@MAIN", pos: 275, content: filter_secion_content1 }, + { label: "@MAIN", pos: 322, content: filter_secion_content2 } ], "match" => [ - { pos: 368, content: match_secion_content1 }, - { pos: 413, content: match_secion_content2 } + { label: "@MAIN", pos: 370, content: match_secion_content1 }, + { label: "@MAIN", pos: 416, content: match_secion_content2 } ] } expected = { @@ -157,14 +157,34 @@ class Fluentd { pos: 61, content: source_section_content2 }, ], "label:@INPUT" => [ - { pos: 124, sections: input_sections } + { label: "@INPUT", pos: 124, sections: input_sections } ], "label:@MAIN" => [ - { pos: 259, sections: main_sections } + { label: "@MAIN", pos: 260, sections: main_sections } ] } assert_equal(expected, actual) end end + + sub_test_case "#dump_parsed_config" do + test "simple" do + parsed_config = @agent.__send__(:parse_config, fixture_content("config/simple.conf")) + config = @agent.__send__(:dump_parsed_config, parsed_config) + assert_equal(fixture_content("config/simple.conf"), config) + end + + test "simple label" do + parsed_config = @agent.__send__(:parse_config, fixture_content("config/label.conf")) + config = @agent.__send__(:dump_parsed_config, parsed_config) + assert_equal(fixture_content("config/label.conf"), config) + end + + test "multiple labels" do + parsed_config = @agent.__send__(:parse_config, fixture_content("config/multi-label.conf")) + config = @agent.__send__(:dump_parsed_config, parsed_config) + assert_equal(fixture_content("config/multi-label.conf"), config) + end + end end end