Skip to content
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

(PUP-12077) Respect rich_data setting in base context #9471

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/puppet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ 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],
# `stringify_rich` controls whether `rich_data` is stringified into a lossy format
# instead of a lossless format. Catalogs should not be stringified, though to_yaml
# and the resource application have uses for a lossy, user friendly format.
:stringify_rich => false
}
end

Expand Down
6 changes: 5 additions & 1 deletion lib/puppet/application/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ def main
# If the specified environment does not exist locally, fall back to the default (production) environment
env = Puppet.lookup(:environments).get(Puppet[:environment]) || create_default_environment

Puppet.override(:current_environment => env, :loaders => Puppet::Pops::Loaders.new(env)) do
Puppet.override(
current_environment: env,
loaders: Puppet::Pops::Loaders.new(env),
stringify_rich: true
) do
type, name, params = parse_args(command_line.args)

raise _("Editing with Yaml output is not supported") if options[:edit] and options[:to_yaml]
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def to_data_hash
# To get stringified parameter values the flag :stringify_rich can be set
# in the puppet context.
#
stringify = Puppet.lookup(:stringify_rich) { false }
stringify = Puppet.lookup(:stringify_rich)
converter = stringify ? Puppet::Pops::Serialization::ToStringifiedConverter.new : nil

params = {}
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
7 changes: 0 additions & 7 deletions spec/unit/application/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,6 @@ def string
expect { @resource_app.main }.not_to raise_error
end

it "should raise an error when printing yaml by default" 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'/)
end

it "should ensure all values to be printed are in the external encoding" do
resources = [
Puppet::Type.type(:user).new(:name => "\u2603".force_encoding(Encoding::UTF_8)).to_resource,
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
21 changes: 21 additions & 0 deletions spec/unit/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,27 @@ 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 'serializes rich data' do
resource = compile_to_catalog('notify {"foo": message => Deferred("func", ["a", "b", "c"])}')

# This assume rich_data is true by default
expect(resource.to_data_hash.class).to be(Hash)
end

it 'raises when rich data is disabled' do
resource = compile_to_catalog('notify {"foo": message => Deferred("func", ["a", "b", "c"])}')
expect {
Puppet.override(rich_data: false) do
resource.to_data_hash
end
}.to raise_error(Puppet::PreformattedError)
joshcooper marked this conversation as resolved.
Show resolved Hide resolved
end
end

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