testotron 0.0.3

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 ADDED
@@ -0,0 +1,4 @@
1
+ module Testotron
2
+ class Test
3
+ end
4
+ end
data/lib/testotron.rb ADDED
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'mail'
4
+ require 'net/smtp'
5
+ require 'net/http'
6
+ require 'uri'
7
+
8
+ require 'tests/http'
9
+ require 'tests/smtp'
10
+
11
+ module Testotron
12
+ class TestFailed < Exception
13
+ end
14
+
15
+ TEST_CLASSES = [ Tests::HTTP, Tests::SMTP ]
16
+
17
+ class TestBuilder
18
+ def initialize(runner)
19
+ @runner = runner
20
+ @report_methods = [ :local_mail ]
21
+ end
22
+
23
+ # TODO: save for later execution
24
+ TEST_CLASSES.each { |klass|
25
+ define_method klass.const_get(:KEY) do |*args|
26
+ test = klass.new(*args)
27
+ begin
28
+ test.run(@runner)
29
+ rescue TestFailed => failure
30
+ @runner.report_error(@report_methods, test, failure)
31
+ end
32
+ end
33
+ }
34
+
35
+ def report_with(*methods)
36
+ @report_methods = methods.map &:to_sym
37
+ end
38
+ end
39
+
40
+ # TODO: set mail target
41
+ class TestRunner
42
+ def report(test, msg)
43
+ puts test.class.to_s.rjust(20) + ": #{msg}"
44
+ end
45
+
46
+ REPORT_METHODS = [ :local_mail, :stderr, :xosdutil ]
47
+
48
+ def report_error(methods, test, failure)
49
+ if methods.include?(:local_mail)
50
+ mail = Mail.new do
51
+ from 'conftest@localhost'
52
+ to `whoami`.chomp
53
+ subject "Configuration test failed!"
54
+ body <<EOF
55
+ Test:
56
+ #{test.human_name}
57
+
58
+ Message:
59
+ #{failure.message}
60
+ EOF
61
+ end
62
+
63
+ mail.delivery_method :sendmail
64
+ mail.deliver
65
+ end
66
+
67
+ if methods.include? :xosdutil
68
+ system "xosdutilctl", "echo", "Configuration test (#{test.human_name}) failed!"
69
+ end
70
+
71
+ if methods.include? :stderr
72
+ STDERR.puts "Test failed: #{test.human_name} (message: #{failure.message})"
73
+ end
74
+ end
75
+ end
76
+
77
+ def self.test(*args)
78
+ runner = TestRunner.new
79
+
80
+ if block_given?
81
+ yield(TestBuilder.new(runner))
82
+ else
83
+ raise ArgumentError if args.empty?
84
+ test = args.shift.to_sym
85
+ keys = TEST_CLASSES.map { |x| x.const_get(:KEY).to_sym }
86
+ raise KeyError, "Unknown test: #{test}" unless TEST_CLASSES.map { |x| x.const_get(:KEY).to_sym }.include?(test)
87
+ TEST_CLASSES.each { |klass|
88
+ if klass.const_get(:KEY).to_sym == test
89
+ klass.new(*args).run(runner)
90
+ break
91
+ end
92
+ }
93
+ end
94
+ end
95
+ end
data/lib/tests/http.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'test'
2
+
3
+ module Testotron::Tests
4
+ class HTTP < Testotron::Test
5
+ KEY = "http"
6
+
7
+ def initialize(host, port = 80, requests = nil)
8
+ if requests.nil?
9
+ requests = "http://#{host}/"
10
+ end
11
+ @host, @port, @requests = host, port, [*requests]
12
+ end
13
+
14
+ def human_name
15
+ "HTTP test of #{@host}, port #{@port}, requests #{@requests.join ','}"
16
+ end
17
+
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
24
+
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
32
+
33
+ if response.code.to_i != 200
34
+ raise TestFailed, "Response not 200 on #{@post}:#{@port} GET #{page}"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
data/lib/tests/smtp.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'test'
2
+
3
+ module Testotron::Tests
4
+ class SMTP < Testotron::Test
5
+ KEY = "smtp"
6
+
7
+ def initialize(host, port = 25)
8
+ @host, @port = host, port
9
+ end
10
+
11
+ def human_name
12
+ "SMTP test of #{@host}, port #{@port}"
13
+ end
14
+
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
27
+ end
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: testotron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michal Pokorny
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mail
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ! "\t\tTestotron can automatically test basic functions of common servers,
31
+ so you\n\t\tcan get an alert when some piece of your configuration stops working.\n"
32
+ email: pok@rny.cz
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/testotron.rb
38
+ - lib/test.rb
39
+ - lib/tests/smtp.rb
40
+ - lib/tests/http.rb
41
+ homepage: http://github.com/MichalPokorny/testotron
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.23
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Simple server testing
65
+ test_files: []