summaryrefslogtreecommitdiff
path: root/t/test-custodian-testfactory.rb
blob: 65abc5b1bf15ae9dd5b389ee467b291924bf1225 (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
#!/usr/bin/ruby -Ilib/ -I../lib/


require 'test/unit'

require 'custodian/protocoltest'



class TestTestFactory < Test::Unit::TestCase

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

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


  #
  # Test the FTP-test may be created
  #
  def test_ftp_uri

    assert_nothing_raised do
      assert( Custodian::TestFactory.create( "ftp.example.com must  run ftp." ) )
      assert( Custodian::TestFactory.create( "ftp://ftp.example.com/ must run ftp." ) )
      assert( Custodian::TestFactory.create( "ftp://ftp.example.com/ must run ftp on 21." ) )
      assert( Custodian::TestFactory.create( "ftp://ftp.example.com/ must run ftp on 21 otherwise 'xxx'." ) )
    end


    assert( Custodian::TestFactory.create( "ftp.example.com        must run ftp." ).target() == "ftp.example.com"  )
    assert( Custodian::TestFactory.create( "ftp://ftp.example.com/ must run ftp." ).target() == "ftp.example.com"  )


    #
    #  Test the port detection
    #
    data = {
      "foo must run ftp." => "21",
      "ftp://ftp.example.com/ must run ftp." => "21",
      "foo must run ftp on  1 otherwise 'x'." => "1",
      "foo must run ftp on 33 otherwise"   => "33",
    }

    #
    #  Run each test
    #
    data.each do |str,prt|
      assert_nothing_raised do

        obj = Custodian::TestFactory.create( str )

        #
        #  Ensure we got the object, and the port was correct.
        #
        assert(obj, "created object via TestFactory.create('#{str}')")
        assert( obj.port().to_s == prt , "'#{str}' gave expected port '#{prt}'.")
      end
    end

  end



  #
  # Test the rsync-test creation.
  #
  def test_rsync_uri


    assert_nothing_raised do
      assert( Custodian::TestFactory.create( "example.com must  run rsync." ) )
      assert( Custodian::TestFactory.create( "ftp://example.com/ must run rsync." ) )
      assert( Custodian::TestFactory.create( "ftp://example.com/ must run rsync on 333." ) )
      assert( Custodian::TestFactory.create( "ftp://example.com/ must run rsync on 3311 otherwise 'xxx'." ) )
    end

    assert( Custodian::TestFactory.create( "rsync.example.com  must run rsync." ).target() ==
            "rsync.example.com"  )
    assert( Custodian::TestFactory.create( "rsync://rsync.example.com/ must run rsync." ).target() ==
            "rsync.example.com"  )


    #
    #  Test the ports
    #
    data = {
      "foo must run rsync." => "873",
      "rsync://foo/ must run rsync." => "873",
      "foo must run rsync on 1 otherwise 'x'." => "1",
      "foo must run rsync on 33 otherwise"   => "33",
    }

    #
    #  Run each test
    #
    data.each do |str,prt|
      assert_nothing_raised do

        obj = Custodian::TestFactory.create( str )

        #
        #  Ensure we got the object, and the port was correct.
        #
        assert(obj, "created object via TestFactory.create('#{str}')")
        assert( obj.port().to_s == prt , "'#{str}' gave expected port '#{prt}'.")
      end
    end
  end




  #
  # Test the DNS test may be created
  #
  def test_dns_handler

    assert_nothing_raised do
      assert( Custodian::TestFactory.create( "a.ns.bytemark.co.uk must run dns for bytemark.co.uk resolving NS as '80.68.80.26;85.17.170.78;80.68.80.27'." ) )
    end

    #
    #  Missing record-type
    #
    assert_raise ArgumentError do
      Custodian::TestFactory.create( "a.ns.bytemark.co.uk must run dns for bytemark.co.uk as '80.68.80.26;85.17.170.78;80.68.80.27'." )
    end

    #
    #  Missing target
    #
    assert_raise ArgumentError do
      assert( Custodian::TestFactory.create( "a.ns.bytemark.co.uk must run dns resolving NS as '80.68.80.26;85.17.170.78;80.68.80.27'." ) )
    end

    #
    #  Missing expected results
    #
    assert_raise ArgumentError do
      assert( Custodian::TestFactory.create( "a.ns.bytemark.co.uk must run dns for www.bytemark.co.uk resolving NS " ) )
    end
  end




  #
  # Test the creation of inverted tests.
  #
  def test_inverted_tests


    assert_nothing_raised do
      assert( Custodian::TestFactory.create( "example.com must not run rsync." ) )
      assert( Custodian::TestFactory.create( "ftp://example.com/ must not run rsync." ) )
      assert( Custodian::TestFactory.create( "ftp://example.com/ must not run rsync on 333." ) )
    end


    #
    #  Test some inversions
    #
    data = {
      "foo must run rsync."              => false,
      "rsync://foo/ must run rsync."     => false,
      "foo must run ping otherwise"      => false,
      "foo must not run ping otherwise"  => true,
      "foo must not run ssh otherwise"   => true,
      "foo must not run ldap otherwise"  => true,
    }

    #
    #  Run each test
    #
    data.each do |str,inv|
      assert_nothing_raised do

        obj = Custodian::TestFactory.create( str )

        #
        #  Ensure we got the object, and the port was correct.
        #
        assert(obj, "created object via TestFactory.create('#{str}')")
        assert( obj.inverted() == inv, "#{str} -> #{inv}" )
      end
    end

  end

  #
  # Get all the types we know about.
  #
  def test_types
    registered = Custodian::TestFactory.known_tests()

    registered.each do |obj|


      #
      #  Try to get the name
      #
      name=obj.to_s
      if ( name =~ /protocoltest::(.*)Test$/i )
        tst = $1.dup.downcase

        #
        # NOTE: Skip the DNS test - it is more complex.
        #
        # Skip the HTTP test because the HTTPS test is a super-set.
        #
        next if ( tst =~ /^(dns|http)$/ )

        # normal
        test_one = "http://foo/ must run #{tst} on 1234"
        test_two = "http://foo/ must not run #{tst} on 12345"

        assert_nothing_raised do

          test_one_obj = Custodian::TestFactory.create( test_one )
          assert( !test_one_obj.inverted() )

          test_two_obj = Custodian::TestFactory.create( test_two )
          assert( test_two_obj.inverted(), "Found inverted test for #{tst}" )

          assert_equal( tst, test_one_obj.get_type )
          assert_equal( tst, test_two_obj.get_type )
        end
      end
    end
  end

end