Skip to content

Commit

Permalink
[puppetlabs#9470] Respect environment/settings in base context rich_d…
Browse files Browse the repository at this point in the history
…ata value
  • Loading branch information
justinstoller committed Sep 5, 2024
1 parent 7739378 commit ae55600
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/puppet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def self.base_context(settings)
:ssl_context => proc { Puppet.runtime[:http].default_ssl_context },
:http_session => proc { Puppet.runtime[:http].create_session },
:plugins => proc { Puppet::Plugins::Configuration.load_plugins },
:rich_data => false
:rich_data => Puppet[:rich_data]
}
end

Expand Down
4 changes: 3 additions & 1 deletion lib/puppet/application/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ def main

if options[:to_yaml]
data = resources.map do |resource|
resource.prune_parameters(:parameters_to_include => @extra_params).to_hiera_hash
Puppet.override(rich_data: false) do
resource.prune_parameters(:parameters_to_include => @extra_params).to_hiera_hash
end
end.inject(:merge!)
text = YAML.dump(type.downcase => data)
else
Expand Down
6 changes: 6 additions & 0 deletions spec/integration/application/apply_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ def bogus()
Puppet[:strict] = :warning
end

around :each do |test|
Puppet.override(rich_data: false) do
test.run
end
end

it 'will notify a string that is the result of Regexp#inspect (from Runtime3xConverter)' do
catalog = compile_to_catalog(execute, node)
apply.command_line.args = ['--catalog', file_containing('manifest', catalog.to_json)]
Expand Down
8 changes: 5 additions & 3 deletions spec/unit/application/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ def string
expect { @resource_app.main }.not_to raise_error
end

it "should raise an error when printing yaml by default" do
it "should raise an error when printing yaml if rich_data is off" do
@resource_app.options[:to_yaml] = true
allow(@resource_app.command_line).to receive(:args).and_return(['stringify', 'hello', 'ensure=present', 'string=asd'])
expect { @resource_app.main }.to raise_error( Puppet::PreformattedError,
/Stringify\[hello\]\['string'\] contains a Puppet::Util::Execution::ProcessOutput value. It will be converted to the String 'test'/)
Puppet.override(rich_data: false) do
expect { @resource_app.main }.to raise_error( Puppet::PreformattedError,
/Stringify\[hello\]\['string'\] contains a Puppet::Util::Execution::ProcessOutput value. It will be converted to the String 'test'/)
end
end

it "should ensure all values to be printed are in the external encoding" do
Expand Down
7 changes: 6 additions & 1 deletion spec/unit/resource/catalog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -965,10 +965,15 @@ class {'multi_param_class':

context 'and rich_data is disabled' do
before(:each) do
Puppet[:rich_data] = false
Puppet[:strict] = :warning # do not want to stub out behavior in tests
end

around(:each) do |test|
Puppet.override(rich_data: false) do
test.run
end
end

let(:catalog_w_regexp) { compile_to_catalog("notify {'foo': message => /[a-z]+/ }") }

it 'should not generate rich value hash for parameter values that are not Data' do
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,26 @@ def inject_and_set_defaults(resource, scope)
# Note: to_stringified_spec.rb has tests for all other data types
end

describe 'when serializing resources' do
require 'puppet_spec/compiler'
include PuppetSpec::Compiler

it 'should do something' do
resource = compile_to_catalog('notify {"foo": message => Deferred("func", ["a", "b", "c"])}')

# This should be true by default
if Puppet[:rich_data]
expect(resource.to_data_hash.class).to be(Hash)
end

expect {
Puppet.override(rich_data: false) do
resource.to_data_hash
end
}.to raise_error(Puppet::PreformattedError)
end
end

describe "when converting from json" do
before do
@data = {
Expand Down

0 comments on commit ae55600

Please sign in to comment.