summaryrefslogtreecommitdiff
path: root/t/test-custodian-parser.rb
blob: e7b0bb55a2636a1b3aa56fbf5e03ca48b3454c8a (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/ruby -I./lib/ -I../lib/


require 'test/unit'
require 'custodian/parser'




#
# Unit test for our parser.
#
class TestCustodianParser < Test::Unit::TestCase




  #
  # Create the test suite environment: NOP.
  #
  def setup
  end




  #
  # Destroy the test suite environment: NOP.
  #
  def teardown
  end




  #
  #  Test we can create a new parser object - specifically
  # that it throws exceptions if it is not given a filename
  # that exists.
  #
  def test_init

    #
    #  Constructor
    #
    assert_nothing_raised do
      Custodian::Parser.new()
    end
  end


  def test_period
    parser = Custodian::Parser.new()
    result = parser.parse_line( "example.vm.bytemark.co.uk must run ping except between 00-23" )
    assert( result.nil? )
  end



  #
  #  Test the different kinds of parsing:
  #
  #  1.  By string - single line.
  #  2.  By array - with multiple lines.
  #  3.  By lines - with newlines.
  #
  def test_parsing_api

    #
    #  1.  By string.
    #
    parser = Custodian::Parser.new()

    #  1.a.  Comment lines return nil.
    result = parser.parse_line( "# this is a comment" )
    assert( result.nil? )

    #  1.b.  Non-strings are an error
    assert_raise ArgumentError do
      result = parser.parse_line( nil )
    end


    #  1.c.  Adding a test will return an array of test-objects.
    result = parser.parse_line( "smtp.bytemark.co.uk must run smtp on 25 otherwise 'failure'." );
    assert( !result.nil? )
    assert( result.kind_of? Array )
    assert( result.size == 1 )


    #
    # 2.  By array.
    #
    parser = Custodian::Parser.new()
    #  2.a.  Comment lines return nil.
    tmp    = Array.new()
    tmp.push( "# This is a comment.." )
    assert( parser.parse_lines( tmp ).nil? )

    #  2.b.  Adding a test will return an array of test-objects.
    tmp = Array.new()
    tmp.push( "smtp.bytemark.co.uk must run ssh on 22 otherwise 'oops'." );
    ret = parser.parse_lines( tmp )
    assert( ret.kind_of? Array );
    assert( ret.size == 1 )

    #
    # 3.  By lines
    #
    parser = Custodian::Parser.new()
    #  3.a.  Comment lines return nil.
    str =<<EOF
# This is a comment
# This is also a fine comment
EOF
    assert( parser.parse_lines( str ).nil? )

    #  3.b.  Adding a test will return an array of test-objects.
    str = <<EOF
smtp.bytemark.co.uk must run smtp on 25.
google.com must run ping otherwise 'internet broken?'.
EOF
    ret = parser.parse_lines( str )
    assert( ret.kind_of? Array );
    assert( ret.size == 1 )

  end




  #
  #  Test that we can define macros.
  #
  def test_macros_lines

    parser = Custodian::Parser.new()

    #
    #  Input text
    #
    text =<<EOF
FOO is  kvm1.vm.bytemark.co.uk.
TEST is kvm2.vm.bytemark.co.uk.
EOF

    #
    # Test the parser with this text
    #
    parser.parse_lines( text )


    #
    #  We should now have two macros.
    #
    macros = parser.macros
    assert( ! macros.empty? )
    assert( macros.size() == 2 )
  end




  #
  #  Test that we can define macros.
  #
  def test_macros_array

    parser = Custodian::Parser.new()

    #
    #  Input text
    #
    text = Array.new()
    text.push( "FOO  is  kvm1.vm.bytemark.co.uk." );
    text.push( "FOO2 is  kvm2.vm.bytemark.co.uk." );

    #
    # Test the parser with this text
    #
    parser.parse_lines( text )


    #
    #  We should now have two macros.
    #
    macros = parser.macros
    assert( ! macros.empty? )
    assert( macros.size() == 2 )
  end




  #
  # Duplicate macros are a bug
  #
  def test_duplicate_macros

    parser = Custodian::Parser.new()

    #
    #  Input text to parse.
    #
    text = Array.new()
    text.push( "FOO is kvm1.vm.bytemark.co.uk." );
    text.push( "FOO is kvm2.vm.bytemark.co.uk." );

    #
    # Test the parser with this text
    #
    assert_raise ArgumentError do
      parser.parse_lines( text )
    end


    #
    #  We should now have one macro.
    #
    macros = parser.macros
    assert( ! macros.empty? )
    assert( macros.size() == 1 )
  end




  #
  # Test the expansion of macros.
  #
  def test_macro_expansion

    #
    #  Create a parser - validate it is free of macros.
    #
    parser = Custodian::Parser.new()
    macros = parser.macros
    assert( macros.empty? )

    #
    #  Expand a line - which should result in no change
    # as the line does not involve a known-macro
    #
    in_txt  = "example.bytemark.co.uk must run smtp."
    out_txt = parser.expand_macro( in_txt )

    #
    #  The difference is the return value will be an array
    #
    assert( out_txt.kind_of? Array )
    assert( out_txt.size() == 1 )
    assert( out_txt[0] == in_txt )


    #
    #  Now define a macro
    #
    parser.parse_line( "TARGET is example1.bytemark.co.uk and example2.bytemark.co.uk." )
    macros = parser.macros
    assert( !macros.empty? )

    #
    # Now we have a two-host macro, repeat the expansion
    #
    ret = parser.expand_macro( "TARGET must run smtp on 25." )

    #
    # The result should be an array
    #
    assert( ret.kind_of? Array )
    assert_equal( ret.size(), 2 )
    assert( ret[0] =~ /example1/)
    assert( ret[1] =~ /example2/)

  end


  #
  # Test that the parser works for HTTP-redirection
  #
  def test_http_redirection

    #
    # test data
    #
    data = {
      "http://example must run http."                         => true,
      "http://example must run http with status 200."         => true,
      "http://example must run http with content 'bar'."      => true,
      "http://example must run http following redirects."     => true,
      "http://example must run http not following redirects." => false,
      "http://example must run http not following redirect."  => false,
    }

    data.each do |str,follow|
      assert_nothing_raised do

        #
        # Create the new parser
        #
        obj = Custodian::TestFactory.create( str )
        assert(obj, "Instantiating a test works")
        assert( str == obj.to_s, "Getting back the test works" )

        if ( follow )
          assert( obj.follow_redirects? )
        else
          assert( ! obj.follow_redirects? )
        end
      end
    end
  end



  #
  # Test that the parser works for cache-busting.
  #
  def test_http_cache_busting

    #
    # test data
    #
    data = {
      "http://example must run http."                         => true,
      "http://example must run http with status 200."         => true,
      "http://example must run http with content 'bar'."      => true,
      "http://example must run http without cache busting."   => false,
    }

    data.each do |str,cb|
      assert_nothing_raised do

        #
        # Create the new parser
        #
        obj = Custodian::TestFactory.create( str )
        assert(obj, "Instantiating a test works")
        assert( str == obj.to_s, "Getting back the test works" )

        if ( cb )
          assert( obj.cache_busting? )
        else
          assert( ! obj.cache_busting? )
        end
      end
    end
  end




  #
  # Test that the text we're going to use in alerters is present.
  #
  def test_alert_text

    #
    # test data
    #
    data = {
      "foo must run rsync."                     => nil,
      "foo must run redis."                     => nil,
      "foo must not run ping."                  => nil,
      "foo must not run ssh otherwise 'fail'"   => "fail",
      "foo must not run ssh otherwise 'fail'."  => "fail",
      "foo must run redis otherwise 'memorystorage service is dead'" => "memorystorage service is dead",
      "foo must run ping otherwise 'don't you love me?'" => "don"
    }

    #
    #  For each test
    #
    data.each do |str,fail|
      assert_nothing_raised do

        #
        # Create the new parser
        #
        obj = Custodian::TestFactory.create( str )
        assert(obj, "Instantiating a test works")
        assert( str == obj.to_s, "Getting back the test works" )

        if ( fail.nil? )
          assert( obj.get_notification_text().nil? )
        else
          assert_equal( obj.get_notification_text(), fail )
        end

      end
    end
  end
end