From ea4e48e4833fbb8eb2b52f3f9263a989b87a0a1f Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Wed, 21 Nov 2012 16:41:44 +0000 Subject: Added stub for class-factory + test objects. --- lib/custodian/protocoltest.rb | 66 +++++++++++++++++++++++++++++++++++++++ lib/custodian/protocoltest/tcp.rb | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 lib/custodian/protocoltest.rb create mode 100644 lib/custodian/protocoltest/tcp.rb (limited to 'lib') diff --git a/lib/custodian/protocoltest.rb b/lib/custodian/protocoltest.rb new file mode 100644 index 0000000..271e64c --- /dev/null +++ b/lib/custodian/protocoltest.rb @@ -0,0 +1,66 @@ + +# +# +# Base class for custodian protocol-tests +# +# Each subclass will register themselves, via the call +# to 'register_test_type'. +# +# This class is a factory that will return the correct +# derived class for a given line from our configuration +# file. +# +# +class ProtocolTest + + + # + # The subclasses we have. + # + @@subclasses = { } + + + # + # Create a test-type object given a line of text from our parser. + # + # The line will be like "target must run tcp|ssh|ftp|smtp .." + # + # + def self.create( line ) + if ( line =~ /must\s+run\s+(\S+)(\s+|\.|$)/ ) + test_type = $1.dup + test_type.chomp!( "." ) + + c = @@subclasses[test_type] + if c + c.new( line ) + else + raise ArgumentError, "Bad test type: #{test_type}" + end + else + raise "Uknown line given" + end + end + + + # + # Register a new test type - this must be called by our derived classes + # + def self.register_test_type name + @@subclasses[name] = self + end + + + # + # Return an array of test-types we know about + # + # i.e. Derived classes that have registered themselves. + # + # + def types + @@subclasses + end + + +end + diff --git a/lib/custodian/protocoltest/tcp.rb b/lib/custodian/protocoltest/tcp.rb new file mode 100644 index 0000000..fef6dea --- /dev/null +++ b/lib/custodian/protocoltest/tcp.rb @@ -0,0 +1,54 @@ +# +# The TCP-protocol test. +# +# This object is instantiated if the parser sees a line such as: +# +### +### foo.vm.bytemark.co.uk must run tcp on 22 with banner 'ssh' otherwise 'ssh fail'. +### +# +# The specification of the port is mandatory, the banner is optional. +# +class TCPTest < ProtocolTest + + + # + # Constructor + # + # Ensure we received a port to run the TCP-test against. + # + def initialize( line ) + raise ArgumentError, "Missing port" unless ( line =~ /on\s+([0-9]+)/ ); + @error = nil + end + + + # + # Helper for development. + # + def display + puts "I'm a TCP-test!" + end + + + # + # Run the TCP-protocol test. + # + def run_test + + # reset the error, in case we were previously executed. + @error = nil + + end + + + # + # If the test fails then report the error. + # + def error + @error + end + + register_test_type "tcp" + +end -- cgit v1.2.1