testotron 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/test.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  module Testotron
2
+ class TestFailed < Exception
3
+ end
4
+
2
5
  class Test
3
6
  end
4
7
  end
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
- @runner.report_error(@report_methods, test, failure)
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
- puts test.class.to_s.rjust(20) + ": #{msg}"
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
- REPORT_METHODS = [ :local_mail, :stderr, :xosdutil ]
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::Tests
4
- class HTTP < Testotron::Test
5
- KEY = "http"
3
+ module Testotron
4
+ module Tests
5
+ class HTTP < Test
6
+ KEY = "http"
6
7
 
7
- def initialize(host, port = 80, requests = nil)
8
- if requests.nil?
9
- requests = "http://#{host}/"
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
- def human_name
15
- "HTTP test of #{@host}, port #{@port}, requests #{@requests.join ','}"
16
- end
15
+ def human_name
16
+ "HTTP test of #{@host}, port #{@port}, requests #{@requests.join ','}"
17
+ end
17
18
 
18
- def run(runner)
19
- runner.report self, "Testing HTTP server on #{@host} port #{@port}..."
20
- http = Net::HTTP.new(@host, @port)
21
- @requests.each do |page|
22
- runner.report self, "Trying #{page}..."
23
- request = Net::HTTP::Get.new URI.parse(page).request_uri
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
- begin
26
- response = http.request(request)
27
- rescue Errno::ETIMEDOUT
28
- raise TestFailed, "HTTP connection timed out"
29
- rescue Errno::ECONNREFUSED
30
- raise TestFailed, "HTTP connection refused"
31
- end
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
- if response.code.to_i != 200
34
- raise TestFailed, "Response not 200 on #{@post}:#{@port} GET #{page}"
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::Tests
4
- class SMTP < Testotron::Test
5
- KEY = "smtp"
3
+ module Testotron
4
+ module Tests
5
+ class SMTP < Test
6
+ KEY = "smtp"
6
7
 
7
- def initialize(host, port = 25)
8
- @host, @port = host, port
9
- end
8
+ def initialize(host, port = 25)
9
+ @host, @port = host, port
10
+ end
10
11
 
11
- def human_name
12
- "SMTP test of #{@host}, port #{@port}"
13
- end
12
+ def human_name
13
+ "SMTP test of #{@host}, port #{@port}"
14
+ end
14
15
 
15
- def run(runner)
16
- runner.report self, "Testing SMTP server of #{@host} port #{@port}..."
17
- smtp = Net::SMTP.new(@host, @port)
18
- begin
19
- smtp.start
20
- smtp.finish
21
- rescue Errno::ECONNREFUSED => e
22
- raise TestFailed, "Server refused SMTP connection"
23
- rescue EOFError => e
24
- raise TestFailed, "EOF reached while connecting to SMTP server"
25
- rescue Exception
26
- raise TestFailed
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: testotron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: