ruby-qa/lib/test.rb

91 lines
2.0 KiB
Ruby
Raw Normal View History

2024-06-10 02:16:20 +00:00
module RubyQA
require 'erb'
class Test
DEFAULT_OPTIONS={
:site => "",
:cluster => false,
:has_key => "",
2024-06-29 01:14:07 +00:00
:template => nil,
2024-06-10 02:16:20 +00:00
}.freeze
attr_reader :name, :options, :proc
def initialize (name, options ={}, &test_block)
@tests = Hash.new
@name = name
@options = DEFAULT_OPTIONS.merge(options)
@test = test_block
@description = ""
2024-06-29 01:14:07 +00:00
if @options[:description]
2024-06-10 02:16:20 +00:00
@description=options[:description]
2024-06-29 01:14:07 +00:00
@options.delete(:description)
end
if @options.has_key? :template
@template = @options[:template]
@options.delete(:template)
2024-06-10 02:16:20 +00:00
end
end
def valid_host(host)
## By default if options weren't changed, then
## we just assume it is to be tested against all
## provided hosts
is_valid = 0
if @options == DEFAULT_OPTIONS
return true
end
DEFAULT_OPTIONS.each do |key,value|
if @options[key] != value and host[key] != @options[key]
return false
end
end
return true
end
def run (host)
test_status = "N/A"
2024-06-29 01:14:07 +00:00
context = Hash.new
if @test.call(host, context)
2024-06-10 02:16:20 +00:00
test_status = "PASSED"
else
test_status = "FAILED"
end
2024-06-29 01:14:07 +00:00
@tests[host.name]={
:host => host,
:status => test_status,
:context => context
}
2024-06-10 02:16:20 +00:00
end
def report
if @tests.count == 0
return
end
2024-06-29 01:14:07 +00:00
template = nil
if @template.nil?
2024-06-10 02:16:20 +00:00
template = ERB.new <<EOF, :trim_mode => '-'
==============================================
Test : "<%= @name %>"
2024-06-29 01:14:07 +00:00
<% if not @description.empty? -%>
Description : "<%= @description %>
2024-06-10 02:16:20 +00:00
<% end -%>
==============================================
<% @tests.each do |hostname,data| -%>
<%= hostname %> : <%= data[:status] %> <% if data[:context].has_key? :note %> (<%= data[:context][:note] %>) <% end %>
2024-06-10 02:16:20 +00:00
<% end -%>
EOF
2024-06-29 01:14:07 +00:00
else
template = @template
end
2024-06-10 02:16:20 +00:00
template.result(binding)
end
end
end