|
| 1 | +require "helper" |
| 2 | +require "fluent/test/driver/input" |
| 3 | +require "fluent/plugin/in_obsolete_plugins" |
| 4 | +require "fluent/plugin/obsolete_plugins_utils" |
| 5 | + |
| 6 | +class ObsoletePluginsInputTest < Test::Unit::TestCase |
| 7 | + |
| 8 | + setup do |
| 9 | + Fluent::Test.setup |
| 10 | + $log = Fluent::Test::TestLogger.new |
| 11 | + @time = Time.now |
| 12 | + Timecop.freeze(@time) |
| 13 | + end |
| 14 | + |
| 15 | + teardown do |
| 16 | + Timecop.return |
| 17 | + end |
| 18 | + |
| 19 | + sub_test_case "plugins_json" do |
| 20 | + CONFIG_JSON = %[ |
| 21 | + plugins_json #{fixture_path("plugins.json")} |
| 22 | + ] |
| 23 | + |
| 24 | + test "no obsolete plugins" do |
| 25 | + d = create_driver(CONFIG_JSON) |
| 26 | + d.run |
| 27 | + assert_equal([], d.events) |
| 28 | + assert_equal([], d.logs) |
| 29 | + end |
| 30 | + |
| 31 | + test "obsolete plugins" do |
| 32 | + stub(Gem).loaded_specs do |
| 33 | + { |
| 34 | + "fluent-plugin-tail-multiline" => nil, |
| 35 | + "fluent-plugin-hostname" => nil |
| 36 | + } |
| 37 | + end |
| 38 | + d = create_driver(CONFIG_JSON) |
| 39 | + d.run |
| 40 | + assert_equal([], d.events) |
| 41 | + expected_logs = [ |
| 42 | + "#{@time} [warn]: fluent-plugin-tail-multiline is obsolete: Merged in in_tail in Fluentd v0.10.45. [fluent/fluentd#269](https://github.com/fluent/fluentd/issues/269)\n", |
| 43 | + "#{@time} [warn]: fluent-plugin-hostname is obsolete: Use [filter\\_record\\_transformer](http://docs.fluentd.org/v0.12/articles/filter_record_transformer) instead.\n" |
| 44 | + ] |
| 45 | + assert_equal(expected_logs, d.logs) |
| 46 | + end |
| 47 | + |
| 48 | + test "raise error when detect obsolete plugins" do |
| 49 | + stub(Gem).loaded_specs do |
| 50 | + { |
| 51 | + "fluent-plugin-tail-multiline" => nil, |
| 52 | + "fluent-plugin-hostname" => nil |
| 53 | + } |
| 54 | + end |
| 55 | + |
| 56 | + ex = assert_raise(Fluent::ConfigError) do |
| 57 | + create_driver(CONFIG_JSON + "raise_error yes") |
| 58 | + end |
| 59 | + assert_equal("Detected obsolete plugins", ex.message) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + sub_test_case "error handling" do |
| 64 | + test "invalid json" do |
| 65 | + d = create_driver("plugins_json #{fixture_path('invalid.json')}") |
| 66 | + |
| 67 | + expected_logs = [ |
| 68 | + "#{@time} [info]: Failed to notify obsolete plugins error_class=JSON::ParserError error=\"expected ',' or '}' after object value, got: EOF at line 11 column 1\"\n", |
| 69 | + ] |
| 70 | + |
| 71 | + assert_equal(expected_logs, d.logs) |
| 72 | + end |
| 73 | + |
| 74 | + test "timeout with slow server" do |
| 75 | + server = create_slow_webserver(port: 12345) |
| 76 | + |
| 77 | + mock(Fluent::Plugin::ObsoletePluginsUtils).notify.never |
| 78 | + |
| 79 | + d = create_driver(%[ |
| 80 | + plugins_json http://localhost:12345/plugins.json |
| 81 | + timeout 1 |
| 82 | + ]) |
| 83 | + |
| 84 | + sleep 2 |
| 85 | + |
| 86 | + expected_logs = [ |
| 87 | + "#{@time} [info]: Failed to notify obsolete plugins error_class=Timeout::Error error=\"execution expired\"\n", |
| 88 | + ] |
| 89 | + |
| 90 | + assert_equal(expected_logs, d.logs) |
| 91 | + ensure |
| 92 | + server.shutdown |
| 93 | + end |
| 94 | + |
| 95 | + end |
| 96 | + |
| 97 | + private |
| 98 | + |
| 99 | + def create_driver(conf) |
| 100 | + Fluent::Test::Driver::Input.new(Fluent::Plugin::ObsoletePluginsInput).configure(conf) |
| 101 | + end |
| 102 | + |
| 103 | + def create_slow_webserver(port: 12345) |
| 104 | + require "webrick" |
| 105 | + |
| 106 | + server = WEBrick::HTTPServer.new(Port: port) |
| 107 | + server.mount_proc '/' do |req, res| |
| 108 | + sleep 60 |
| 109 | + |
| 110 | + res['Content-Type'] = 'application/json' |
| 111 | + res.body = File.read(fixture_path("plugins.json")) |
| 112 | + end |
| 113 | + |
| 114 | + server |
| 115 | + end |
| 116 | +end |
0 commit comments