require 'minitest/autorun'
require 'mail'
# I assume cat'ing to the LOAD path goes away when we're a real gem.
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
require 'nagios-herald'
require 'nagios-herald/messages/pager'
# Test Formatter::Base.
class TestMessagePager < MiniTest::Unit::TestCase
# TODO: We need a similar set of tests for RECOVERY pages.
# Initial setup before we execute tests
def setup
@recipient = 'ops@example.com'
@options = { :replyto => 'nagios@example.com' }
@message = NagiosHerald::Message::Pager.new(@recipient, @options)
@message.content = {
:attachments=>[],
:html => {
:host_info=>"
Host: web.example.com Service: Disk Space
",
:state_info=>"State is now: CRITICAL for 0d 0h 5m 12s (was CRITICAL) after 3 / 3 checks
",
:additional_info=>"Additional Info: DISK CRITICAL - free space: / 7002 MB (18% inode 60%): /data 16273093 MB (26% inode 99%):
",
:action_url=>"Action URL: http://runbook.example.com/disk_space_alerts.html
",
:notes=>"",
:additional_details=>"Additional Details:
\nTHRESHOLDS - WARNING:15%;CRITICAL:20%;\n\nFilesystem Size Used Avail Use% Mounted on\n/dev/vda 40G 31G 6.9G 82% /\ntmpfs 2.5G 83M 2.4G 4% /dev/shm\nnfs.example.example.com:/mnt/user/homes\n 59T 43T 16T 74% /data\n
",
:recipients_email_link=>"Sent to ops
",
:notification_info=>"Notification sent at: Thu May 16 21:06:38 UTC 2013 (notification number 1)
",
:alert_ack_url=>"Acknowledge this alert: http://nagios.example.com/nagios/cgi-bin/cmd.cgi?cmd_typ=34&host=web.example.com&service=Disk%20Space
Alternatively, reply to this message with the word 'ack' in the body to acknowledge the alert.
"
},
:subject => "PROBLEM Service web.example.com/Disk Space is CRITICAL",
:text => {
:host_info=>"Host: web.example.com Service: Disk Space\n\n",
:state_info=>"State is now: CRITICAL for 0d 0h 5m 12s (was CRITICAL) after 3 / 3 checks\n\n",
:additional_info=>"Additional Info: DISK CRITICAL - free space: / 7002 MB (18% inode 60%): /data 16273093 MB (26% inode 99%):\n\n",
:action_url=>"Action URL: http://runbook.example.com/disk_space_alerts.html\n\n",
:notes=>"",
:additional_details=>"Additional Details: \nTHRESHOLDS - WARNING:15%;CRITICAL:20%;\n\nFilesystem Size Used Avail Use% Mounted on\n/dev/vda 40G 31G 6.9G 82% /\ntmpfs 2.5G 83M 2.4G 4% /dev/shm\nnfs.example.example.com:/mnt/user/homes\n 59T 43T 16T 74% /data\n\n",
:recipients_email_link=>"Sent to ops\n\n",
:notification_info=>"Notification sent at: Thu May 16 21:06:38 UTC 2013 (notification number 1)\n\n",
:alert_ack_url=>"Acknowledge this alert: http://nagios.example.com/nagios/cgi-bin/cmd.cgi?cmd_typ=34&host=web.example.com&service=Disk%20Space\nAlternatively, reply to this message with the word 'ack' in the body to acknowledge the alert.\n"
},
:short_text => {
:host_info=>"web.example.com/Disk Space\n\n",
:state_info=>"CRITICAL for 0d 0h 5m 12s (was CRITICAL) after 3 / 3 checks\n\n",
:additional_info=>"DISK CRITICAL - free space: / 7002 MB (18% inode 60%): /data 16273093 MB (26% inode 99%):\n\n",
:action_url=>"Action URL: http://runbook.example.com/disk_space_alerts.html\n\n",
:notes=>"",
:additional_details=>"THRESHOLDS - WARNING:15%;CRITICAL:20%;\n\nFilesystem Size Used Avail Use% Mounted on\n/dev/vda 40G 31G 6.9G 82% /\ntmpfs 2.5G 83M 2.4G 4% /dev/shm\nnfs.example.example.com:/mnt/user/homes\n 59T 43T 16T 74% /data\n\n",
:recipients_email_link=>"Sent to ops\n\n",
:notification_info=>"Notification sent at: Thu May 16 21:06:38 UTC 2013 (notification number 1)\n\n",
:alert_ack_url=>"Acknowledge this alert: http://nagios.example.com/nagios/cgi-bin/cmd.cgi?cmd_typ=34&host=web.example.com&service=Disk%20Space\nAlternatively, reply to this message with the word 'ack' in the body to acknowledge the alert.\n"
}
}
end
def teardown
end
# Test that we have a new NagiosHerald::Message::Pager object.
def test_new_message_pager
assert_instance_of NagiosHerald::Message::Pager, @message
end
def test_message_delivery
# This test depends on the mailcatcher gem being installed and
# `mailcatcher` running. `mailcatcher` runs locally on tcp/1025.
# NOTE: NagiosHerald::Message::Pager#send calls #build_message before
# delivering the message. We need to override some SMTP settings for this
# test so we call #deliver! here.
mail = @message.build_message
# Set 'return_response' to true so that #deliver! returns the
# Net::SMTP::Response for us to validate.
mail.delivery_method :smtp, { :address => 'localhost', :port => 1025, :return_response => true }
response = mail.deliver!
assert_equal "250", response.status
end
end