diff options
author | Ian Chilton <ian.chilton@bytemark.co.uk> | 2018-02-28 13:07:54 +0000 |
---|---|---|
committer | Ian Chilton <ian.chilton@bytemark.co.uk> | 2018-02-28 13:07:54 +0000 |
commit | c42a0bef7d7194989cdacc317409fa4ac0b802a0 (patch) | |
tree | d0c759084b69d6f2220ca0265f34b9979a1c0b80 | |
parent | 25b1792c7adca856bedadbeae1fa4de7972c8b5b (diff) | |
parent | 1f3d5467758529812601111b8b4fdf2566abd3d4 (diff) |
Merge branch '21-allow-custom-subjects' into 'master'
Resolve "Feature: allow specifying `with subject 'foo'` in checks to declare what the Subject should be"
Closes #21
See merge request open-source/custodian!15
-rw-r--r-- | debian/changelog | 6 | ||||
-rw-r--r-- | lib/custodian/alerts/file.rb | 12 | ||||
-rw-r--r-- | lib/custodian/alerts/mauve.rb | 13 | ||||
-rw-r--r-- | lib/custodian/testfactory.rb | 27 | ||||
-rw-r--r-- | lib/custodian/util/prefix.rb | 42 | ||||
-rwxr-xr-x | t/test-custodian-parser.rb | 35 |
6 files changed, 86 insertions, 49 deletions
diff --git a/debian/changelog b/debian/changelog index ac1a9f6..61a2a7d 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +custodian (0.41) stable; urgency=low + + * Allow tests to override the alert-subject raised via mauve (#21, !15) + + -- Steve Kemp <steve@bytemark.co.uk> Thu, 22 Feb 2018 14:22:41 +0200 + custodian (0.40) stable; urgency=high * Fake-release to ensure all hosts are updated. diff --git a/lib/custodian/alerts/file.rb b/lib/custodian/alerts/file.rb index 06a704e..0c7a6dd 100644 --- a/lib/custodian/alerts/file.rb +++ b/lib/custodian/alerts/file.rb @@ -31,7 +31,12 @@ module Custodian # Record a raise event for the given test. # def raise - write_message("RAISE: #{test.target} failed #{test.get_type}-test - #{test.error}") + subject = test.target + subject = test.get_subject() unless test.get_subject().nil? + + write_message("RAISE: #{subject} failed #{test.get_type}-test - #{test.error}") + + end @@ -47,7 +52,10 @@ module Custodian # Record a clear event for the given test. # def clear - write_message("CLEAR: #{test.target} failed #{test.get_type}-test") + subject = test.target + subject = test.get_subject() unless test.get_subject().nil? + + write_message("CLEAR: #{subject} failed #{test.get_type}-test") end diff --git a/lib/custodian/alerts/mauve.rb b/lib/custodian/alerts/mauve.rb index 0aed390..13ac747 100644 --- a/lib/custodian/alerts/mauve.rb +++ b/lib/custodian/alerts/mauve.rb @@ -2,7 +2,6 @@ require 'custodian/util/bytemark' require 'custodian/util/dns' -require 'custodian/util/prefix' require 'digest/sha1' @@ -228,10 +227,16 @@ module Custodian id_key += test.class.to_s alert.id = Digest::SHA1.hexdigest(id_key) - # Look for a subject-prefix - subject_prefix = Custodian::Util::Prefix.text() + # + # Set the subject of the alert + # + alert.subject = subject + + # + # But allow it to be overwritten if something was specified. + # + alert.subject = test.get_subject() unless test.get_subject().nil? - alert.subject = subject_prefix + subject alert.summary = "The #{test_type} test failed against #{test_host}" # diff --git a/lib/custodian/testfactory.rb b/lib/custodian/testfactory.rb index db3f339..358e676 100644 --- a/lib/custodian/testfactory.rb +++ b/lib/custodian/testfactory.rb @@ -99,10 +99,21 @@ module Custodian # # We do this only after we've instantiated the test. # - if line =~ /\s+otherwise\s+'([^']+)'/ + if line =~ /\s+otherwise\s+'([^']+)'/ obj.set_notification_text($1.dup) end + + # + # Some tests will replace their subject. + # + # + if line =~ /\s+with\s+subject\s+'([^']+)'/ + obj.set_subject($1.dup) + else + obj.set_subject( nil ) + end + # # Is the test inverted? # @@ -168,6 +179,20 @@ module Custodian end + # + # If this test has a custom subject then return it + # + def get_subject + @subject + end + + + # + # Setup a custom subject for the (mauve) alert we raise + # + def set_subject( subject ) + @subject = subject + end # diff --git a/lib/custodian/util/prefix.rb b/lib/custodian/util/prefix.rb deleted file mode 100644 index 743a422..0000000 --- a/lib/custodian/util/prefix.rb +++ /dev/null @@ -1,42 +0,0 @@ - - -# -# This class allows a custom-prefix to be prepended to any alert -# subjects. -# -module Custodian - - module Util - - class Prefix - - - # - # Return the custom-prefix to use, if any. - # - def Prefix.text() - # Default to no prefix. - default = "" - - # Look for matches - last one wins. - Dir.glob( "/store/clients/*/custodian-prefix.cfg" ).each do |file| - begin - default = File.read( file ) - rescue Errno::EACCES - # Permission-denied. - end - end - - # Remove any newline characters - default.gsub!( /[\r\n]/, '' ) - - # Truncate, if required. - max = 32 - default = default[0...max] if ( default.length > max ) - - default - end - - end - end -end diff --git a/t/test-custodian-parser.rb b/t/test-custodian-parser.rb index 2239503..8cbc2ac 100755 --- a/t/test-custodian-parser.rb +++ b/t/test-custodian-parser.rb @@ -429,6 +429,41 @@ EOF end end + + # + # Test that the parser allows subject-setting. + # + def test_http_subject_setting + + # + # test data + # + data = { + "http://example must run http with subject 'foo'." => "foo", + "http://example must run http with status 200 with subject 'bart simpson'." => "bart simpson", + "http://example must run http with content 'bar'." => nil, + 'http://example must run http without cache busting.' => nil + } + + data.each do |str, sub| + assert_nothing_raised do + + # + # Create the new parser + # + obj = Custodian::TestFactory.create(str) + + assert(!obj.nil?) + assert(obj.kind_of?(Array)) + assert(obj.size == 1) + assert_equal(obj[0].to_s, str) + + assert_equal(obj[0].get_subject, sub) + end + end + end + + # # Test that the parser works for basic-auth. # |