From 8c8e5ae926e0009743fe92dccb588783640a6022 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Wed, 17 Aug 2011 11:08:07 +0100 Subject: * Reminder notifications now take the same path to notify as initial alerts * Threading tidied -- processor will not do anything unless the timer has frozen * Person#send_alert now tidied and merged with alert_changed * POP3 server only shows alerts relevant to the user * Server now defaults to using an in-memory SQLite database (good for testing) * Server initializes a blank mauve config. * Tests tidied up --- test/tc_mauve_alert.rb | 110 +++++++++++++++++++-- test/tc_mauve_alert_changed.rb | 76 ++++++++++++++ test/tc_mauve_alert_group.rb | 4 +- test/tc_mauve_configuration_builder.rb | 20 +++- .../tc_mauve_configuration_builders_alert_group.rb | 4 +- test/tc_mauve_configuration_builders_logger.rb | 8 +- ...e_configuration_builders_notification_method.rb | 4 +- test/tc_mauve_configuration_builders_person.rb | 4 +- test/tc_mauve_configuration_builders_server.rb | 4 +- test/tc_mauve_notification.rb | 106 +++++++++++++------- test/tc_mauve_person.rb | 17 ++++ test/tc_mauve_source_list.rb | 4 +- test/tc_mauve_time.rb | 4 +- test/test_mauve.rb | 3 + test/th_mauve.rb | 91 +++++++++++++++++ 15 files changed, 392 insertions(+), 67 deletions(-) create mode 100644 test/tc_mauve_alert_changed.rb create mode 100644 test/tc_mauve_person.rb create mode 100644 test/th_mauve.rb (limited to 'test') diff --git a/test/tc_mauve_alert.rb b/test/tc_mauve_alert.rb index ef80424..f79859f 100644 --- a/test/tc_mauve_alert.rb +++ b/test/tc_mauve_alert.rb @@ -1,16 +1,30 @@ $:.unshift "../lib" -require 'test/unit' + +require 'th_mauve' +require 'th_mauve_resolv' + require 'mauve/alert' +require 'mauve/proto' +require 'mauve/server' require 'mauve/configuration' require 'mauve/configuration_builder' -require 'th_mauve_resolv' -require 'pp' +require 'mauve/configuration_builders' -class TcMauveAlert < Test::Unit::TestCase +class TcMauveAlert < Mauve::UnitTest + include Mauve - def test_source_list + def setup + super + setup_database + end + + def teardown + teardown_database + super + end + def test_source_list config=< "test-host", + :alert_id => "test" ) + + a.raise! + end + + def test_alert_reception + Server.instance.setup + + update = Proto::AlertUpdate.new + update.source = "test-host" + message = Proto::Alert.new + update.alert << message + message.id = "test1" + message.summary = "test summary" + message.detail = "test detail" + message.raise_time = Time.now.to_i + message.clear_time = Time.now.to_i+5.minutes + + Alert.receive_update(update, Time.now, "127.0.0.1") + + a = Alert.first(:alert_id => 'test1') + + assert(a.raised?) + assert_equal("test-host", a.subject) + assert_equal("test-host", a.source) + assert_equal("test detail", a.detail) + assert_equal("test summary", a.summary) + + end + + def test_alert_ackowledgement + person = Mauve::Person.new + person.username = "test-user" + + Server.instance.setup + + Mauve::Configuration.current.people[person.username] = person + + alert = Alert.new( + :alert_id => "test-acknowledge", + :source => "test", + :subject => "test" + ) + alert.raise! + assert(alert.raised?) + + alert.acknowledge!(person, Time.now + 3.minutes) + assert(alert.acknowledged?) + + next_alert = Alert.find_next_with_event + assert_equal(next_alert.id, alert.id) + assert_equal(Time.now+3.minutes, next_alert.due_at) + + Timecop.freeze(next_alert.due_at) + + alert.poll + + # + # The alert should unacknowledge itself. + # + assert(!alert.acknowledged?) + + + end + end diff --git a/test/tc_mauve_alert_changed.rb b/test/tc_mauve_alert_changed.rb new file mode 100644 index 0000000..52f1f25 --- /dev/null +++ b/test/tc_mauve_alert_changed.rb @@ -0,0 +1,76 @@ +$:.unshift "../lib" + +require 'mauve/alert' +require 'mauve/alert_changed' +require 'mauve/configuration' +require 'mauve/configuration_builder' +require 'mauve/configuration_builders' +require 'th_mauve' + +class TcMauveAlertChanged < Mauve::UnitTest + include Mauve + + def setup + super + setup_database + end + + def teardown + teardown_database + super + end + + def test_reminder + + config=< "test", :alert_id => "test_alert", :summary => "test alert") + alert.raise! + + reminders = 1 + notifications = 1 + + mins = 0 + 121.times do + mins += 1 + + assert_equal(notifications, Server.instance.notification_buffer.length) + assert_equal(reminders, AlertChanged.count) + + Timecop.freeze(Time.now+1.minutes) + + if mins % 5 == 0 + notifications += 1 + reminders += 1 + end + + AlertChanged.all.each{|ac| ac.poll} + end + + end + + +end + + + diff --git a/test/tc_mauve_alert_group.rb b/test/tc_mauve_alert_group.rb index 12f25ef..d6416a2 100644 --- a/test/tc_mauve_alert_group.rb +++ b/test/tc_mauve_alert_group.rb @@ -1,11 +1,11 @@ $:.unshift "../lib" -require 'test/unit' +require 'th_mauve' require 'mauve/alert_group' require 'th_mauve_resolv' require 'pp' -class TcMauveAlert < Test::Unit::TestCase +class TcMauveAlert < Mauve::UnitTest def test_matches_alert diff --git a/test/tc_mauve_configuration_builder.rb b/test/tc_mauve_configuration_builder.rb index 03b86ca..faa0bfa 100644 --- a/test/tc_mauve_configuration_builder.rb +++ b/test/tc_mauve_configuration_builder.rb @@ -1,13 +1,16 @@ $:.unshift "../lib/" -require 'test/unit' -require 'pp' +require 'th_mauve' require 'mauve/configuration_builder' -class TcMauveConfigurationBuildersPeopleAndSourceLists < Test::Unit::TestCase +class TcMauveConfigurationBuildersPeopleAndSourceLists < Mauve::UnitTest def setup - Log4r::Logger.new("Mauve").outputters = Log4r::Outputter.stdout + setup_logger + end + + def teardown + teardown_logger end def test_people_list @@ -48,7 +51,16 @@ people_list "htc-highroad", EOF x = nil + # + # This should generate two warnings: + # * duplicate list + # * Lars already being on a list + # assert_nothing_raised { x = Mauve::ConfigurationBuilder.parse(config) } + + assert_match(/Lars/, logger_pop()) + assert_match(/Duplicate/, logger_pop()) + assert_equal(1, x.people_lists.keys.length) assert_equal(["mark c","mark r","Lars","Bernie","Danny"].sort, x.people_lists["htc-highroad"].list.sort) end diff --git a/test/tc_mauve_configuration_builders_alert_group.rb b/test/tc_mauve_configuration_builders_alert_group.rb index a4104bb..77d16b5 100644 --- a/test/tc_mauve_configuration_builders_alert_group.rb +++ b/test/tc_mauve_configuration_builders_alert_group.rb @@ -1,10 +1,10 @@ $:.unshift "../lib/" -require 'test/unit' +require 'th_mauve' require 'pp' require 'mauve/configuration_builders/alert_group' -class TcMauveConfigurationBuildersNotificationMethod < Test::Unit::TestCase +class TcMauveConfigurationBuildersNotificationMethod < Mauve::UnitTest def test_load diff --git a/test/tc_mauve_configuration_builders_logger.rb b/test/tc_mauve_configuration_builders_logger.rb index 8a4bc4e..2d2bb80 100644 --- a/test/tc_mauve_configuration_builders_logger.rb +++ b/test/tc_mauve_configuration_builders_logger.rb @@ -1,10 +1,12 @@ $:.unshift "../lib/" -require 'test/unit' -require 'pp' +require 'th_mauve' require 'mauve/configuration_builders/logger' -class TcMauveConfigurationBuildersLogger < Test::Unit::TestCase +class TcMauveConfigurationBuildersLogger < Mauve::UnitTest + + def setup + end def test_load diff --git a/test/tc_mauve_configuration_builders_notification_method.rb b/test/tc_mauve_configuration_builders_notification_method.rb index 1ea1e08..e5e8475 100644 --- a/test/tc_mauve_configuration_builders_notification_method.rb +++ b/test/tc_mauve_configuration_builders_notification_method.rb @@ -1,10 +1,10 @@ $:.unshift "../lib/" -require 'test/unit' +require 'th_mauve' require 'pp' require 'mauve/configuration_builders/notification_method' -class TcMauveConfigurationBuildersNotificationMethod < Test::Unit::TestCase +class TcMauveConfigurationBuildersNotificationMethod < Mauve::UnitTest def test_load diff --git a/test/tc_mauve_configuration_builders_person.rb b/test/tc_mauve_configuration_builders_person.rb index cb0b100..b4fbd4f 100644 --- a/test/tc_mauve_configuration_builders_person.rb +++ b/test/tc_mauve_configuration_builders_person.rb @@ -1,10 +1,10 @@ $:.unshift "../lib/" -require 'test/unit' +require 'th_mauve' require 'pp' require 'mauve/configuration_builders/person' -class TcMauveConfigurationBuildersNotificationMethod < Test::Unit::TestCase +class TcMauveConfigurationBuildersNotificationMethod < Mauve::UnitTest def test_load diff --git a/test/tc_mauve_configuration_builders_server.rb b/test/tc_mauve_configuration_builders_server.rb index 6f4255f..213ff68 100644 --- a/test/tc_mauve_configuration_builders_server.rb +++ b/test/tc_mauve_configuration_builders_server.rb @@ -1,9 +1,9 @@ $:.unshift "../lib/" -require 'test/unit' +require 'th_mauve' require 'mauve/configuration_builders/server' -class TcMauveConfigurationBuildersServer < Test::Unit::TestCase +class TcMauveConfigurationBuildersServer < Mauve::UnitTest def test_server_params hostname = "test.example.com" diff --git a/test/tc_mauve_notification.rb b/test/tc_mauve_notification.rb index 03e6b96..435d5da 100644 --- a/test/tc_mauve_notification.rb +++ b/test/tc_mauve_notification.rb @@ -1,28 +1,34 @@ $:.unshift "../lib" -require 'test/unit' +require 'th_mauve' require 'mauve/alert' require 'mauve/notification' require 'mauve/configuration' require 'mauve/configuration_builder' require 'mauve/configuration_builders' require 'mauve/mauve_time' -require 'th_mauve_resolv' -require 'th_mauve_time' -require 'th_logger' -require 'pp' +class TcMauveDuringRunner < Mauve::UnitTest + include Mauve -class TcMauveDuringRunner < Test::Unit::TestCase + def setup + super + setup_database + end + + def teardown + teardown_database + super + end def test_initialize - alert = Mauve::Alert.new + alert = Alert.new time = Time.now during = Proc.new { false } - dr = Mauve::DuringRunner.new(time, alert, &during) + dr = DuringRunner.new(time, alert, &during) assert_equal(dr.alert, alert) assert_equal(dr.time, time) @@ -31,11 +37,12 @@ class TcMauveDuringRunner < Test::Unit::TestCase end def test_now? - alert = Mauve::Alert.new + alert = Alert.new time = Time.now during = Proc.new { @test_time } - dr = Mauve::DuringRunner.new(time, alert, &during) + dr = DuringRunner.new(time, alert, &during) + assert_equal(time, dr.now?) assert_equal(time+3600, dr.now?(time+3600)) assert_equal(time, dr.time) @@ -57,36 +64,37 @@ class TcMauveDuringRunner < Test::Unit::TestCase # This should give us midnight last sunday night. # now = Time.now - midnight_sunday = now - (now.hour.hours + now.min.minutes + now.sec.seconds + now.wday.days) # # first working hour on Monday - monday_morning = midnight_sunday.in_x_hours(0,"working") + workday_morning = now.in_x_hours(0,"working") + + assert(workday_morning != now, "booo") # # This should alert at exactly first thing on Monday morning. # - dr = Mauve::DuringRunner.new(midnight_sunday, nil){ working_hours? } - assert_equal(dr.find_next(6.hours), monday_morning) + dr = DuringRunner.new(now, nil){ working_hours? } + assert_equal(dr.find_next(6.hours), workday_morning) # # This should alert six hours later than the last one. # - dr = Mauve::DuringRunner.new(monday_morning, nil){ working_hours? } - assert_equal(dr.find_next(6.hours), monday_morning + 6.hours) + dr = DuringRunner.new(workday_morning, nil){ working_hours? } + assert_equal(dr.find_next(6.hours), workday_morning + 6.hours) # # Now assuming the working day is not 12 hours long, if we progress to 6 # hours in the future then the next alert should be first thing on Tuesday. # - dr = Mauve::DuringRunner.new(monday_morning + 6.hours, nil){ working_hours? } - tuesday_morning = monday_morning+24.hours + dr = DuringRunner.new(workday_morning + 6.hours, nil){ working_hours? } + tuesday_morning = workday_morning+24.hours assert_equal(dr.find_next(6.hours), tuesday_morning) # # If an alert is too far in the future (a week) return nil. # - dr = Mauve::DuringRunner.new(monday_morning, nil){ @test_time > (@time + 12.days) } + dr = DuringRunner.new(workday_morning, nil){ @test_time > (@time + 12.days) } assert_nil(dr.find_next) end @@ -101,7 +109,20 @@ class TcMauveDuringRunner < Test::Unit::TestCase end -class TcMauveNotification < Test::Unit::TestCase +class TcMauveNotification < Mauve::UnitTest + + include Mauve + + def setup + @logger = setup_logger + Timecop.freeze(Time.local(2011,8,1,0,0,0,0)) + end + + def teardown + teardown_logger + Timecop.return + DataObjects::Pooling.pools.each{|pool| pool.dispose} + end def test_notify t = Time.now @@ -141,7 +162,7 @@ alert_group("default") { } notify("test2") { - during { @test_time.to_i >= #{(t + 1.hour).to_i} } + during { hours_in_day 1..23 } every 10.minutes } @@ -153,19 +174,28 @@ alert_group("default") { } EOF - - assert_nothing_raised { - Mauve::Configuration.current = Mauve::ConfigurationBuilder.parse(config) - Mauve::Server.instance.setup - alert = Mauve::Alert.new( - :alert_id => "test", - :source => "test", - :subject => "test" - ) - alert.raise! - } + Configuration.current = ConfigurationBuilder.parse(config) + Server.instance.setup + alert = Alert.new( + :alert_id => "test", + :source => "test", + :subject => "test" + ) + alert.raise! + + assert_equal(1, Alert.count, "Wrong number of alerts saved") - assert_equal(1, Mauve::Alert.count) + # + # Although there are four clauses above for notifications, test1 should be + # alerted in 10 minutes time, and the 15 minutes clause is ignored, since + # 10 minutes is sooner. + # + assert_equal(3, AlertChanged.count, "Wrong number of reminders inserted") + + # + # Also make sure that only 1 notification has been sent.. + # + assert_equal(1, Server.instance.notification_buffer.size, "Wrong number of notifications sent") reminder_times = { "test1" => t + 10.minutes, @@ -173,12 +203,12 @@ EOF "test3" => t + 2.hours } - Mauve::AlertChanged.all.each do |a| - pp a - assert_equal("urgent", a.level, "Level is wrong") - assert_equal("raised", a.update_type, "Update type is wrong") - assert_in_delta(reminder_times[a.person].to_f, a.remind_at.to_time.to_f, 10.0, "reminder time is wrong for #{a.person}") + AlertChanged.all.each do |a| + assert_equal("urgent", a.level, "Level is wrong for #{a.person}") + assert_equal("raised", a.update_type, "Update type is wrong for #{a.person}") + assert_equal(reminder_times[a.person], a.remind_at,"reminder time is wrong for #{a.person}") end + end end diff --git a/test/tc_mauve_person.rb b/test/tc_mauve_person.rb new file mode 100644 index 0000000..8ac3141 --- /dev/null +++ b/test/tc_mauve_person.rb @@ -0,0 +1,17 @@ +$:.unshift "../lib" + +require 'th_mauve' +require 'mauve/person' +require 'mauve/configuration' +require 'mauve/configuration_builder' +require 'mauve/configuration_builders' +require 'pp' + +class TcMauvePerson < Mauve::UnitTest + + + +end + + + diff --git a/test/tc_mauve_source_list.rb b/test/tc_mauve_source_list.rb index d07791f..bc68094 100644 --- a/test/tc_mauve_source_list.rb +++ b/test/tc_mauve_source_list.rb @@ -1,11 +1,11 @@ $:.unshift "../lib/" -require 'test/unit' +require 'th_mauve' require 'mauve/source_list' require 'th_mauve_resolv' require 'pp' -class TcMauveSourceList < Test::Unit::TestCase +class TcMauveSourceList < Mauve::UnitTest def test_hostname_match sl = Mauve::SourceList.new("test") diff --git a/test/tc_mauve_time.rb b/test/tc_mauve_time.rb index 0749fef..7a8fefa 100644 --- a/test/tc_mauve_time.rb +++ b/test/tc_mauve_time.rb @@ -1,10 +1,10 @@ $: << "../lib/" -require 'test/unit' +require 'th_mauve' require 'mauve/mauve_time' require 'pp' -class TestMauveTime < Test::Unit::TestCase +class TestMauveTime < Mauve::UnitTest def test_in_x_hours diff --git a/test/test_mauve.rb b/test/test_mauve.rb index a8dba9b..87aa188 100644 --- a/test/test_mauve.rb +++ b/test/test_mauve.rb @@ -4,6 +4,7 @@ $:.unshift libdir if File.directory?(libdir) end +require 'pp' require 'test/unit' %w( @@ -15,8 +16,10 @@ tc_mauve_configuration_builders_person.rb tc_mauve_configuration_builders_server.rb tc_mauve_source_list.rb tc_mauve_people_list.rb +tc_mauve_person.rb tc_mauve_alert.rb tc_mauve_alert_group.rb +tc_mauve_alert_changed.rb tc_mauve_notification.rb tc_mauve_time.rb ).each do |s| diff --git a/test/th_mauve.rb b/test/th_mauve.rb new file mode 100644 index 0000000..a113ad0 --- /dev/null +++ b/test/th_mauve.rb @@ -0,0 +1,91 @@ +require 'test/unit' +require 'mauve/datamapper' +require 'timecop' +require 'log4r' +require 'pp' + +module Mauve + class TestOutputter < Log4r::Outputter + def initialize( _name, hash={}) + @buffer = [] + super + end + + def pop ; @buffer.pop ; end + def shift ; @buffer.shift ; end + + def write(data) + @buffer << data + end + + def flush + print "\n" if @buffer.length > 0 + while d = @buffer.shift + print d + end + end + + end +end + + +module Mauve + class UnitTest < Test::Unit::TestCase + + def setup + setup_logger + setup_time + end + + def teardown + teardown_logger + teardown_time + end + + def setup_logger + @logger = Log4r::Logger.new 'Mauve' + @outputter = Mauve::TestOutputter.new("test") + @outputter.formatter = Log4r::PatternFormatter.new( :pattern => "%d %l %m" ) + @outputter.level = Log4r::DEBUG + @logger.outputters << @outputter + return @logger + end + + def logger_pop + @outputter.pop + end + + def teardown_logger + logger = Log4r::Logger['Mauve'] + return if logger.nil? + + o = logger.outputters.find{|o| o.name == "test"} + o.flush if o.respond_to?("flush") + # Delete the logger. + Log4r::Logger::Repository.instance.loggers.delete('Mauve') + end + + def setup_database + DataMapper::Model.raise_on_save_failure = true + end + + def teardown_database + DataObjects::Pooling.pools.each{|pool| pool.dispose} + end + + def setup_time + Timecop.freeze(Time.local(2011,8,1,0,0,0,0)) + end + + def teardown_time + Timecop.return + end + + def default_test + # + # + flunk("No tests specified") unless self.class == Mauve::UnitTest + end + + end +end -- cgit v1.2.1