spec/inputs/lumberjack_spec.rb in logstash-input-lumberjack-0.1.5 vs spec/inputs/lumberjack_spec.rb in logstash-input-lumberjack-0.1.7
- old
+ new
@@ -2,56 +2,103 @@
require "spec_helper"
require "stud/temporary"
require 'logstash/inputs/lumberjack'
require "logstash/codecs/plain"
require "logstash/codecs/multiline"
+require "logstash/event"
+require "lumberjack/client"
+require_relative "../support/logstash_test"
describe LogStash::Inputs::Lumberjack do
-
- let(:ssl_cert) { Stud::Temporary.pathname("ssl_certificate") }
- let(:ssl_key) { Stud::Temporary.pathname("ssl_key") }
-
+ let(:connection) { double("connection") }
+ let(:certificate) { LogStashTest.certificate }
+ let(:port) { LogStashTest.random_port }
let(:queue) { Queue.new }
- let(:config) { { "port" => 0, "ssl_certificate" => ssl_cert, "ssl_key" => ssl_key, "type" => "example" } }
+ let(:config) { { "port" => 0, "ssl_certificate" => certificate.ssl_cert, "ssl_key" => certificate.ssl_key, "type" => "example", "tags" => "lumberjack" } }
+ subject(:lumberjack) { LogStash::Inputs::Lumberjack.new(config) }
- before do
- system("openssl req -x509 -batch -nodes -newkey rsa:2048 -keyout #{ssl_key} -out #{ssl_cert} -subj /CN=localhost > /dev/null 2>&1")
- end
-
context "#register" do
-
it "raise no exception" do
plugin = LogStash::Inputs::Lumberjack.new(config)
- expect { plugin.register}.not_to raise_error
+ expect { plugin.register }.not_to raise_error
end
end
describe "#processing of events" do
+ let(:lines) { {"line" => "one\ntwo\n two.2\nthree\n", "tags" => ["syslog"]} }
- context "#codecs" do
+ before do
+ allow(connection).to receive(:run).and_yield(lines)
+ lumberjack.register
+ expect_any_instance_of(Lumberjack::Server).to receive(:accept).and_return(connection)
+ end
+ context "#codecs" do
let(:config) do
- { "port" => 6969, "ssl_certificate" => ssl_cert, "ssl_key" => ssl_key,
+ { "port" => port, "ssl_certificate" => certificate.ssl_cert, "ssl_key" => certificate.ssl_key,
"type" => "example", "codec" => codec }
end
let(:codec) { LogStash::Codecs::Multiline.new("pattern" => '\n', "what" => "previous") }
- subject!(:lumberjack) { LogStash::Inputs::Lumberjack.new(config) }
- let(:connection) { double("connection") }
- let(:lines) { {"line" => "one\ntwo\n two.2\nthree\n"} }
-
- before(:each) do
- allow(connection).to receive(:run).and_yield(lines)
- lumberjack.register
- end
-
it "clone the codec per connection" do
- expect_any_instance_of(Lumberjack::Server).to receive(:accept).and_return(connection)
expect(lumberjack.codec).to receive(:clone).once
expect(lumberjack).to receive(:invoke).and_throw(:msg)
lumberjack.run(queue)
end
-
end
end
+
+ context "when we have the maximum clients connected" do
+ let(:max_clients) { 1 }
+ let(:window_size) { 1 }
+ let(:config) do
+ {
+ "port" => port,
+ "ssl_certificate" => certificate.ssl_cert,
+ "ssl_key" => certificate.ssl_key,
+ "type" => "testing",
+ "max_clients" => max_clients
+ }
+ end
+
+ let(:client_options) do
+ {
+ :port => port,
+ :address => "127.0.0.1",
+ :ssl_certificate => certificate.ssl_cert,
+ :window_size => window_size
+ }
+ end
+
+ before do
+ lumberjack.register
+
+ @server = Thread.new do
+ lumberjack.run(queue)
+ end
+
+ sleep(0.1) # wait for the server to correctly accept messages
+ end
+
+ after do
+ @server.raise(LogStash::ShutdownSignal)
+ @server.join
+ end
+
+ it "stops accepting new connection" do
+ client1 = Lumberjack::Socket.new(client_options)
+
+ # Since the connection is stopped on the other side and OS X and
+ # linux doesn't behave the same. The client could raise a IOError
+ # or an SSLError. On OSX I had to try to send some data to trip
+ # the error.
+ expect {
+ client2 = Lumberjack::Socket.new(client_options)
+
+ (window_size + 1).times do
+ client2.write_hash({"line" => "message"})
+ end
+ }.to raise_error
+ end
+ end
end