Skip to content

Add 'select delivery method' option to mail notifier #11

Merged
merged 7 commits into from Jan 20, 2012
+90 −93
View
7 Gemfile
@@ -6,12 +6,9 @@ group :test do
gem 'tinder', :require => false
gem 'rake'
- gem 'thin'
- gem 'rack'
- gem 'sinatra'
+
+ gem 'fakeweb'
gem 'minitest', '~> 2.0.2', :platforms => :ruby_18
gem 'ruby-debug', :platforms => :ruby_18
-
-# gem 'ruby-debug19', :platforms => :ruby_19
end
View
14 Gemfile.lock
@@ -4,8 +4,8 @@ GEM
activesupport (3.0.3)
addressable (2.2.2)
columnize (0.3.2)
- daemons (1.1.0)
eventmachine (0.12.10)
+ fakeweb (1.3.0)
faraday (0.5.4)
addressable (~> 2.2.2)
multipart-post (~> 1.1.0)
@@ -30,14 +30,6 @@ GEM
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
- sinatra (1.1.2)
- rack (~> 1.1)
- tilt (~> 1.2)
- thin (1.2.7)
- daemons (>= 1.0.9)
- eventmachine (>= 0.12.6)
- rack (>= 1.0.0)
- tilt (1.2.2)
tinder (1.4.3)
activesupport
eventmachine
@@ -55,12 +47,10 @@ PLATFORMS
ruby
DEPENDENCIES
+ fakeweb
mail
minitest (~> 2.0.2)
net-ping
- rack
rake
ruby-debug
- sinatra
- thin
tinder
View
2 README.markdown
@@ -25,7 +25,7 @@ So, summing it all up, Nagios in Ruby, much cooler!
## Installing
-Outpost is tested with Ruby 1.8.7 and Ruby 1.9.2.
+Outpost is tested with Ruby 1.8.7 and Ruby 1.9.3.
gem install outpost
View
3 lib/outpost/notifiers/email.rb
@@ -23,6 +23,7 @@ def initialize(options={})
@from = options[:from]
@to = options[:to]
@subject = options[:subject] || 'Outpost notification'
+ @delivery_method = options[:delivery_method] || []
unless @from && @to
raise ArgumentError, 'You need to set :from and :to to send emails.'
@@ -41,6 +42,8 @@ def notify(outpost)
mail.subject = @subject
mail.body = build_message(outpost)
+ mail.send(:delivery_method, *@delivery_method) unless @delivery_method.empty?
+
mail.deliver
end
View
7 lib/outpost/scouts/http.rb
@@ -34,13 +34,18 @@ def setup(options)
@port = options[:port] || 80
@path = options[:path] || '/'
@http_class = options[:http_class] || Net::HTTP
+ @request_head = options.has_key?(:request_head) ? options[:request_head] : false
end
# Runs the scout, connecting to the host and getting the response code,
# body and time.
def execute
previous_time = Time.now
- response = @http_class.get_response(@host, @path, @port)
+ if @request_head
+ response = @http_class.request_head(@host, @path, @port)
+ else
+ response = @http_class.get_response(@host, @path, @port)
+ end
@response_time = (Time.now - previous_time) * 1000 # Miliseconds
@response_code = response.code.to_i
View
6 test/integration/basic_application_test.rb
@@ -3,12 +3,6 @@
require 'outpost/scouts/http'
describe "basic application integration test" do
- before(:each) do
- @server = Server.new
- @server.boot(TestApp)
- @server.wait_until_booted
- end
-
class ExampleSuccess < Outpost::Application
using Outpost::Scouts::Http => 'master http server' do
options :host => 'localhost', :port => 9595
View
6 test/integration/more_complex_test.rb
@@ -3,12 +3,6 @@
require 'outpost/scouts'
describe "using more complex application integration test" do
- before(:each) do
- @server = Server.new
- @server.boot(TestApp)
- @server.wait_until_booted
- end
-
class ExamplePingAndHttp < Outpost::Application
using Outpost::Scouts::Http => 'master http server' do
options :host => 'localhost', :port => 9595, :path => '/'
View
6 test/integration/no_subclassing_test.rb
@@ -4,11 +4,7 @@
require 'outpost/notifiers/email'
describe "creating outpost apps without subclassing" do
- before(:each) do
- @server = Server.new
- @server.boot(TestApp)
- @server.wait_until_booted
-
+ before do
Mail.defaults do
delivery_method :test
end
View
4 test/integration/reporting_test.rb
@@ -15,10 +15,6 @@ class RetrieveServerData < Outpost::Application
end
before(:each) do
- @server = Server.new
- @server.boot(TestApp)
- @server.wait_until_booted
-
@outpost = RetrieveServerData.new
@outpost.run
end
View
26 test/outpost/notifiers/email_test.rb
@@ -40,6 +40,32 @@
end
end
+ describe "#delivery_method" do
+ it "should use other delivery method if present" do
+ subject = Outpost::Notifiers::Email.new(
+ :from => 'mail@localhost',
+ :to => 'mailer@example.com',
+ #using file_delivery send method for testing
+ :delivery_method => [:file, {:location => Dir.tmpdir}]
+ )
+
+ subject.notify(outpost_stub)
+
+ assert_equal Mail::TestMailer.deliveries, []
+ end
+
+ it "should use default if not present" do
+ subject = Outpost::Notifiers::Email.new(
+ :from => 'mail@example.com',
+ :to => 'mailer@example.com'
+ )
+
+ subject.notify(outpost_stub)
+
+ assert_equal ["mail@example.com"], Mail::TestMailer.deliveries.first.from
+ end
+ end
+
describe "#notify" do
it "should send a simple email" do
subject = Outpost::Notifiers::Email.new(
View
27 test/outpost/scouts/http_test.rb
@@ -1,4 +1,5 @@
require 'test_helper'
+require 'ostruct'
describe Outpost::Scouts::Http do
class NetHttpStub
@@ -8,6 +9,22 @@ def response(&block); @response = block; end
def get_response(*args)
@response.call
end
+
+ def request_head(*args)
+ throw "Unexpected call"
+ end
+ end
+ end
+
+ class NetHttpHeadStub < NetHttpStub
+ class << self
+ def request_head(*args)
+ OpenStruct.new :code => 200, :body => 'Body'
+ end
+
+ def get_response(*args)
+ throw "Unexpected call"
+ end
end
end
@@ -47,6 +64,16 @@ def get_response(*args)
refute @subject.response_body
end
+ it "should make HEAD request if option is passed" do
+ config_stub = config_stub(:host => 'localhost', :http_class => NetHttpHeadStub, :request_head => true)
+ @subject = Outpost::Scouts::Http.new("description", config_stub)
+
+ @subject.run
+
+ assert_equal 200 , @subject.response_code
+ assert_equal 'Body', @subject.response_body
+ end
+
private
def config_stub(options={})
View
19 test/outpost/scouts/tcp_test.rb
@@ -2,15 +2,24 @@
describe Outpost::Scouts::Tcp do
it "should set the time of ping in milliseconds" do
- @server = Server.new
- @server.boot(TestApp)
- @server.wait_until_booted
+ cls = Class.new do
+ def initialize(host, port, timeout)
+ end
- config = config_stub(:port => 9595)
+ def duration
+ 0.01
+ end
+
+ def ping?
+ true
+ end
+ end
+
+ config = config_stub(:port => 9595, :pinger => cls)
subject = Outpost::Scouts::Tcp.new "test", config
subject.execute
- assert subject.response_time < 100
+ assert subject.response_time.to_i < 100
end
it "should set the time to nil when it fails" do
View
6 test/support/requests.rb
@@ -0,0 +1,6 @@
+require 'fakeweb'
+
+FakeWeb.allow_net_connect = false
+FakeWeb.register_uri(:get, "http://localhost:9595", :body => 'Up and running!', :status => 200)
+FakeWeb.register_uri(:get, "http://localhost:9595/fail", :body => 'Omg fail', :status => 500)
+FakeWeb.register_uri(:get, "http://localhost:9595/warning", :body => 'Omg need payment', :status => 402)
View
29 test/support/server.rb
@@ -1,29 +0,0 @@
-require 'rack/handler/thin'
-require 'net/http'
-
-class Server
- def wait_until_booted
- while !responsive?
- sleep 0.1
- end
- end
-
- # Got it from Capybara, but simplified it a bit.
- # lib/capybara/server.rb
- def responsive?
- res = Net::HTTP.start('localhost', 9595) { |http| http.get('/') }
-
- res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
- rescue Errno::ECONNREFUSED, Errno::EBADF
- return false
- end
-
- def boot(app)
- if not responsive?
- Thread.new do
- Thin::Logging.silent = true
- Rack::Handler::Thin.run(app, :Port => 9595)
- end
- end
- end
-end
View
15 test/support/test_app.rb
@@ -1,15 +0,0 @@
-require 'sinatra/base'
-
-class TestApp < Sinatra::Base
- get '/' do
- [200, 'Up and running!']
- end
-
- get '/fail' do
- [500, 'Omg fail']
- end
-
- get '/warning' do
- [402, 'Omg need payment']
- end
-end
View
6 test/test_helper.rb
@@ -8,8 +8,7 @@
require 'minitest/autorun'
# Integration test helpers
-require 'support/test_app'
-require 'support/server'
+require 'support/requests'
require 'support/stubs'
require 'support/nothing_raised'
@@ -19,5 +18,4 @@
require 'outpost/scouts'
include Support::Stubs
-include Support::NothingRaised
-
+include Support::NothingRaised
Something went wrong with that request. Please try again.