ruby-qa/lib/test.rb
2024-06-29 13:07:30 -05:00

91 lines
2.0 KiB
Ruby

module RubyQA
require 'erb'
class Test
DEFAULT_OPTIONS={
:site => "",
:cluster => false,
:has_key => "",
:template => nil,
}.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 = ""
if @options[:description]
@description=options[:description]
@options.delete(:description)
end
if @options.has_key? :template
@template = @options[:template]
@options.delete(:template)
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"
context = Hash.new
if @test.call(host, context)
test_status = "PASSED"
else
test_status = "FAILED"
end
@tests[host.name]={
:host => host,
:status => test_status,
:context => context
}
end
def report
if @tests.count == 0
return
end
template = nil
if @template.nil?
template = ERB.new <<EOF, :trim_mode => '-'
==============================================
Test : "<%= @name %>"
<% if not @description.empty? -%>
Description : "<%= @description %>
<% end -%>
==============================================
<% @tests.each do |hostname,data| -%>
<%= hostname %> : <%= data[:status] %> <% if data[:context].has_key? :note %> (<%= data[:context][:note] %>) <% end %>
<% end -%>
EOF
else
template = @template
end
template.result(binding)
end
end
end