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
|
require "support/shared/integration/integration_helper"
describe "Resource.load_current_value" do
include IntegrationSupport
module Namer
extend self
attr_accessor :current_index
def incrementing_value
@incrementing_value += 1
@incrementing_value
end
attr_writer :incrementing_value
end
before(:all) { Namer.current_index = 1 }
before { Namer.current_index += 1 }
before { Namer.incrementing_value = 0 }
let(:resource_name) { :"load_current_value_dsl#{Namer.current_index}" }
let(:resource_class) do
result = Class.new(Chef::Resource) do
def self.to_s; resource_name.to_s; end
def self.inspect; resource_name.inspect; end
property :x, default: lazy { "default #{Namer.incrementing_value}" }
def self.created_x=(value)
@created = value
end
def self.created_x
@created
end
action :create do
new_resource.class.created_x = new_resource.x
end
end
result.provides resource_name
result
end
# Pull on resource_class to initialize it
before { resource_class }
context "with a resource with load_current_value" do
before :each do
resource_class.load_current_value do
x "loaded #{Namer.incrementing_value} (#{self.class.properties.sort_by { |name, p| name }
.select { |name, p| p.is_set?(self) }
.map { |name, p| "#{name}=#{p.get(self)}" }
.join(", ")})"
end
end
context "and a resource with x set to a desired value" do
let(:resource) do
e = self
r = nil
converge do
r = public_send(e.resource_name, "blah") do
x "desired"
end
end
r
end
it "current_resource is passed name but not x" do
expect(resource.current_value.x).to eq "loaded 3 (name=blah)"
end
it "resource.current_value returns a different resource" do
expect(resource.current_value.x).to eq "loaded 3 (name=blah)"
expect(resource.x).to eq "desired"
end
it "resource.current_value constructs the resource anew each time" do
expect(resource.current_value.x).to eq "loaded 3 (name=blah)"
expect(resource.current_value.x).to eq "loaded 4 (name=blah)"
end
it "the provider accesses the current value of x" do
expect(resource.class.created_x).to eq "desired"
end
context "and identity: :i and :d with desired_state: false" do
before do
resource_class.class_eval do
property :i, identity: true
property :d, desired_state: false
end
end
before do
resource.i "desired_i"
resource.d "desired_d"
end
it "i, name and d are passed to load_current_value, but not x" do
expect(resource.current_value.x).to eq "loaded 3 (d=desired_d, i=desired_i, name=blah)"
end
end
context "and name_property: :i and :d with desired_state: false" do
before do
resource_class.class_eval do
property :i, name_property: true
property :d, desired_state: false
end
end
before do
resource.i "desired_i"
resource.d "desired_d"
end
it "i, name and d are passed to load_current_value, but not x" do
expect(resource.current_value.x).to eq "loaded 3 (d=desired_d, i=desired_i, name=blah)"
end
end
end
let (:subresource_name) do
:"load_current_value_subresource_dsl#{Namer.current_index}"
end
let (:subresource_class) do
r = Class.new(resource_class) do
property :y, default: lazy { "default_y #{Namer.incrementing_value}" }
end
r.provides subresource_name
r
end
# Pull on subresource_class to initialize it
before { subresource_class }
let(:subresource) do
e = self
r = nil
converge do
r = public_send(e.subresource_name, "blah") do
x "desired"
end
end
r
end
context "and a child resource class with no load_current_value" do
it "the parent load_current_value is used" do
expect(subresource.current_value.x).to eq "loaded 3 (name=blah)"
end
it "load_current_value yields a copy of the child class" do
expect(subresource.current_value).to be_kind_of(subresource_class)
end
end
context "And a child resource class with load_current_value" do
before do
subresource_class.load_current_value do
y "loaded_y #{Namer.incrementing_value} (#{self.class.properties.sort_by { |name, p| name }
.select { |name, p| p.is_set?(self) }
.map { |name, p| "#{name}=#{p.get(self)}" }
.join(", ")})"
end
end
it "the overridden load_current_value is used" do
current_resource = subresource.current_value
expect(current_resource.x).to eq "default 4"
expect(current_resource.y).to eq "loaded_y 3 (name=blah)"
end
end
context "and a child resource class with load_current_value calling super()" do
before do
subresource_class.load_current_value do
super()
y "loaded_y #{Namer.incrementing_value} (#{self.class.properties.sort_by { |name, p| name }
.select { |name, p| p.is_set?(self) }
.map { |name, p| "#{name}=#{p.get(self)}" }
.join(", ")})"
end
end
it "the original load_current_value is called as well as the child one" do
current_resource = subresource.current_value
expect(current_resource.x).to eq "loaded 5 (name=blah)"
expect(current_resource.y).to eq "loaded_y 6 (name=blah, x=loaded 5 (name=blah))"
end
end
end
end
describe "simple load_current_value tests" do
let(:resource_class) do
Class.new(Chef::Resource) do
attr_writer :index # this is our hacky global state
def index; @index ||= 1; end
property :myindex, Integer
load_current_value do |new_resource|
myindex new_resource.index
end
action :run do
new_resource.index += 1
end
end
end
let(:node) { Chef::Node.new }
let(:events) { Chef::EventDispatch::Dispatcher.new }
let(:run_context) { Chef::RunContext.new(node, {}, events) }
let(:new_resource) { resource_class.new("test", run_context) }
let(:provider) { new_resource.provider_for_action(:run) }
it "calling the action on the provider sets the current_resource" do
expect(events).to receive(:resource_current_state_loaded).with(new_resource, :run, anything)
provider.run_action(:run)
expect(provider.current_resource.myindex).to eql(1)
end
it "calling the action on the provider sets the after_resource" do
expect(events).to receive(:resource_after_state_loaded).with(new_resource, :run, anything)
provider.run_action(:run)
expect(provider.after_resource.myindex).to eql(2)
end
end
describe "simple load_current_resource tests" do
let(:provider_class) do
Class.new(Chef::Provider) do
provides :no_load_current_value
def load_current_resource
@current_resource = new_resource.dup
@current_resource.myindex = 1
end
action :run do
end
end
end
let(:resource_class) do
provider_class # vivify the provider_class
Class.new(Chef::Resource) do
provides :no_load_current_value
property :myindex, Integer
end
end
let(:node) { Chef::Node.new }
let(:events) { Chef::EventDispatch::Dispatcher.new }
let(:run_context) { Chef::RunContext.new(node, {}, events) }
let(:new_resource) { resource_class.new("test", run_context) }
let(:provider) { new_resource.provider_for_action(:run) }
it "calling the action on the provider sets the current_resource" do
expect(events).to receive(:resource_current_state_loaded).with(new_resource, :run, anything)
provider.run_action(:run)
expect(provider.current_resource.myindex).to eql(1)
end
it "calling the action on the provider sets the after_resource" do
expect(events).to receive(:resource_after_state_loaded).with(new_resource, :run, new_resource)
provider.run_action(:run)
expect(provider.after_resource.myindex).to eql(nil)
end
end
describe "simple load_current_resource and load_after_resource tests" do
let(:provider_class) do
Class.new(Chef::Provider) do
provides :load_after
def load_current_resource
@current_resource = new_resource.dup
@current_resource.myindex = 1
end
def load_after_resource
@after_resource = new_resource.dup
@after_resource.myindex = 2
end
action :run do
end
end
end
let(:resource_class) do
provider_class # autovivify provider class
Class.new(Chef::Resource) do
provides :load_after
property :myindex, Integer
end
end
let(:node) { Chef::Node.new }
let(:events) { Chef::EventDispatch::Dispatcher.new }
let(:run_context) { Chef::RunContext.new(node, {}, events) }
let(:new_resource) { resource_class.new("test", run_context) }
let(:provider) { new_resource.provider_for_action(:run) }
it "calling the action on the provider sets the current_resource" do
expect(events).to receive(:resource_current_state_loaded).with(new_resource, :run, anything)
provider.run_action(:run)
expect(provider.current_resource.myindex).to eql(1)
end
it "calling the action on the provider sets the after_resource" do
expect(events).to receive(:resource_after_state_loaded).with(new_resource, :run, anything)
provider.run_action(:run)
expect(provider.after_resource.myindex).to eql(2)
end
end
|