Skip to content

Commit 4c125d3

Browse files
authored
Merge pull request #9033 from tvpartytonight/PUP-11751-strictly-strict
(PUP-11751) Handle interpolation errors from hiera_config
2 parents 180c62d + ae1c68f commit 4c125d3

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

acceptance/tests/lookup/config5_interpolation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# lookup in a masterless setup.
1111
'server'
1212

13-
pending_test('unexpected `::roles` returning undefined here, but the test passes when strict=warning')
13+
# pending_test('unexpected `::roles` returning undefined here, but the test passes when strict=warning')
1414
# Should revisit this in PUP-11751
1515

1616
app_type = File.basename(__FILE__, '.*')

lib/puppet/parser/scope.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,12 @@ def variable_not_found(name, reason=nil)
535535
Puppet.warn_once(UNDEFINED_VARIABLES_KIND, _("Variable: %{name}") % { name: name },
536536
_("Undefined variable '%{name}'; %{reason}") % { name: name, reason: reason } )
537537
when :error
538-
raise ArgumentError, _("Undefined variable '%{name}'; %{reason}") % { name: name, reason: reason }
538+
if Puppet.lookup(:avoid_hiera_interpolation_errors){false}
539+
Puppet.warn_once(UNDEFINED_VARIABLES_KIND, _("Variable: %{name}") % { name: name },
540+
_("Interpolation failed with '%{name}', but compilation continuing; %{reason}") % { name: name, reason: reason } )
541+
else
542+
raise ArgumentError, _("Undefined variable '%{name}'; %{reason}") % { name: name, reason: reason }
543+
end
539544
end
540545
end
541546
nil

lib/puppet/pops/lookup/hiera_config.rb

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def scope_interpolations_stable?(lookup_invocation)
252252
scope = lookup_invocation.scope
253253
lookup_invocation.without_explain do
254254
@scope_interpolations.all? do |key, root_key, segments, old_value|
255-
value = scope[root_key]
255+
value = Puppet.override(avoid_hiera_interpolation_errors: true) { scope[root_key] }
256256
unless value.nil? || segments.empty?
257257
found = nil;
258258
catch(:no_such_key) { found = sub_lookup(key, lookup_invocation, segments, value) }
@@ -633,23 +633,26 @@ def create_configured_data_providers(lookup_invocation, parent_data_provider, us
633633
entry_datadir = @config_root + (he[KEY_DATADIR] || datadir)
634634
entry_datadir = Pathname(interpolate(entry_datadir.to_s, lookup_invocation, false))
635635
location_key = LOCATION_KEYS.find { |key| he.include?(key) }
636-
locations = case location_key
637-
when KEY_PATHS
638-
resolve_paths(entry_datadir, he[location_key], lookup_invocation, @config_path.nil?)
639-
when KEY_PATH
640-
resolve_paths(entry_datadir, [he[location_key]], lookup_invocation, @config_path.nil?)
641-
when KEY_GLOBS
642-
expand_globs(entry_datadir, he[location_key], lookup_invocation)
643-
when KEY_GLOB
644-
expand_globs(entry_datadir, [he[location_key]], lookup_invocation)
645-
when KEY_URIS
646-
expand_uris(he[location_key], lookup_invocation)
647-
when KEY_URI
648-
expand_uris([he[location_key]], lookup_invocation)
649-
when KEY_MAPPED_PATHS
650-
expand_mapped_paths(entry_datadir, he[location_key], lookup_invocation)
651-
else
652-
nil
636+
locations = []
637+
Puppet.override(avoid_hiera_interpolation_errors: true) do
638+
locations = case location_key
639+
when KEY_PATHS
640+
resolve_paths(entry_datadir, he[location_key], lookup_invocation, @config_path.nil?)
641+
when KEY_PATH
642+
resolve_paths(entry_datadir, [he[location_key]], lookup_invocation, @config_path.nil?)
643+
when KEY_GLOBS
644+
expand_globs(entry_datadir, he[location_key], lookup_invocation)
645+
when KEY_GLOB
646+
expand_globs(entry_datadir, [he[location_key]], lookup_invocation)
647+
when KEY_URIS
648+
expand_uris(he[location_key], lookup_invocation)
649+
when KEY_URI
650+
expand_uris([he[location_key]], lookup_invocation)
651+
when KEY_MAPPED_PATHS
652+
expand_mapped_paths(entry_datadir, he[location_key], lookup_invocation)
653+
else
654+
nil
655+
end
653656
end
654657
next if @config_path.nil? && !locations.nil? && locations.empty? # Default config and no existing paths found
655658
options = he[KEY_OPTIONS] || defaults[KEY_OPTIONS]

spec/unit/functions/lookup_spec.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ def explain(key, options = {})
286286
YAML
287287

288288
it 'fails and reports error' do
289-
Puppet[:strict] = :error
290289
expect { lookup('a') }.to raise_error(
291290
"'default_hierarchy' is only allowed in the module layer (file: #{code_dir}/hiera.yaml, line: 5)")
292291
end
@@ -302,8 +301,7 @@ def explain(key, options = {})
302301
YAML
303302

304303
it 'fails and reports errors when strict == error' do
305-
Puppet[:strict] = :error
306-
expect { lookup('a') }.to raise_error("Undefined variable '::nonesuch' (file: #{code_dir}/hiera.yaml, line: 4)")
304+
expect { lookup('a') }.to raise_error("Function lookup() did not find a value for the name 'a'")
307305
end
308306
end
309307

@@ -316,7 +314,6 @@ def explain(key, options = {})
316314
YAML
317315

318316
it 'fails and reports errors when strict == error' do
319-
Puppet[:strict] = :error
320317
expect { lookup('a') }.to raise_error("Interpolation using method syntax is not allowed in this context (file: #{code_dir}/hiera.yaml)")
321318
end
322319
end
@@ -604,9 +601,9 @@ def explain(key, options = {})
604601
context 'using global variable reference' do
605602
let(:data_path) { 'x%{::var.sub}.yaml' }
606603

607-
it 'raises an error when reloads the configuration if interpolating undefined values' do
604+
it 'does not raise an error when reloads the configuration if interpolating undefined values' do
608605
collect_notices("notice('success')") do |scope|
609-
expect { lookup_func.call(scope, 'y') }.to raise_error(/Undefined variable '::var'/)
606+
expect { lookup_func.call(scope, 'y') }.to_not raise_error
610607
end
611608
end
612609

0 commit comments

Comments
 (0)