testotron 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/test.rb +3 -0
- data/lib/testotron.rb +38 -6
- data/lib/tests/http.rb +34 -26
- data/lib/tests/smtp.rb +25 -21
- metadata +1 -1
data/lib/test.rb
CHANGED
data/lib/testotron.rb
CHANGED
@@ -9,9 +9,6 @@ require 'tests/http'
|
|
9
9
|
require 'tests/smtp'
|
10
10
|
|
11
11
|
module Testotron
|
12
|
-
class TestFailed < Exception
|
13
|
-
end
|
14
|
-
|
15
12
|
TEST_CLASSES = [ Tests::HTTP, Tests::SMTP ]
|
16
13
|
|
17
14
|
class TestBuilder
|
@@ -27,23 +24,57 @@ module Testotron
|
|
27
24
|
begin
|
28
25
|
test.run(@runner)
|
29
26
|
rescue TestFailed => failure
|
30
|
-
|
27
|
+
complain(test, failure)
|
31
28
|
end
|
32
29
|
end
|
33
30
|
}
|
34
31
|
|
32
|
+
def complain(test, failure)
|
33
|
+
if @complaint_block
|
34
|
+
@complaint_block.call(test, failure)
|
35
|
+
end
|
36
|
+
@runner.report_error(@report_methods, test, failure)
|
37
|
+
end
|
38
|
+
|
35
39
|
def report_with(*methods)
|
40
|
+
methods = methods.first if methods.length == 1
|
41
|
+
methods = [methods] unless methods.is_a? Array
|
36
42
|
@report_methods = methods.map &:to_sym
|
37
43
|
end
|
44
|
+
|
45
|
+
def complain_using(&block)
|
46
|
+
raise ArgumentError, "No block given to complain to" unless block
|
47
|
+
@complaint_block = block
|
48
|
+
end
|
49
|
+
|
50
|
+
def quiet
|
51
|
+
@runner.quiet
|
52
|
+
end
|
53
|
+
|
54
|
+
def quiet=(value)
|
55
|
+
@runner.quiet = value
|
56
|
+
end
|
57
|
+
|
58
|
+
def quiet!
|
59
|
+
quiet = true
|
60
|
+
end
|
38
61
|
end
|
39
62
|
|
40
63
|
# TODO: set mail target
|
41
64
|
class TestRunner
|
42
65
|
def report(test, msg)
|
43
|
-
|
66
|
+
unless @quiet
|
67
|
+
puts test.class.to_s.rjust(20) + ": #{msg}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
attr_accessor :quiet
|
72
|
+
|
73
|
+
def initialize
|
74
|
+
@quiet = false
|
44
75
|
end
|
45
76
|
|
46
|
-
|
77
|
+
VALID_REPORT_METHODS = [ :local_mail, :stderr, :xosdutil ]
|
47
78
|
|
48
79
|
def report_error(methods, test, failure)
|
49
80
|
if methods.include?(:local_mail)
|
@@ -76,6 +107,7 @@ EOF
|
|
76
107
|
|
77
108
|
def self.test(*args)
|
78
109
|
runner = TestRunner.new
|
110
|
+
runner.quiet = true
|
79
111
|
|
80
112
|
if block_given?
|
81
113
|
yield(TestBuilder.new(runner))
|
data/lib/tests/http.rb
CHANGED
@@ -1,37 +1,45 @@
|
|
1
1
|
require 'test'
|
2
2
|
|
3
|
-
module Testotron
|
4
|
-
|
5
|
-
|
3
|
+
module Testotron
|
4
|
+
module Tests
|
5
|
+
class HTTP < Test
|
6
|
+
KEY = "http"
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def initialize(host, port = 80, requests = nil)
|
9
|
+
if requests.nil?
|
10
|
+
requests = "http://#{host}/"
|
11
|
+
end
|
12
|
+
@host, @port, @requests = host, port, [*requests]
|
10
13
|
end
|
11
|
-
@host, @port, @requests = host, port, [*requests]
|
12
|
-
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
def human_name
|
16
|
+
"HTTP test of #{@host}, port #{@port}, requests #{@requests.join ','}"
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
def run(runner)
|
20
|
+
runner.report self, "Testing HTTP server on #{@host} port #{@port}..."
|
21
|
+
http = Net::HTTP.new(@host, @port)
|
22
|
+
http.read_timeout = 2
|
23
|
+
http.open_timeout = 2
|
24
|
+
@requests.each do |page|
|
25
|
+
runner.report self, "Trying #{page}..."
|
26
|
+
request = Net::HTTP::Get.new URI.parse(page).request_uri
|
24
27
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
begin
|
29
|
+
response = http.request(request)
|
30
|
+
rescue Errno::ETIMEDOUT
|
31
|
+
raise TestFailed, "HTTP connection timed out"
|
32
|
+
rescue Errno::ECONNREFUSED
|
33
|
+
raise TestFailed, "HTTP connection refused"
|
34
|
+
rescue SocketError
|
35
|
+
raise TestFailed, "HTTP connection failed (SocketError)"
|
36
|
+
end
|
32
37
|
|
33
|
-
|
34
|
-
|
38
|
+
good_codes = 100...400
|
39
|
+
code = response.code.to_i
|
40
|
+
unless good_codes.include? code
|
41
|
+
raise TestFailed, "Response code #{code} on #{@post}:#{@port} GET #{page}"
|
42
|
+
end
|
35
43
|
end
|
36
44
|
end
|
37
45
|
end
|
data/lib/tests/smtp.rb
CHANGED
@@ -1,29 +1,33 @@
|
|
1
1
|
require 'test'
|
2
2
|
|
3
|
-
module Testotron
|
4
|
-
|
5
|
-
|
3
|
+
module Testotron
|
4
|
+
module Tests
|
5
|
+
class SMTP < Test
|
6
|
+
KEY = "smtp"
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def initialize(host, port = 25)
|
9
|
+
@host, @port = host, port
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def human_name
|
13
|
+
"SMTP test of #{@host}, port #{@port}"
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
smtp.
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
16
|
+
def run(runner)
|
17
|
+
runner.report self, "Testing SMTP server of #{@host} port #{@port}..."
|
18
|
+
smtp = Net::SMTP.new(@host, @port)
|
19
|
+
smtp.read_timeout = 2
|
20
|
+
smtp.open_timeout = 2
|
21
|
+
begin
|
22
|
+
smtp.start
|
23
|
+
smtp.finish
|
24
|
+
rescue Errno::ECONNREFUSED => e
|
25
|
+
raise TestFailed, "Server refused SMTP connection"
|
26
|
+
rescue EOFError => e
|
27
|
+
raise TestFailed, "EOF reached while connecting to SMTP server"
|
28
|
+
rescue Exception
|
29
|
+
raise TestFailed
|
30
|
+
end
|
27
31
|
end
|
28
32
|
end
|
29
33
|
end
|