Fixes for LOGSTASH-827 and LOGSTASH-841

Adding a solution for testing output/email using rumbster (https://github.com/aesterline/rumbster)
This commit is contained in:
Wiibaa 2013-01-28 12:57:38 +01:00
parent e4b466ffa4
commit f2feaa3c31
3 changed files with 122 additions and 1 deletions

View file

@ -18,12 +18,15 @@ class LogStash::Outputs::Email < LogStash::Outputs::Base
config :match, :validate => :hash, :required => true
# The To address setting - fully qualified email address to send to
# This field also accept a comma separated list of emails like "me@host.com, you@host.com"
# You can also use dynamic field from the event with the %{fieldname} syntax
config :to, :validate => :string, :required => true
# The From setting for email - fully qualified email address for the From:
config :from, :validate => :string, :default => "logstash.alert@nowhere.com"
# cc - send to others
# See *to* field for accepted value description
config :cc, :validate => :string, :default => ""
# how to send email: either smtp or sendmail - default to 'smtp'
@ -236,7 +239,7 @@ class LogStash::Outputs::Email < LogStash::Outputs::Base
if successful
# first add our custom field - matchName - so we can use it in the sprintf function
event["matchName"] = matchName
@logger.debug("Sending mail with these settings : ", :via => @via, :options => @options, :from => @from, :to => @to, :cc => @cc, :subject => @subject, :body => @body, :content_type => @contenttype, :htmlbody => @htmlbody, :attachments => @attachments, :to => to, :to => to)
@logger.debug? and @logger.debug("Creating mail with these settings : ", :via => @via, :options => @options, :from => @from, :to => @to, :cc => @cc, :subject => @subject, :body => @body, :content_type => @contenttype, :htmlbody => @htmlbody, :attachments => @attachments, :to => to, :to => to)
formatedSubject = event.sprintf(@subject)
formattedBody = event.sprintf(@body)
formattedHtmlBody = event.sprintf(@htmlbody)
@ -247,10 +250,12 @@ class LogStash::Outputs::Email < LogStash::Outputs::Base
mail.cc = event.sprintf(@cc)
mail.subject = formatedSubject
if @htmlbody.empty?
formattedBody.gsub!(/\\n/, "\n") # Take new line in the email
mail.body = formattedBody
else
mail.text_part = Mail::Part.new do
content_type "text/plain; charset=UTF-8"
formattedBody.gsub!(/\\n/, "\n") # Take new line in the email
body formattedBody
end
mail.html_part = Mail::Part.new do
@ -261,6 +266,7 @@ class LogStash::Outputs::Email < LogStash::Outputs::Base
@attachments.each do |fileLocation|
mail.add_file(fileLocation)
end # end @attachments.each
@logger.debug? and @logger.debug("Sending mail with these values : ", :from => mail.from, :to => mail.to, :cc => mail.cc, :subject => mail.subject)
mail.deliver!
end # end if successful
end # def receive

View file

@ -90,4 +90,5 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency "shoulda"
gem.add_runtime_dependency "rspec"
gem.add_runtime_dependency "insist", "0.0.8"
gem.add_runtime_dependency "rumbster" # For faking smtp in email tests
end

114
spec/outputs/email.rb Normal file
View file

@ -0,0 +1,114 @@
require "test_utils"
require "rumbster"
require "message_observers"
describe "outputs/email" do
extend LogStash::RSpec
@@port=2525
let (:rumbster) { Rumbster.new(@@port) }
let (:message_observer) { MailMessageObserver.new }
before :each do
rumbster.add_observer message_observer
rumbster.start
end
after :each do
rumbster.stop
end
describe "use a list of email as mail.to (LOGSTASH-827)" do
config <<-CONFIG
input {
generator {
message => "hello world"
count => 1
type => "generator"
}
}
filter {
noop {
add_field => ["dummy_match", "ok"]
}
}
output{
email {
to => "email1@host, email2@host"
match => ["mymatch", "dummy_match,ok"]
options => ["port", #{@@port}]
}
}
CONFIG
agent do
insist {message_observer.messages.size} == 1
insist {message_observer.messages[0].to} == ["email1@host", "email2@host"]
end
end
describe "use an array of email as mail.to (LOGSTASH-827)" do
config <<-CONFIG
input {
generator {
message => "hello world"
count => 1
type => "generator"
}
}
filter {
noop {
add_field => ["dummy_match", "ok"]
add_field => ["to_addr", "email1@host"]
add_field => ["to_addr", "email2@host"]
}
}
output{
email {
to => "%{to_addr}"
match => ["mymatch", "dummy_match,ok"]
options => ["port", #{@@port}]
}
}
CONFIG
agent do
insist {message_observer.messages.size} == 1
insist {message_observer.messages[0].to} == ["email1@host", "email2@host"]
end
end
describe "multi-lined text body (LOGSTASH-841)" do
config <<-CONFIG
input {
generator {
message => "hello world"
count => 1
type => "generator"
}
}
filter {
noop {
add_field => ["dummy_match", "ok"]
}
}
output{
email {
to => "me@host"
subject => "Hello World"
body => "Line1\\nLine2\\nLine3"
match => ["mymatch", "dummy_match,*"]
options => ["port", #{@@port}]
}
}
CONFIG
agent do
insist {message_observer.messages.size} == 1
insist {message_observer.messages[0].subject} == "Hello World"
insist {message_observer.messages[0].body.raw_source} == "Line1\r\nLine2\r\nLine3"
end
end
end