Added options template feature

This commit is contained in:
Tristan Ancelet 2024-06-28 20:14:07 -05:00
parent 994f8a906b
commit e3910e3c09
2 changed files with 26 additions and 43 deletions

View File

@ -1,36 +0,0 @@
#!/usr/bin/env ruby
require './rubyqa'
include RubyQA
$Manager = Manager.new
host = Host.new(
:ip => '127.0.0.1',
:user => 'tristan',
:cluster => true
)
Manager.new_test("hostname in /etc/hostname") {|host|
hostname = host.exec('cat /etc/hostname').strip
facts = host.resources['facts'].data
facts['networking']['hostname'] == hostname
}
Manager.new_test("hostname in /etc/hosts", :cluster => true ) {|host|
facts = host.resources['facts'].data
hostname = facts['networking']['hostname']
hosts = host.exec('cat /etc/hosts')
match_regex = /127\.0\.0\.1.+#{hostname.downcase}/
hosts.match? match_regex
}
Manager.new_test("Has user tristan") do |host|
passwd_file = host.exec("cat /etc/passwd")
passwd_file.match? /tristan.*/
end
$Manager.add_host(host)
$Manager.run_tests
$Manager.report

View File

@ -5,6 +5,7 @@ module RubyQA
:site => "",
:cluster => false,
:has_key => "",
:template => nil,
}.freeze
attr_reader :name, :options, :proc
@ -15,8 +16,13 @@ module RubyQA
@options = DEFAULT_OPTIONS.merge(options)
@test = test_block
@description = ""
if options[:description]
if @options[:description]
@description=options[:description]
@options.delete(:description)
end
if @options.has_key? :template
@template = @options[:template]
@options.delete(:template)
end
end
@ -40,30 +46,43 @@ module RubyQA
def run (host)
test_status = "N/A"
if @test.call(host)
context = Hash.new
if @test.call(host, context)
test_status = "PASSED"
else
test_status = "FAILED"
end
@tests[host.name]=test_status
@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 %>
<% if not @description.empty? -%>
Description : "<%= @description %>
<% end -%>
==============================================
<% @tests.each do |key,val| -%>
<%= key %> : <%= val %>
<% @tests.each do |key,data| -%>
<%= key %> : <%= data[:status] %> <% if data[:context].has_key? :note %> (<%= data[:context][:note] %>) <% end %>
<% end -%>
EOF
else
template = @template
end
template.result(binding)
end
end