From eef7473a0bb8d7e0bc46687d9e118124b0c89bc2 Mon Sep 17 00:00:00 2001 From: Karl Southern Date: Sun, 22 Nov 2015 23:19:29 +0000 Subject: [PATCH] Pushing. --- README.md | 13 ++++-- logstash-output-jdbc.gemspec | 2 +- spec/outputs/jdbc_spec.rb | 90 ++++++++++++++++++++++++++++++++++-- 3 files changed, 97 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 76978c1..2bc603b 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ This plugin does not bundle any JDBC jar files, and does expect them to be in a particular location. Please ensure you read the 4 installation lines below. ## Headlines - - Support for connection pooling added in 0.2.0 [unreleased until #10 is resolved] - - Support for unsafe statement handling (allowing dynamic queries) in 0.2.0 [unreleased until #10 is resolved] - - Altered exception handling to now count sequential flushes with exceptions thrown in 0.2.0 [untested and unreleased until #10 is resolved] + - Support for connection pooling added in 0.2.0 + - Support for unsafe statement handling (allowing dynamic queries) in 0.2.0 + - Altered exception handling to now count sequential flushes with exceptions thrown in 0.2.0 ## Versions - See master branch for logstash v2+ @@ -30,6 +30,13 @@ particular location. Please ensure you read the 4 installation lines below. - Add JDBC jar files to vendor/jar/jdbc in your logstash installation - And then configure (examples below) +## Running tests +Assuming valid JDBC jar, and jruby is setup and installed, and you have issued `jruby -S bundle install` in the development directory + - `SQL_JAR=path/to/your.jar jruby -S bundle exec rspec` +If you need to provide username and password you may do this via the environment variables `SQL_USERNAME` and `SQL_PASSWORD`. + +Tests are not yet 100% complete. + ## Configuration options | Option | Type | Description | Required? | Default | diff --git a/logstash-output-jdbc.gemspec b/logstash-output-jdbc.gemspec index 90ac188..c889fbe 100644 --- a/logstash-output-jdbc.gemspec +++ b/logstash-output-jdbc.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-output-jdbc' - s.version = "0.2.0.rc5" + s.version = "0.2.1" s.licenses = [ "Apache License (2.0)" ] s.summary = "This plugin allows you to output to SQL, via JDBC" s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program" diff --git a/spec/outputs/jdbc_spec.rb b/spec/outputs/jdbc_spec.rb index 3e648ba..9e2da38 100644 --- a/spec/outputs/jdbc_spec.rb +++ b/spec/outputs/jdbc_spec.rb @@ -1,13 +1,95 @@ require "logstash/devutils/rspec/spec_helper" require "logstash/outputs/jdbc" require "stud/temporary" +require "java" describe LogStash::Outputs::Jdbc do + def fetch_log_table_rowcount + # sleep for a second to let the flush happen + sleep 1 + + stmt = @sql.createStatement() + rs = stmt.executeQuery("select count(*) as total from log") + count = 0 + while rs.next() + count = rs.getInt("total") + end + stmt.close() - it "should register without errors" do - plugin = LogStash::Plugin.lookup("output", "jdbc").new({}) - expect { plugin.register }.to_not raise_error + return count + end + + let(:base_settings) { { + "driver_jar_path" => @driver_jar_path, + "connection_string" => @test_connection_string, + "username" => ENV['SQL_USERNAME'], + "password" => ENV['SQL_PASSWORD'], + "statement" => [ "insert into log (message) values(?)", "message" ], + "max_pool_size" => 1, + "flush_size" => 1, + "max_flush_exceptions" => 1 + } } + let(:test_settings) { {} } + let(:plugin) { LogStash::Outputs::Jdbc.new(base_settings.merge(test_settings)) } + let(:event_fields) { { "message" => "This is a message!" } } + let(:event) { LogStash::Event.new(event_fields) } + + before(:all) do + @driver_jar_path = File.absolute_path(ENV['SQL_JAR']) + @test_db_path = File.join(Stud::Temporary.directory, "test.db") + @test_connection_string = "jdbc:sqlite:#{@test_db_path}" + + require @driver_jar_path + + @sql = java.sql.DriverManager.get_connection(@test_connection_string, ENV['SQL_USERNAME'].to_s, ENV['SQL_PASSWORD'].to_s) + stmt = @sql.createStatement() + stmt.executeUpdate("CREATE table log (host text, timestamp datetime, message text);") + stmt.close() + end + + before(:each) do + stmt = @sql.createStatement() + stmt.executeUpdate("delete from log") + stmt.close() + end + + after(:all) do + File.unlink(@test_db_path) + Dir.rmdir(File.dirname(@test_db_path)) + end + + describe "safe statement" do + it "should register without errors" do + expect { plugin.register }.to_not raise_error + end + + it "receive event, without error" do + plugin.register + expect { plugin.receive(event) }.to_not raise_error + + expect(fetch_log_table_rowcount).to eq(1) + end + + end + + describe "unsafe statement" do + let(:event_fields) { + { "message" => "This is a message!", "table" => "log" } + } + let(:test_settings) { { + "statement" => [ "insert into %{table} (message) values(?)", "message" ], + "unsafe_statement" => true + } } + + it "should register without errors" do + expect { plugin.register }.to_not raise_error + end + + it "receive event, without error" do + plugin.register + plugin.receive(event) + expect(fetch_log_table_rowcount).to eq(1) + end end - end