Add test/lib/*_test.rb

Signed-off-by: Kenji Okimoto <okimoto@clear-code.com>
This commit is contained in:
Kenji Okimoto 2018-08-02 10:05:21 +09:00
parent a6d31ee20d
commit 90e6df54f3
No known key found for this signature in database
GPG Key ID: F9E3E329A5C5E4A1
3 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,36 @@
require "test_helper"
class FluentdUITest < ActiveSupport::TestCase
sub_test_case ".update_available?" do
setup do
@current_version = FluentdUI::VERSION
end
test "unavailable" do
FluentdUI.latest_version = @current_version
assert do
!FluentdUI.update_available?
end
end
test "available" do
FluentdUI.latest_version = @current_version.succ
assert do
FluentdUI.update_available?
end
end
end
sub_test_case ".fluentd_version" do
test "not ready" do
stub(Fluentd).instance { nil }
assert_nil(FluentdUI.fluentd_version)
end
test "ready" do
target = FactoryBot.build(:fluentd)
stub(Fluentd).instance{ target }
assert_equal(target.agent.version, FluentdUI.fluentd_version)
end
end
end

View File

@ -0,0 +1,86 @@
require "test_helper"
module RegexpPreview
class MultilineTest < ActiveSupport::TestCase
test "simple usage" do
config = {
"format_firstline" => "/foo/",
"time_format" => "time_format",
}
config["format1"] = "/(?<foo>foo)\n/"
config["format2"] = "/(?<bar>bar)/"
3.upto(Fluentd::Setting::InTail::MULTI_LINE_MAX_FORMAT_COUNT) do |i|
config["format#{i}"] = "//"
end
preview = RegexpPreview::MultiLine.new(fixture_path("error0.log"), "multiline", config)
matches = [
{
whole: "foo\nbar\nbaz\n1\n2\n3\n4\n5\n6\n10\n11\n12",
matches: [
{ key: "foo", matched: "foo", pos: [0, 3] },
{ key: "bar", matched: "bar", pos: [4, 7] }
]
}
]
assert_equal(matches, preview.matches[:matches])
end
test "detect only continuous patterns" do
config = {
"format_firstline" => "/foo/",
"time_format" => "time_format",
}
config["format1"] = "/(?<foo>foo)\n/"
config["format2"] = "/(?<bar>baz)/"
3.upto(Fluentd::Setting::InTail::MULTI_LINE_MAX_FORMAT_COUNT) do |i|
config["format#{i}"] = "//"
end
preview = RegexpPreview::MultiLine.new(fixture_path("error0.log"), "multiline", config)
assert_equal([], preview.matches[:matches])
end
# http://docs.fluentd.org/articles/in_tail
test "example on document" do
config = {
"format_firstline" => "/\\d{4}-\\d{1,2}-\\d{1,2}/",
"format1" => "/^(?<time>\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}) \\[(?<thread>.*)\\] (?<level>[^\\s]+)(?<message>.*)/",
"time_format" => "%Y-%m-%d %H:%M:%S",
"keep_time_key" => true
}
2.upto(Fluentd::Setting::InTail::MULTI_LINE_MAX_FORMAT_COUNT) do |i|
config["format#{i}"] = "//"
end
preview = RegexpPreview::MultiLine.new(fixture_path("multiline_example.log"), "multiline", config)
matches = [
{
whole: "2013-3-03 14:27:33 [main] INFO Main - Start\n",
matches: [
{ key: "time", matched: "2013-3-03 14:27:33", pos: [0, 18] },
{ key: "thread", matched: "main", pos: [20, 24] },
{ key: "level", matched: "INFO", pos: [26, 30] },
{ key: "message", matched: " Main - Start\n", pos: [30, 45] }
]
},
{
whole: "2013-3-03 14:27:33 [main] ERROR Main - Exception\njavax.management.RuntimeErrorException: null\n at Main.main(Main.java:16) ~[bin/:na]\n",
matches: [
{ key: "time", matched: "2013-3-03 14:27:33", pos: [0, 18] },
{ key: "thread", matched: "main", pos: [20, 24] },
{ key: "level", matched: "ERROR", pos: [26, 31] },
{ key: "message", matched: " Main - Exception\njavax.management.RuntimeErrorException: null\n at Main.main(Main.java:16) ~[bin/:na]\n", pos: [31, 136] },
]
},
{
whole: "2013-3-03 14:27:33 [main] INFO Main - End",
matches: [
{ key: "time", matched: "2013-3-03 14:27:33", pos: [0, 18] },
{ key: "thread", matched: "main", pos: [20, 24] },
{ key: "level", matched: "INFO", pos: [26, 30] },
{ key: "message", matched: " Main - End", pos: [30, 42] },
]
}
]
assert_equal(matches, preview.matches[:matches])
end
end
end

View File

@ -0,0 +1,87 @@
require "test_helper"
require "fluent/plugin/parser_apache"
require "fluent/plugin/parser_nginx"
module RegexpPreview
class SingleLineTest < ActiveSupport::TestCase
data("regexp" => ["regexp", Fluent::Plugin::RegexpParser, { "expression" => "(?<catefory>\[.+\])", "time_format" => "%Y/%m/%d" }],
"ltsv" => ["ltsv", Fluent::Plugin::LabeledTSVParser, {}],
"json" => ["json", Fluent::Plugin::JSONParser, {}],
"csv" => ["csv", Fluent::Plugin::CSVParser, { "keys" => "column1,column2" }],
"tsv" => ["tsv", Fluent::Plugin::TSVParser, { "keys" => "column1,column2" }],
"syslog" => ["syslog", Fluent::Plugin::SyslogParser, {}],
"apache" => ["apache", Fluent::Plugin::ApacheParser, {}],
"nginx" => ["nginx", Fluent::Plugin::NginxParser, {}])
test "create parser plugin instance from selected plugin name" do |(name, klass, config)|
preview = RegexpPreview::SingleLine.new("log_file.log", name, config)
assert_instance_of(klass, preview.plugin)
end
sub_test_case "#matches" do
test "regexp" do
config = {
"expression" => "(?<regexp>bar)", # bar from error0.log
"time_format" => "time_format",
}
preview = RegexpPreview::SingleLine.new(fixture_path("error0.log"), "regexp", config)
matches = [
{
whole: "bar",
matches: [
{ key: "regexp", matched: "bar", pos: [0, 3] }
]
}
]
assert_equal(config, preview.matches[:pluginConfig])
assert_equal(matches,preview.matches[:matches])
end
test "csv" do
config = { "keys" => "column1,column2" }
preview = RegexpPreview::SingleLine.new(fixture_path("error0.log"), "csv", config)
assert do
preview.matches[:matches].empty?
end
end
test "syslog" do
config = {
"time_format" => "%Y-%m-%d %H:%M:%S %z",
"keep_time_key" => true
}
preview = RegexpPreview::SingleLine.new(fixture_path("error4.log"), "syslog", config)
matches = [
{
whole: "2014-05-27 10:54:37 +0900 [info]: listening fluent socket on 0.0.0.0:24224",
matches: [
{ key: "time", matched: "2014-05-27 10:54:37 +0900", pos: [0, 25] },
{ key: "host", matched: "[info]:", pos: [26, 33] },
{ key: "ident", matched: "listening", pos: [34, 43] },
{ key: "message", matched: "24224", pos: [69, 74] }
]
}
]
assert_equal(matches, preview.matches[:matches])
end
test "syslog when keep_time_key is false" do
config = {
"time_format" => "%Y-%m-%d %H:%M:%S %z",
"keep_time_key" => false
}
preview = RegexpPreview::SingleLine.new(fixture_path("error4.log"), "syslog", config)
matches = [
{
whole: "2014-05-27 10:54:37 +0900 [info]: listening fluent socket on 0.0.0.0:24224",
matches: [
{ key: "host", matched: "[info]:", pos: [26, 33] },
{ key: "ident", matched: "listening", pos: [34, 43] },
{ key: "message", matched: "24224", pos: [69, 74] }
]
}
]
assert_equal(matches, preview.matches[:matches])
end
end
end
end