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-885) Add support for init_only attributes #52

Merged
merged 3 commits into from
Mar 29, 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
18 changes: 16 additions & 2 deletions lib/puppet/resource_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def feature_support?(feature)
# puts "#{name}: #{options.inspect}"

if options[:behaviour]
unless [:read_only, :namevar, :parameter].include? options[:behaviour]
unless [:read_only, :namevar, :parameter, :init_only].include? options[:behaviour]
raise Puppet::ResourceError, "`#{options[:behaviour]}` is not a valid behaviour value"
end
end
Expand Down Expand Up @@ -270,6 +270,20 @@ def feature_support?(feature)

Puppet.debug("Target State: #{target_state.inspect}")

# enforce init_only attributes
if Puppet.settings[:strict] != :off && @rapi_current_state && (@rapi_current_state[:ensure] == :present && target_state[:ensure] == :present)
target_state.each do |name, value|
next unless definition[:attributes][name][:behaviour] == :init_only && value != @rapi_current_state[name]
message = "Attempting to change `#{name}` init_only attribute value from `#{@rapi_current_state[name]}` to `#{value}`"
case Puppet.settings[:strict]
when :warning
Puppet.warning(message)
when :error
raise Puppet::ResourceError, message
end
end
end

if feature_support?('supports_noop')
my_provider.set(context, { title => { is: @rapi_current_state, should: target_state } }, noop: noop?)
else
Expand Down Expand Up @@ -302,7 +316,7 @@ def feature_support?(feature)
when :warning
Puppet.warning(message)
when :error
raise Puppet::Error, message
raise Puppet::DevError, message
end

return nil
Expand Down
139 changes: 134 additions & 5 deletions spec/puppet/resource_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,10 @@ def extract_values(function)
it { expect { described_class.register_type(definition) }.not_to raise_error }

describe 'the registered type' do
subject(:type) { Puppet::Type.type(:with_parameters) }
subject(:type) { Puppet::Type.type(:no_type) }

it { is_expected.not_to be_nil }
it { expect(type.parameters[1]).to eq :test_string }
it { expect(type.parameters[0]).to eq :name }
end
end

Expand All @@ -382,10 +382,123 @@ def extract_values(function)
it { expect { described_class.register_type(definition) }.not_to raise_error }

describe 'the registered type' do
subject(:type) { Puppet::Type.type(:with_parameters) }
subject(:type) { Puppet::Type.type(:behaviour) }

it { is_expected.not_to be_nil }
it { expect(type.parameters[1]).to eq :test_string }
it { expect(type.parameters[0]).to eq :name }
end
end

context 'when registering a type with an `init_only` attribute', agent_test: true do
let(:definition) do
{
name: 'init_behaviour',
attributes: {
ensure: {
type: 'Enum[present, absent]',
},
name: {
type: 'String',
behavior: :namevar,
},
immutable: {
type: 'String',
behaviour: :init_only,
},
mutable: {
type: 'String',
},
},
}
end

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

describe 'the registered type' do
subject(:type) { Puppet::Type.type(:init_behaviour) }

it { is_expected.not_to be_nil }
it { expect(type.parameters[0]).to eq :name }
end

describe 'an instance of the type' do
let(:provider_class) do
Class.new do
def get(_context)
[{ name: 'init', ensure: :present, immutable: 'physics', mutable: 'bank balance' }]
end

def set(_context, _changes); end
end
end

before(:each) do
stub_const('Puppet::Provider::InitBehaviour', Module.new)
stub_const('Puppet::Provider::InitBehaviour::InitBehaviour', provider_class)
end

context 'when a manifest wants to set the value of an init_only attribute' do
let(:instance) { Puppet::Type.type(:init_behaviour).new(name: 'new_init', ensure: :present, immutable: 'new', mutable: 'flexible') }

context 'when Puppet strict setting is :error' do
let(:strict_level) { :error }

it { expect { instance.flush }.not_to raise_error }
it {
expect(Puppet).not_to receive(:warning)
instance.flush
}
end

context 'when Puppet strict setting is :warning' do
let(:strict_level) { :warning }

it { expect { instance.flush }.not_to raise_error }
it {
expect(Puppet).not_to receive(:warning)
instance.flush
}
end

context 'when Puppet strict setting is :off' do
let(:strict_level) { :off }

it { expect { instance.flush }.not_to raise_error }
it {
expect(Puppet).not_to receive(:warning)
instance.flush
}
end
end

context 'when a manifest wants to change the value of an init_only attribute' do
let(:instance) { Puppet::Type.type(:init_behaviour).new(name: 'init', ensure: :present, immutable: 'lies', mutable: 'overdraft') }

context 'when Puppet strict setting is :error' do
let(:strict_level) { :error }

it { expect { instance.flush }.to raise_error Puppet::ResourceError, %r{Attempting to change `immutable` init_only attribute value from} }
end

context 'when Puppet strict setting is :warning' do
let(:strict_level) { :warning }

it {
expect(Puppet).to receive(:warning).with(%r{Attempting to change `immutable` init_only attribute value from})
instance.flush
}
end

context 'when Puppet strict setting is :off' do
let(:strict_level) { :off }

it { expect { instance.flush }.not_to raise_error }
it {
expect(Puppet).not_to receive(:warning)
instance.flush
}
end
end
end
end

Expand Down Expand Up @@ -592,7 +705,7 @@ def set(_context, changes)
it 'will throw an exception' do
expect {
instance.strict_check({})
}.to raise_error(Puppet::Error, %r{has not provided canonicalized values})
}.to raise_error(Puppet::DevError, %r{has not provided canonicalized values})
end
end

Expand Down Expand Up @@ -1096,6 +1209,22 @@ def set(_context, changes) end
it { expect { described_class.register_type(definition) }.not_to raise_error }
end

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

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

context 'with :namevar behaviour' do
let(:definition) do
{
Expand Down