|
| 1 | +require "helper" |
| 2 | +require "fluent/test/driver/input" |
| 3 | + |
| 4 | +class SqlInputCustomTimeTest < Test::Unit::TestCase |
| 5 | + def setup |
| 6 | + Fluent::Test.setup |
| 7 | + end |
| 8 | + |
| 9 | + def teardown |
| 10 | + end |
| 11 | + |
| 12 | + CONFIG = %[ |
| 13 | + adapter postgresql |
| 14 | + host localhost |
| 15 | + port 5432 |
| 16 | + database fluentd_test |
| 17 | +
|
| 18 | + username fluentd |
| 19 | + password fluentd |
| 20 | +
|
| 21 | + schema_search_path public |
| 22 | +
|
| 23 | + tag_prefix db |
| 24 | +
|
| 25 | + <table> |
| 26 | + table messages_custom_time |
| 27 | + tag logs |
| 28 | + update_column updated_at |
| 29 | + time_column custom_time |
| 30 | + </table> |
| 31 | + ] |
| 32 | + |
| 33 | + def create_driver(conf = CONFIG) |
| 34 | + Fluent::Test::Driver::Input.new(Fluent::Plugin::SQLInput).configure(conf) |
| 35 | + end |
| 36 | + |
| 37 | + def test_configure |
| 38 | + d = create_driver |
| 39 | + expected = { |
| 40 | + host: "localhost", |
| 41 | + port: 5432, |
| 42 | + adapter: "postgresql", |
| 43 | + database: "fluentd_test", |
| 44 | + username: "fluentd", |
| 45 | + password: "fluentd", |
| 46 | + schema_search_path: "public", |
| 47 | + tag_prefix: "db" |
| 48 | + } |
| 49 | + actual = { |
| 50 | + host: d.instance.host, |
| 51 | + port: d.instance.port, |
| 52 | + adapter: d.instance.adapter, |
| 53 | + database: d.instance.database, |
| 54 | + username: d.instance.username, |
| 55 | + password: d.instance.password, |
| 56 | + schema_search_path: d.instance.schema_search_path, |
| 57 | + tag_prefix: d.instance.tag_prefix |
| 58 | + } |
| 59 | + assert_equal(expected, actual) |
| 60 | + tables = d.instance.instance_variable_get(:@tables) |
| 61 | + assert_equal(1, tables.size) |
| 62 | + messages_custom_time = tables.first |
| 63 | + assert_equal("messages_custom_time", messages_custom_time.table) |
| 64 | + assert_equal("logs", messages_custom_time.tag) |
| 65 | + end |
| 66 | + |
| 67 | + def test_message |
| 68 | + d = create_driver(CONFIG + "select_interval 1") |
| 69 | + |
| 70 | + start_time = Fluent::EventTime.now |
| 71 | + |
| 72 | + # Create one message with a valid timestamp containing milliseconds and a time zone |
| 73 | + Message.create!(message: "message 1", custom_time: '2020-08-27 15:00:16.100758000 -0400') |
| 74 | + |
| 75 | + # Create one message without a timestamp so that we can test auto-creation |
| 76 | + Message.create!(message: "message 2 (no timestamp)", custom_time: nil) |
| 77 | + |
| 78 | + # Create one message with an unparseable timestamp so that we can check that a valid |
| 79 | + # one is auto-generated. |
| 80 | + Message.create!(message: "message 3 (bad timestamp)", custom_time: 'foo') |
| 81 | + |
| 82 | + d.end_if do |
| 83 | + d.record_count >= 3 |
| 84 | + end |
| 85 | + d.run(timeout: 5) |
| 86 | + |
| 87 | + assert_equal("db.logs", d.events[0][0]) |
| 88 | + expected = [ |
| 89 | + [d.events[0][1], "message 1"], |
| 90 | + [d.events[1][1], "message 2 (no timestamp)"], |
| 91 | + [d.events[2][1], "message 3 (bad timestamp)"], |
| 92 | + ] |
| 93 | + |
| 94 | + actual = [ |
| 95 | + [Fluent::EventTime.parse(d.events[0][2]["custom_time"]), d.events[0][2]["message"]], |
| 96 | + d.events[1][2]["message"], |
| 97 | + d.events[2][2]["message"], |
| 98 | + ] |
| 99 | + |
| 100 | + assert_equal(expected[0], actual[0]) |
| 101 | + |
| 102 | + # Messages 2 and 3 should have the same messages but (usually) a slightly later |
| 103 | + # timestamps because they are generated by the input plugin instead of the test |
| 104 | + # code |
| 105 | + [1,2].each do |i| |
| 106 | + assert_equal(expected[i][1], actual[i]) |
| 107 | + assert_operator(expected[i][0], :>=, start_time) |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + class Message < ActiveRecord::Base |
| 112 | + self.table_name = "messages_custom_time" |
| 113 | + end |
| 114 | +end |
0 commit comments