Skip to content

Commit 74811af

Browse files
authoredMay 3, 2024··
Merge pull request #9326 from mhashizume/PUP-11719/main/validate-hash-fix
Update validate_data_hash to return modified hash
2 parents 1d188e1 + 7a86ebf commit 74811af

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed
 

‎lib/puppet/pops/lookup/module_data_provider.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ def key_lookup_in_default(key, lookup_invocation, merge)
4747
def validate_data_hash(data_hash)
4848
super
4949
module_prefix = "#{module_name}::"
50-
data_hash.each_key.reduce(data_hash) do |memo, k|
51-
next memo if k == LOOKUP_OPTIONS || k.start_with?(module_prefix)
52-
53-
msg = "#{yield} must use keys qualified with the name of the module"
54-
memo = memo.clone if memo.equal?(data_hash)
55-
memo.delete(k)
56-
Puppet.warning("Module '#{module_name}': #{msg}")
57-
memo
50+
data_hash_to_return = {}
51+
data_hash.keys.each do |k|
52+
if k == LOOKUP_OPTIONS || k.start_with?(module_prefix)
53+
data_hash_to_return[k] = data_hash[k]
54+
else
55+
msg = "#{yield} must use keys qualified with the name of the module"
56+
Puppet.warning("Module '#{module_name}': #{msg}; got #{k}")
57+
end
5858
end
59-
data_hash
59+
data_hash_to_return
6060
end
6161

6262
protected

‎spec/unit/functions/lookup_fixture_spec.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def compile_and_get_notifications(code)
4747
Puppet[:code] = code
4848
node.environment.check_for_reparse
4949
catalog = block_given? ? compiler.compile { |cat| yield(compiler.topscope); cat } : compiler.compile
50-
catalog.resources.map(&:ref).select { |r| r.start_with?('Notify[') }.map { |r| r[7..-2] }
50+
catalog.resources.map(&:ref).filter_map { |r| r[7..-2] if r.start_with?('Notify[') }
5151
end
5252

5353
# There is a fully configured 'production' environment in fixtures at this location
@@ -376,8 +376,8 @@ def compile_and_get_notifications(code)
376376
Puppet[:code] = "include bad_data\nlookup('bad_data::b')"
377377
expect { compiler.compile }.to raise_error(Puppet::ParseError, /did not find a value for the name 'bad_data::b'/)
378378
end
379-
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
380-
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module")
379+
warnings = logs.filter_map { |log| log.message if log.level == :warning }
380+
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module; got b")
381381
end
382382

383383
it 'will succeed finding prefixed keys even when a key in the function provided module data is not prefixed' do
@@ -390,8 +390,8 @@ def compile_and_get_notifications(code)
390390
PUPPET
391391
expect(resources).to include('module_c')
392392
end
393-
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
394-
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module")
393+
warnings = logs.filter_map { |log| log.message if log.level == :warning }
394+
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module; got b")
395395
end
396396

397397
it 'will resolve global, environment, and module correctly' do
@@ -422,8 +422,8 @@ def compile_and_get_notifications(code)
422422
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
423423
compiler.compile
424424
end
425-
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
426-
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module")
425+
warnings = logs.filter_map { |log| log.message if log.level == :warning }
426+
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module; got b")
427427
end
428428

429429
it 'a warning will be logged when key in the hiera provided module data is not prefixed' do
@@ -432,8 +432,8 @@ def compile_and_get_notifications(code)
432432
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
433433
compiler.compile
434434
end
435-
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
436-
expect(warnings).to include("Module 'hieraprovider': Value returned from data_hash function 'json_data', when using location '#{environmentpath}/production/modules/hieraprovider/data/first.json', must use keys qualified with the name of the module")
435+
warnings = logs.filter_map { |log| log.message if log.level == :warning }
436+
expect(warnings).to include("Module 'hieraprovider': Value returned from data_hash function 'json_data', when using location '#{environmentpath}/production/modules/hieraprovider/data/first.json', must use keys qualified with the name of the module; got test::param_b")
437437
end
438438
end
439439

0 commit comments

Comments
 (0)
Please sign in to comment.