summaryrefslogtreecommitdiff
path: root/spec/unit/dsl/declare_resource_spec.rb
blob: ead0be29677398aad6d8d843f20e2c28197b8e5f (plain)
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
#
# Copyright:: Copyright 2008-2018, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

describe Chef::ResourceCollection do
  let(:run_context) do
    cookbook_repo = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "data", "cookbooks"))
    cookbook_loader = Chef::CookbookLoader.new(cookbook_repo)
    cookbook_loader.load_cookbooks
    node = Chef::Node.new
    cookbook_collection = Chef::CookbookCollection.new(cookbook_loader)
    events = Chef::EventDispatch::Dispatcher.new
    Chef::RunContext.new(node, cookbook_collection, events)
  end

  let(:recipe) do
    Chef::Recipe.new("hjk", "test", run_context)
  end

  describe "mixed in correctly" do
    it "the resources() method winds up in the right classes" do
      methods = %i{resources find_resource find_resource! edit_resource edit_resource! delete_resource delete_resource! declare_resource build_resource}
      expect(Chef::Resource.instance_methods).to include(*methods)
      expect(Chef::Recipe.instance_methods).to include(*methods)
      expect(Chef::Provider.instance_methods).to include(*methods)
    end
  end

  describe "#declare_resource" do
    before do
      recipe.declare_resource(:zen_master, "monkey") do
        something true
      end
    end

    it "inserts into the resource collection" do
      expect(run_context.resource_collection.first.to_s).to eql("zen_master[monkey]")
    end

    it "sets the property from the block" do
      expect(run_context.resource_collection.first.something).to be true
    end
  end

  describe "#edit_resource!" do
    it "raises if nothing is found" do
      expect do
        recipe.edit_resource!(:zen_master, "monkey") do
          something true
        end
      end.to raise_error(Chef::Exceptions::ResourceNotFound)
    end

    it "raises if nothing is found and no block is given" do
      expect do
        recipe.edit_resource!(:zen_master, "monkey")
      end.to raise_error(Chef::Exceptions::ResourceNotFound)
    end

    it "edits the resource if it finds one" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect(
        recipe.edit_resource!(:zen_master, "monkey") do
          something true
        end
      ).to eql(resource)
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first.something).to be true
    end

    it "acts like find_resource! if not given a block and the resource exists" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect(
        recipe.edit_resource!(:zen_master, "monkey")
      ).to eql(resource)
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first.something).to be false
    end
  end

  describe "#edit_resource" do
    it "inserts a resource if nothing is found" do
      resource = recipe.edit_resource(:zen_master, "monkey") do
        something true
      end
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first).to eql(resource)
      expect(run_context.resource_collection.first.something).to be true
    end

    it "inserts a resource even if not given a block" do
      resource = recipe.edit_resource(:zen_master, "monkey")
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first).to eql(resource)
    end

    it "edits the resource if it finds one" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect(
        recipe.edit_resource(:zen_master, "monkey") do
          something true
        end
      ).to eql(resource)
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first.something).to be true
    end

    it "acts like find_resource if not given a block and the resource exists" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect(
        recipe.edit_resource(:zen_master, "monkey")
      ).to eql(resource)
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first.something).to be false
    end
  end

  describe "#find_resource!" do
    it "raises if nothing is found" do
      expect do
        recipe.find_resource!(:zen_master, "monkey")
      end.to raise_error(Chef::Exceptions::ResourceNotFound)
    end

    it "raises if given a block" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect do
        recipe.find_resource!(:zen_master, "monkey") do
          something false
        end
      end.to raise_error(ArgumentError)
    end

    it "returns the resource if it finds one" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect(
        recipe.find_resource!(:zen_master, "monkey")
      ).to eql(resource)
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first.something).to be false
    end
  end

  describe "#find_resource without block" do
    it "returns nil if nothing is found" do
      expect(recipe.find_resource(:zen_master, "monkey")).to be nil
      expect(run_context.resource_collection.all_resources.size).to eql(0)
    end

    it "returns the resource if it finds one" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect(
        recipe.find_resource(:zen_master, "monkey")
      ).to eql(resource)
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first.something).to be false
    end
  end

  describe "#find_resource with block" do
    it "inserts a resource if nothing is found" do
      resource = recipe.find_resource(:zen_master, "monkey") do
        something true
      end
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first).to eql(resource)
      expect(run_context.resource_collection.first.something).to be true
    end

    it "returns the resource if it finds one" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect(
        recipe.find_resource(:zen_master, "monkey") do
          something true
        end
      ).to eql(resource)
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(run_context.resource_collection.first.something).to be false
    end
  end

  describe "#delete_resource" do
    it "returns nil if nothing is found" do
      expect(
        recipe.delete_resource(:zen_master, "monkey")
      ).to be nil
    end

    it "deletes and returns the resource if it finds one" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect(
        recipe.delete_resource(:zen_master, "monkey")
      ).to eql(resource)
      expect(run_context.resource_collection.all_resources.size).to eql(0)
    end
  end

  describe "#delete_resource!" do
    it "raises if nothing is found" do
      expect do
        recipe.delete_resource!(:zen_master, "monkey")
      end.to raise_error(Chef::Exceptions::ResourceNotFound)
    end

    it "deletes and returns the resource if it finds one" do
      resource = recipe.declare_resource(:zen_master, "monkey") do
        something false
      end
      expect(
        recipe.delete_resource!(:zen_master, "monkey")
      ).to eql(resource)
      expect(run_context.resource_collection.all_resources.size).to eql(0)
    end

    it "removes pending delayed notifications" do
      recipe.declare_resource(:zen_master, "one")
      recipe.declare_resource(:zen_master, "two") do
        notifies :win, "zen_master[one]"
      end
      recipe.delete_resource(:zen_master, "two")
      resource = recipe.declare_resource(:zen_master, "two")
      expect(resource.delayed_notifications).to eql([])
    end

    it "removes pending immediate notifications" do
      recipe.declare_resource(:zen_master, "one")
      recipe.declare_resource(:zen_master, "two") do
        notifies :win, "zen_master[one]", :immediate
      end
      recipe.delete_resource(:zen_master, "two")
      resource = recipe.declare_resource(:zen_master, "two")
      expect(resource.immediate_notifications).to eql([])
    end

    it "removes pending before notifications" do
      recipe.declare_resource(:zen_master, "one")
      recipe.declare_resource(:zen_master, "two") do
        notifies :win, "zen_master[one]", :before
      end
      recipe.delete_resource(:zen_master, "two")
      resource = recipe.declare_resource(:zen_master, "two")
      expect(resource.before_notifications).to eql([])
    end
  end

  describe "run_context helpers" do

    let(:parent_run_context) do
      run_context.create_child
    end

    let(:child_run_context) do
      parent_run_context.create_child
    end

    let(:parent_recipe) do
      Chef::Recipe.new("hjk", "parent", parent_run_context)
    end

    let(:child_recipe) do
      Chef::Recipe.new("hjk", "child", child_run_context)
    end

    before do
      # wire up our outer run context to the root Chef.run_context
      allow(Chef).to receive(:run_context).and_return(run_context)
    end

    it "our tests have correct separation" do
      child_resource = child_recipe.declare_resource(:zen_master, "child") do
        something false
      end
      parent_resource = parent_recipe.declare_resource(:zen_master, "parent") do
        something false
      end
      root_resource = recipe.declare_resource(:zen_master, "root") do
        something false
      end
      expect(run_context.resource_collection.first).to eql(root_resource)
      expect(run_context.resource_collection.first.to_s).to eql("zen_master[root]")
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(parent_run_context.resource_collection.first).to eql(parent_resource)
      expect(parent_run_context.resource_collection.first.to_s).to eql("zen_master[parent]")
      expect(parent_run_context.resource_collection.all_resources.size).to eql(1)
      expect(child_run_context.resource_collection.first).to eql(child_resource)
      expect(child_run_context.resource_collection.first.to_s).to eql("zen_master[child]")
      expect(child_run_context.resource_collection.all_resources.size).to eql(1)
    end

    it "with_run_context with :parent lets us build resources in the parent run_context from the child" do
      child_recipe.instance_eval do
        with_run_context(:parent) do
          declare_resource(:zen_master, "parent") do
            something false
          end
        end
      end
      expect(run_context.resource_collection.all_resources.size).to eql(0)
      expect(parent_run_context.resource_collection.all_resources.size).to eql(1)
      expect(parent_run_context.resource_collection.first.to_s).to eql("zen_master[parent]")
      expect(child_run_context.resource_collection.all_resources.size).to eql(0)
    end

    it "with_run_context with :root lets us build resources in the root run_context from the child" do
      child_recipe.instance_eval do
        with_run_context(:root) do
          declare_resource(:zen_master, "root") do
            something false
          end
        end
      end
      expect(run_context.resource_collection.first.to_s).to eql("zen_master[root]")
      expect(run_context.resource_collection.all_resources.size).to eql(1)
      expect(parent_run_context.resource_collection.all_resources.size).to eql(0)
      expect(child_run_context.resource_collection.all_resources.size).to eql(0)
    end

    it "with_run_context also takes a RunContext object as an argument" do
      child_recipe.instance_exec(parent_run_context) do |parent_run_context|
        with_run_context(parent_run_context) do
          declare_resource(:zen_master, "parent") do
            something false
          end
        end
      end
      expect(run_context.resource_collection.all_resources.size).to eql(0)
      expect(parent_run_context.resource_collection.all_resources.size).to eql(1)
      expect(parent_run_context.resource_collection.first.to_s).to eql("zen_master[parent]")
      expect(child_run_context.resource_collection.all_resources.size).to eql(0)
    end

    it "with_run_context returns the return value of the block" do
      child_recipe.instance_eval do
        ret = with_run_context(:root) do
          "return value"
        end
        raise "failed" unless ret == "return value"
      end
    end
  end
end