spec/inputs/jdbc_spec.rb in logstash-input-jdbc-2.0.5 vs spec/inputs/jdbc_spec.rb in logstash-input-jdbc-2.1.0

- old
+ new

@@ -3,13 +3,22 @@ require "jdbc/derby" require "sequel" require "sequel/adapters/jdbc" require "timecop" require "stud/temporary" +require "time" +require "date" describe LogStash::Inputs::Jdbc do - let(:mixin_settings) { {"jdbc_user" => ENV['USER'], "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver", "jdbc_connection_string" => "jdbc:derby:memory:testdb;create=true"} } + # This is a necessary change test-wide to guarantee that no local timezone + # is picked up. It could be arbitrarily set to any timezone, but then the test + # would have to compensate differently. That's why UTC is chosen. + ENV["TZ"] = "Etc/UTC" + let(:mixin_settings) do + { "jdbc_user" => ENV['USER'], "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver", + "jdbc_connection_string" => "jdbc:derby:memory:testdb;create=true"} + end let(:settings) { {} } let(:plugin) { LogStash::Inputs::Jdbc.new(mixin_settings.merge(settings)) } let(:queue) { Queue.new } let (:db) do Sequel.connect(mixin_settings['jdbc_connection_string'], :user=> nil, :password=> nil) @@ -40,10 +49,24 @@ mixin_settings['jdbc_password'] = 'pass' expect { plugin.register }.to_not raise_error plugin.stop end + it "should load all drivers when passing an array" do + mixin_settings['jdbc_driver_library'] = '/foo/bar,/bar/foo' + expect(plugin).to receive(:load_drivers).with(['/foo/bar', '/bar/foo']) + plugin.register + plugin.stop + end + + it "should load all drivers when using a single value" do + mixin_settings['jdbc_driver_library'] = '/foo/bar' + expect(plugin).to receive(:load_drivers).with(['/foo/bar']) + plugin.register + plugin.stop + end + it "should stop without raising exception" do plugin.register expect { plugin.stop }.to_not raise_error end @@ -197,10 +220,81 @@ event = queue.pop expect(event["custom_time"]).to be_a(LogStash::Timestamp) end end + context "when fetching time data with jdbc_default_timezone set" do + let(:mixin_settings) do + { "jdbc_user" => ENV['USER'], "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver", + "jdbc_connection_string" => "jdbc:derby:memory:testdb;create=true", + "jdbc_default_timezone" => "America/Chicago" + } + end + + let(:settings) do + { + "statement" => "SELECT * from test_table", + } + end + + let(:num_rows) { 10 } + + before do + num_rows.times do + db[:test_table].insert(:num => 1, :custom_time => "2015-01-01 12:00:00", :created_at => Time.now.utc) + end + + plugin.register + end + + after do + plugin.stop + end + + it "should convert the time to reflect the timezone " do + plugin.run(queue) + event = queue.pop + # This reflects a 6 hour time difference between UTC and America/Chicago + expect(event["custom_time"].time).to eq(Time.iso8601("2015-01-01T18:00:00Z")) + end + end + + context "when fetching time data without jdbc_default_timezone set" do + let(:mixin_settings) do + { "jdbc_user" => ENV['USER'], "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver", + "jdbc_connection_string" => "jdbc:derby:memory:testdb;create=true" + } + end + + let(:settings) do + { + "statement" => "SELECT * from test_table", + } + end + + let(:num_rows) { 1 } + + before do + num_rows.times do + db.run "INSERT INTO test_table (created_at, num, custom_time) VALUES (TIMESTAMP('2015-01-01 12:00:00'), 1, TIMESTAMP('2015-01-01 12:00:00'))" + end + + plugin.register + end + + after do + plugin.stop + end + + it "should not convert the time to reflect the timezone " do + plugin.run(queue) + event = queue.pop + # With no timezone set, no change should occur + expect(event["custom_time"].time).to eq(Time.iso8601("2015-01-01T12:00:00Z")) + end + end + context "when iteratively running plugin#run" do let(:settings) do {"statement" => "SELECT num, created_at FROM test_table WHERE created_at > :sql_last_start"} end @@ -377,6 +471,39 @@ allow(Sequel).to receive(:connect).and_raise(Sequel::PoolTimeout) expect(plugin.logger).to receive(:error).with("Failed to connect to database. 0 second timeout exceeded.") expect { plugin.register }.to raise_error(Sequel::PoolTimeout) end end + + context "when using logging" do + + let(:settings) do + { + "statement" => "SELECT * from test_table", "sql_log_level" => "debug" + } + end + + let(:num_rows) { 5 } + + before do + plugin.instance_variable_set("@logger", logger) + allow(logger).to receive(:debug?) + num_rows.times do + db[:test_table].insert(:num => 1) + end + + plugin.register + end + + after do + plugin.stop + end + + let(:logger) { double("logger") } + + it "should report the staments to logging" do + expect(logger).to receive(:debug).with(kind_of(String)).once + plugin.run(queue) + end + end + end