| 
1 | 1 | require "logstash/devutils/rspec/spec_helper"  | 
2 | 2 | require "logstash/inputs/jdbc"  | 
 | 3 | +require "sequel"  | 
 | 4 | +require "sequel/adapters/jdbc"  | 
3 | 5 | 
 
  | 
4 | 6 | # This test requires: Firebird installed to Mac OSX, it uses the built-in example database `employee`  | 
5 | 7 | 
 
  | 
 | 
8 | 10 |   # is picked up.  It could be arbitrarily set to any timezone, but then the test  | 
9 | 11 |   # would have to compensate differently.  That's why UTC is chosen.  | 
10 | 12 |   ENV["TZ"] = "Etc/UTC"  | 
11 |  | -  let(:mixin_settings) do  | 
12 |  | -    { "jdbc_user" => "SYSDBA", "jdbc_driver_class" => "org.firebirdsql.jdbc.FBDriver", "jdbc_driver_library" => "/elastic/tmp/jaybird-full-3.0.4.jar",  | 
13 |  | -    "jdbc_connection_string" => "jdbc:firebirdsql://localhost:3050//Library/Frameworks/Firebird.framework/Versions/A/Resources/examples/empbuild/employee.fdb", "jdbc_password" => "masterkey"}  | 
 | 13 | +  # For Travis and CI based on docker, we source from ENV  | 
 | 14 | +  jdbc_connection_string = ENV.fetch("PG_CONNECTION_STRING",  | 
 | 15 | +                                     "jdbc:postgresql://postgresql:5432") + "/jdbc_input_db?user=postgres"  | 
 | 16 | + | 
 | 17 | +  let(:settings) do  | 
 | 18 | +    { "jdbc_driver_class" => "org.postgresql.Driver",  | 
 | 19 | +      "jdbc_connection_string" => jdbc_connection_string,  | 
 | 20 | +      "jdbc_driver_library" => "/usr/share/logstash/postgresql.jar",  | 
 | 21 | +      "jdbc_user" => "postgres",  | 
 | 22 | +      "statement" => 'SELECT FIRST_NAME, LAST_NAME FROM "employee" WHERE EMP_NO = 2'  | 
 | 23 | +    }  | 
14 | 24 |   end  | 
15 |  | -  let(:settings) { {"statement" => "SELECT FIRST_NAME, LAST_NAME FROM EMPLOYEE WHERE EMP_NO > 144"} }  | 
16 |  | -  let(:plugin) { LogStash::Inputs::Jdbc.new(mixin_settings.merge(settings)) }  | 
 | 25 | + | 
 | 26 | +  let(:plugin) { LogStash::Inputs::Jdbc.new(settings) }  | 
17 | 27 |   let(:queue) { Queue.new }  | 
18 | 28 | 
 
  | 
19 |  | -  context "when passing no parameters" do  | 
 | 29 | +  context "when connecting to a postgres instance" do  | 
20 | 30 |     before do  | 
21 | 31 |       plugin.register  | 
22 | 32 |     end  | 
 | 
25 | 35 |       plugin.stop  | 
26 | 36 |     end  | 
27 | 37 | 
 
  | 
28 |  | -    it "should retrieve params correctly from Event" do  | 
 | 38 | +    it "should populate the event with database entries" do  | 
29 | 39 |       plugin.run(queue)  | 
30 | 40 |       event = queue.pop  | 
31 | 41 |       expect(event.get('first_name')).to eq("Mark")  | 
32 | 42 |       expect(event.get('last_name')).to eq("Guckenheimer")  | 
33 | 43 |     end  | 
34 | 44 |   end  | 
 | 45 | + | 
 | 46 | +  context "when supplying a non-existent library" do  | 
 | 47 | +    let(:settings) do  | 
 | 48 | +      super.merge(  | 
 | 49 | +          "jdbc_driver_library" => "/no/path/to/postgresql.jar"  | 
 | 50 | +      )  | 
 | 51 | +    end  | 
 | 52 | + | 
 | 53 | +    it "should not register correctly" do  | 
 | 54 | +      plugin.register  | 
 | 55 | +      q = Queue.new  | 
 | 56 | +      expect do  | 
 | 57 | +        plugin.run(q)  | 
 | 58 | +      end.to raise_error(::LogStash::PluginLoadingError)  | 
 | 59 | +    end  | 
 | 60 | +  end  | 
 | 61 | + | 
 | 62 | +  context "when connecting to a non-existent server" do  | 
 | 63 | +    let(:settings) do  | 
 | 64 | +      super.merge(  | 
 | 65 | +          "jdbc_connection_string" => "jdbc:postgresql://localhost:65000/somedb"  | 
 | 66 | +      )  | 
 | 67 | +    end  | 
 | 68 | + | 
 | 69 | +    it "should not register correctly" do  | 
 | 70 | +      plugin.register  | 
 | 71 | +      q = Queue.new  | 
 | 72 | +      expect do  | 
 | 73 | +        plugin.run(q)  | 
 | 74 | +      end.to raise_error(::Sequel::DatabaseConnectionError)  | 
 | 75 | +    end  | 
 | 76 | +  end  | 
35 | 77 | end  | 
36 | 78 | 
 
  | 
0 commit comments