1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
require 'support/shared/integration/integration_helper'
describe "Chef::Resource#identity and #state" do
include IntegrationSupport
class NewResourceNamer
@i = 0
def self.next
"chef_resource_property_spec_#{@i += 1}"
end
end
def self.new_resource_name
NewResourceNamer.next
end
let(:resource_class) do
new_resource_name = self.class.new_resource_name
Class.new(Chef::Resource) do
resource_name new_resource_name
end
end
let(:resource) do
resource_class.new("blah")
end
def self.english_join(values)
return '<nothing>' if values.size == 0
return values[0].inspect if values.size == 1
"#{values[0..-2].map { |v| v.inspect }.join(", ")} and #{values[-1].inspect}"
end
def self.with_property(*properties, &block)
tags_index = properties.find_index { |p| !p.is_a?(String)}
if tags_index
properties, tags = properties[0..tags_index-1], properties[tags_index..-1]
else
tags = []
end
properties = properties.map { |property| "property #{property}" }
context "With properties #{english_join(properties)}", *tags do
before do
properties.each do |property_str|
resource_class.class_eval(property_str, __FILE__, __LINE__)
end
end
instance_eval(&block)
end
end
# identity
context "Chef::Resource#identity_properties" do
with_property ":x" do
it "name is the default identity" do
expect(resource_class.identity_properties).to eq [ Chef::Resource.properties[:name] ]
expect(Chef::Resource.properties[:name].identity?).to be_falsey
expect(resource.name).to eq 'blah'
expect(resource.identity).to eq 'blah'
end
it "identity_properties :x changes the identity" do
expect(resource_class.identity_properties :x).to eq [ resource_class.properties[:x] ]
expect(resource_class.identity_properties).to eq [ resource_class.properties[:x] ]
expect(Chef::Resource.properties[:name].identity?).to be_falsey
expect(resource_class.properties[:x].identity?).to be_truthy
expect(resource.x 'woo').to eq 'woo'
expect(resource.x).to eq 'woo'
expect(resource.name).to eq 'blah'
expect(resource.identity).to eq 'woo'
end
with_property ":y, identity: true" do
context "and identity_properties :x" do
before do
resource_class.class_eval do
identity_properties :x
end
end
it "only returns :x as identity" do
resource.x 'foo'
resource.y 'bar'
expect(resource_class.identity_properties).to eq [ resource_class.properties[:x] ]
expect(resource.identity).to eq 'foo'
end
it "does not flip y.desired_state off" do
resource.x 'foo'
resource.y 'bar'
expect(resource_class.state_properties).to eq [
resource_class.properties[:x],
resource_class.properties[:y]
]
expect(resource.state_for_resource_reporter).to eq(x: 'foo', y: 'bar')
end
end
end
context "With a subclass" do
let(:subresource_class) do
new_resource_name = self.class.new_resource_name
Class.new(resource_class) do
resource_name new_resource_name
end
end
let(:subresource) do
subresource_class.new('sub')
end
it "name is the default identity on the subclass" do
expect(subresource_class.identity_properties).to eq [ Chef::Resource.properties[:name] ]
expect(Chef::Resource.properties[:name].identity?).to be_falsey
expect(subresource.name).to eq 'sub'
expect(subresource.identity).to eq 'sub'
end
context "With identity_properties :x on the superclass" do
before do
resource_class.class_eval do
identity_properties :x
end
end
it "The subclass inherits :x as identity" do
expect(subresource_class.identity_properties).to eq [ subresource_class.properties[:x] ]
expect(Chef::Resource.properties[:name].identity?).to be_falsey
expect(subresource_class.properties[:x].identity?).to be_truthy
subresource.x 'foo'
expect(subresource.identity).to eq 'foo'
end
context "With property :y, identity: true on the subclass" do
before do
subresource_class.class_eval do
property :y, identity: true
end
end
it "The subclass's identity includes both x and y" do
expect(subresource_class.identity_properties).to eq [
subresource_class.properties[:x],
subresource_class.properties[:y]
]
subresource.x 'foo'
subresource.y 'bar'
expect(subresource.identity).to eq(x: 'foo', y: 'bar')
end
end
with_property ":y, String" do
context "With identity_properties :y on the subclass" do
before do
subresource_class.class_eval do
identity_properties :y
end
end
it "y is part of state" do
subresource.x 'foo'
subresource.y 'bar'
expect(subresource.state_for_resource_reporter).to eq(x: 'foo', y: 'bar')
expect(subresource_class.state_properties).to eq [
subresource_class.properties[:x],
subresource_class.properties[:y]
]
end
it "y is the identity" do
expect(subresource_class.identity_properties).to eq [ subresource_class.properties[:y] ]
subresource.x 'foo'
subresource.y 'bar'
expect(subresource.identity).to eq 'bar'
end
it "y still has validation" do
expect { subresource.y 12 }.to raise_error Chef::Exceptions::ValidationFailed
end
end
end
end
end
end
with_property ":string_only, String, identity: true", ":string_only2, String" do
it "identity_properties does not change validation" do
resource_class.identity_properties :string_only
expect { resource.string_only 12 }.to raise_error Chef::Exceptions::ValidationFailed
expect { resource.string_only2 12 }.to raise_error Chef::Exceptions::ValidationFailed
end
end
with_property ":x, desired_state: false" do
it "identity_properties does not change desired_state" do
resource_class.identity_properties :x
resource.x 'hi'
expect(resource.identity).to eq 'hi'
expect(resource_class.properties[:x].desired_state?).to be_falsey
expect(resource_class.state_properties).to eq []
expect(resource.state_for_resource_reporter).to eq({})
end
end
context "With custom property custom_property defined only as methods, using different variables for storage" do
before do
resource_class.class_eval do
def custom_property
@blarghle ? @blarghle*3 : nil
end
def custom_property=(x)
@blarghle = x*2
end
end
end
context "And identity_properties :custom_property" do
before do
resource_class.class_eval do
identity_properties :custom_property
end
end
it "identity_properties comes back as :custom_property" do
expect(resource_class.properties[:custom_property].identity?).to be_truthy
expect(resource_class.identity_properties).to eq [ resource_class.properties[:custom_property] ]
end
it "custom_property becomes part of desired_state" do
resource.custom_property = 1
expect(resource.state_for_resource_reporter).to eq(custom_property: 6)
expect(resource_class.properties[:custom_property].desired_state?).to be_truthy
expect(resource_class.state_properties).to eq [
resource_class.properties[:custom_property]
]
end
it "identity_properties does not change custom_property's getter or setter" do
resource.custom_property = 1
expect(resource.custom_property).to eq 6
end
it "custom_property is returned as the identity" do
expect(resource.identity).to be_nil
resource.custom_property = 1
expect(resource.identity).to eq 6
end
end
end
end
context "Property#identity" do
with_property ":x, identity: true" do
it "name is only part of the identity if an identity attribute is defined" do
expect(resource_class.identity_properties).to eq [ resource_class.properties[:x] ]
resource.x 'woo'
expect(resource.identity).to eq 'woo'
end
end
with_property ":x, identity: true, default: 'xxx'",
":y, identity: true, default: 'yyy'",
":z, identity: true, default: 'zzz'" do
it "identity_property raises an error if multiple identity values are defined" do
expect { resource_class.identity_property }.to raise_error Chef::Exceptions::MultipleIdentityError
end
it "identity_attr raises an error if multiple identity values are defined" do
expect { resource_class.identity_attr }.to raise_error Chef::Exceptions::MultipleIdentityError
end
it "identity returns all identity values in a hash if multiple are defined" do
resource.x 'foo'
resource.y 'bar'
resource.z 'baz'
expect(resource.identity).to eq(x: 'foo', y: 'bar', z: 'baz')
end
it "identity returns all values whether any value is set or not" do
expect(resource.identity).to eq(x: 'xxx', y: 'yyy', z: 'zzz')
end
it "identity_properties wipes out any other identity attributes if multiple are defined" do
resource_class.identity_properties :y
resource.x 'foo'
resource.y 'bar'
resource.z 'baz'
expect(resource.identity).to eq 'bar'
end
end
with_property ":x, identity: true, name_property: true" do
it "identity when x is not defined returns the value of x" do
expect(resource.identity).to eq 'blah'
end
it "state when x is not defined returns the value of x" do
expect(resource.state_for_resource_reporter).to eq(x: 'blah')
end
end
end
# state_properties
context "Chef::Resource#state_properties" do
it "state_properties is empty by default" do
expect(Chef::Resource.state_properties).to eq []
expect(resource.state_for_resource_reporter).to eq({})
end
with_property ":x", ":y", ":z" do
it "x, y and z are state attributes" do
resource.x 1
resource.y 2
resource.z 3
expect(resource_class.state_properties).to eq [
resource_class.properties[:x],
resource_class.properties[:y],
resource_class.properties[:z]
]
expect(resource.state_for_resource_reporter).to eq(x: 1, y: 2, z: 3)
end
it "values that are not set are not included in state" do
resource.x 1
expect(resource.state_for_resource_reporter).to eq(x: 1)
end
it "when no values are set, nothing is included in state" do
end
end
with_property ":x", ":y, desired_state: false", ":z, desired_state: true" do
it "x and z are state attributes, and y is not" do
resource.x 1
resource.y 2
resource.z 3
expect(resource_class.state_properties).to eq [
resource_class.properties[:x],
resource_class.properties[:z]
]
expect(resource.state_for_resource_reporter).to eq(x: 1, z: 3)
end
end
with_property ":x, name_property: true" do
# it "Unset values with name_property are included in state" do
# expect(resource.state_for_resource_reporter).to eq({ x: 'blah' })
# end
it "Set values with name_property are included in state" do
resource.x 1
expect(resource.state_for_resource_reporter).to eq(x: 1)
end
end
with_property ":x, default: 1" do
it "Unset values with defaults are not included in state" do
expect(resource.state_for_resource_reporter).to eq({})
end
it "Set values with defaults are included in state" do
resource.x 1
expect(resource.state_for_resource_reporter).to eq(x: 1)
end
end
context "With a class with a normal getter and setter" do
before do
resource_class.class_eval do
def x
@blah*3
end
def x=(value)
@blah = value*2
end
end
end
it "state_properties(:x) causes the value to be included in properties" do
resource_class.state_properties(:x)
resource.x = 1
expect(resource.x).to eq 6
expect(resource.state_for_resource_reporter).to eq(x: 6)
end
end
context "When state_properties happens before properties are declared" do
before do
resource_class.class_eval do
state_properties :x
property :x
end
end
it "the property works and is in state_properties" do
expect(resource_class.state_properties).to include(resource_class.properties[:x])
resource.x = 1
expect(resource.x).to eq 1
expect(resource.state_for_resource_reporter).to eq(x: 1)
end
end
with_property ":x, Integer, identity: true" do
it "state_properties(:x) leaves the property in desired_state" do
resource_class.state_properties(:x)
resource.x 10
expect(resource_class.properties[:x].desired_state?).to be_truthy
expect(resource_class.state_properties).to eq [
resource_class.properties[:x]
]
expect(resource.state_for_resource_reporter).to eq(x: 10)
end
it "state_properties(:x) does not turn off validation" do
resource_class.state_properties(:x)
expect { resource.x 'ouch' }.to raise_error Chef::Exceptions::ValidationFailed
end
it "state_properties(:x) does not turn off identity" do
resource_class.state_properties(:x)
resource.x 10
expect(resource_class.identity_properties).to eq [ resource_class.properties[:x] ]
expect(resource_class.properties[:x].identity?).to be_truthy
expect(resource.identity).to eq 10
end
end
with_property ":x, Integer, identity: true, desired_state: false" do
before do
resource_class.class_eval do
def y
20
end
end
end
it "state_properties(:x) leaves x identical" do
old_value = resource_class.properties[:y]
resource_class.state_properties(:x)
resource.x 10
expect(resource_class.properties[:y].object_id).to eq old_value.object_id
expect(resource_class.properties[:x].desired_state?).to be_truthy
expect(resource_class.properties[:x].identity?).to be_truthy
expect(resource_class.identity_properties).to eq [
resource_class.properties[:x]
]
expect(resource.identity).to eq(10)
expect(resource_class.state_properties).to eq [
resource_class.properties[:x]
]
expect(resource.state_for_resource_reporter).to eq(x: 10)
end
it "state_properties(:y) adds y to desired state" do
old_value = resource_class.properties[:x]
resource_class.state_properties(:y)
resource.x 10
expect(resource_class.properties[:x].object_id).to eq old_value.object_id
expect(resource_class.properties[:x].desired_state?).to be_falsey
expect(resource_class.properties[:y].desired_state?).to be_truthy
expect(resource_class.state_properties).to eq [
resource_class.properties[:y]
]
expect(resource.state_for_resource_reporter).to eq(y: 20)
end
context "With a subclassed resource" do
let(:subresource_class) do
new_resource_name = self.class.new_resource_name
Class.new(resource_class) do
resource_name new_resource_name
end
end
let(:subresource) do
subresource_class.new('blah')
end
it "state_properties(:x) adds x to desired state" do
old_value = resource_class.properties[:y]
subresource_class.state_properties(:x)
subresource.x 10
expect(subresource_class.properties[:y].object_id).to eq old_value.object_id
expect(subresource_class.properties[:x].desired_state?).to be_truthy
expect(subresource_class.properties[:x].identity?).to be_truthy
expect(subresource_class.identity_properties).to eq [
subresource_class.properties[:x]
]
expect(subresource.identity).to eq(10)
expect(subresource_class.state_properties).to eq [
subresource_class.properties[:x]
]
expect(subresource.state_for_resource_reporter).to eq(x: 10)
end
it "state_properties(:y) adds y to desired state" do
old_value = resource_class.properties[:x]
subresource_class.state_properties(:y)
subresource.x 10
expect(subresource_class.properties[:x].object_id).to eq old_value.object_id
expect(subresource_class.properties[:y].desired_state?).to be_truthy
expect(subresource_class.state_properties).to eq [
subresource_class.properties[:y]
]
expect(subresource.state_for_resource_reporter).to eq(y: 20)
expect(subresource_class.properties[:x].identity?).to be_truthy
expect(subresource_class.identity_properties).to eq [
subresource_class.properties[:x]
]
expect(subresource.identity).to eq(10)
end
end
end
end
end
|