ruby-qa/lib/rubyqa/test.rb
2024-12-11 10:25:13 -06:00

78 lines
1.7 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".green
else
test_status = "FAILED".red
end
@tests[host.name]={
:host => host,
:status => test_status,
:context => context
}
end
def report
if @tests.count == 0
return
end
if @template.nil?
self.render
else
@template.result(binding)
end
end
end
RubyQA::Template::BASE_TEST_TEMPLATE.def_method(RubyQA::Test, 'render')
end