diff options
| -rw-r--r-- | lib/custodian/alerts/mauve.rb | 10 | ||||
| -rw-r--r-- | lib/custodian/testfactory.rb | 62 | ||||
| -rw-r--r-- | lib/custodian/worker.rb | 65 | ||||
| -rwxr-xr-x | t/test-http-vs-https.rb | 8 | ||||
| -rwxr-xr-x | t/test-ldap-probe.rb | 9 | 
5 files changed, 101 insertions, 53 deletions
| diff --git a/lib/custodian/alerts/mauve.rb b/lib/custodian/alerts/mauve.rb index 66379a5..9a5d9ed 100644 --- a/lib/custodian/alerts/mauve.rb +++ b/lib/custodian/alerts/mauve.rb @@ -213,14 +213,20 @@ module Custodian          test_host = test.target          test_type = test.get_type -          alert         = Mauve::Proto::Alert.new          #          # Mauve only lets us use IDs which are <= 255 characters in length          # hash the line from the parser to ensure it is short enough.          # (IDs must be unique, per-source) -        alert.id      = Digest::SHA1.hexdigest(test.to_s) +        # +        # Because there might be N-classes which implemented the test +        # we need to make sure these are distinct too. +        # +        id_key  = test.to_s +        id_key += test.class + +        alert.id = Digest::SHA1.hexdigest(id_key)          alert.subject = subject          alert.summary = "The #{test_type} test failed against #{test_host}" diff --git a/lib/custodian/testfactory.rb b/lib/custodian/testfactory.rb index 603b69f..b00703f 100644 --- a/lib/custodian/testfactory.rb +++ b/lib/custodian/testfactory.rb @@ -36,29 +36,45 @@ module Custodian        raise ArgumentError, "The type of test to create must be a string" unless ( line.kind_of? String )        # +      #  The array we return. +      # +      result = Array.new() + + +      #        # If this is an obvious protocol test.        #        if ( line =~ /must\s+(not\s+)?run\s+(\S+)(\s+|\.|$)/ ) +          test_type = $2.dup          test_type.chomp!( "." ) -        c = @@subclasses[test_type] -        if c - -          obj = c.new( line ) - -          # -          # Get the notification text, which is not specific to the test-type -          # -          # We do this only after we've instantiated the test. -          # -          if ( line =~ /\s+otherwise\s+'([^']+)'/ ) -            obj.set_notification_text( $1.dup ) -          end -          return obj -        else -          raise ArgumentError, "Bad test type: '#{test_type}'" +        # +        #  For each of the test-classes that implement the type +        # +        @@subclasses[test_type].each do |impl| + +          if impl +            obj = impl.new( line ) + +            # +            # Get the notification text, which is not specific to the test-type +            # +            # We do this only after we've instantiated the test. +            # +            if ( line =~ /\s+otherwise\s+'([^']+)'/ ) +              obj.set_notification_text( $1.dup ) +            end + +            result.push(obj) +          else +            raise ArgumentError, "Bad test type: '#{test_type}'" +          end          end + +        # return the test-types. +        return( result ) +        else          raise "Unknown line given - Failed to instantiate a suitable protocol-test: '#{line}'"        end @@ -69,7 +85,8 @@ module Custodian      # Register a new test type - this must be called by our derived classes      #      def self.register_test_type name -      @@subclasses[name] = self +      @@subclasses[name] ||=  Array.new() +      @@subclasses[name].push(self)      end @@ -88,9 +105,14 @@ module Custodian      # Get the friendly-type of this class      #      def get_type -      @@subclasses.each do |name,value| -        if ( value == self.class  ) -          return name +      # get each registed type +      @@subclasses.keys.each do |name| + +        # for each handler .. +        @@subclasses[name].each do |impl| +          if ( impl == self.class ) +            return name +          end          end        end        nil diff --git a/lib/custodian/worker.rb b/lib/custodian/worker.rb index 78e7cf0..87f2c7e 100644 --- a/lib/custodian/worker.rb +++ b/lib/custodian/worker.rb @@ -128,46 +128,54 @@ module Custodian -      # -    # Fetch a single job from the queue, and process it. +    # Fetch a single job from the queue, and dispatch it for +    # processing.      #      def process_single_job -      result = false +      # +      #  Acquire a job from our queue. +      # +      job = @queue.fetch(1) -      begin +      # +      #  Ensure that the job is sane. +      # +      raise ArgumentError, "Job was empty" if (job.nil?) +      raise ArgumentError, "Job was not a string" unless job.kind_of?(String) -        # -        #  Acquire a job. -        # -        job = @queue.fetch(1) -        log_message( "Job aquired: #{job}" ) -        # -        #  Get the job body -        # -        raise ArgumentError, "Job was empty" if (job.nil?) -        raise ArgumentError, "Job was not a string" unless job.kind_of?(String) +      # +      # Create the test-object from our class-factory +      # +      Custodian::TestFactory.create( job ).each do |test| +        puts "CREATED FROM JOB: #{test.class}" +        process_single_test( test ) +      end +    end -        # -        # The count of times this test has run. -        # -        count = 1 +    # +    # Fetch a single job from the queue, and process it. +    # +    def process_single_test( test ) + +      begin +        log_message( "Received job: #{test.to_s}" )          # -        # Create the test-object. +        # The count of times this test has run, the result, and the start-time          # -        test = Custodian::TestFactory.create( job ) - +        count      = 1 +        result     = false          start_time = Time.now          # -        #  We'll run no more than MAX times. +        #  If a job fails we'll repeat it, but no more than MAX times.          # -        #  We stop the execution on a single success. +        #  We exit here if we receive a single success.          #          while ( ( count < ( @retry_count + 1 ) ) && ( result == false ) ) @@ -182,11 +190,10 @@ module Custodian            if ( result )              log_message( "Test succeeed - clearing alert" )              do_clear( test ) -            success = true            end            # -          #  Some of our routers don't like being hammered. +          #  Some of our routers/hosts don't like being hammered.            #            #  We delay before re-testing, but we only do this if            # we're not on the last count. @@ -200,7 +207,7 @@ module Custodian            end            # -          #  Increase count. +          #  Increase the log of times we've repeated the test.            #            count += 1          end @@ -210,12 +217,16 @@ module Custodian          #          end_time = Time.now -          #          #  Duration of the test-run, in milliseconds          #          duration = (( end_time - start_time ) * 1000.0).to_i + +        # +        #  Record that, if we have any alerters that are interested +        # in run-times. +        #          do_duration( test, duration ) diff --git a/t/test-http-vs-https.rb b/t/test-http-vs-https.rb index 8ad5aea..b320777 100755 --- a/t/test-http-vs-https.rb +++ b/t/test-http-vs-https.rb @@ -46,7 +46,9 @@ class TestTestName < Test::Unit::TestCase      end      assert( test ) -    assert_equal( test.get_type, "http" ) +    assert( test.kind_of? Array ) +    assert( ! test.empty? ) +    assert_equal( test[0].get_type, "http" )    end @@ -61,7 +63,9 @@ class TestTestName < Test::Unit::TestCase      end      assert( test ) -    assert_equal( test.get_type, "https" ) +    assert( test.kind_of? Array ) +    assert( ! test.empty? ) +    assert_equal( test[0].get_type, "https" )    end diff --git a/t/test-ldap-probe.rb b/t/test-ldap-probe.rb index 4e552b4..9d6b6f0 100755 --- a/t/test-ldap-probe.rb +++ b/t/test-ldap-probe.rb @@ -32,8 +32,9 @@ class TestLDAPProbe < Test::Unit::TestCase       test = Custodian::TestFactory.create( "auth.bytemark.co.uk must run ldap on 389 with username 'testing' with password 'bob' otherwise 'LDAP dead?'." )      end -    assert( test ) -    assert_equal( test.get_type, "ldap" ) +    assert( test.kind_of? Array ) +    assert( ! test.empty? ) +    assert_equal( test[0].get_type, "ldap" )    end @@ -57,6 +58,10 @@ class TestLDAPProbe < Test::Unit::TestCase      data.each do |str|        assert_raise ArgumentError do          test = Custodian::TestFactory.create( str ) + +        assert( test.kind_of? Array ) +        assert( ! test.empty? ) +        end      end    end | 
