summaryrefslogtreecommitdiff
path: root/spec/unit/runner_spec.rb
blob: 46074728c27a53c8310f9b516b3034b624d0e9d1 (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
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

# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, 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'

class SnitchyProvider < Chef::Provider
  def self.all_actions_called
    @all_actions_called ||= []
  end

  def self.action_called(action)
    all_actions_called << action
  end

  def self.clear_action_record
    @all_actions_called = nil
  end

  def load_current_resource
    true
  end

  def action_first_action
    @new_resource.updated_by_last_action(true)
    self.class.action_called(:first)
  end

  def action_second_action
    @new_resource.updated_by_last_action(true)
    self.class.action_called(:second)
  end

  def action_third_action
    @new_resource.updated_by_last_action(true)
    self.class.action_called(:third)
  end

end

class FailureResource < Chef::Resource

  attr_accessor :action

  def initialize(*args)
    super
    @action = :fail
  end

  def provider
    FailureProvider
  end
end

class FailureProvider < Chef::Provider

  class ChefClientFail < StandardError; end

  def load_current_resource
    true
  end

  def action_fail
    raise ChefClientFail, "chef had an error of some sort"
  end
end

describe Chef::Runner do

  let(:node) do
    node = Chef::Node.new
    node.name "latte"
    node.automatic[:platform] = "mac_os_x"
    node.automatic[:platform_version] = "10.5.1"
    node
  end

  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:run_context) { Chef::RunContext.new(node, Chef::CookbookCollection.new({}), events) }
  let(:first_resource) { Chef::Resource::Cat.new("loulou1", run_context) }
  let(:runner) { Chef::Runner.new(run_context) }

  before do
    run_context.resource_collection << first_resource
  end

  context "when we fall through to old Chef::Platform resolution" do
    let(:provider_resolver) { Chef::ProviderResolver.new(node, first_resource, nil) }
    before do
      # set up old Chef::Platform resolution instead of provider_resolver
      Chef::Platform.set(
        :resource => :cat,
        :provider => Chef::Provider::SnakeOil,
      )
      allow(Chef::ProviderResolver).to receive(:new).and_return(provider_resolver)
      allow(provider_resolver).to receive(:maybe_dynamic_provider_resolution).with(first_resource, anything()).and_return(nil)
    end

    it "should use the platform provider if it has one" do
      expect(Chef::Platform).to receive(:find_provider_for_node).with(node, first_resource).and_call_original
      runner.converge
    end
  end

  context "when we are doing dynamic provider resolution" do

    it "should pass each resource in the collection to a provider" do
      expect(run_context.resource_collection).to receive(:execute_each_resource).once
      runner.converge
    end

    it "should use the provider specified by the resource (if it has one)" do
      provider = Chef::Provider::Easy.new(run_context.resource_collection[0], run_context)
      # Expect provider to be called twice, because will fall back to old provider lookup
      expect(run_context.resource_collection[0]).to receive(:provider).twice.and_return(Chef::Provider::Easy)
      expect(Chef::Provider::Easy).to receive(:new).once.and_return(provider)
      runner.converge
    end

    it "should run the action for each resource" do
      provider = Chef::Provider::SnakeOil.new(run_context.resource_collection[0], run_context)
      expect(provider).to receive(:action_sell).once.and_return(true)
      expect(Chef::Provider::SnakeOil).to receive(:new).once.and_return(provider)
      runner.converge
    end

    it "should raise exceptions as thrown by a provider" do
      provider = Chef::Provider::SnakeOil.new(run_context.resource_collection[0], run_context)
      allow(Chef::Provider::SnakeOil).to receive(:new).once.and_return(provider)
      allow(provider).to receive(:action_sell).once.and_raise(ArgumentError)
      expect { runner.converge }.to raise_error(ArgumentError)
    end

    it "should not raise exceptions thrown by providers if the resource has ignore_failure set to true" do
      allow(run_context.resource_collection[0]).to receive(:ignore_failure).and_return(true)
      provider = Chef::Provider::SnakeOil.new(run_context.resource_collection[0], run_context)
      allow(Chef::Provider::SnakeOil).to receive(:new).once.and_return(provider)
      allow(provider).to receive(:action_sell).once.and_raise(ArgumentError)
      expect { runner.converge }.not_to raise_error
    end

    it "should retry with the specified delay if retries are specified" do
      first_resource.retries 3
      provider = Chef::Provider::SnakeOil.new(run_context.resource_collection[0], run_context)
      allow(Chef::Provider::SnakeOil).to receive(:new).once.and_return(provider)
      allow(provider).to receive(:action_sell).and_raise(ArgumentError)
      expect(first_resource).to receive(:sleep).with(2).exactly(3).times
      expect { runner.converge }.to raise_error(ArgumentError)
    end

    it "should execute immediate actions on changed resources" do
      notifying_resource = Chef::Resource::Cat.new("peanut", run_context)
      notifying_resource.action = :purr # only action that will set updated on the resource

      run_context.resource_collection << notifying_resource
      first_resource.action = :nothing # won't be updated unless notified by other resource

      notifying_resource.notifies(:purr, first_resource, :immediately)

      runner.converge

      expect(first_resource).to be_updated
    end

    it "should follow a chain of actions" do
      first_resource.action = :nothing

      middle_resource = Chef::Resource::Cat.new("peanut", run_context)
      middle_resource.action = :nothing
      run_context.resource_collection << middle_resource
      middle_resource.notifies(:purr, first_resource, :immediately)

      last_resource = Chef::Resource::Cat.new("snuffles", run_context)
      last_resource.action = :purr
      run_context.resource_collection << last_resource
      last_resource.notifies(:purr, middle_resource, :immediately)

      runner.converge

      expect(last_resource).to be_updated   # by action(:purr)
      expect(middle_resource).to be_updated # by notification from last_resource
      expect(first_resource).to be_updated # by notification from middle_resource
    end

    it "should execute delayed actions on changed resources" do
      first_resource.action = :nothing
      second_resource = Chef::Resource::Cat.new("peanut", run_context)
      second_resource.action = :purr

      run_context.resource_collection << second_resource
      second_resource.notifies(:purr, first_resource, :delayed)

      runner.converge

      expect(first_resource).to be_updated
    end

    it "should execute delayed notifications when a failure occurs in the chef client run" do
      first_resource.action = :nothing
      second_resource = Chef::Resource::Cat.new("peanut", run_context)
      second_resource.action = :purr

      run_context.resource_collection << second_resource
      second_resource.notifies(:purr, first_resource, :delayed)

      third_resource = FailureResource.new("explode", run_context)
      run_context.resource_collection << third_resource

      expect { runner.converge }.to raise_error(FailureProvider::ChefClientFail)

      expect(first_resource).to be_updated
    end

    it "should execute delayed notifications when a failure occurs in a notification" do
      first_resource.action = :nothing
      second_resource = Chef::Resource::Cat.new("peanut", run_context)
      second_resource.action = :purr

      run_context.resource_collection << second_resource

      third_resource = FailureResource.new("explode", run_context)
      third_resource.action = :nothing
      run_context.resource_collection << third_resource

      second_resource.notifies(:fail, third_resource, :delayed)
      second_resource.notifies(:purr, first_resource, :delayed)

      expect {runner.converge}.to raise_error(FailureProvider::ChefClientFail)

      expect(first_resource).to be_updated
    end

    it "should execute delayed notifications when a failure occurs in multiple notifications" do
      first_resource.action = :nothing
      second_resource = Chef::Resource::Cat.new("peanut", run_context)
      second_resource.action = :purr

      run_context.resource_collection << second_resource

      third_resource = FailureResource.new("explode", run_context)
      third_resource.action = :nothing
      run_context.resource_collection << third_resource

      fourth_resource = FailureResource.new("explode again", run_context)
      fourth_resource.action = :nothing
      run_context.resource_collection << fourth_resource

      second_resource.notifies(:fail, third_resource, :delayed)
      second_resource.notifies(:fail, fourth_resource, :delayed)
      second_resource.notifies(:purr, first_resource, :delayed)

      exception = nil
      begin
        runner.converge
      rescue => e
        exception = e
      end
      expect(exception).to be_a(Chef::Exceptions::MultipleFailures)

      expected_message =<<-E
Multiple failures occurred:
* FailureProvider::ChefClientFail occurred in delayed notification: [explode] (dynamically defined) had an error: FailureProvider::ChefClientFail: chef had an error of some sort
* FailureProvider::ChefClientFail occurred in delayed notification: [explode again] (dynamically defined) had an error: FailureProvider::ChefClientFail: chef had an error of some sort
      E
      expect(exception.message).to eq(expected_message)

      expect(first_resource).to be_updated
    end

    it "does not duplicate delayed notifications" do
      SnitchyProvider.clear_action_record

      first_resource.action = :nothing
      first_resource.provider = SnitchyProvider

      second_resource = Chef::Resource::Cat.new("peanut", run_context)
      second_resource.action = :first_action
      second_resource.provider = SnitchyProvider
      run_context.resource_collection << second_resource

      third_resource = Chef::Resource::Cat.new("snickers", run_context)
      third_resource.action = :first_action
      third_resource.provider = SnitchyProvider
      run_context.resource_collection << third_resource

      second_resource.notifies(:second_action, first_resource, :delayed)
      second_resource.notifies(:third_action, first_resource, :delayed)

      third_resource.notifies(:second_action, first_resource, :delayed)
      third_resource.notifies(:third_action, first_resource, :delayed)

      runner.converge
      # resources 2 and 3 call :first_action in the course of normal resource
      # execution, and schedule delayed actions :second and :third on the first
      # resource. The duplicate actions should "collapse" to a single notification
      # and order should be preserved.
      expect(SnitchyProvider.all_actions_called).to eq([:first, :first, :second, :third])
    end

    it "executes delayed notifications in the order they were declared" do
      SnitchyProvider.clear_action_record

      first_resource.action = :nothing
      first_resource.provider = SnitchyProvider

      second_resource = Chef::Resource::Cat.new("peanut", run_context)
      second_resource.action = :first_action
      second_resource.provider = SnitchyProvider
      run_context.resource_collection << second_resource

      third_resource = Chef::Resource::Cat.new("snickers", run_context)
      third_resource.action = :first_action
      third_resource.provider = SnitchyProvider
      run_context.resource_collection << third_resource

      second_resource.notifies(:second_action, first_resource, :delayed)
      second_resource.notifies(:second_action, first_resource, :delayed)

      third_resource.notifies(:third_action, first_resource, :delayed)
      third_resource.notifies(:third_action, first_resource, :delayed)

      runner.converge
      expect(SnitchyProvider.all_actions_called).to eq([:first, :first, :second, :third])
    end

    it "does not fire notifications if the resource was not updated by the last action executed" do
      # REGRESSION TEST FOR CHEF-1452
      SnitchyProvider.clear_action_record

      first_resource.action = :first_action
      first_resource.provider = SnitchyProvider

      second_resource = Chef::Resource::Cat.new("peanut", run_context)
      second_resource.action = :nothing
      second_resource.provider = SnitchyProvider
      run_context.resource_collection << second_resource

      third_resource = Chef::Resource::Cat.new("snickers", run_context)
      third_resource.action = :nothing
      third_resource.provider = SnitchyProvider
      run_context.resource_collection << third_resource

      first_resource.notifies(:second_action, second_resource, :immediately)
      second_resource.notifies(:third_action, third_resource, :immediately)

      runner.converge

      # All of the resources should only fire once:
      expect(SnitchyProvider.all_actions_called).to eq([:first, :second, :third])

      # all of the resources should be marked as updated for reporting purposes
      expect(first_resource).to be_updated
      expect(second_resource).to be_updated
      expect(third_resource).to be_updated
    end

    it "should check a resource's only_if and not_if if notified by another resource" do
      first_resource.action = :buy

      only_if_called_times = 0
      first_resource.only_if {only_if_called_times += 1; true}

      not_if_called_times = 0
      first_resource.not_if {not_if_called_times += 1; false}

      second_resource = Chef::Resource::Cat.new("carmel", run_context)
      run_context.resource_collection << second_resource
      second_resource.notifies(:purr, first_resource, :delayed)
      second_resource.action = :purr

      # hits only_if first time when the resource is run in order, second on notify
      runner.converge

      expect(only_if_called_times).to eq(2)
      expect(not_if_called_times).to eq(2)
    end

    it "should resolve resource references in notifications when resources are defined lazily" do
      first_resource.action = :nothing

      lazy_resources = lambda {
        last_resource = Chef::Resource::Cat.new("peanut", run_context)
        run_context.resource_collection << last_resource
        last_resource.notifies(:purr, first_resource.to_s, :delayed)
        last_resource.action = :purr
      }
      second_resource = Chef::Resource::RubyBlock.new("myblock", run_context)
      run_context.resource_collection << second_resource
      second_resource.block { lazy_resources.call }

      runner.converge

      expect(first_resource).to be_updated
    end

  end
end