diff options
| -rw-r--r-- | lib/custodian/alerter.rb | 47 | ||||
| -rwxr-xr-x | t/test-alerter.rb | 40 | 
2 files changed, 69 insertions, 18 deletions
| diff --git a/lib/custodian/alerter.rb b/lib/custodian/alerter.rb index 2a86bbe..72bf37a 100644 --- a/lib/custodian/alerter.rb +++ b/lib/custodian/alerter.rb @@ -46,17 +46,42 @@ class Alerter +  # +  # Is the named target inside the Bytemark IP-range? +  # +  def inside_bytemark?( target ) + +    # +    #  Test trange, and format the appropriate message. +    # +    inside = false + +    if ( BYTEMARK_RANGES.any?{|range| range.include?(IPAddr.new(target))} ) +      inside = true +    end + +    inside +  end + + +  # +  # Expand to a message indicating whether a hostname is inside the Bytemark network. +  # or not.    # -  # Expand to a message indicating whether a hostname is inside -  # the Bytemark network.    #    def expand_inside_bytemark( host ) +    # +    #  If the host is a URL then we need to work with the hostname component alone. +    # +    #  We'll also make the host a link that can be clicked in the alert we raise. +    #      target = host      if ( target =~ /https?:\/\/([^\/]+)/ )        target = $1.dup +      host   = "<a href=\"#{host}\">#{host}</a>"      end @@ -65,7 +90,6 @@ class Alerter      #      resolved = nil -      #      #  Resolve the target to an IP, unless it is already an address.      # @@ -84,22 +108,9 @@ class Alerter      # -    # Make any HTTP target a link in the details. -    # -    if ( host =~ /^http/ ) -        host = "<a href=\"#{host}\">#{host}</a>" -    end - +    #  Return the formatted message      # -    #  Test trange, and format the appropriate message. -    # -    inside = false; -    if ( BYTEMARK_RANGES.any?{|range| range.include?(IPAddr.new(resolved.to_s))} ) -      inside = true -    end - - -    if ( inside ) +    if ( inside_bytemark?( resolved.to_s ) )        if ( resolved == target )          return "<p>#{host} is inside the Bytemark network.</p>"        else diff --git a/t/test-alerter.rb b/t/test-alerter.rb index 995b27b..a09c666 100755 --- a/t/test-alerter.rb +++ b/t/test-alerter.rb @@ -86,6 +86,7 @@ class TestAlerter < Test::Unit::TestCase        if ( text =~ /OUTSIDE/ )          assert( inside == false )        end +      end      # @@ -102,4 +103,43 @@ class TestAlerter < Test::Unit::TestCase +  # +  #  Test inside_bytemark? +  # +  def test_inside_bytemark + +    # +    #  Hash of hostnames and version of address. +    # +    to_test = { + +      # +      # Hosts inside the Bytemark network +      # +      "80.68.85.48"               => true, +      "2001:41c8:125:46::10"      => true, + +      # +      # Hosts outside the Bytemark network +      # +      "127.0.0.1"                 => false, +      "192.168.1.1"               => false, +      "2a00:1450:400c:c00::93"    => false, +    } + + +    to_test.each do |name,inside| + +      obj = Alerter.new( nil ) + +      if ( inside ) +        assert( obj.inside_bytemark?( name ) == true  ) +      else +        assert( obj.inside_bytemark?( name ) == false ) +      end +    end + +  end + +  end | 
