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

(PDK-875) Validate behaviour values when registering a type #49

Merged
merged 1 commit into from
Mar 26, 2018
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: 6 additions & 0 deletions lib/puppet/resource_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ def feature_support?(feature)
definition[:attributes].each do |name, options|
# puts "#{name}: #{options.inspect}"

if options[:behaviour]
unless [:read_only, :namevar, :parameter].include? options[:behaviour]
raise Puppet::ResourceError, "`#{options[:behaviour]}` is not a valid behaviour value"
end
end

# TODO: using newparam everywhere would suppress change reporting
# that would allow more fine-grained reporting through context,
# but require more invest in hooking up the infrastructure to emulate existing data
Expand Down
66 changes: 66 additions & 0 deletions spec/puppet/resource_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1046,4 +1046,70 @@ def set(_context, changes) end
expect { described_class.register_type(definition) }.not_to raise_error
end
end

context 'when loading a type, containing a behaviour' do
context 'with :namevar behaviour' do
let(:definition) do
{
name: 'test_behaviour',
attributes: {
id: {
type: 'String',
behavior: :namevar,
},
},
}
end

it { expect { described_class.register_type(definition) }.not_to raise_error }
end

context 'with :parameter behaviour' do
let(:definition) do
{
name: 'test_behaviour',
attributes: {
param: {
type: 'String',
behavior: :parameter,
},
},
}
end

it { expect { described_class.register_type(definition) }.not_to raise_error }
end

context 'with :read_only behaviour' do
let(:definition) do
{
name: 'test_behaviour',
attributes: {
param_ro: {
type: 'String',
behavior: :read_only,
},
},
}
end

it { expect { described_class.register_type(definition) }.not_to raise_error }
end

context 'with :namevar behaviour' do
let(:definition) do
{
name: 'test_behaviour',
attributes: {
source: {
type: 'String',
behavior: :bad,
},
},
}
end

it { expect { described_class.register_type(definition) }.to raise_error Puppet::ResourceError, %r{^`bad` is not a valid behaviour value$} }
end
end
end