New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #16740 - Access host params through macro #3983
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ def default_url_options | |
:foreman_server_url, :log_debug, :log_info, :log_warn, :log_error, :log_fatal, :template_name, :dns_lookup, | ||
:pxe_kernel_options ] | ||
ALLOWED_HOST_HELPERS ||= [ :grub_pass, :ks_console, :root_pass, | ||
:media_path, :param_true?, :param_false?, :match ] | ||
:media_path, :param_true?, :param_false?, :match, | ||
:host_param_true?, :host_param_false?, | ||
:host_param, :host_puppet_classes, :host_enc ] | ||
|
||
ALLOWED_HELPERS ||= ALLOWED_GENERIC_HELPERS + ALLOWED_HOST_HELPERS | ||
|
||
|
@@ -26,6 +28,30 @@ def template_logger | |
@template_logger ||= Foreman::Logging.logger('templates') | ||
end | ||
|
||
def host_enc(*path) | ||
@enc ||= @host.info.deep_dup | ||
return @enc if path.compact.empty? | ||
enc = @enc | ||
path.each { |step| enc = enc.fetch step } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test coverage of this branch, it only covers |
||
enc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No duplication here, but it does above if there is no path? This should probably dup too. |
||
end | ||
|
||
def host_param(param_name) | ||
@host.params[param_name] | ||
end | ||
|
||
def host_puppet_classes | ||
@host.puppetclasses | ||
end | ||
|
||
def host_param_true?(name) | ||
@host.params.has_key?(name) && Foreman::Cast.to_bool(@host.params[name]) | ||
end | ||
|
||
def host_param_false?(name) | ||
@host.params.has_key?(name) && Foreman::Cast.to_bool(@host.params[name]) == false | ||
end | ||
|
||
def render_safe(template, allowed_methods = [], allowed_vars = {}) | ||
if Setting[:safemode_render] | ||
box = Safemode::Box.new self, allowed_methods | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'test_helper' | ||
|
||
class RendererTest < ActiveSupport::TestCase | ||
class DummyRenderer | ||
attr_accessor :host | ||
|
||
include Foreman::Renderer | ||
end | ||
|
||
test 'should show the deprecation for @host.info' do | ||
renderer = DummyRenderer.new | ||
renderer.host = FactoryGirl.create(:host, :with_puppet) | ||
template = FactoryGirl.create(:provisioning_template, :template => '<%= @host.info %>') | ||
Rails.logger.expects(:warn).with("DEPRECATION WARNING: you are using deprecated @host.info in a template, it will be removed in 1.17. Use host_enc instead.").once | ||
renderer.unattended_render template | ||
end | ||
|
||
test 'should show the deprecation for @host.params' do | ||
renderer = DummyRenderer.new | ||
renderer.host = FactoryGirl.create(:host, :with_puppet) | ||
template = FactoryGirl.create(:provisioning_template, :template => '<%= @host.params %>') | ||
Rails.logger.expects(:warn).with("DEPRECATION WARNING: you are using deprecated @host.params in a template, it will be removed in 1.17. Use host_param instead.").once | ||
renderer.unattended_render template | ||
end | ||
|
||
test 'should render host param using "host_param" helper without deprecation' do | ||
renderer = DummyRenderer.new | ||
renderer.host = FactoryGirl.create(:host, :with_puppet) | ||
Rails.logger.expects(:warn).never | ||
assert renderer.render_safe("<%= host_param('test') %>", DummyRenderer::ALLOWED_HELPERS).present? | ||
end | ||
|
||
test 'should render host info using "host_enc" helper without deprecation' do | ||
renderer = DummyRenderer.new | ||
renderer.host = FactoryGirl.create(:host, :with_puppet) | ||
Rails.logger.expects(:warn).never | ||
assert renderer.render_safe("<%= host_enc %>", DummyRenderer::ALLOWED_HELPERS).present? | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using
host_param
in a template, two deprecation warnings seem to be logged. Please check this works properly. A simple unit test that fails:The logic on this line seems incorrect to me, it skips the deprecation warning only from render_safe. Surely any other entry in the callstack would trigger this?
I'm unsure why template rendering triggers two deprecation warnings without further investigation, perhaps something else internally is calling
@host.params
and triggering a second. Please check it's working correctly in real usage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed the problem with
@host.params
being called internally, similar thing was happening tohost_enc
as well. I added tests to cover these cases.