summaryrefslogtreecommitdiff
path: root/lib/custodian/testfactory.rb
blob: db3f3393c5403eaa82fd4acb3447316cd4d4af81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
module Custodian


  #
  #  The result of a single test:
  #
  #  If a test returns "TEST_FAILED" an alert will be raised.
  #
  #  If a test returns "TEST_PASSED" any outstanding alert will clear.
  #
  #  If a test returns "TEST_SKIPPED" NEITHER of those things will happen.
  #
  class TestResult
    TEST_PASSED  = 2
    TEST_FAILED  = 4
    TEST_SKIPPED = 8


    #
    #  Allow one of our symbols to be converted back
    #
    def self.to_str(status)
      for sym in self.constants
        if self.const_get(sym) == status
          return sym.to_s
        end
      end
    end
  end



  #
  #
  # 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 TestFactory


    #
    # 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)


      raise ArgumentError, 'The type of test to create cannot be nil' if line.nil?
      raise ArgumentError, 'The type of test to create must be a string' unless line.kind_of? String

      #
      #  The array of tests we return.
      #
      #  This is required because a single test-definition may result in
      # multiple tests being executed.
      #
      result = []


      #
      # If this is an obvious protocol test.
      #
      if  line =~ /must\s+(not\s+)?run\s+(\S+)(\s+|\.|$)/

        test_type = $2.dup
        test_type = test_type.downcase
        test_type.chomp!('.')

        if  @@subclasses[test_type].nil?
            raise ArgumentError, "There is no handler registered for the '#{test_type}' test-type"
        end


        #
        #  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

            #
            # Is the test inverted?
            #
            obj.set_inverted(line =~ /must\s+not\s+run/ ? true : false)

            result.push(obj)
          else
            raise ArgumentError, "Bad test type: '#{test_type}'"
          end
        end

        # return the test-types.
        return(result)

      else
        raise "Failed to instantiate a suitable protocol-test for '#{line}'"
      end
    end


    #
    # Register a new test type - this must be called by our derived classes
    #
    def self.register_test_type(name)
      @@subclasses[name] ||= []
      @@subclasses[name].push(self)
    end


    #
    # Return the test-types we know about.
    #
    # i.e. Derived classes that have registered themselves.
    #
    def self.known_tests
      @@subclasses
    end


    #
    # Get the friendly-type of derived-classes
    #
    def get_type
      # 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
    end


    #
    # Return the target of this test.
    #
    def target
      @host
    end




    #
    # Return the user-text which is returned on error
    #
    def get_notification_text
      @notification_text
    end




    #
    # Set the user-text which is returned on error.
    #
    def set_notification_text(str)
      @notification_text = str
    end




    #
    #  Is this test inverted?
    #
    def set_inverted(val)
      @inverted = val
    end

    def inverted?
      @inverted
    end



    #
    #  Return the port of this test.
    #
    def port
      @port
    end




  end

end