Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from Jan 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions Gemfile
Expand Up @@ -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
14 changes: 2 additions & 12 deletions Gemfile.lock
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -55,12 +47,10 @@ PLATFORMS
ruby

DEPENDENCIES
fakeweb
mail
minitest (~> 2.0.2)
net-ping
rack
rake
ruby-debug
sinatra
thin
tinder
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lib/outpost/notifiers/email.rb
Expand Up @@ -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.'
Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion lib/outpost/scouts/http.rb
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions test/integration/basic_application_test.rb
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions test/integration/more_complex_test.rb
Expand Up @@ -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 => '/'
Expand Down
6 changes: 1 addition & 5 deletions test/integration/no_subclassing_test.rb
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/integration/reporting_test.rb
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/outpost/notifiers/email_test.rb
Expand Up @@ -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(
Expand Down
27 changes: 27 additions & 0 deletions test/outpost/scouts/http_test.rb
@@ -1,4 +1,5 @@
require 'test_helper'
require 'ostruct'

describe Outpost::Scouts::Http do
class NetHttpStub
Expand All @@ -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

Expand Down Expand Up @@ -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={})
Expand Down
19 changes: 14 additions & 5 deletions test/outpost/scouts/tcp_test.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions 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)
29 changes: 0 additions & 29 deletions test/support/server.rb

This file was deleted.

15 changes: 0 additions & 15 deletions test/support/test_app.rb

This file was deleted.

6 changes: 2 additions & 4 deletions test/test_helper.rb
Expand Up @@ -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'

Expand All @@ -19,5 +18,4 @@
require 'outpost/scouts'

include Support::Stubs
include Support::NothingRaised

include Support::NothingRaised