From 58f5a950200f5957307a8552b92ce9e92f948b73 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Thu, 3 May 2012 19:42:20 +0100 Subject: * Person and people lists have now become interchangeable, with notifications only recording the username, rather than the Person/PeopleList. This means that notifications in alert groups can use default #during clauses and #every values from people lists and persons. * Notify clauses can now take an array of persons/people_lists, and people_lists can refer to other people lists. * PeopleList names are now in the same namespace as usernames. * Tests to go with. --- test/tc_mauve_alert_changed.rb | 2 + test/tc_mauve_alert_group.rb | 129 ++++++++++- .../tc_mauve_configuration_builders_people_list.rb | 14 +- test/tc_mauve_notification.rb | 253 ++++++++++++++++++--- test/tc_mauve_people_list.rb | 4 +- 5 files changed, 351 insertions(+), 51 deletions(-) (limited to 'test') diff --git a/test/tc_mauve_alert_changed.rb b/test/tc_mauve_alert_changed.rb index 6cd51fb..679b9bb 100644 --- a/test/tc_mauve_alert_changed.rb +++ b/test/tc_mauve_alert_changed.rb @@ -43,6 +43,7 @@ alert_group("test_group") { notify("test_person") { every 5.minutes + during { true } } } @@ -118,6 +119,7 @@ alert_group("test_group") { notify("test_person") { every 5.minutes + during { true } } } diff --git a/test/tc_mauve_alert_group.rb b/test/tc_mauve_alert_group.rb index 7e7c4dd..92a9d61 100644 --- a/test/tc_mauve_alert_group.rb +++ b/test/tc_mauve_alert_group.rb @@ -1,17 +1,33 @@ $:.unshift "../lib" require 'th_mauve' -require 'mauve/alert_group' require 'th_mauve_resolv' +require 'mauve/alert_group' +require 'mauve/server' +require 'mauve/configuration' +require 'mauve/configuration_builder' +require 'mauve/configuration_builders' require 'pp' class TcMauveAlertGroup < Mauve::UnitTest + include Mauve + + def setup + super + setup_database + end + + def teardown + teardown_database + super + end + def test_matches_alert - alert = Mauve::Alert.new + alert = Alert.new - alert_group = Mauve::AlertGroup.new("test") + alert_group = AlertGroup.new("test") alert_group.includes = Proc.new { true } assert( alert_group.matches_alert?(alert) ) @@ -38,7 +54,112 @@ class TcMauveAlertGroup < Mauve::UnitTest assert( ! alert_group.matches_alert?(alert) ) end - + def test_notify + config=< "test", + :source => "test", + :subject => "test" + ) + + a.raise! + assert_equal("test1@example.com", notification_buffer.pop[2]) + assert(notification_buffer.empty?) + + Timecop.freeze(Time.now + 5.minutes) + a.acknowledge!(Configuration.current.people["test2"], Time.now + 5.minutes) + assert_equal(2, notification_buffer.length) + assert_equal(["test1@example.com", "test2@example.com"], notification_buffer.collect{|m| m[2]}.sort) + notification_buffer.pop until notification_buffer.empty? + + Timecop.freeze(Time.now + 5.minutes) + a.clear! + assert_equal(2, notification_buffer.length) + assert_equal(["test1@example.com", "test2@example.com"], notification_buffer.collect{|m| m[2]}.sort) + notification_buffer.pop until notification_buffer.empty? + + # + # If we raise it again, test2 shouldn't get notified. + # + Timecop.freeze(Time.now + 5.minutes) + a.raise! + assert_equal("test1@example.com", notification_buffer.pop[2]) + assert(notification_buffer.empty?) + + Timecop.freeze(Time.now + 5.minutes) + a.clear! + assert_equal("test1@example.com", notification_buffer.pop[2]) + assert(notification_buffer.empty?) + + # + # Freeze to 1am + # + Timecop.freeze(Time.local(2012,5,2,1,0,0)) + + a.raise! + assert_equal("test2@example.com", notification_buffer.pop[2]) + assert(notification_buffer.empty?) + + Timecop.freeze(Time.now + 5.minutes) + a.acknowledge!(Configuration.current.people["test1"], Time.now + 5.minutes) + assert_equal("test2@example.com", notification_buffer.pop[2]) + assert(notification_buffer.empty?) + + # + # Test1 shouldn't get notified, even though he ack'd it. + # + Timecop.freeze(Time.now + 5.minutes) + a.clear! + assert_equal("test2@example.com", notification_buffer.pop[2]) + assert(notification_buffer.empty?) + end end diff --git a/test/tc_mauve_configuration_builders_people_list.rb b/test/tc_mauve_configuration_builders_people_list.rb index 7e0840f..d74f25a 100644 --- a/test/tc_mauve_configuration_builders_people_list.rb +++ b/test/tc_mauve_configuration_builders_people_list.rb @@ -39,9 +39,9 @@ people_list("garmin-cervelo", %w( EOF x = nil assert_nothing_raised { x = Mauve::ConfigurationBuilder.parse(config) } - assert_equal(2, x.people_lists.keys.length) - assert_equal(["team sky","garmin-cervelo"].sort,x.people_lists.keys.sort) - assert_equal(%w(geraint edvald bradley rigoberto ben), x.people_lists["team sky"].list) + assert_equal(2, x.people.keys.length) + assert_equal(["team sky","garmin-cervelo"].sort,x.people.keys.sort) + assert_equal(%w(geraint edvald bradley rigoberto ben), x.people["team sky"].list) end def test_duplicate_people_list @@ -61,13 +61,7 @@ EOF # * 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) + assert_raise(ArgumentError) { x = Mauve::ConfigurationBuilder.parse(config) } end end diff --git a/test/tc_mauve_notification.rb b/test/tc_mauve_notification.rb index 00005e1..f78ec27 100644 --- a/test/tc_mauve_notification.rb +++ b/test/tc_mauve_notification.rb @@ -316,10 +316,12 @@ alert_group("default") { level URGENT notify("test1") { + during { true } every 10.minutes } notify("testers") { + during { true } every 15.minutes } @@ -563,48 +565,229 @@ EOF :subject => "test" ) - # - # This should only alert test1 - # - assert_equal(0, Time.now.hour) - alert.raise! - assert_equal(1, notification_buffer.size, "Wrong number of notifications sent") - assert_equal("test1@example.com", notification_buffer.pop[2]) + # At midnight just test1 + # At 1am just test2 + # At 2am, both test1 and test2 (via testers) + # At 3am both test1 and test2 (via testers clause with during) + # At 4am no-one - alert.clear! - assert_equal(1, notification_buffer.size, "Wrong number of notifications sent") - assert_equal("test1@example.com", notification_buffer.pop[2]) + [ + [0, %w(test1)], + [1, %w(test2)], + [2, %w(test1 test2)], + [3, %w(test1 test2)], + [4, []] + ].each do |hour, people| + + assert_equal(hour, Time.now.hour) + alert.raise! + assert_equal(people.length, notification_buffer.size, "Wrong number of notifications sent after a raise at #{hour} am") + sent = [] + sent << notification_buffer.pop while !notification_buffer.empty? + assert_equal(people.collect{|u| "#{u}@example.com"}.sort, sent.collect{|n| n[2]}.sort) + + alert.clear! + assert_equal(people.length, notification_buffer.size, "Wrong number of notifications sent after a clear at #{hour} am") + sent = [] + sent << notification_buffer.pop while !notification_buffer.empty? + assert_equal(people.collect{|u| "#{u}@example.com"}.sort, sent.collect{|n| n[2]}.sort) + + # + # Wind the clock forward for the next round of tests. + # + Timecop.freeze(Time.now+1.hours) + end + end + + def test_individual_notification_preferences_part_deux + config=< "test", + :source => "test", + :subject => "test" + ) + # - # Wind forward to 1am when test2 should get alerted - # - Timecop.freeze(Time.now+1.hours) + # At midnight just test1 should be notified. + # At 1am, just test2 + # At 2am both test1 and test2 + + [ + [0, %w(test1)], + [1, %w(test2)], + [2, %w(test1 test2)] + ].each do |hour, people| + + assert_equal(hour, Time.now.hour) + alert.raise! + assert_equal(people.length, notification_buffer.size, "Wrong number of notifications sent after a raise at #{hour} am") + sent = [] + sent << notification_buffer.pop while !notification_buffer.empty? + assert_equal(people.collect{|u| "#{u}@example.com"}.sort, sent.collect{|n| n[2]}.sort) + + alert.clear! + assert_equal(people.length, notification_buffer.size, "Wrong number of notifications sent after a clear at #{hour} am") + sent = [] + sent << notification_buffer.pop while !notification_buffer.empty? + assert_equal(people.collect{|u| "#{u}@example.com"}.sort, sent.collect{|n| n[2]}.sort) + + # + # Wind the clock forward for the next round of tests. + # + Timecop.freeze(Time.now+1.hours) + end - assert_equal(1, Time.now.hour) - alert.raise! - assert_equal(1, notification_buffer.size, "Wrong number of notifications sent") - assert_equal("test2@example.com", notification_buffer.pop[2]) - - alert.clear! - assert_equal(1, notification_buffer.size, "Wrong number of notifications sent") - assert_equal("test2@example.com", notification_buffer.pop[2]) + end - # - # Wind forward to 2am when the testers group should get alerted - # - Timecop.freeze(Time.now+1.hours) + def test_notify_array_of_usernames + config=< "test", + :source => "test", + :subject => "test" + ) + assert_equal("test", alert.alert_group.name) + + # At midnight no-one should be alerted + # At 1am just test1 should be alerted + # At 2am just test2 should be alerted (via the testers1 group) + # At 3am no-one should be alerted + # At 4am test1, test2, and test3 should all be alerted (via the testers2 group) + + [ + [0, []], + [1, %w(test1)], + [2, %w(test2)], + [3, []], + [4, %w(test1 test2 test3)] + ].each do |hour, people| + + assert_equal(hour, Time.now.hour) + alert.raise! + assert_equal(people.length, notification_buffer.size, "Wrong number of notifications sent after a raise at #{hour} am") + sent = [] + sent << notification_buffer.pop while !notification_buffer.empty? + assert_equal(people.collect{|u| "#{u}@example.com"}.sort, sent.collect{|n| n[2]}.sort) + + alert.clear! + assert_equal(people.length, notification_buffer.size, "Wrong number of notifications sent after a clear at #{hour} am") + sent = [] + sent << notification_buffer.pop while !notification_buffer.empty? + assert_equal(people.collect{|u| "#{u}@example.com"}.sort, sent.collect{|n| n[2]}.sort) + + # + # Wind the clock forward for the next round of tests. + # + Timecop.freeze(Time.now+1.hours) + end + + end end diff --git a/test/tc_mauve_people_list.rb b/test/tc_mauve_people_list.rb index 78586d2..2f6755a 100644 --- a/test/tc_mauve_people_list.rb +++ b/test/tc_mauve_people_list.rb @@ -60,7 +60,7 @@ EOF notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue Server.instance.setup - people_list = Configuration.current.people_lists["testers"] + people_list = Configuration.current.people["testers"] alert = Alert.new( :alert_id => "test", @@ -142,7 +142,7 @@ EOF stub_request(:get, "http://localhost/api/attendees/support_shift/2011-08-01T00:05:00"). to_return(:status => 200, :body => YAML.dump(%w(test2))) - people_list = Configuration.current.people_lists["testers"] + people_list = Configuration.current.people["testers"] assert_equal([Configuration.current.people["test1"]], people_list.people) assert_equal([Configuration.current.people["test2"]], people_list.people(Time.now + 5.minutes)) end -- cgit v1.2.1