spec/inputs/jdbc_spec.rb in logstash-input-jdbc-3.0.2 vs spec/inputs/jdbc_spec.rb in logstash-input-jdbc-3.1.0

- old
+ new

@@ -1,5 +1,6 @@ +# encoding: utf-8 require "logstash/devutils/rspec/spec_helper" require "logstash/inputs/jdbc" require "jdbc/derby" require "sequel" require "sequel/adapters/jdbc" @@ -27,10 +28,11 @@ before :each do Jdbc::Derby.load_driver db.create_table :test_table do DateTime :created_at Integer :num + String :string DateTime :custom_time end end after :each do @@ -79,10 +81,45 @@ end let(:config) { mixin_settings.merge(settings) } end end + context "when both jdbc_password and jdbc_password_filepath arguments are passed" do + let(:statement) { "SELECT * from test_table" } + let(:jdbc_password) { "secret" } + let(:jdbc_password_file_path) { Stud::Temporary.pathname } + let(:settings) { { "jdbc_password_filepath" => jdbc_password_file_path, + "jdbc_password" => jdbc_password, + "statement" => statement } } + + it "should fail to register" do + expect{ plugin.register }.to raise_error(LogStash::ConfigurationError) + end + end + + context "when jdbc_password is passed in from a file" do + let(:statement) { "SELECT * from test_table" } + let(:jdbc_password) { "secret" } + let(:jdbc_password_file_path) { Stud::Temporary.pathname } + let(:settings) { { "jdbc_password_filepath" => jdbc_password_file_path, + "statement" => statement } } + + before do + File.write(jdbc_password_file_path, jdbc_password) + plugin.register + end + + after do + plugin.stop + end + + it "should read in jdbc_password from file" do + expect(plugin.jdbc_password).to eq(jdbc_password) + end + end + + context "when neither statement and statement_filepath arguments are passed" do it "should fail to register" do expect{ plugin.register }.to raise_error(LogStash::ConfigurationError) end end @@ -833,8 +870,113 @@ it "should not fail when passed a non-positive value" do mixin_settings['connection_retry_attempts'] = -2 expect { plugin.register }.to_not raise_error plugin.stop + end + end + + context "when encoding of some columns need to be changed" do + + let(:settings) {{ "statement" => "SELECT * from test_table" }} + let(:events) { [] } + let(:row) do + { + "column0" => "foo", + "column1" => "bar".force_encoding(Encoding::ISO_8859_1), + "column2" => 3 + } + end + + before(:each) do + allow_any_instance_of(Sequel::JDBC::Derby::Dataset).to receive(:each).and_yield(row) + plugin.register + end + + after(:each) do + plugin.stop + end + + it "should not convert any column by default" do + encoded_row = { + "column0" => "foo", + "column1" => "bar".force_encoding(Encoding::ISO_8859_1), + "column2" => 3 + } + expect(LogStash::Event).to receive(:new) do |row| + row.each do |k, v| + next unless v.is_a?(String) + expect(row[k].encoding).to eq(encoded_row[k].encoding) + end + end + plugin.run(events) + end + + context "when all string columns should be encoded" do + + let(:settings) do + { + "statement" => "SELECT * from test_table", + "charset" => "ISO-8859-1" + } + end + + let(:row) do + { + "column0" => "foo".force_encoding(Encoding::ISO_8859_1), + "column1" => "bar".force_encoding(Encoding::ISO_8859_1), + "column2" => 3 + } + end + + it "should transform all column string to UTF-8, default encoding" do + encoded_row = { + "column0" => "foo", + "column1" => "bar", + "column2" => 3 + } + expect(LogStash::Event).to receive(:new) do |row| + row.each do |k, v| + next unless v.is_a?(String) + expect(row[k].encoding).to eq(encoded_row[k].encoding) + end + end + plugin.run(events) + end + end + + context "when only an specific column should be converted" do + + let(:settings) do + { + "statement" => "SELECT * from test_table", + "columns_charset" => { "column1" => "ISO-8859-1" } + } + end + + let(:row) do + { + "column0" => "foo", + "column1" => "bar".force_encoding(Encoding::ISO_8859_1), + "column2" => 3, + "column3" => "berlin".force_encoding(Encoding::ASCII_8BIT) + } + end + + it "should only convert the selected column" do + encoded_row = { + "column0" => "foo", + "column1" => "bar", + "column2" => 3, + "column3" => "berlin".force_encoding(Encoding::ASCII_8BIT) + } + expect(LogStash::Event).to receive(:new) do |row| + row.each do |k, v| + next unless v.is_a?(String) + expect(row[k].encoding).to eq(encoded_row[k].encoding) + end + end + plugin.run(events) + end end end end