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-884) Handle missing namevars returned by providers #47

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
3 changes: 3 additions & 0 deletions lib/puppet/resource_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ def feature_support?(feature)
property = definition[:attributes][key.first]
attr_def[key.first] = property
end
if resource_hash[namevar_name].nil?
raise Puppet::ResourceError, "`#{name}.get` did not return a value for the `#{namevar_name}` namevar attribute"
end
Puppet::ResourceApi::TypeShim.new(resource_hash[namevar_name], resource_hash, name, namevar_name, attr_def)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class Puppet::Provider::TitleProvider::TitleProvider < Puppet::ResourceApi::Simp
def get(_context)
[
{
name: 'foo',
namevar: 'foo',
ensure: :present,
},
{
name: 'bar',
namevar: 'bar',
ensure: :present,
},
]
Expand Down
25 changes: 25 additions & 0 deletions spec/puppet/resource_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,31 @@ def extract_values(function)
it('the namevar is set to the name') { expect(instance[:not_name]).to eq 'test' }
end
end

describe 'a provider that does not return the namevar', agent_test: true do
subject(:instance) { Puppet::Type.type(:not_name_namevar) }

let(:provider_class) do
Class.new do
def get(_context)
[{ name: 'some title' }]
end

def set(_context, changes) end
end
end

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

it('throws an error') {
expect {
instance.instances
}.to raise_error Puppet::ResourceError, %r{^`not_name_namevar.get` did not return a value for the `not_name` namevar attribute$}
}
end
end

describe '#load_provider', agent_test: true do
Expand Down