Skip to content

Commit 34d001c

Browse files
seanmilLukasAud
authored andcommitted
Add SimpleProvider tests for custom namevars
As of the prior commit the simple provider correctly matches not only for types with composite namevars, but also types with arbitrarily named single namevars. Also, update the comments at the top of the SimpleProvider class to remove the note about namevar having to be "name".
1 parent efee0a1 commit 34d001c

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

lib/puppet/resource_api/simple_provider.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Puppet::ResourceApi
66
# This class provides a default implementation for set(), when your resource does not benefit from batching.
77
# Instead of processing changes yourself, the `create`, `update`, and `delete` functions, are called for you,
88
# with proper logging already set up.
9-
# Note that your type needs to use `name` as its namevar, and `ensure` in the conventional way to signal presence
10-
# and absence of resources.
9+
# Note that your type needs to use `ensure` in the conventional way with values of `prsesent`
10+
# and `absent` to signal presence and absence of resources.
1111
class SimpleProvider
1212
def set(context, changes)
1313
namevars = context.type.namevars

spec/puppet/resource_api/simple_provider_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ def delete(context, _name); end
143143
expect(type_def).to receive(:check_schema).never
144144
provider.set(context, changes)
145145
end
146+
147+
context 'when the namevar is not name' do
148+
let(:is_values) { { key: 'title', ensure: 'present' } }
149+
let(:should_values) { { key: 'title', ensure: 'present' } }
150+
151+
before(:each) do
152+
allow(type_def).to receive(:namevars).and_return([:key])
153+
end
154+
155+
it 'calls update once' do
156+
expect(provider).to receive(:update).with(context, 'title', should_values).once
157+
provider.set(context, changes)
158+
end
159+
end
146160
end
147161

148162
context 'with a single change to update a resource without :is supplied' do
@@ -196,6 +210,20 @@ def delete(context, _name); end
196210
provider.set(context, changes)
197211
end
198212
end
213+
214+
context 'when the namevar is not name' do
215+
let(:is_values) { [{ key: 'title', ensure: 'present' }] }
216+
let(:should_values) { { key: 'title', ensure: 'present' } }
217+
218+
before(:each) do
219+
allow(type_def).to receive(:namevars).and_return([:key])
220+
end
221+
222+
it 'calls update once' do
223+
expect(provider).to receive(:update).with(context, 'title', should_values).once
224+
provider.set(context, changes)
225+
end
226+
end
199227
end
200228

201229
context 'with a single change to update a resource with an optional ensure' do
@@ -263,6 +291,20 @@ def delete(context, _name); end
263291
expect(provider).to receive(:get).never
264292
provider.set(context, changes)
265293
end
294+
295+
context 'when the namevar is not name' do
296+
let(:is_values) { { key: 'title', ensure: 'present' } }
297+
let(:should_values) { { key: 'title', ensure: 'absent' } }
298+
299+
before(:each) do
300+
allow(type_def).to receive(:namevars).and_return([:key])
301+
end
302+
303+
it 'calls delete once' do
304+
expect(provider).to receive(:delete).with(context, 'title').once
305+
provider.set(context, changes)
306+
end
307+
end
266308
end
267309

268310
context 'with a single change to delete a resource without :is supplied' do
@@ -314,6 +356,20 @@ def delete(context, _name); end
314356
provider.set(context, changes)
315357
end
316358
end
359+
360+
context 'when the namevar is not name' do
361+
let(:is_values) { [{ key: 'title', ensure: 'present' }] }
362+
let(:should_values) { { key: 'title', ensure: 'absent' } }
363+
364+
before(:each) do
365+
allow(type_def).to receive(:namevars).and_return([:key])
366+
end
367+
368+
it 'calls delete once' do
369+
expect(provider).to receive(:delete).with(context, 'title').once
370+
provider.set(context, changes)
371+
end
372+
end
317373
end
318374

319375
context 'with multiple changes' do

0 commit comments

Comments
 (0)