spec/outputs/charrington_spec.rb in logstash-output-charrington-0.2.2 vs spec/outputs/charrington_spec.rb in logstash-output-charrington-0.3.0

- old
+ new

@@ -1,64 +1,143 @@ require_relative '../charrington_spec_helper' -require 'insist' describe LogStash::Outputs::Charrington do - describe 'when initializing' do - it 'shouldn\'t register without a config' do - expect do - LogStash::Plugin.lookup('output', 'charrington').new - end.to raise_error(LogStash::ConfigurationError) + include_context 'postgres' + include_context 'pipeline' + + describe 'a new payload with one event' do + let(:config) do + <<-CONFIG + input { + generator { + message => '{"app_name": "Web App", "event": "From Agent", "meta": { "type": "XML" } }' + codec => 'json' + count => 1 + } + } + + output { + charrington { + connection_string => '#{@url}' + driver_jar_path => '#{driver_path}' + } + } + CONFIG end + + it 'creates a table and inserts a record' do + drop_table('from_agent') + run_pipeline + expect(query('SELECT * FROM from_agent')).to match_array([{id: "1", app_name: "Web App", event: "From Agent", inserted_at: a_kind_of(String), :meta_type => "XML"}]) + expect(query('SELECT COUNT(1) FROM from_agent').first[:count]).to eq("1") + end end - # describe 'integration tests with agent' do - # config <<-HEREDOC - # input { - # generator { - # message => '{"app_name": "Web App", "event": "From Agent"}' - # codec => 'json' - # count => 1 - # } - # } + describe '2 event payloads with different metadata' do + let(:config) do + <<-CONFIG + input { + generator { + lines => [ + '{"app_name": "Web App", "event": "From Agent", "meta": { "type": "XML" } }', + '{"app_name": "Web App", "event": "From Agent", "meta": { "type": "XML", "file_name": "virus.pdf" } }' + ] + codec => 'json' + count => 1 + } + } - # output { - # charrington { - # connection_string => 'jdbc:postgresql://localhost:5432/winston?user=postgres&password=postgres' - # driver_jar_path => '/projects/logstash-output-charrington/vendor/postgresql-42.2.5.jar' - # schema => 'dea' - # } - # } - # HEREDOC + output { + charrington { + connection_string => '#{@url}' + driver_jar_path => '#{driver_path}' + } + } + CONFIG + end - # agent do - # puts "IT'S WORKING!!!!!" - # end - # end + it 'creates a table and inserts the first record and alters the table for the second record' do + drop_table('from_agent') + run_pipeline + expect(query('SELECT * FROM from_agent')).to match_array([ + {app_name: 'Web App', event: 'From Agent', id: '1', inserted_at: a_kind_of(String), meta_file_name: nil, meta_type: 'XML'}, + {app_name: 'Web App', event: 'From Agent', id: '2', inserted_at: a_kind_of(String), meta_file_name: 'virus.pdf', meta_type: 'XML'} + ]) + expect(query('SELECT COUNT(1) FROM from_agent').first[:count]).to eq("2") + end + end - describe 'integration tests' do - let(:config) do <<-CONFIG + describe 'event payload for an existing table with existing data' do + let(:config) do + <<-CONFIG input { generator { - message => '{"app_name": "Web App", "event": "Hi - Dan"}' + message => '{"app_name": "Web App", "event": "From Agent", "meta": { "type": "XML" } }' codec => 'json' count => 1 } } output { charrington { - connection_string => 'jdbc:postgresql://localhost:5432/winston?user=postgres&password=postgres' - driver_jar_path => '/projects/logstash-output-charrington/vendor/postgresql-42.2.5.jar' - schema => 'dea' + connection_string => '#{@url}' + driver_jar_path => '#{driver_path}' } } CONFIG end - it("isn't effed") do - pipeline = new_pipeline_from_string(config) - pipeline.run + it 'can insert into an existing table' do + # setup pre-existing data + drop_table('from_agent') + create_table('CREATE TABLE from_agent (id SERIAL PRIMARY KEY, inserted_at TIMESTAMP DEFAULT NOW(), app_name VARCHAR(255))') + insert("INSERT INTO from_agent (app_name) VALUES ('Not Agent')") + expect(query('SELECT * FROM from_agent')).to match_array([{id: "1", inserted_at: a_kind_of(String), app_name: "Not Agent"}]) + + run_pipeline + expect(query('SELECT * FROM from_agent')).to match_array([ + {id: '1', app_name: 'Not Agent', event: nil, inserted_at: a_kind_of(String), meta_type: nil}, + {id: '2', app_name: 'Web App', event: 'From Agent', inserted_at: a_kind_of(String), meta_type: 'XML'} + ]) end end + + describe '2 event payloads with different metadata and different schema' do + let(:schema) { "dea_test" } + let(:config) do + <<-CONFIG + input { + generator { + lines => [ + '{"app_name": "Web App", "event": "From Agent", "meta": { "type": "XML" } }', + '{"app_name": "Web App", "event": "From Agent", "meta": { "type": "XML", "file_name": "virus.pdf" } }' + ] + codec => 'json' + count => 1 + } + } + + output { + charrington { + connection_string => '#{@url}' + driver_jar_path => '#{driver_path}' + schema => '#{schema}' + } + } + CONFIG + end + + it 'creates a table and inserts the first record and alters the table for the second record' do + create("CREATE SCHEMA IF NOT EXISTS #{schema}") + drop_table("#{schema}.from_agent") + run_pipeline + expect(query("SELECT * FROM #{schema}.from_agent")).to match_array([ + {app_name: 'Web App', event: 'From Agent', id: '1', inserted_at: a_kind_of(String), meta_file_name: nil, meta_type: 'XML'}, + {app_name: 'Web App', event: 'From Agent', id: '2', inserted_at: a_kind_of(String), meta_file_name: 'virus.pdf', meta_type: 'XML'} + ]) + expect(query("SELECT COUNT(1) FROM #{schema}.from_agent").first[:count]).to eq("2") + end + end + end