Skip to content

Commit

Permalink
Update validate_data_hash to return modified hash
Browse files Browse the repository at this point in the history
Puppet::Pops::Lookup::ModuleDataProvider#validate_data_hash is a method
that is supposed to prune a data hash of any keys that are not prefixed
with a given module's name. However prior to this commit it incorrectly
took the data hash, cloned it, operated on the clone, then returned the
unchanged original hash.

This commit updates validate_data_hash to instead return the modified
hash, and updates the warning message generated when a key is pruned to
include the key.
  • Loading branch information
mhashizume committed Apr 19, 2024
1 parent fb44fd9 commit de99364
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
3 changes: 1 addition & 2 deletions lib/puppet/pops/lookup/module_data_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ def validate_data_hash(data_hash)
next memo if k == LOOKUP_OPTIONS || k.start_with?(module_prefix)

msg = "#{yield} must use keys qualified with the name of the module"
memo = memo.clone if memo.equal?(data_hash)
memo.delete(k)
Puppet.warning("Module '#{module_name}': #{msg}")
Puppet.warning("Module '#{module_name}': #{msg}; got #{k}")
memo
end
data_hash
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/functions/lookup_fixture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def compile_and_get_notifications(code)
expect { compiler.compile }.to raise_error(Puppet::ParseError, /did not find a value for the name 'bad_data::b'/)
end
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
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")
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")
end

it 'will succeed finding prefixed keys even when a key in the function provided module data is not prefixed' do
Expand All @@ -391,7 +391,7 @@ def compile_and_get_notifications(code)
expect(resources).to include('module_c')
end
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
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")
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")
end

it 'will resolve global, environment, and module correctly' do
Expand Down Expand Up @@ -422,8 +422,8 @@ def compile_and_get_notifications(code)
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
compiler.compile
end
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
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")
warnings = logs.filter_map { |log| log.message if log.level == :warning }
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")
end

it 'a warning will be logged when key in the hiera provided module data is not prefixed' do
Expand All @@ -432,8 +432,8 @@ def compile_and_get_notifications(code)
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
compiler.compile
end
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
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")
warnings = logs.filter_map { |log| log.message if log.level == :warning }
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")
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/unit/pops/lookup/lookup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Lookup
a: a (from global)
d: d (from global)
mod::e: mod::e (from global)
other::y: should be removed
YAML
}
}
Expand Down

0 comments on commit de99364

Please sign in to comment.