Skip to content

Commit

Permalink
Fixes #18205 - Add more useful macros
Browse files Browse the repository at this point in the history
  • Loading branch information
ares committed Jan 23, 2017
1 parent a005b7d commit 5f413e6
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 3 deletions.
52 changes: 50 additions & 2 deletions lib/foreman/renderer.rb
Expand Up @@ -2,15 +2,20 @@

module Foreman
module Renderer
class RenderingError < Foreman::Exception; end
class HostUnknown < RenderingError; end
class HostOSUnknown < RenderingError; end

include ::Foreman::ForemanUrlRenderer

ALLOWED_GENERIC_HELPERS ||= [ :foreman_url, :snippet, :snippets, :snippet_if_exists, :indent, :foreman_server_fqdn,
:foreman_server_url, :log_debug, :log_info, :log_warn, :log_error, :log_fatal, :template_name, :dns_lookup,
:pxe_kernel_options ]
:pxe_kernel_options, :save_to_file ]
ALLOWED_HOST_HELPERS ||= [ :grub_pass, :ks_console, :root_pass,
:media_path, :param_true?, :param_false?, :match,
:host_param_true?, :host_param_false?,
:host_param, :host_puppet_classes, :host_enc ]
:host_param, :host_puppet_classes, :host_enc, :host_os_rhel_compatible?, :host_os_family, :host_os_name,
:host_os_major, :host_os_minor, :host_puppet_enabled? ]

ALLOWED_HELPERS ||= ALLOWED_GENERIC_HELPERS + ALLOWED_HOST_HELPERS

Expand Down Expand Up @@ -114,6 +119,40 @@ def snippet_if_exists(name, options = {})
snippet name, options.merge(:silent => true)
end

def save_to_file(filename, content)
"cat << EOF > #{filename}\n#{content}EOF"
end

def host_os_rhel_compatible?
check_host_os
host_os_family == 'Redhat' && host_os_name != 'Fedora'
end

def host_os_family
check_host_os
@host.operatingsystem.try(:family)
end

def host_os_name
check_host_os
@host.operatingsystem.try(:name)
end

def host_os_major
check_host_os
@host.operatingsystem.try(:major).to_i
end

def host_os_minor
check_host_os
@host.operatingsystem.try(:minor).to_i
end

def host_puppet_enabled?
check_host
!@host.puppetmaster.empty?
end

def indent(count)
return unless block_given? && (text = yield.to_s)
prefix = " " * count
Expand Down Expand Up @@ -176,6 +215,15 @@ def load_template_vars

private

def check_host
raise HostUnknown, _('This templates requires a host to render but none was specified') if @host.nil?
end

def check_host_os
check_host
raise HostOSUnknown, _('Operating system not set for host %s') % @host.name if @host.operatingsystem.nil?
end

# takes variable names array and loads instance variables with the same name like this
# { :name => @name, :another => @another }
def allowed_variables_mapping(variable_names)
Expand Down
23 changes: 23 additions & 0 deletions test/factories/operatingsystem.rb
Expand Up @@ -105,5 +105,28 @@
type 'Solaris'
title 'Solaris 10.8'
end

factory :fedora, class: Redhat do
sequence(:name) { 'Fedora' }
major '24'
type 'Redhat'
title 'Fedora 24'
end

factory :centos, class: Redhat do
sequence(:name) { 'Centos' }
major '6'
minor '8'
type 'Redhat'
title 'Centos 6.8'
end

factory :rhel, class: Redhat do
sequence(:name) { 'RedHat' }
major '7'
minor '2'
type 'Redhat'
title 'RedHat 7.2'
end
end
end
119 changes: 118 additions & 1 deletion test/unit/foreman/renderer_test.rb
Expand Up @@ -173,13 +173,109 @@ def setup_safemode_renderer
assert_equal 'A B C D', tmpl
end

test "#{renderer_name} should render a snippet_if_exists with variables" do
test "#{renderer_name} should render a snippets with variables" do
send "setup_#{renderer_name}"
snippet = FactoryGirl.create(:provisioning_template, :snippet, :template => "A <%= @b + ' ' + @c -%> D")
tmpl = @renderer.snippets(snippet.name, :variables => { :b => 'B', :c => 'C' })
assert_equal 'A B C D', tmpl
end

test "#{renderer_name} should render a save_to_file macro" do
assert_renders('<%= save_to_file("/etc/puppet/puppet.conf", "[main]\nserver=example.com\n") %>', "cat << EOF > /etc/puppet/puppet.conf\n[main]\nserver=example.com\nEOF", nil)
end

test "#{renderer_name} should render host_os_rhel_compatible? macro to true for rhel" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(
:host,
:operatingsystem => FactoryGirl.build(:rhel)
)
assert_renders('<%= host_os_rhel_compatible? %>', 'true', host)
end

test "#{renderer_name} should render host_os_rhel_compatible? macro to true for centos" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(
:host,
:operatingsystem => FactoryGirl.build(:centos)
)
assert_renders('<%= host_os_rhel_compatible? %>', 'true', host)
end

test "#{renderer_name} should render host_os_rhel_compatible? macro to true for fedora" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(
:host,
:operatingsystem => FactoryGirl.build(:fedora)
)
assert_renders('<%= host_os_rhel_compatible? %>', 'false', host)
end

test "#{renderer_name} should render host_os_family to Redhat for fedora" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(
:host,
:operatingsystem => FactoryGirl.build(:fedora)
)
assert_renders('<%= host_os_family %>', 'Redhat', host)
end

test "#{renderer_name} should render host_os_name to Fedora for fedora" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(
:host,
:operatingsystem => FactoryGirl.build(:fedora)
)
assert_renders('<%= host_os_name %>', 'Fedora', host)
end

test "#{renderer_name} should render host_os_major to 24 for fedora" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(
:host,
:operatingsystem => FactoryGirl.build(:fedora, :major => 24)
)
assert_renders('<%= host_os_major %>', '24', host)
end

test "#{renderer_name} should render host_os_minor to 2 for rhel" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(
:host,
:operatingsystem => FactoryGirl.build(:rhel, :minor => 2)
)
assert_renders('<%= host_os_minor %>', '2', host)
end

test "#{renderer_name} should render puppet_enabled? to true for host with puppetmaster set" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(:host, :with_puppet)
assert_renders('<%= host_puppet_enabled? %>', 'true', host)
end

test "#{renderer_name} should render puppet_enabled? to false for host without puppetmaster set" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(:host)
assert_renders('<%= host_puppet_enabled? %>', 'false', host)
end

test "#{renderer_name} should check host presence for host macros" do
%w(host_os_rhel_compatible? host_os_family host_os_name host_os_major host_os_minor host_puppet_enabled?).each do |macro|
assert_renders_with_error("<%= #{macro} %>", nil, Foreman::Renderer::HostUnknown)
end
end

test "#{renderer_name} should check host OS presence for host OS macros" do
send "setup_#{renderer_name}"
host = FactoryGirl.build(
:host,
:operatingsystem => nil
)
%w(host_os_rhel_compatible? host_os_family host_os_name host_os_major host_os_minor).each do |macro|
assert_renders_with_error("<%= #{macro} %>", host, Foreman::Renderer::HostOSUnknown)
end
end

test "#{renderer_name} should render a templates_used" do
send "setup_#{renderer_name}"
@renderer.host = FactoryGirl.build(
Expand Down Expand Up @@ -294,4 +390,25 @@ def setup_safemode_renderer
test 'templates_used is allowed to render for host' do
assert Safemode.find_jail_class(Host::Managed).allowed? :templates_used
end

private

def assert_renders(template_content, output, host)
@renderer.host = host
template = mock('template')
template.stubs(:template).returns(template_content)
assert_nothing_raised do
content = @renderer.unattended_render(template)
assert_equal(output, content)
end
end

def assert_renders_with_error(template_content, host, error)
@renderer.host = host
template = mock('template')
template.stubs(:template).returns(template_content)
assert_raises(error) do
@renderer.unattended_render(template)
end
end
end

0 comments on commit 5f413e6

Please sign in to comment.