diff --git a/Gemfile b/Gemfile index 922fb4aa..f8e1f7ff 100644 --- a/Gemfile +++ b/Gemfile @@ -16,7 +16,7 @@ group :tests do # unless we're dealing with jruby... if RUBY_PLATFORM == 'java' # load any rubocop version that works on java for the Rakefile - gem 'rubocop' + gem 'rubocop', '0.41.2' elsif Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.2.0') gem 'rubocop', '0.57.2' # the last version of parallel to support ruby 2.1 diff --git a/lib/puppet/resource_api/type_definition.rb b/lib/puppet/resource_api/type_definition.rb index 40bd3713..ea573a42 100644 --- a/lib/puppet/resource_api/type_definition.rb +++ b/lib/puppet/resource_api/type_definition.rb @@ -63,6 +63,10 @@ def validate(resource) error_msg = "The following mandatory attributes were not provided:\n * " + missing_attrs.join(", \n * ") raise Puppet::ResourceError, error_msg if missing_attrs.any? end + + def notify_schema_errors(message) + raise Puppet::DevError, message + end end # Base RSAPI schema Object @@ -144,6 +148,10 @@ def check_schema(resource, message_prefix = nil) return if rejected_keys.empty? && bad_values.empty? + notify_schema_errors(message) + end + + def notify_schema_errors(message) if Puppet.settings[:strict] == :off Puppet.debug(message) elsif Puppet.settings[:strict] == :warning diff --git a/spec/acceptance/transport/transport_spec.rb b/spec/acceptance/transport/transport_spec.rb index 3fe7c0b5..581d3ad5 100644 --- a/spec/acceptance/transport/transport_spec.rb +++ b/spec/acceptance/transport/transport_spec.rb @@ -57,8 +57,10 @@ def is_device_apply_supported? f.write 'notify { "foo": }' f.close - stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + stdout_str, status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + expect(status.exitstatus).to eq 0 expect(stdout_str).not_to match %r{Value type mismatch} + expect(stdout_str).not_to match %r{Error} expect(stdout_str).to match %r{transport connection_info:} expect(stdout_str).to match %r{:username=>"foo"} @@ -88,12 +90,15 @@ def is_device_apply_supported? f.write 'notify { "foo": }' f.close - stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + stdout_str, status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + expect(stdout_str).to match %r{Error} expect(stdout_str).to match %r{Value type mismatch} expect(stdout_str).to match %r{secret_string: << redacted value >> } expect(stdout_str).not_to match %r{optional_secret} expect(stdout_str).not_to match %r{array_secret} expect(stdout_str).not_to match %r{variant_secret} + + expect(status.exitstatus).to eq 1 end end end @@ -116,12 +121,15 @@ def is_device_apply_supported? f.write 'notify { "foo": }' f.close - stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + stdout_str, status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + expect(stdout_str).to match %r{Error} expect(stdout_str).to match %r{Value type mismatch} expect(stdout_str).not_to match %r{secret_string } expect(stdout_str).to match %r{optional_secret: << redacted value >>} expect(stdout_str).not_to match %r{array_secret} expect(stdout_str).not_to match %r{variant_secret} + + expect(status.exitstatus).to eq 1 end end end @@ -144,12 +152,15 @@ def is_device_apply_supported? f.write 'notify { "foo": }' f.close - stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + stdout_str, status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + expect(stdout_str).to match %r{Error} expect(stdout_str).to match %r{Value type mismatch} expect(stdout_str).not_to match %r{secret_string } expect(stdout_str).not_to match %r{optional_secret} expect(stdout_str).to match %r{array_secret: << redacted value >>} expect(stdout_str).not_to match %r{variant_secret} + + expect(status.exitstatus).to eq 1 end end end @@ -172,12 +183,15 @@ def is_device_apply_supported? f.write 'notify { "foo": }' f.close - stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + stdout_str, status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") + expect(stdout_str).to match %r{Error} expect(stdout_str).to match %r{Value type mismatch} expect(stdout_str).not_to match %r{secret_string } expect(stdout_str).not_to match %r{optional_secret} expect(stdout_str).not_to match %r{array_secret} expect(stdout_str).to match %r{variant_secret: << redacted value >>} + + expect(status.exitstatus).to eq 1 end end end diff --git a/spec/puppet/resource_api/transport_schema_def_spec.rb b/spec/puppet/resource_api/transport_schema_def_spec.rb index 92cac1fa..f4074560 100644 --- a/spec/puppet/resource_api/transport_schema_def_spec.rb +++ b/spec/puppet/resource_api/transport_schema_def_spec.rb @@ -42,4 +42,14 @@ it { expect { type.validate(resource) }.not_to raise_error } end end + + describe '#notify_schema_errors' do + context 'when a message is received ' do + let(:message) { 'Missing attribute: foo' } + + it 'raises an error with the supplied message' do + expect { type.notify_schema_errors(message) }.to raise_error Puppet::DevError, message + end + end + end end